phaser/src/time/Clock.js

376 lines
8.8 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../utils/Class');
var PluginManager = require('../boot/PluginManager');
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-02-06 01:22:22 +00:00
* [description]
*
* @class Clock
* @memberOf Phaser.Time
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
var Clock = new Class({
initialize:
function Clock (scene)
{
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @name Phaser.Time.Clock#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @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
/**
* [description]
*
* @name Phaser.Time.Clock#now
* @type {number}
* @since 3.0.0
*/
this.now = Date.now();
// 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
/**
* [description]
*
* @name Phaser.Time.Clock#timeScale
* @type {float}
* @default 1
* @since 3.0.0
*/
this.timeScale = 1;
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @name Phaser.Time.Clock#paused
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.paused = false;
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @name Phaser.Time.Clock#_active
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._active = [];
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @name Phaser.Time.Clock#_pendingInsertion
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingInsertion = [];
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @name Phaser.Time.Clock#_pendingRemoval
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingRemoval = [];
scene.sys.events.on('start', this.start, this);
},
2017-06-28 16:17:54 +00:00
2018-02-06 01:22:22 +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
*
* @method Phaser.Time.Clock#start
* @private
* @since 3.4.1
2018-02-06 01:22:22 +00:00
*/
start: function ()
2018-01-16 02:08:22 +00:00
{
var eventEmitter = this.systems.events;
eventEmitter.on('preupdate', this.preUpdate, this);
eventEmitter.on('update', this.update, this);
eventEmitter.once('shutdown', this.shutdown, this);
eventEmitter.once('destroy', this.destroy, this);
2018-01-16 02:08:22 +00:00
},
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @method Phaser.Time.Clock#addEvent
* @since 3.0.0
*
2018-03-21 14:02:10 +00:00
* @param {TimerEventConfig} config - [description]
2018-02-06 01:22:22 +00:00
*
* @return {Phaser.Time.TimerEvent} [description]
*/
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
/**
* [description]
*
* @method Phaser.Time.Clock#delayedCall
* @since 3.0.0
*
* @param {number} delay - [description]
* @param {function} callback - [description]
2018-03-22 12:51:30 +00:00
* @param {Array.<*>} args - [description]
2018-03-21 14:02:10 +00:00
* @param {*} callbackScope - [description]
2018-02-06 01:22:22 +00:00
*
* @return {Phaser.Time.TimerEvent} [description]
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
/**
* [description]
*
* @method Phaser.Time.Clock#clearPendingEvents
* @since 3.0.0
*
2018-02-06 01:22:22 +00:00
* @return {Phaser.Time.Clock} [description]
*/
clearPendingEvents: function ()
{
this._pendingInsertion = [];
2018-02-06 01:22:22 +00:00
return this;
},
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @method Phaser.Time.Clock#removeAllEvents
* @since 3.0.0
*
* @return {Phaser.Time.Clock} [description]
*/
removeAllEvents: function ()
2017-06-28 16:17:54 +00:00
{
this._pendingRemoval = this._pendingRemoval.concat(this._active);
return this;
},
2018-02-06 01:22:22 +00:00
/**
* [description]
*
* @method Phaser.Time.Clock#preUpdate
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
2018-02-16 18:44:07 +00:00
preUpdate: function ()
{
var toRemove = this._pendingRemoval.length;
var toInsert = this._pendingInsertion.length;
if (toRemove === 0 && toInsert === 0)
{
// Quick bail
return;
}
var i;
var event;
2017-06-28 16:17:54 +00:00
// Delete old events
for (i = 0; i < toRemove; i++)
2017-06-28 16:17:54 +00:00
{
event = this._pendingRemoval[i];
var index = this._active.indexOf(event);
2017-06-28 16:17:54 +00:00
if (index > -1)
{
this._active.splice(index, 1);
}
// Pool them?
event.destroy();
2017-06-28 16:17:54 +00:00
}
for (i = 0; i < toInsert; i++)
{
event = this._pendingInsertion[i];
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
/**
* [description]
*
* @method Phaser.Time.Clock#update
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
2017-06-28 16:17:54 +00:00
update: function (time, delta)
{
this.now = time;
2017-06-28 16:17:54 +00:00
if (this.paused)
2017-06-28 16:17:54 +00:00
{
return;
2017-06-28 16:17:54 +00:00
}
2018-01-23 13:37:25 +00:00
delta *= this.timeScale;
2017-06-28 16:17:54 +00:00
for (var i = 0; i < this._active.length; i++)
{
var event = this._active[i];
if (event.paused)
{
continue;
}
// 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
if (!event.hasDispatched && event.callback)
{
event.hasDispatched = true;
event.callback.apply(event.callbackScope, event.args);
}
2017-06-28 16:17:54 +00:00
if (event.repeatCount > 0)
2017-06-28 16:17:54 +00:00
{
event.repeatCount--;
event.elapsed = remainder;
event.hasDispatched = false;
2017-06-28 16:17:54 +00:00
}
else
{
this._pendingRemoval.push(event);
}
}
}
},
2017-06-28 16:17:54 +00:00
2018-02-06 01:22:22 +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
* @private
2018-02-06 01:22:22 +00:00
* @since 3.0.0
*/
shutdown: function ()
{
var i;
for (i = 0; i < this._pendingInsertion.length; i++)
{
this._pendingInsertion[i].destroy();
}
for (i = 0; i < this._active.length; i++)
{
this._active[i].destroy();
}
for (i = 0; i < this._pendingRemoval.length; i++)
{
this._pendingRemoval[i].destroy();
}
this._active.length = 0;
this._pendingRemoval.length = 0;
this._pendingInsertion.length = 0;
var eventEmitter = this.systems.events;
eventEmitter.off('preupdate', this.preUpdate, this);
eventEmitter.off('update', this.update, this);
eventEmitter.off('shutdown', this.shutdown, this);
},
2018-02-06 01:22:22 +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
* @private
2018-02-06 01:22:22 +00:00
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.scene.sys.events.off('start', this.start, this);
this.scene = null;
this.systems = null;
}
2017-06-28 16:17:54 +00:00
});
2017-06-28 16:17:54 +00:00
PluginManager.register('Clock', Clock, 'time');
2018-01-16 02:08:22 +00:00
2017-06-28 16:17:54 +00:00
module.exports = Clock;