2013-11-24 11:04:58 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer constructor.
|
|
|
|
*
|
|
|
|
* @class Phaser.Timer
|
|
|
|
* @classdesc A Timer
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game A reference to the currently running game.
|
|
|
|
*/
|
|
|
|
Phaser.Timer = function (game) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* The time at which this Timer instance started.
|
|
|
|
* @property {number} _started
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._started = 0;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* The time (in ms) that the last second counter ticked over.
|
|
|
|
* @property {number} _timeLastSecond
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._timeLastSecond = 0;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.running = false;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.events = [];
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onEvent = new Phaser.Signal();
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// Need to add custom FPS rate, for now we'll just use seconds
|
2013-11-24 11:04:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Phaser.Timer.prototype = {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// delay could be from now, when the timer is created, or relative to an already running timer
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// add: function (delay, callback, callbackContext) {
|
|
|
|
add: function (delay) {
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.events.push({
|
|
|
|
delay: delay,
|
|
|
|
dispatched: false,
|
|
|
|
args: Array.prototype.splice.call(arguments, 1)
|
|
|
|
});
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// this.events.push({
|
|
|
|
// delay: delay,
|
|
|
|
// dispatched: false,
|
|
|
|
// callback: callback,
|
|
|
|
// callbackContext: callbackContext,
|
|
|
|
// args: Array.prototype.splice.call(arguments, 3)
|
|
|
|
// });
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
start: function() {
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._started = this.game.time.now;
|
|
|
|
this.running = true;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// sort the events based on delay here, also don't run unless events is populated
|
|
|
|
// add ability to auto-stop once all events are done
|
|
|
|
// add support for maximum duration
|
|
|
|
// add support for delay before starting
|
|
|
|
// add signals?
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
stop: function() {
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.running = false;
|
|
|
|
this.events.length = 0;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
update: function() {
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// TODO: Game Paused support
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.running)
|
|
|
|
{
|
|
|
|
var seconds = this.seconds();
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
for (var i = 0, len = this.events.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if (this.events[i].dispatched === false && seconds >= this.events[i].delay)
|
|
|
|
{
|
|
|
|
this.events[i].dispatched = true;
|
|
|
|
// this.events[i].callback.apply(this.events[i].callbackContext, this.events[i].args);
|
|
|
|
this.onEvent.dispatch.apply(this, this.events[i].args);
|
|
|
|
// ought to slice it now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
seconds: function() {
|
|
|
|
return (this.game.time.now - this._started) * 0.001;
|
|
|
|
}
|
2013-11-24 11:04:58 +00:00
|
|
|
|
|
|
|
}
|