Skip to content

Commit d2fa377

Browse files
committed
Working on the new TickerLoop.
1 parent 8264351 commit d2fa377

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

v3/src/boot/TickerLoop.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
2+
3+
var TickerLoop = function (game, framerate)
4+
{
5+
this.game = game;
6+
7+
this.raf = new RequestAnimationFrame();
8+
9+
this.started = false;
10+
this.running = false;
11+
12+
this.lastUpdate = 0;
13+
14+
this.startTime = 0;
15+
this.elapsed = 0;
16+
this.time = 0;
17+
this.nextTime = 0;
18+
this.frame = 0;
19+
this.fps = false;
20+
21+
this.lagThreshold = 500;
22+
this.adjustedLag = 33;
23+
24+
this.useRAF = true;
25+
};
26+
27+
TickerLoop.prototype.constructor = TickerLoop;
28+
29+
TickerLoop.prototype = {
30+
31+
start: function (fps, useRAF)
32+
{
33+
if (this.started)
34+
{
35+
return this;
36+
}
37+
38+
this.started = true;
39+
this.running = true;
40+
41+
this.startTime = Date.now();
42+
this.lastUpdate = Date.now();
43+
44+
this.useRAF = !!this.game.config.forceSetTimeOut;
45+
46+
this.raf.start(this.step.bind(this), useRAF);
47+
},
48+
49+
step: function (manual) {
50+
51+
var elapsed = Date.now() - this.lastUpdate;
52+
53+
if (elapsed > this.lagThreshold)
54+
{
55+
this.startTime += elapsed - this.adjustedLag;
56+
}
57+
58+
this.lastUpdate += elapsed;
59+
60+
this.time = (this.lastUpdate - this.startTime) / 1000;
61+
62+
this.elapsed = elapsed;
63+
64+
var overlap = this.time - this.nextTime;
65+
66+
// var elapsed = _getTime() - _lastUpdate,
67+
// overlap, dispatch;
68+
// if (elapsed > _lagThreshold) {
69+
// _startTime += elapsed - _adjustedLag;
70+
// }
71+
// _lastUpdate += elapsed;
72+
// _self.time = (_lastUpdate - _startTime) / 1000;
73+
// overlap = _self.time - _nextTime;
74+
75+
if (!this.fps || overlap > 0 || manual)
76+
{
77+
this.frame++;
78+
this.nextTime += overlap + (overlap >= this.gap ? 0.004 : this.gap - overlap);
79+
}
80+
81+
// if (!manual)
82+
// {
83+
// }
84+
85+
// if (!_fps || overlap > 0 || manual === true) {
86+
// _self.frame++;
87+
// _nextTime += overlap + (overlap >= _gap ? 0.004 : _gap - overlap);
88+
// dispatch = true;
89+
// }
90+
// if (manual !== true) { //make sure the request is made before we dispatch the "tick" event so that timing is maintained. Otherwise, if processing the "tick" requires a bunch of time (like 15ms) and we're using a setTimeout() that's based on 16.7ms, it'd technically take 31.7ms between frames otherwise.
91+
// _id = _req(_tick);
92+
// }
93+
// if (dispatch) {
94+
// _self.dispatchEvent(_tickWord);
95+
// }
96+
97+
98+
},
99+
100+
tick: function ()
101+
{
102+
this.step(true);
103+
},
104+
105+
lagSmoothing: function (threshold, adjustedLag)
106+
{
107+
this.lagThreshold = threshold || (1 / 0.0000000001); //zero should be interpreted as basically unlimited
108+
this.adjustedLag = Math.min(adjustedLag, this.lagThreshold, 0);
109+
},
110+
111+
sleep: function ()
112+
{
113+
if (this.running)
114+
{
115+
this.raf.stop();
116+
117+
this.running = false;
118+
}
119+
},
120+
121+
wake: function (seamless)
122+
{
123+
if (this.running)
124+
{
125+
this.sleep();
126+
}
127+
else if (seamless)
128+
{
129+
this.startTime += -this.lastUpdate + (this.lastUpdate = Date.now());
130+
}
131+
else if (this.frame > 10)
132+
{
133+
this.lastUpdate = Date.now() - this.lagThreshold + 5;
134+
}
135+
136+
this.raf.start(this.step.bind(this), useRAF);
137+
138+
this.running = true;
139+
140+
this.step(true);
141+
},
142+
143+
setFps: function (value)
144+
{
145+
this.fps = value;
146+
this.gap = 1 / (value || 60);
147+
this.nextTime = this.time + this.gap;
148+
149+
this.wake();
150+
},
151+
152+
getFps: function ()
153+
{
154+
return this.fps;
155+
},
156+
157+
stop: function ()
158+
{
159+
this.running = false;
160+
this.started = false;
161+
162+
this.raf.stop();
163+
164+
return this;
165+
}
166+
167+
};
168+
169+
module.exports = TickerLoop;

0 commit comments

Comments
 (0)