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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-01-04 02:53:32 +00:00
|
|
|
* A Timer is a way to create small re-usable or disposable objects that do nothing but wait for a specific moment in time, and then dispatch an event.
|
2014-01-08 01:53:28 +00:00
|
|
|
* You can add as many events to a Timer as you like, each with their own delays. A Timer uses milliseconds as its unit of time. There are 1000 ms in 1 second.
|
|
|
|
* So if you want to fire an event every quarter of a second you'd need to set the delay to 250.
|
2013-11-24 11:04:58 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Timer
|
2014-01-04 02:53:32 +00:00
|
|
|
* @classdesc A Timer is a way to create small re-usable or disposable objects that do nothing but wait for a specific moment in time, and then dispatch an event.
|
2013-11-24 11:04:58 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game A reference to the currently running game.
|
2014-01-04 02:53:32 +00:00
|
|
|
* @param {boolean} [autoDestroy=true] - A Timer that is set to automatically destroy itself will do so after all of its events have been dispatched (assuming no looping events).
|
2013-11-24 11:04:58 +00:00
|
|
|
*/
|
2014-01-08 01:53:28 +00:00
|
|
|
Phaser.Timer = function (game, autoDestroy) {
|
2014-01-04 02:53:32 +00:00
|
|
|
|
|
|
|
if (typeof autoDestroy === 'undefined') { autoDestroy = true; }
|
2013-11-24 11:04:58 +00:00
|
|
|
|
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
|
|
|
/**
|
2014-01-08 11:21:30 +00:00
|
|
|
* @property {number} _started - The time at which this Timer instance started running.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._started = 0;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-01-04 02:53:32 +00:00
|
|
|
* @property {boolean} running - True if the Timer is actively running.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.running = false;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} pauseWithGame - If true then the timer will update itself automatically if the game pauses, otherwise it will carry on dispatching regardless.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.pauseWithGame = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} autoDestroy - A Timer that is set to automatically destroy itself will do so after all of its events have been dispatched (assuming no looping events).
|
|
|
|
*/
|
|
|
|
this.autoDestroy = autoDestroy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} expired - An expired Timer is one in which all of its events have been dispatched and none are pending.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.expired = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} events - An array holding the event data.
|
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.events = [];
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
2014-01-08 11:21:30 +00:00
|
|
|
* @property {Phaser.Signal} onComplete - This signal will be dispatched when this Timer has completed, meaning there are no more events in the queue.
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
2014-01-08 11:21:30 +00:00
|
|
|
this.onComplete = new Phaser.Signal();
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
2014-01-08 01:53:28 +00:00
|
|
|
* @property {number} nextTick - The time the next tick will occur. Do not set this value directly.
|
|
|
|
* @protected
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
2014-01-08 01:53:28 +00:00
|
|
|
this.nextTick = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _now - The current start-time adjusted time.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
this._now = 0;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Timer.MINUTE = 60000;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Timer.SECOND = 1000;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Timer.HALF = 500;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Timer.QUARTER = 250;
|
|
|
|
|
|
|
|
Phaser.Timer.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Event on this Timer.
|
|
|
|
* @method Phaser.Timer#_create
|
|
|
|
* @private
|
2014-01-08 11:21:30 +00:00
|
|
|
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
|
|
|
|
* @param {boolean} loop - Should the event loop or not?
|
|
|
|
* @param {number} repeatCount - The number of times the event will repeat.
|
|
|
|
* @param {function} callback - The callback that will be called when the Timer event occurs.
|
|
|
|
* @param {object} callbackContext - The context in which the callback will be called.
|
|
|
|
* @param {array} arguments - The values to be sent to your callback function when it is called.
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
2014-01-08 11:21:30 +00:00
|
|
|
create: function (delay, loop, repeatCount, callback, callbackContext, args) {
|
|
|
|
console.log(arguments);
|
|
|
|
var tick = delay;
|
|
|
|
|
|
|
|
if (this.running)
|
|
|
|
{
|
|
|
|
tick += this._now;
|
|
|
|
}
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.events.push({
|
|
|
|
delay: delay,
|
2014-01-08 11:21:30 +00:00
|
|
|
tick: tick,
|
|
|
|
repeatCount: repeatCount - 1,
|
2014-01-04 02:53:32 +00:00
|
|
|
loop: loop,
|
2014-01-08 11:21:30 +00:00
|
|
|
callback: callback,
|
|
|
|
callbackContext: callbackContext,
|
2014-01-04 02:53:32 +00:00
|
|
|
args: args
|
2013-11-25 04:40:04 +00:00
|
|
|
});
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-08 01:53:28 +00:00
|
|
|
this.order();
|
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
this.expired = false;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
2014-01-08 01:53:28 +00:00
|
|
|
* Adds a new Event to this Timer. The event will fire after the given amount of 'delay' in milliseconds has passed, once the Timer has started running.
|
|
|
|
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
|
2014-01-08 11:21:30 +00:00
|
|
|
* If the Timer is already running the delay will be calculated based on the timers current time.
|
2014-01-04 02:53:32 +00:00
|
|
|
* @method Phaser.Timer#add
|
2014-01-08 11:21:30 +00:00
|
|
|
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
|
|
|
|
* @param {function} callback - The callback that will be called when the Timer event occurs.
|
|
|
|
* @param {object} callbackContext - The context in which the callback will be called.
|
|
|
|
* @param {...} arguments - The values to be sent to your callback function when it is called.
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
2014-01-08 11:21:30 +00:00
|
|
|
add: function (delay, callback, callbackContext) {
|
2014-01-04 02:53:32 +00:00
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
this.create(delay, false, 0, callback, callbackContext, Array.prototype.splice.call(arguments, 3));
|
2014-01-04 02:53:32 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new Event to this Timer that will repeat for the given number of iterations.
|
2014-01-08 01:53:28 +00:00
|
|
|
* The event will fire after the given amount of 'delay' milliseconds has passed once the Timer has started running.
|
|
|
|
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
|
2014-01-08 11:21:30 +00:00
|
|
|
* If the Timer is already running the delay will be calculated based on the timers current time.
|
2014-01-04 02:53:32 +00:00
|
|
|
* @method Phaser.Timer#repeat
|
2014-01-08 11:21:30 +00:00
|
|
|
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
|
|
|
|
* @param {number} repeatCount - The number of times the event will repeat.
|
|
|
|
* @param {function} callback - The callback that will be called when the Timer event occurs.
|
|
|
|
* @param {object} callbackContext - The context in which the callback will be called.
|
|
|
|
* @param {...} arguments - The values to be sent to your callback function when it is called.
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
2014-01-08 11:21:30 +00:00
|
|
|
repeat: function (delay, repeatCount, callback, callbackContext) {
|
2014-01-03 12:43:58 +00:00
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
this.create(delay, false, repeatCount, callback, callbackContext, Array.prototype.splice.call(arguments, 4));
|
2014-01-03 12:43:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* Adds a new looped Event to this Timer that will repeat forever or until the Timer is stopped.
|
2014-01-08 01:53:28 +00:00
|
|
|
* The event will fire after the given amount of 'delay' milliseconds has passed once the Timer has started running.
|
|
|
|
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
|
2014-01-08 11:21:30 +00:00
|
|
|
* If the Timer is already running the delay will be calculated based on the timers current time.
|
2014-01-04 02:53:32 +00:00
|
|
|
* @method Phaser.Timer#loop
|
2014-01-08 11:21:30 +00:00
|
|
|
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
|
|
|
|
* @param {function} callback - The callback that will be called when the Timer event occurs.
|
|
|
|
* @param {object} callbackContext - The context in which the callback will be called.
|
|
|
|
* @param {...} arguments - The values to be sent to your callback function when it is called.
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
2014-01-08 11:21:30 +00:00
|
|
|
loop: function (delay, callback, callbackContext) {
|
2014-01-03 12:43:58 +00:00
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
this.create(delay, true, 0, callback, callbackContext, Array.prototype.splice.call(arguments, 3));
|
2014-01-03 12:43:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* Starts this Timer running.
|
|
|
|
* @method Phaser.Timer#start
|
|
|
|
*/
|
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
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* Stops this Timer from running. Does not cause it to be destroyed if autoDestroy is set to true.
|
|
|
|
* @method Phaser.Timer#stop
|
|
|
|
*/
|
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
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
/**
|
|
|
|
* Orders the events on this Timer so they are in tick order.
|
|
|
|
* @method Phaser.Timer#order
|
|
|
|
*/
|
2014-01-08 01:53:28 +00:00
|
|
|
order: function () {
|
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
if (this.events.length > 0)
|
|
|
|
{
|
|
|
|
// Sort the events so the one with the lowest tick is first
|
|
|
|
this.events.sort(this.sortHandler);
|
2014-01-08 01:53:28 +00:00
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
this.nextTick = this.events[0].tick;
|
|
|
|
}
|
2014-01-08 01:53:28 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
/**
|
|
|
|
* Sort handler used by Phaser.Timer.order.
|
|
|
|
* @method Phaser.Timer#sortHandler
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-01-08 01:53:28 +00:00
|
|
|
sortHandler: function (a, b) {
|
|
|
|
|
|
|
|
if (a.tick < b.tick)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (a.tick > b.tick)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
2014-01-08 01:53:28 +00:00
|
|
|
* The main Timer update event, called automatically by the Game clock.
|
2014-01-04 02:53:32 +00:00
|
|
|
* @method Phaser.Timer#update
|
|
|
|
* @protected
|
|
|
|
* @param {number} time - The time from the core game clock.
|
2014-01-08 11:21:30 +00:00
|
|
|
* @return {boolean} True if there are still events waiting to be dispatched, otherwise false if this Timer can be destroyed.
|
2014-01-04 02:53:32 +00:00
|
|
|
*/
|
|
|
|
update: function(time) {
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-08 01:53:28 +00:00
|
|
|
this._now = time - this._started;
|
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
var len = this.events.length;
|
|
|
|
|
|
|
|
if (this.running && this._now >= this.nextTick && len > 0)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-01-08 01:53:28 +00:00
|
|
|
var i = 0;
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-08 01:53:28 +00:00
|
|
|
while (i < len)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-01-08 01:53:28 +00:00
|
|
|
if (this._now >= this.events[i].tick)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-01-03 12:43:58 +00:00
|
|
|
if (this.events[i].loop)
|
|
|
|
{
|
2014-01-08 01:53:28 +00:00
|
|
|
this.events[i].tick += this.events[i].delay - (this._now - this.events[i].tick);
|
2014-01-08 11:21:30 +00:00
|
|
|
this.events[i].callback.apply(this.events[i].callbackContext, this.events[i].args);
|
2014-01-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
else if (this.events[i].repeatCount > 0)
|
|
|
|
{
|
|
|
|
this.events[i].repeatCount--;
|
2014-01-08 01:53:28 +00:00
|
|
|
this.events[i].tick += this.events[i].delay - (this._now - this.events[i].tick);
|
2014-01-08 11:21:30 +00:00
|
|
|
this.events[i].callback.apply(this.events[i].callbackContext, this.events[i].args);
|
2014-01-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-08 11:21:30 +00:00
|
|
|
this.events[i].callback.apply(this.events[i].callbackContext, this.events[i].args);
|
2014-01-08 01:53:28 +00:00
|
|
|
this.events.splice(i, 1);
|
|
|
|
len--;
|
2014-01-03 12:43:58 +00:00
|
|
|
}
|
2014-01-04 02:53:32 +00:00
|
|
|
|
2014-01-08 01:53:28 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
2014-01-04 02:53:32 +00:00
|
|
|
{
|
2014-01-08 01:53:28 +00:00
|
|
|
break;
|
2014-01-04 02:53:32 +00:00
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-01-04 02:53:32 +00:00
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
// There are no events left
|
2014-01-08 01:53:28 +00:00
|
|
|
if (this.events.length > 0)
|
2014-01-04 02:53:32 +00:00
|
|
|
{
|
|
|
|
this.expired = true;
|
2014-01-08 11:21:30 +00:00
|
|
|
this.onComplete.dispatch(this);
|
2014-01-04 02:53:32 +00:00
|
|
|
}
|
2014-01-08 01:53:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.order();
|
|
|
|
}
|
2014-01-04 02:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.expired && this.autoDestroy)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* Destroys this Timer. Events are not dispatched.
|
|
|
|
* @method Phaser.Timer#destroy
|
|
|
|
*/
|
|
|
|
destroy: function() {
|
|
|
|
|
2014-01-08 11:21:30 +00:00
|
|
|
this.onComplete.removeAll();
|
2014-01-04 02:53:32 +00:00
|
|
|
this.running = false;
|
|
|
|
this.events = [];
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-11-24 11:04:58 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
2014-01-04 02:53:32 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Timer#ms
|
|
|
|
* @property {number} ms - The duration in milliseconds that this Timer has been running for.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Timer.prototype, "ms", {
|
|
|
|
|
|
|
|
get: function () {
|
2014-01-08 11:21:30 +00:00
|
|
|
return this._now;
|
2014-01-04 02:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Timer#seconds
|
|
|
|
* @property {number} seconds - The duration in seconds that this Timer has been running for.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Timer.prototype, "seconds", {
|
|
|
|
|
|
|
|
get: function () {
|
2014-01-08 11:21:30 +00:00
|
|
|
return this._now * 0.001;
|
2014-01-04 02:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.Timer.prototype.constructor = Phaser.Timer;
|