2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
var Class = require('../utils/Class');
|
2018-05-15 11:51:50 +00:00
|
|
|
var PluginCache = require('../plugins/PluginCache');
|
2017-06-28 16:17:54 +00:00
|
|
|
var TimerEvent = require('./TimerEvent');
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* @classdesc
|
2018-10-19 11:32:43 +00:00
|
|
|
* The Clock is a Scene plugin which creates and updates Timer Events for its Scene.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @class Clock
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Time
|
2018-02-06 01:22:22 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @param {Phaser.Scene} scene - The Scene which owns this Clock.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
var Clock = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function Clock (scene)
|
2017-06-28 21:19:41 +00:00
|
|
|
{
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* The Scene which owns this Clock.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-14 13:30:20 +00:00
|
|
|
this.scene = scene;
|
2017-06-28 21:19:41 +00:00
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* The Scene Systems object of the Scene which owns this Clock.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#systems
|
|
|
|
* @type {Phaser.Scenes.Systems}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-16 02:08:22 +00:00
|
|
|
this.systems = scene.sys;
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* The current time of the Clock, in milliseconds.
|
|
|
|
*
|
|
|
|
* If accessed externally, this is equivalent to the `time` parameter normally passed to a Scene's `update` method.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#now
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.now = Date.now();
|
|
|
|
|
2017-06-29 13:05:27 +00:00
|
|
|
// Scale the delta time coming into the Clock by this factor
|
|
|
|
// which then influences anything using this Clock for calculations, like TimerEvents
|
2018-02-06 01:22:22 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* The scale of the Clock's time delta.
|
2018-10-25 15:26:34 +00:00
|
|
|
*
|
|
|
|
* The time delta is the time elapsed between two consecutive frames and influences the speed of time for this Clock and anything which uses it, such as its Timer Events. Values higher than 1 increase the speed of time, while values smaller than 1 decrease it. A value of 0 freezes time and is effectively equivalent to pausing the Clock.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#timeScale
|
2018-06-26 22:19:14 +00:00
|
|
|
* @type {number}
|
2018-02-06 01:22:22 +00:00
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-29 13:05:27 +00:00
|
|
|
this.timeScale = 1;
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Whether the Clock is paused (`true`) or active (`false`).
|
|
|
|
*
|
|
|
|
* When paused, the Clock will not update any of its Timer Events, thus freezing time.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#paused
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.paused = false;
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* An array of all Timer Events whose delays haven't expired - these are actively updating Timer Events.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#_active
|
|
|
|
* @type {Phaser.Time.TimerEvent[]}
|
|
|
|
* @private
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this._active = [];
|
2018-02-06 01:22:22 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* An array of all Timer Events which will be added to the Clock at the start of the frame.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#_pendingInsertion
|
|
|
|
* @type {Phaser.Time.TimerEvent[]}
|
|
|
|
* @private
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this._pendingInsertion = [];
|
2018-02-06 01:22:22 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* An array of all Timer Events which will be removed from the Clock at the start of the frame.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Time.Clock#_pendingRemoval
|
|
|
|
* @type {Phaser.Time.TimerEvent[]}
|
|
|
|
* @private
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this._pendingRemoval = [];
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2018-04-17 11:25:45 +00:00
|
|
|
scene.sys.events.once('boot', this.boot, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
scene.sys.events.on('start', this.start, this);
|
2017-06-28 21:19:41 +00:00
|
|
|
},
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2018-04-17 11:25:45 +00:00
|
|
|
/**
|
|
|
|
* This method is called automatically, only once, when the Scene is first created.
|
|
|
|
* Do not invoke it directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#boot
|
|
|
|
* @private
|
|
|
|
* @since 3.5.1
|
|
|
|
*/
|
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
this.systems.events.once('destroy', this.destroy, this);
|
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* This method is called automatically by the Scene when it is starting up.
|
|
|
|
* It is responsible for creating local systems, properties and listening for Scene events.
|
|
|
|
* Do not invoke it directly.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
2018-04-13 16:12:17 +00:00
|
|
|
* @method Phaser.Time.Clock#start
|
|
|
|
* @private
|
2018-04-15 11:44:47 +00:00
|
|
|
* @since 3.5.0
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2018-04-13 16:12:17 +00:00
|
|
|
start: function ()
|
2018-01-16 02:08:22 +00:00
|
|
|
{
|
2018-01-18 14:00:31 +00:00
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
|
|
|
eventEmitter.on('preupdate', this.preUpdate, this);
|
|
|
|
eventEmitter.on('update', this.update, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
eventEmitter.once('shutdown', this.shutdown, this);
|
2018-01-16 02:08:22 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Creates a Timer Event and adds it to the Clock at the start of the frame.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#addEvent
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @param {TimerEventConfig} config - The configuration for the Timer Event.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @return {Phaser.Time.TimerEvent} The Timer Event which was created.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2017-06-28 16:17:54 +00:00
|
|
|
addEvent: function (config)
|
|
|
|
{
|
|
|
|
var event = new TimerEvent(config);
|
|
|
|
|
|
|
|
this._pendingInsertion.push(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Creates a Timer Event and adds it to the Clock at the start of the frame.
|
|
|
|
*
|
|
|
|
* This is a shortcut for {@link #addEvent} which can be shorter and is compatible with the syntax of the GreenSock Animation Platform (GSAP).
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#delayedCall
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @param {number} delay - The delay of the function call, in milliseconds.
|
|
|
|
* @param {function} callback - The function to call after the delay expires.
|
|
|
|
* @param {Array.<*>} args - The arguments to call the function with.
|
|
|
|
* @param {*} callbackScope - The scope (`this` object) to call the function with.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @return {Phaser.Time.TimerEvent} The Timer Event which was created.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2017-06-28 16:17:54 +00:00
|
|
|
delayedCall: function (delay, callback, args, callbackScope)
|
|
|
|
{
|
|
|
|
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
|
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Clears and recreates the array of pending Timer Events.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#clearPendingEvents
|
|
|
|
* @since 3.0.0
|
2018-03-19 01:03:17 +00:00
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @return {Phaser.Time.Clock} This Clock object.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
clearPendingEvents: function ()
|
|
|
|
{
|
|
|
|
this._pendingInsertion = [];
|
2018-02-06 01:22:22 +00:00
|
|
|
|
|
|
|
return this;
|
2017-06-28 21:19:41 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Schedules all active Timer Events for removal at the start of the frame.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#removeAllEvents
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @return {Phaser.Time.Clock} This Clock object.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
removeAllEvents: function ()
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
this._pendingRemoval = this._pendingRemoval.concat(this._active);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Updates the arrays of active and pending Timer Events. Called at the start of the frame.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#preUpdate
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-13 07:09:44 +00:00
|
|
|
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
|
|
|
|
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2018-02-16 18:44:07 +00:00
|
|
|
preUpdate: function ()
|
2017-06-28 21:19:41 +00:00
|
|
|
{
|
|
|
|
var toRemove = this._pendingRemoval.length;
|
|
|
|
var toInsert = this._pendingInsertion.length;
|
|
|
|
|
|
|
|
if (toRemove === 0 && toInsert === 0)
|
|
|
|
{
|
|
|
|
// Quick bail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i;
|
2017-06-29 13:05:27 +00:00
|
|
|
var event;
|
2017-06-28 21:19:41 +00:00
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
// Delete old events
|
2017-06-28 21:19:41 +00:00
|
|
|
for (i = 0; i < toRemove; i++)
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
2017-06-29 13:05:27 +00:00
|
|
|
event = this._pendingRemoval[i];
|
2017-06-28 16:39:40 +00:00
|
|
|
|
|
|
|
var index = this._active.indexOf(event);
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
{
|
|
|
|
this._active.splice(index, 1);
|
|
|
|
}
|
2017-06-28 16:39:40 +00:00
|
|
|
|
|
|
|
// Pool them?
|
|
|
|
event.destroy();
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
for (i = 0; i < toInsert; i++)
|
|
|
|
{
|
2017-06-29 13:05:27 +00:00
|
|
|
event = this._pendingInsertion[i];
|
2017-06-28 21:19:41 +00:00
|
|
|
|
|
|
|
this._active.push(event);
|
|
|
|
}
|
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
// Clear the lists
|
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Updates the Clock's internal time and all of its Timer Events.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-13 07:09:44 +00:00
|
|
|
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
|
|
|
|
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
|
2018-02-06 01:22:22 +00:00
|
|
|
*/
|
2017-06-28 16:17:54 +00:00
|
|
|
update: function (time, delta)
|
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
this.now = time;
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
if (this.paused)
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
return;
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 13:37:25 +00:00
|
|
|
delta *= this.timeScale;
|
2017-06-29 13:05:27 +00:00
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
for (var i = 0; i < this._active.length; i++)
|
|
|
|
{
|
|
|
|
var event = this._active[i];
|
|
|
|
|
2017-06-28 16:39:40 +00:00
|
|
|
if (event.paused)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
// Use delta time to increase elapsed.
|
|
|
|
// Avoids needing to adjust for pause / resume.
|
|
|
|
// Automatically smoothed by TimeStep class.
|
|
|
|
// In testing accurate to +- 1ms!
|
|
|
|
event.elapsed += delta * event.timeScale;
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
if (event.elapsed >= event.delay)
|
|
|
|
{
|
|
|
|
var remainder = event.elapsed - event.delay;
|
|
|
|
|
|
|
|
// Limit it, in case it's checked in the callback
|
|
|
|
event.elapsed = event.delay;
|
|
|
|
|
|
|
|
// Process the event
|
2017-06-28 21:19:41 +00:00
|
|
|
if (!event.hasDispatched && event.callback)
|
2017-06-28 16:39:40 +00:00
|
|
|
{
|
|
|
|
event.hasDispatched = true;
|
|
|
|
event.callback.apply(event.callbackScope, event.args);
|
|
|
|
}
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2017-06-29 13:05:27 +00:00
|
|
|
if (event.repeatCount > 0)
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
|
|
|
event.repeatCount--;
|
|
|
|
|
|
|
|
event.elapsed = remainder;
|
2017-06-28 16:39:40 +00:00
|
|
|
event.hasDispatched = false;
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._pendingRemoval.push(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 21:19:41 +00:00
|
|
|
},
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* The Scene that owns this plugin is shutting down.
|
|
|
|
* We need to kill and reset all internal properties as well as stop listening to Scene events.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#shutdown
|
2018-04-13 16:12:17 +00:00
|
|
|
* @private
|
2018-02-06 01:22:22 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 02:31:31 +00:00
|
|
|
shutdown: function ()
|
2017-06-28 21:19:41 +00:00
|
|
|
{
|
2017-06-30 02:31:31 +00:00
|
|
|
var i;
|
|
|
|
|
|
|
|
for (i = 0; i < this._pendingInsertion.length; i++)
|
|
|
|
{
|
|
|
|
this._pendingInsertion[i].destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < this._active.length; i++)
|
2017-06-28 21:19:41 +00:00
|
|
|
{
|
|
|
|
this._active[i].destroy();
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
for (i = 0; i < this._pendingRemoval.length; i++)
|
|
|
|
{
|
|
|
|
this._pendingRemoval[i].destroy();
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
this._active.length = 0;
|
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
2018-04-13 16:12:17 +00:00
|
|
|
|
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
|
|
|
eventEmitter.off('preupdate', this.preUpdate, this);
|
|
|
|
eventEmitter.off('update', this.update, this);
|
|
|
|
eventEmitter.off('shutdown', this.shutdown, this);
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 01:22:22 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* The Scene that owns this plugin is being destroyed.
|
|
|
|
* We need to shutdown and then kill off all external references.
|
2018-02-06 01:22:22 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Time.Clock#destroy
|
2018-04-13 16:12:17 +00:00
|
|
|
* @private
|
2018-02-06 01:22:22 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 02:31:31 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.shutdown();
|
|
|
|
|
2018-04-13 16:12:17 +00:00
|
|
|
this.scene.sys.events.off('start', this.start, this);
|
|
|
|
|
|
|
|
this.scene = null;
|
|
|
|
this.systems = null;
|
2017-06-28 21:19:41 +00:00
|
|
|
}
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
});
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2018-05-15 11:51:50 +00:00
|
|
|
PluginCache.register('Clock', Clock, 'time');
|
2018-01-16 02:08:22 +00:00
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
module.exports = Clock;
|