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}
|
|
|
|
*/
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
var GetFastValue = require('../utils/object/GetFastValue');
|
|
|
|
|
2018-03-21 14:02:10 +00:00
|
|
|
/**
|
|
|
|
* @typedef {object} TimerEventConfig
|
|
|
|
*
|
|
|
|
* @property {number} [delay=0] - [description]
|
|
|
|
* @property {number} [repeat=0] - [description]
|
|
|
|
* @property {boolean} [loop=false] - [description]
|
|
|
|
* @property {function} [callback] - [description]
|
|
|
|
* @property {*} [callbackScope] - [description]
|
2018-03-22 12:51:30 +00:00
|
|
|
* @property {Array.<*>} [args] - [description]
|
2018-03-21 14:02:10 +00:00
|
|
|
* @property {number} [timeScale=1] - [description]
|
|
|
|
* @property {number} [startAt=1] - [description]
|
|
|
|
* @property {boolean} [paused=false] - [description]
|
|
|
|
*/
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class TimerEvent
|
|
|
|
* @memberOf Phaser.Time
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-21 14:02:10 +00:00
|
|
|
* @param {TimerEventConfig} config - [description]
|
2018-02-10 02:31:42 +00:00
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
var TimerEvent = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function TimerEvent (config)
|
2017-06-28 16:39:40 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* The delay in ms at which this TimerEvent fires.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#delay
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @readOnly
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.delay = 0;
|
|
|
|
|
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* The total number of times this TimerEvent will repeat before finishing.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#repeat
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @readOnly
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-29 13:05:49 +00:00
|
|
|
this.repeat = 0;
|
|
|
|
|
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* If repeating this contains the current repeat count.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#repeatCount
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.repeatCount = 0;
|
|
|
|
|
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* True if this TimerEvent loops, otherwise false.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#loop
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @readOnly
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.loop = false;
|
|
|
|
|
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* The callback that will be called when the TimerEvent occurs.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#callback
|
|
|
|
* @type {function}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.callback;
|
2017-06-28 16:39:40 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* The scope in which the callback will be called.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#callbackScope
|
|
|
|
* @type {object}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.callbackScope;
|
2017-06-28 16:39:40 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
/**
|
2018-02-10 02:31:42 +00:00
|
|
|
* Additional arguments to be passed to the callback.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#args
|
|
|
|
* @type {array}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.args;
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* Scale the time causing this TimerEvent to update.
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#timeScale
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.timeScale = 1;
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* Start this many MS into the elapsed (useful if you want a long duration with repeat, but for the first loop to fire quickly)
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#startAt
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.startAt = 0;
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#elapsed
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.elapsed = 0;
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#paused
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.paused = false;
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Time.TimerEvent#hasDispatched
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
this.hasDispatched = false;
|
|
|
|
|
|
|
|
this.reset(config);
|
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#reset
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-21 14:02:10 +00:00
|
|
|
* @param {TimerEventConfig} config - [description]
|
2018-02-10 02:31:42 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Time.TimerEvent} This TimerEvent object.
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
reset: function (config)
|
|
|
|
{
|
|
|
|
this.delay = GetFastValue(config, 'delay', 0);
|
|
|
|
|
2017-06-29 13:05:49 +00:00
|
|
|
// Can also be set to -1 for an infinite loop (same as setting loop: true)
|
|
|
|
this.repeat = GetFastValue(config, 'repeat', 0);
|
2017-06-28 21:19:41 +00:00
|
|
|
|
|
|
|
this.loop = GetFastValue(config, 'loop', false);
|
|
|
|
|
|
|
|
this.callback = GetFastValue(config, 'callback', undefined);
|
|
|
|
|
|
|
|
this.callbackScope = GetFastValue(config, 'callbackScope', this.callback);
|
|
|
|
|
|
|
|
this.args = GetFastValue(config, 'args', []);
|
|
|
|
|
|
|
|
this.timeScale = GetFastValue(config, 'timeScale', 1);
|
|
|
|
|
|
|
|
this.startAt = GetFastValue(config, 'startAt', 0);
|
|
|
|
|
|
|
|
this.paused = GetFastValue(config, 'paused', false);
|
|
|
|
|
2018-01-23 15:34:33 +00:00
|
|
|
this.elapsed = this.startAt;
|
2017-06-28 21:19:41 +00:00
|
|
|
this.hasDispatched = false;
|
2017-06-29 13:05:49 +00:00
|
|
|
this.repeatCount = (this.repeat === -1 || this.loop) ? 999999999999 : this.repeat;
|
2017-06-28 21:19:41 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* Gets the progress of the current iteration, not factoring in repeats.
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#getProgress
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {number} [description]
|
|
|
|
*/
|
2017-06-28 16:17:54 +00:00
|
|
|
getProgress: function ()
|
|
|
|
{
|
|
|
|
return (this.elapsed / this.delay);
|
2017-06-28 16:39:40 +00:00
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* Gets the progress of the timer overall, factoring in repeats.
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#getOverallProgress
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {number} [description]
|
|
|
|
*/
|
2017-06-29 13:05:49 +00:00
|
|
|
getOverallProgress: function ()
|
|
|
|
{
|
|
|
|
if (this.repeat > 0)
|
|
|
|
{
|
|
|
|
var totalDuration = this.delay + (this.delay * this.repeat);
|
|
|
|
var totalElapsed = this.elapsed + (this.delay * (this.repeat - this.repeatCount));
|
|
|
|
|
|
|
|
return (totalElapsed / totalDuration);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this.getProgress();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#getRepeatCount
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {number} [description]
|
|
|
|
*/
|
2017-06-29 13:05:49 +00:00
|
|
|
getRepeatCount: function ()
|
|
|
|
{
|
|
|
|
return this.repeatCount;
|
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#getElapsed
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {number} [description]
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
getElapsed: function ()
|
2017-06-28 16:39:40 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
return this.elapsed;
|
2017-06-28 16:39:40 +00:00
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#getElapsedSeconds
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {number} [description]
|
|
|
|
*/
|
2017-06-28 21:19:41 +00:00
|
|
|
getElapsedSeconds: function ()
|
2017-06-28 16:39:40 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
return this.elapsed * 0.001;
|
2017-06-28 16:39:40 +00:00
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#remove
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {function} dispatchCallback - [description]
|
|
|
|
*/
|
2017-06-28 16:39:40 +00:00
|
|
|
remove: function (dispatchCallback)
|
|
|
|
{
|
|
|
|
if (dispatchCallback === undefined) { dispatchCallback = false; }
|
|
|
|
|
|
|
|
this.elapsed = this.delay;
|
|
|
|
|
2018-03-09 10:47:45 +00:00
|
|
|
this.hasDispatched = !dispatchCallback;
|
2017-06-28 16:39:40 +00:00
|
|
|
|
|
|
|
this.repeatCount = 0;
|
|
|
|
},
|
|
|
|
|
2018-02-10 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Time.TimerEvent#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-28 16:39:40 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.callback = undefined;
|
|
|
|
this.callbackScope = undefined;
|
2017-11-06 04:49:57 +00:00
|
|
|
this.args = [];
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
});
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
module.exports = TimerEvent;
|