Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
//callIntervals.js
(function () {
  /* global adventurejs A */

  var p = adventurejs.Game.prototype;

  /**
   * callIntervals uses pointers to asset properties to
   * print messages or apply custom logic every turn.
   * For example, turn on a water faucet and have
   * the faucet pour water every turn until it's turned
   * off, or turn on a radio and have it play music lyrics.
   * Properties can be strings or arrays or functions.
   * @memberof adventurejs.Game
   * @method adventurejs.Game#callIntervals
   */
  p.callIntervals = function Game_callIntervals() {
    this.game.log("L1616", "log", "high", `[Game.js] callIntervals()`, "Game");
    var game = this;
    Object.keys(this.world._intervals).forEach(function (id) {
      Object.keys(game.world._intervals[id]).forEach(function (callback) {
        let interval = game.world._intervals[id][callback];
        let results;

        let asset;
        asset = id === game.game_name ? game : game.getAsset(id);

        if (!asset || "undefined" === typeof asset[callback]) return;

        // check frequency vs last
        if (interval.frequency > 1 && interval.last < interval.frequency) {
          // increment last and pass
          interval.last = interval.last + 1;
          return;
        }

        // check for random frequency
        if (interval.frequency < 1) {
          // if we fail dice roll, return
          var rand = Math.random(1);
          if (rand > interval.frequency) return;
        }

        results = A.getSAF.call(game, asset[callback], asset);
        if ("string" === typeof results) game.print(results);

        interval.last = 0;

        // check times
        if ("number" === typeof interval.times) {
          interval.times = interval.times - 1;
          if (interval.times <= 0) {
            game.stopInterval({ id: id, callback: callback });
          }
        }
      });
    });
  };
})();