//startInterval.js
(function () {
/* global adventurejs A */
var p = adventurejs.Game.prototype;
/**
* Intervals are callback functions used to fire in-game events
* every turn. For example, turn on a water faucet and the faucet
* pours water every turn until it's turned off.
* startInterval adds functions to the list. The optional
* turns parameter allows for setting a limit on the number of
* turns to repeat.
* @memberof adventurejs.Game
* @method adventurejs.Game#startInterval
* @param {Object} params The parameters for the interval.
* @param {string} params.id The id of the Asset which owns the function.
* @param {Function} params.callback The function to call on each interval.
* @param {number} [params.times=null] The number of turns to repeat, or null for indefinite.
* @param {number} [params.frequency=1] How often to trigger, in turns.
*/
p.startInterval = function Game_startInterval({
id,
callback,
times = null,
frequency = 1,
} = {}) {
this.game.log(
"L1618",
"log",
"high",
`[Game.js] startInterval(${id})`,
"Game"
);
if (!id || !callback) return null;
if (id && id !== this.game.game_name) id = A.serialize(id);
// is there already a property with this id?
if ("undefined" === typeof this.world._intervals[id]) {
this.world._intervals[id] = {};
}
// is there already a callback with this name on this id?
if ("undefined" !== typeof this.world._intervals[id][callback]) {
return null;
}
// create the new callback property
this.world._intervals[id][callback] = {
callback: callback,
frequency: frequency,
times: times,
count: 0,
last: 0,
};
};
})();