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
388 lines (330 loc) · 9.12 KB
/
Copy pathClock.js
File metadata and controls
388 lines (330 loc) · 9.12 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
/**
* @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 PluginCache = require('../plugins/PluginCache');
var TimerEvent = require('./TimerEvent');
/**
* @classdesc
* [description]
*
* @class Clock
* @memberOf Phaser.Time
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
var Clock = new Class({
initialize:
function Clock (scene)
{
/**
* [description]
*
* @name Phaser.Time.Clock#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* [description]
*
* @name Phaser.Time.Clock#systems
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.systems = scene.sys;
/**
* [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
/**
* [description]
*
* @name Phaser.Time.Clock#timeScale
* @type {number}
* @default 1
* @since 3.0.0
*/
this.timeScale = 1;
/**
* [description]
*
* @name Phaser.Time.Clock#paused
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.paused = false;
/**
* [description]
*
* @name Phaser.Time.Clock#_active
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._active = [];
/**
* [description]
*
* @name Phaser.Time.Clock#_pendingInsertion
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingInsertion = [];
/**
* [description]
*
* @name Phaser.Time.Clock#_pendingRemoval
* @type {Phaser.Time.TimerEvent[]}
* @private
* @default []
* @since 3.0.0
*/
this._pendingRemoval = [];
scene.sys.events.once('boot', this.boot, this);
scene.sys.events.on('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 ()
{
this.systems.events.once('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('preupdate', this.preUpdate, this);
eventEmitter.on('update', this.update, this);
eventEmitter.once('shutdown', this.shutdown, this);
},
/**
* [description]
*
* @method Phaser.Time.Clock#addEvent
* @since 3.0.0
*
* @param {TimerEventConfig} config - [description]
*
* @return {Phaser.Time.TimerEvent} [description]
*/
addEvent: function (config)
{
var event = new TimerEvent(config);
this._pendingInsertion.push(event);
return event;
},
/**
* [description]
*
* @method Phaser.Time.Clock#delayedCall
* @since 3.0.0
*
* @param {number} delay - [description]
* @param {function} callback - [description]
* @param {Array.<*>} args - [description]
* @param {*} callbackScope - [description]
*
* @return {Phaser.Time.TimerEvent} [description]
*/
delayedCall: function (delay, callback, args, callbackScope)
{
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
},
/**
* [description]
*
* @method Phaser.Time.Clock#clearPendingEvents
* @since 3.0.0
*
* @return {Phaser.Time.Clock} [description]
*/
clearPendingEvents: function ()
{
this._pendingInsertion = [];
return this;
},
/**
* [description]
*
* @method Phaser.Time.Clock#removeAllEvents
* @since 3.0.0
*
* @return {Phaser.Time.Clock} [description]
*/
removeAllEvents: function ()
{
this._pendingRemoval = this._pendingRemoval.concat(this._active);
return this;
},
/**
* [description]
*
* @method Phaser.Time.Clock#preUpdate
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
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;
},
/**
* [description]
*
* @method Phaser.Time.Clock#update
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
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('preupdate', this.preUpdate, this);
eventEmitter.off('update', this.update, this);
eventEmitter.off('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('start', this.start, this);
this.scene = null;
this.systems = null;
}
});
PluginCache.register('Clock', Clock, 'time');
module.exports = Clock;