forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeStep.js
More file actions
378 lines (282 loc) · 10.2 KB
/
Copy pathTimeStep.js
File metadata and controls
378 lines (282 loc) · 10.2 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
var Class = require('../utils/Class');
var GetValue = require('../utils/object/GetValue');
var NOOP = require('../utils/NOOP');
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
// Frame Rate config
// fps: {
// min: 10,
// target: 60,
// max: 120
// forceSetTimeOut: false,
// deltaHistory: 10,
// panicMax: 120
// }
// http://www.testufo.com/#test=animation-time-graph
var TimeStep = new Class({
initialize:
function TimeStep (game, config)
{
this.game = game;
this.raf = new RequestAnimationFrame();
this.started = false;
this.running = false;
this.minFps = GetValue(config, 'min', 5);
this.maxFps = GetValue(config, 'max', 120);
this.targetFps = GetValue(config, 'target', 60);
this._min = 1000 / this.minFps; // 200ms between frames (i.e. super slow!)
this._max = 1000 / this.maxFps; // 8.333ms between frames (i.e. super fast, 120Hz displays)
this._target = 1000 / this.targetFps; // 16.666ms between frames (i.e. normal)
// 200 / 1000 = 0.2 (5fps)
// 8.333 / 1000 = 0.008333 (120fps)
// 16.666 / 1000 = 0.01666 (60fps)
/**
* @property {number} fps - An exponential moving average of the frames per second.
* @readOnly
*/
this.actualFps = this.targetFps;
this.nextFpsUpdate = 0;
this.framesThisSecond = 0;
this.callback = NOOP;
this.forceSetTimeOut = GetValue(config, 'forceSetTimeOut', false);
this.time = 0;
this.startTime = 0;
this.lastTime = 0;
this.frame = 0;
this.inFocus = true;
this._pauseTime = 0;
this._coolDown = 0;
this.delta = 0;
this.deltaIndex = 0;
this.deltaHistory = [];
this.deltaSmoothingMax = GetValue(config, 'deltaHistory', 10);
this.panicMax = GetValue(config, 'panicMax', 120);
// The actual elapsed time in ms between one update and the next.
// No smoothing, no capping, no averaging. So please be aware of this when using the contents of this property.
this.rawDelta = 0;
},
// Called when the DOM window.onBlur event triggers
blur: function ()
{
this.inFocus = false;
// console.log('TimeStep.blur');
},
// Called when the DOM window.onFocus event triggers
focus: function ()
{
this.inFocus = true;
// console.log('TimeStep.focus');
this.resetDelta();
},
// Called when the visibility API says the game is 'hidden' (tab switch, etc)
pause: function ()
{
// console.log('TimeStep.pause');
this._pauseTime = window.performance.now();
},
// Called when the visibility API says the game is 'visible' again (tab switch, etc)
resume: function ()
{
this.resetDelta();
this.startTime += this.time - this._pauseTime;
// console.log('TimeStep.resume - paused for', (this.time - this._pauseTime));
},
resetDelta: function ()
{
var now = window.performance.now();
this.time = now;
this.lastTime = now;
this.nextFpsUpdate = now + 1000;
this.framesThisSecond = 0;
this.frame = 0;
// Pre-populate smoothing array
for (var i = 0; i < this.deltaSmoothingMax; i++)
{
this.deltaHistory[i] = this._target;
}
this.delta = 0;
this.deltaIndex = 0;
this._coolDown = this.panicMax;
},
start: function (callback)
{
if (this.started)
{
return this;
}
this.started = true;
this.running = true;
this.deltaHistory = [];
this.resetDelta();
this.startTime = window.performance.now();
this.callback = callback;
this.raf.start(this.step.bind(this), this.forceSetTimeOut);
},
// time comes from requestAnimationFrame and is either a high res time value,
// or Date.now if using setTimeout
step: function (time)
{
// Debug only
// var debug = 0;
// var dump = [];
this.frame++;
this.rawDelta = time - this.lastTime;
var idx = this.deltaIndex;
var history = this.deltaHistory;
var max = this.deltaSmoothingMax;
// delta time (time is in ms)
var dt = (time - this.lastTime);
// When a browser switches tab, then comes back again, it takes around 10 frames before
// the delta time settles down so we employ a 'cooling down' period before we start
// trusting the delta values again, to avoid spikes flooding through our delta average
if (this._coolDown > 0 || !this.inFocus)
{
this._coolDown--;
dt = this._target;
// debug = (time - this.lastTime);
}
// min / max range (yes, the < and > should be this way around)
if (dt > this._min || dt < this._max)
{
// Probably super bad start time or browser tab context loss,
// so use the last 'sane' dt value
// debug = dt;
dt = history[idx];
// Clamp delta to min max range (in case history has become corrupted somehow)
dt = Math.max(Math.min(dt, this._max), this._min);
}
// Smooth out the delta over the previous X frames
// add the delta to the smoothing array
history[idx] = dt;
// adjusts the delta history array index based on the smoothing count
// this stops the array growing beyond the size of deltaSmoothingMax
this.deltaIndex++;
if (this.deltaIndex > max)
{
this.deltaIndex = 0;
}
// Delta Average
var avg = 0;
// Loop the history array, adding the delta values together
for (var i = 0; i < max; i++)
{
// Debug
// if (history[i] < 16 || history[i] > 17)
// {
// dump.push({ i: i, dt: history[i] });
// }
avg += history[i];
}
// Then divide by the array length to get the average delta
avg /= max;
// Set as the world delta value
this.delta = avg;
// Real-world timer advance
// this.time += avg;
this.time += this.rawDelta;
// Update the estimate of the frame rate, `fps`. Every second, the number
// of frames that occurred in that second are included in an exponential
// moving average of all frames per second, with an alpha of 0.25. This
// means that more recent seconds affect the estimated frame rate more than
// older seconds.
//
// When a browser window is NOT minimized, but is covered up (i.e. you're using
// another app which has spawned a window over the top of the browser), then it
// will start to throttle the raf callback time. It waits for a while, and then
// starts to drop the frame rate at 1 frame per second until it's down to just over 1fps.
// So if the game was running at 60fps, and the player opens a new window, then
// after 60 seconds (+ the 'buffer time') it'll be down to 1fps, so rafin'g at 1Hz.
//
// When they make the game visible again, the frame rate is increased at a rate of
// approx. 8fps, back up to 60fps (or the max it can obtain)
//
// There is no easy way to determine if this drop in frame rate is because the
// browser is throttling raf, or because the game is struggling with performance
// because you're asking it to do too much on the device.
if (time > this.nextFpsUpdate)
{
// Compute the new exponential moving average with an alpha of 0.25.
this.actualFps = 0.25 * this.framesThisSecond + 0.75 * this.actualFps;
this.nextFpsUpdate = time + 1000;
this.framesThisSecond = 0;
// if (this.actualFps < 56)
// {
// console.log(this.actualFps);
// console.log('F', this.frame, 'Avg', avg, 'Dt', debug, 'Panic', this._coolDown);
// }
}
this.framesThisSecond++;
// Interpolation - how far between what is expected and where we are?
var interpolation = avg / this._target;
this.callback(time, avg, interpolation);
// Shift time value over
this.lastTime = time;
// if (debug !== 0)
// {
// console.log('F', this.frame, 'Avg', avg, 'Dt', debug, 'Panic', this._coolDown);
// }
/*
if (debug !== 0 || dump.length)
{
console.group('Frame ' + this.frame);
console.log('Interpolation', interpolation, '%');
if (debug)
{
console.log('Elapsed', debug, 'ms');
}
// console.log('Frame', this.frame, 'Delta', avg, '(average)', debug, '(now)');
console.log('Delta', avg, '(average)');
if (dump.length)
{
console.table(dump);
}
console.groupEnd();
}
*/
},
tick: function ()
{
this.step(window.performance.now());
},
sleep: function ()
{
if (this.running)
{
this.raf.stop();
this.running = false;
}
},
wake: function (seamless)
{
if (this.running)
{
this.sleep();
}
else if (seamless)
{
this.startTime += -this.lastTime + (this.lastTime = window.performance.now());
}
this.raf.start(this.step.bind(this), this.useRAF);
this.running = true;
this.step(window.performance.now());
},
setFps: function (value)
{
this.fps = value;
this.wake();
},
getFps: function ()
{
return this.fps;
},
stop: function ()
{
this.running = false;
this.started = false;
this.raf.stop();
return this;
},
destroy: function ()
{
}
});
module.exports = TimeStep;