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
185 lines (140 loc) · 4.21 KB
/
Copy pathClock.js
File metadata and controls
185 lines (140 loc) · 4.21 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
var Class = require('../utils/Class');
var TimerEvent = require('./TimerEvent');
var Clock = new Class({
initialize:
function Clock (scene)
{
this.scene = scene;
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
this.timeScale = 1;
this.paused = false;
this._active = [];
this._pendingInsertion = [];
this._pendingRemoval = [];
},
addEvent: function (config)
{
var event = new TimerEvent(config);
this._pendingInsertion.push(event);
return event;
},
delayedCall: function (delay, callback, args, callbackScope)
{
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
},
clearPendingEvents: function ()
{
this._pendingInsertion = [];
},
removeAllEvents: function ()
{
this._pendingRemoval = this._pendingRemoval.concat(this._active);
return this;
},
begin: 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];
event.elapsed = event.startAt * event.timeScale;
this._active.push(event);
}
// Clear the lists
this._pendingRemoval.length = 0;
this._pendingInsertion.length = 0;
},
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);
}
}
}
},
// Scene that owns this Clock is shutting down
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;
},
// Game level nuke
destroy: function ()
{
this.shutdown();
this.scene = undefined;
}
});
module.exports = Clock;