Skip to content

Commit 5cef085

Browse files
committed
Updated TimerEvent and Clock
Swapped to Class format. Added timeScale, startAt and more robust loop handling.
1 parent 755fa26 commit 5cef085

4 files changed

Lines changed: 175 additions & 128 deletions

File tree

v3/src/boot/Game.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,25 @@ Game.prototype = {
149149
onHidden: function ()
150150
{
151151
this.loop.pause();
152+
153+
// var active = this.state.active;
154+
155+
// for (var i = 0; i < active.length; i++)
156+
// {
157+
// active[i].state.sys.pause();
158+
// }
152159
},
153160

154161
onVisible: function ()
155162
{
156163
this.loop.resume();
164+
165+
// var active = this.state.active;
166+
167+
// for (var i = 0; i < active.length; i++)
168+
// {
169+
// active[i].state.sys.resume();
170+
// }
157171
},
158172

159173
onBlur: function ()

v3/src/state/Systems.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Systems.prototype = {
135135
list[i].preUpdate(time, delta);
136136
}
137137

138+
// preUpdate TimerEvents
138139
this.time.update(time, delta);
139140

140141
this.tweens.update(time, delta);

v3/src/time/Clock.js

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
1+
var Class = require('../utils/Class');
12
var TimerEvent = require('./TimerEvent');
23

3-
// There is only ever one instance of a MasterClock per game, and it belongs to the Game.
4-
5-
var Clock = function (state, masterclock)
6-
{
7-
this.state = state;
8-
9-
/**
10-
* The `performance.now()` value when the time was last updated.
11-
* @property {float} time
12-
* @protected
13-
*/
14-
this.time = 0;
15-
16-
/**
17-
* The `now` when the previous update occurred.
18-
* @property {float} prevTime
19-
* @protected
20-
*/
21-
this.prevTime = 0;
22-
23-
/**
24-
* Elapsed time since the last time update, in milliseconds, based on `now`.
25-
*
26-
* This value _may_ include time that the game is paused/inactive.
27-
*
28-
* @property {number} elapsed
29-
* @see Lazer.Time.time
30-
* @protected
31-
*/
32-
this.elapsed = 0;
33-
34-
this._pendingInsertion = [];
35-
this._active = [];
36-
this._pendingRemoval = [];
37-
};
38-
39-
Clock.prototype = {
4+
var Clock = new Class({
5+
6+
initialize:
7+
8+
function Clock (state)
9+
{
10+
this.state = state;
11+
12+
this.now = Date.now();
13+
14+
this.paused = false;
15+
16+
this._active = [];
17+
this._pendingInsertion = [];
18+
this._pendingRemoval = [];
19+
},
4020

4121
addEvent: function (config)
4222
{
@@ -52,10 +32,33 @@ Clock.prototype = {
5232
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
5333
},
5434

55-
begin: function (time)
35+
clearPendingEvents: function ()
36+
{
37+
this._pendingInsertion = [];
38+
},
39+
40+
removeAllEvents: function ()
5641
{
42+
this._pendingRemoval = this._pendingRemoval.concat(this._active);
43+
44+
return this;
45+
},
46+
47+
begin: function ()
48+
{
49+
var toRemove = this._pendingRemoval.length;
50+
var toInsert = this._pendingInsertion.length;
51+
52+
if (toRemove === 0 && toInsert === 0)
53+
{
54+
// Quick bail
55+
return;
56+
}
57+
58+
var i;
59+
5760
// Delete old events
58-
for (var i = 0; i < this._pendingRemoval.length; i++)
61+
for (i = 0; i < toRemove; i++)
5962
{
6063
var event = this._pendingRemoval[i];
6164

@@ -70,8 +73,17 @@ Clock.prototype = {
7073
event.destroy();
7174
}
7275

76+
for (i = 0; i < toInsert; i++)
77+
{
78+
var event = this._pendingInsertion[i];
79+
80+
event.elapsed = event.startAt * event.timeScale;
81+
82+
this._active.push(event);
83+
}
84+
7385
// Move pending events to the active list
74-
this._active = this._active.concat(this._pendingInsertion.splice(0));
86+
// this._active = this._active.concat(this._pendingInsertion.splice(0));
7587

7688
// Clear the lists
7789
this._pendingRemoval.length = 0;
@@ -80,20 +92,13 @@ Clock.prototype = {
8092

8193
update: function (time, delta)
8294
{
83-
this.prevTime = this.time;
84-
85-
this.time = time;
95+
this.now = time;
8696

87-
this.elapsed = time - this.prevTime;
88-
89-
if (this._active.length)
97+
if (this.paused)
9098
{
91-
this.processEvents(time, this.elapsed);
99+
return;
92100
}
93-
},
94101

95-
processEvents: function (time, elapsed)
96-
{
97102
for (var i = 0; i < this._active.length; i++)
98103
{
99104
var event = this._active[i];
@@ -103,9 +108,11 @@ Clock.prototype = {
103108
continue;
104109
}
105110

106-
event.elapsed += elapsed;
107-
108-
// console.log(event.elapsed);
111+
// Use delta time to increase elapsed.
112+
// Avoids needing to adjust for pause / resume.
113+
// Automatically smoothed by TimeStep class.
114+
// In testing accurate to +- 1ms!
115+
event.elapsed += delta * event.timeScale;
109116

110117
if (event.elapsed >= event.delay)
111118
{
@@ -115,7 +122,7 @@ Clock.prototype = {
115122
event.elapsed = event.delay;
116123

117124
// Process the event
118-
if (!event.hasDispatched)
125+
if (!event.hasDispatched && event.callback)
119126
{
120127
event.hasDispatched = true;
121128
event.callback.apply(event.callbackScope, event.args);
@@ -134,10 +141,20 @@ Clock.prototype = {
134141
}
135142
}
136143
}
137-
}
144+
},
138145

139-
};
146+
destroy: function ()
147+
{
148+
for (var i = 0; i < this._active.length; i++)
149+
{
150+
this._active[i].destroy();
151+
}
152+
153+
this._active.length = 0;
154+
this._pendingRemoval.length = 0;
155+
this._pendingInsertion.length = 0;
156+
}
140157

141-
Clock.prototype.constructor = Clock;
158+
});
142159

143160
module.exports = Clock;

0 commit comments

Comments
 (0)