¶ GameTimerCopyright(c) 2012 Stefano Balietti MIT Licensed Creates a controllable timer object for nodeGame |
(function (exports, node) {
|
¶ Global scope |
exports.GameTimer = GameTimer;
JSUS = node.JSUS; |
¶ GameTimer status levelsNumerical levels representing the state of the GameTimer @see GameTimer.status |
GameTimer.STOPPED = -5
GameTimer.PAUSED = -3;
GameTimer.UNINITIALIZED = -1;
GameTimer.INITIALIZED = 0;
GameTimer.LOADING = 3;
GameTimer.RUNNING = 5;
|
¶ GameTimer constructorCreates an instance of GameTimer Params
options.
object
Optional. A configuration object
|
function GameTimer (options) {
options = options || {}; |
¶ Public properties |
|
¶ GameTimer.statusNumerical index representing the current the state of the GameTimer object |
this.status = GameTimer.UNINITIALIZED;
|
¶ GameTimer.optionsThe current settings for the GameTimer |
this.options = options; |
¶ GameTimer.timerThe ID of the javascript interval |
this.timer = null; |
¶ GameTimer.timeLeftMilliseconds left before time is up |
this.timeLeft = null;
|
¶ GameTimer.timePassedMilliseconds already passed from the start of the timer |
this.timePassed = 0; |
¶ GameTimer.updateThe frequency of update for the timer (in milliseconds) |
this.update = 1000;
|
¶ GameTimer.timeupEvent string or function to fire when the time is up @see GameTimer.fire |
this.timeup = 'TIMEUP';
|
¶ GameTimer.hooksArray of hook functions to fire at every update The array works as a LIFO queue @see GameTimer.fire |
this.hooks = [];
this.init(); |
TODO: remove into a new addon |
this.listeners();
}; |
¶ GameTimer methods |
|
¶ GameTimer.initInits the GameTimer Takes the configuration as an input parameter or
recycles the settings in The configuration object is of the type var options = {
milliseconds: 4000, // The length of the interval
update: 1000, // How often to update the time counter. Defaults every 1sec
timeup: 'MY_EVENT', // An event ot function to fire when the timer expires
hooks: [ myFunc, // Array of functions or events to fire at every update
'MY_EVENT_UPDATE',
{ hook: myFunc2,
ctx: that, }, Params
options
object
Optional. Configuration object
@see GameTimer.addHook
|
GameTimer.prototype.init = function (options) {
options = options || this.options;
this.status = GameTimer.UNINITIALIZED;
if (this.timer) clearInterval(this.timer);
this.milliseconds = options.milliseconds || 0;
this.timeLeft = this.milliseconds;
this.timePassed = 0;
this.update = options.update || 1000;
this.timeup = options.timeup || 'TIMEUP'; // event to be fire when timer is expired |
TODO: update and milliseconds must be multiple now |
if (options.hooks) {
for (var i=0; i < options.hooks.length; i++){
this.addHook(options.hooks[i]);
}
}
this.status = GameTimer.INITIALIZED;
}; |
¶ GameTimer.fireFires a registered hook If it is a string it is emitted as an event, otherwise it called as a function. Params
h
mixed
The hook to fire
|
GameTimer.prototype.fire = function (h) {
if (!h && !h.hook) return;
var hook = h.hook || h;
if ('function' === typeof hook) {
var ctx = h.ctx || node.game;
hook.call(ctx);
}
else {
node.emit(hook);
}
};
|
¶ GameTimer.startStarts the timer Updates the status of the timer and calls When the timer expires the timeup event is fired, and the timer is stopped @see GameTimer.status @see GameTimer.timeup @see GameTimer.fire |
GameTimer.prototype.start = function() {
this.status = GameTimer.LOADING; |
fire the event immediately if time is zero |
if (this.options.milliseconds === 0) {
node.emit(this.timeup);
return;
}
var that = this;
this.timer = setInterval(function() {
that.status = GameTimer.RUNNING;
node.log('interval started: ' + that.timeLeft, 'DEBUG', 'GameTimer: ');
that.timePassed = that.timePassed + that.update;
that.timeLeft = that.milliseconds - that.timePassed; |
Fire custom hooks from the latest to the first if any |
for (var i = that.hooks.length; i > 0; i--) {
that.fire(that.hooks[(i-1)]);
} |
Fire Timeup Event |
if (that.timeLeft <= 0) { |
First stop the timer and then call the timeup |
that.stop();
that.fire(that.timeup);
node.log('time is up: ' + that.timeup, 'DEBUG', 'GameTimer: ');
}
}, this.update);
};
|
¶ GameTimer.addHookAdd an hook to the hook list after performing conformity checks. The first parameter hook can be a string, a function, or an object containing an hook property. |
GameTimer.prototype.addHook = function (hook, ctx) {
if (!hook) return;
var ctx = ctx || node.game;
if (hook.hook) {
ctx = hook.ctx || ctx;
var hook = hook.hook;
}
this.hooks.push({hook: hook, ctx: ctx});
}; |
¶ GameTimer.pausePauses the timer If the timer was running, clear the interval and sets the
status property to |
GameTimer.prototype.pause = function() {
if (this.status > 0) {
this.status = GameTimer.PAUSED; |
console.log('Clearing Interval... pause') |
clearInterval(this.timer);
}
}; |
¶ GameTimer.resumeResumes a paused timer If the timer was paused, restarts it with the current configuration @see GameTimer.restart |
GameTimer.prototype.resume = function() {
if (this.status !== GameTimer.PAUSED) return; // timer was not paused
var options = JSUS.extend({milliseconds: this.milliseconds - this.timePassed}, this.options);
this.restart(options);
}; |
¶ GameTimer.stopStops the timer If the timer was paused or running, clear the interval, sets the
status property to |
GameTimer.prototype.stop = function() {
if (this.status === GameTimer.UNINITIALIZED) return;
if (this.status === GameTimer.INITIALIZED) return;
if (this.status === GameTimer.STOPPED) return;
this.status = GameTimer.STOPPED;
clearInterval(this.timer);
this.timePassed = 0;
this.timeLeft = null;
}; |
¶ GameTimer.restartRestarts the timer Uses the input parameter as configuration object, or the current settings, if undefined Params
options
object
Optional. A configuration object
@see GameTimer.init
|
GameTimer.prototype.restart = function (options) {
this.init(options);
this.start();
}; |
¶ GameTimer.listenersExperimental. Undocumented (for now) |
GameTimer.prototype.listeners = function () {
var that = this; |
}; |
|
¶ Closure |
})(
'undefined' != typeof node ? node : module.exports
, 'undefined' != typeof node ? node : module.parent.exports
);
|