forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableTimeStep.js
More file actions
188 lines (139 loc) · 3.8 KB
/
Copy pathVariableTimeStep.js
File metadata and controls
188 lines (139 loc) · 3.8 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
var NOOP = require('../utils/NOOP');
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
var VariableTimeStep = function (game, framerate)
{
this.game = game;
this.raf = new RequestAnimationFrame();
this.started = false;
this.running = false;
this.fps = framerate;
this.callback = NOOP;
this.useRAF = true;
this.time = 0;
this.startTime = 0;
this.lastTime = 0;
this.delta = 0;
this.deltaIndex = 0;
this.deltaHistory = [];
this.deltaSmoothingMax = 30;
};
VariableTimeStep.prototype.constructor = VariableTimeStep;
VariableTimeStep.prototype = {
toString: function ()
{
return 'time: ' + this.time + ' delta: ' + this.delta;
},
start: function (useRAF, callback)
{
if (this.started)
{
return this;
}
this.started = true;
this.running = true;
this.time = Date.now();
this.startTime = Date.now();
this.lastTime = Date.now();
// Pre-populate smoothing array
var history = [];
for (var i = 0; i < this.deltaSmoothingMax; i++)
{
history[i] = 0;
}
this.delta = 0;
this.deltaIndex = 0;
this.deltaHistory = history;
this.useRAF = useRAF;
this.callback = callback;
this.raf.start(this.step.bind(this), useRAF);
},
step: function (time)
{
// delta time
var dt = (time - this.lastTime) / 1000;
if (dt < 0 || dt > 1)
{
// Loop skip, probably super bad start time
this.lastTime = time;
return;
}
// clamp delta to 0.0001 to 0.5 range
dt = Math.max(Math.min(dt, 0.5), 0.0001);
// Smooth out the delta over the previous 10 frames
var idx = this.deltaIndex;
var history = this.deltaHistory;
var max = this.deltaSmoothingMax;
// 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 = (idx + 1) % max;
// average
var avg = 0;
// Loop the array, adding the delta values together
for (var i = 0; i < max; 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.callback(this.time, avg * 1000);
// Shift time value over
this.lastTime = time;
},
/*
tick: function ()
{
this.step(true);
},
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.lastUpdate + (this.lastUpdate = Date.now());
}
else if (this.frame > 10)
{
this.lastUpdate = Date.now() - this.lagThreshold + 5;
}
this.raf.start(this.step.bind(this), this.useRAF);
this.running = true;
this.step(true);
},
setFps: function (value)
{
this.fps = value;
this.gap = 1 / (value || 60);
this.nextTime = this.time + this.gap;
this.wake();
},
getFps: function ()
{
return this.fps;
},
*/
stop: function ()
{
this.running = false;
this.started = false;
this.raf.stop();
return this;
}
};
module.exports = VariableTimeStep;