forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.js
More file actions
400 lines (341 loc) · 11.5 KB
/
Copy pathClock.js
File metadata and controls
400 lines (341 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var PluginCache = require('../plugins/PluginCache');
var SceneEvents = require('../scene/events');
var TimerEvent = require('./TimerEvent');
/**
* @classdesc
* The Clock is a Scene plugin which creates and updates Timer Events for its Scene.
*
* @class Clock
* @memberof Phaser.Time
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene which owns this Clock.
*/
var Clock = new Class({
initialize:
function Clock (scene)
{
/**
* The Scene which owns this Clock.
*
* @name Phaser.Time.Clock#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* The Scene Systems object of the Scene which owns this Clock.
*
* @name Phaser.Time.Clock#systems
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.systems = scene.sys;
/**
* 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.
*
* @name Phaser.Time.Clock#now
* @type {number}
* @since 3.0.0
*/
this.now = 0;
// Scale the delta time coming into the Clock by this factor
// which then influences anything using this Clock for calculations, like TimerEvents
/**
* The scale of the Clock's time delta.
*
* 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.
*
* @name Phaser.Time.Clock#timeScale
* @type {number}
* @default 1
* @since 3.0.0
*/
this.timeScale = 1;
/**
* Whether the Clock is paused (`true`) or active (`false`).
*
* When paused, the Clock will not update any of its Timer Events, thus freezing time.
*
* @name Phaser.Time.Clock#paused
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.paused = false;
/**
* An array of all Timer Events whose delays haven't expired - these are actively updating Timer Events.
*
* @name Phaser.Time.Clock#_active
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._active = [];
/**
* An array of all Timer Events which will be added to the Clock at the start of the frame.
*
* @name Phaser.Time.Clock#_pendingInsertion
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingInsertion = [];
/**
* An array of all Timer Events which will be removed from the Clock at the start of the frame.
*
* @name Phaser.Time.Clock#_pendingRemoval
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingRemoval = [];
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
scene.sys.events.on(SceneEvents.START, this.start, this);
},
/**
* 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 ()
{
// Sync with the TimeStep
this.now = this.systems.game.loop.time;
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
},
/**
* 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.
*
* @method Phaser.Time.Clock#start
* @private
* @since 3.5.0
*/
start: function ()
{
var eventEmitter = this.systems.events;
eventEmitter.on(SceneEvents.PRE_UPDATE, this.preUpdate, this);
eventEmitter.on(SceneEvents.UPDATE, this.update, this);
eventEmitter.once(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* Creates a Timer Event and adds it to the Clock at the start of the frame.
*
* @method Phaser.Time.Clock#addEvent
* @since 3.0.0
*
* @param {Phaser.Types.Time.TimerEventConfig} config - The configuration for the Timer Event.
*
* @return {Phaser.Time.TimerEvent} The Timer Event which was created.
*/
addEvent: function (config)
{
var event = new TimerEvent(config);
this._pendingInsertion.push(event);
return event;
},
/**
* 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).
*
* @method Phaser.Time.Clock#delayedCall
* @since 3.0.0
*
* @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.
*
* @return {Phaser.Time.TimerEvent} The Timer Event which was created.
*/
delayedCall: function (delay, callback, args, callbackScope)
{
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
},
/**
* Clears and recreates the array of pending Timer Events.
*
* @method Phaser.Time.Clock#clearPendingEvents
* @since 3.0.0
*
* @return {Phaser.Time.Clock} This Clock object.
*/
clearPendingEvents: function ()
{
this._pendingInsertion = [];
return this;
},
/**
* Schedules all active Timer Events for removal at the start of the frame.
*
* @method Phaser.Time.Clock#removeAllEvents
* @since 3.0.0
*
* @return {Phaser.Time.Clock} This Clock object.
*/
removeAllEvents: function ()
{
this._pendingRemoval = this._pendingRemoval.concat(this._active);
return this;
},
/**
* Updates the arrays of active and pending Timer Events. Called at the start of the frame.
*
* @method Phaser.Time.Clock#preUpdate
* @since 3.0.0
*
* @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.
*/
preUpdate: function ()
{
var toRemove = this._pendingRemoval.length;
var toInsert = this._pendingInsertion.length;
if (toRemove === 0 && toInsert === 0)
{
// Quick bail
return;
}
var i;
var event;
// Delete old events
for (i = 0; i < toRemove; i++)
{
event = this._pendingRemoval[i];
var index = this._active.indexOf(event);
if (index > -1)
{
this._active.splice(index, 1);
}
// Pool them?
event.destroy();
}
for (i = 0; i < toInsert; i++)
{
event = this._pendingInsertion[i];
this._active.push(event);
}
// Clear the lists
this._pendingRemoval.length = 0;
this._pendingInsertion.length = 0;
},
/**
* Updates the Clock's internal time and all of its Timer Events.
*
* @method Phaser.Time.Clock#update
* @since 3.0.0
*
* @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.
*/
update: function (time, delta)
{
this.now = time;
if (this.paused)
{
return;
}
delta *= this.timeScale;
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;
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);
}
if (event.repeatCount > 0)
{
event.repeatCount--;
event.elapsed = remainder;
event.hasDispatched = false;
}
else
{
this._pendingRemoval.push(event);
}
}
}
},
/**
* 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.
*
* @method Phaser.Time.Clock#shutdown
* @private
* @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(SceneEvents.PRE_UPDATE, this.preUpdate, this);
eventEmitter.off(SceneEvents.UPDATE, this.update, this);
eventEmitter.off(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
*
* @method Phaser.Time.Clock#destroy
* @private
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.scene.sys.events.off(SceneEvents.START, this.start, this);
this.scene = null;
this.systems = null;
}
});
PluginCache.register('Clock', Clock, 'time');
module.exports = Clock;