Skip to content

Commit 6ac524e

Browse files
committed
Added State Clock and TimerEvents
1 parent 8952c1d commit 6ac524e

6 files changed

Lines changed: 217 additions & 79 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '319d5420-5bd7-11e7-bbe2-93fbaa8629f2'
2+
build: '55684a60-5c1b-11e7-8400-af0272f167c9'
33
};
44
module.exports = CHECKSUM;

v3/src/state/Systems.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
var CameraManager = require('./systems/CameraManager');
3+
var Clock = require('../time/Clock');
34
var Component = require('../components');
45
var EventDispatcher = require('../events/EventDispatcher');
56
var GameObjectCreator = require('./systems/GameObjectCreator');
@@ -8,8 +9,8 @@ var Loader = require('./systems/Loader');
89
var Settings = require('./Settings');
910
var StableSort = require('../utils/array/StableSort');
1011
var StateManager = require('./systems/StateManager');
11-
var UpdateManager = require('./systems/UpdateManager');
1212
var TweenManager = require('../tween/TweenManager');
13+
var UpdateManager = require('./systems/UpdateManager');
1314

1415
var Systems = function (state, config)
1516
{
@@ -42,6 +43,7 @@ var Systems = function (state, config)
4243
// Reference to State specific managers (Factory, Tweens, Loader, Physics, etc)
4344
this.add;
4445
this.cameras;
46+
this.time;
4547
this.events;
4648
this.load;
4749
this.make;
@@ -83,6 +85,7 @@ Systems.prototype = {
8385

8486
this.add = new GameObjectFactory(this.state);
8587
this.cameras = new CameraManager(this.state);
88+
this.time = new Clock(this.state);
8689
this.events = new EventDispatcher();
8790
this.load = new Loader(this.state);
8891
this.make = new GameObjectCreator(this.state);
@@ -105,12 +108,13 @@ Systems.prototype = {
105108
this.state.textures = this.textures;
106109

107110
this.state.add = this.add;
108-
this.state.make = this.make;
109111
this.state.cameras = this.cameras;
110112
this.state.events = this.events;
111113
this.state.load = this.load;
114+
this.state.make = this.make;
112115
this.state.settings = this.settings;
113116
this.state.state = this.stateManager;
117+
this.state.time = this.time;
114118
this.state.tweens = this.tweens;
115119

116120
this.state.children = this.children;
@@ -120,6 +124,8 @@ Systems.prototype = {
120124

121125
step: function (time, delta)
122126
{
127+
this.time.begin(time);
128+
123129
this.tweens.begin(time);
124130

125131
var list = this.children.list;
@@ -129,36 +135,15 @@ Systems.prototype = {
129135
list[i].preUpdate(time, delta);
130136
}
131137

138+
this.time.update(time, delta);
139+
132140
this.tweens.update(time, delta);
133141

134142
this.cameras.update(time, delta);
135143

136144
this.state.update.call(this.state, time, delta);
137145
},
138146

139-
// Called just once per frame, regardless of speed
140-
141-
/*
142-
begin: function (timestamp, frameDelta)
143-
{
144-
var list = this.children.list;
145-
146-
for (var i = 0; i < list.length; i++)
147-
{
148-
list[i].preUpdate(timestamp, frameDelta);
149-
}
150-
},
151-
152-
// Potentially called multiple times per frame (on super-fast systems)
153-
update: function (timestep, physicsStep)
154-
{
155-
this.cameras.update(timestep);
156-
157-
this.state.update.call(this.state, timestep, physicsStep);
158-
},
159-
*/
160-
161-
// Called just once per frame
162147
render: function (interpolation, renderer)
163148
{
164149
if (!this.settings.visible)

v3/src/time/Clock.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
var TimerEvent = require('./TimerEvent');
2+
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 = {
40+
41+
addEvent: function (config)
42+
{
43+
var event = new TimerEvent(config);
44+
45+
this._pendingInsertion.push(event);
46+
47+
return event;
48+
},
49+
50+
delayedCall: function (delay, callback, args, callbackScope)
51+
{
52+
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
53+
},
54+
55+
begin: function (time)
56+
{
57+
// Delete old events
58+
for (var i = 0; i < this._pendingRemoval.length; i++)
59+
{
60+
var index = this._active.indexOf(this._pendingRemoval[i]);
61+
62+
if (index > -1)
63+
{
64+
this._active.splice(index, 1);
65+
}
66+
}
67+
68+
// Move pending events to the active list
69+
this._active = this._active.concat(this._pendingInsertion.splice(0));
70+
71+
// Clear the lists
72+
this._pendingRemoval.length = 0;
73+
this._pendingInsertion.length = 0;
74+
},
75+
76+
update: function (time, delta)
77+
{
78+
this.prevTime = this.time;
79+
80+
this.time = time;
81+
82+
this.elapsed = time - this.prevTime;
83+
84+
if (this._active.length)
85+
{
86+
this.processEvents(time, this.elapsed);
87+
}
88+
},
89+
90+
processEvents: function (time, elapsed)
91+
{
92+
for (var i = 0; i < this._active.length; i++)
93+
{
94+
var event = this._active[i];
95+
96+
event.elapsed += elapsed;
97+
98+
// console.log(event.elapsed);
99+
100+
if (event.elapsed >= event.delay)
101+
{
102+
var remainder = event.elapsed - event.delay;
103+
104+
// Limit it, in case it's checked in the callback
105+
event.elapsed = event.delay;
106+
107+
// Process the event
108+
event.callback.apply(event.callbackScope, event.args);
109+
110+
if (event.loop || event.repeatCount > 0)
111+
{
112+
event.repeatCount--;
113+
114+
event.elapsed = remainder;
115+
}
116+
else
117+
{
118+
this._pendingRemoval.push(event);
119+
}
120+
}
121+
}
122+
}
123+
124+
};
125+
126+
Clock.prototype.constructor = Clock;
127+
128+
module.exports = Clock;

v3/src/time/MasterClock.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

v3/src/time/TimerEvent.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var GetValue = require('../utils/object/GetValue');
2+
3+
/**
4+
* A TimerEvent is a single event that is processed by a Phaser.Clock
5+
*
6+
* It consists of a delay, which is a value in milliseconds after which the event will fire.
7+
* When the event fires it calls a specific callback with the specified arguments.
8+
*
9+
* TimerEvents are removed by their parent timer once finished firing or repeating.
10+
*
11+
* Use {@link Phaser.Clock#add}, {@link Phaser.Clock#repeat}, or {@link Phaser.Clock#loop} methods to create a new event.
12+
*
13+
* @class Phaser.TimerEvent
14+
* @constructor
15+
* @param {number} delay - The delay in ms at which this TimerEvent fires.
16+
* @param {number} tick - The tick is the next game clock time that this event will fire at.
17+
* @param {number} repeatCount - If this TimerEvent repeats it will do so this many times.
18+
* @param {boolean} loop - True if this TimerEvent loops, otherwise false.
19+
* @param {function} callback - The callback that will be called when the TimerEvent occurs.
20+
* @param {object} callbackContext - The context in which the callback will be called.
21+
* @param {any[]} arguments - Additional arguments to be passed to the callback.
22+
*/
23+
var TimerEvent = function (config)
24+
{
25+
/**
26+
* @property {number} delay - The delay in ms at which this TimerEvent fires.
27+
*/
28+
this.delay = GetValue(config, 'delay', 0);
29+
30+
/**
31+
* @property {number} repeatCount - If this TimerEvent repeats it will do so this many times.
32+
*/
33+
this.repeatCount = GetValue(config, 'repeat', 0);
34+
35+
/**
36+
* @property {boolean} loop - True if this TimerEvent loops, otherwise false.
37+
*/
38+
this.loop = GetValue(config, 'loop', false);
39+
40+
/**
41+
* @property {function} callback - The callback that will be called when the TimerEvent occurs.
42+
*/
43+
this.callback = GetValue(config, 'callback', null);
44+
45+
/**
46+
* @property {object} callbackContext - The context in which the callback will be called.
47+
*/
48+
this.callbackScope = GetValue(config, 'callbackScope', null);
49+
50+
/**
51+
* @property {any[]} arguments - Additional arguments to be passed to the callback.
52+
*/
53+
this.args = GetValue(config, 'args', []);
54+
55+
this.due = 0;
56+
this.elapsed = 0;
57+
};
58+
59+
TimerEvent.prototype = {
60+
61+
getProgress: function ()
62+
{
63+
return (this.elapsed / this.delay);
64+
}
65+
66+
};
67+
68+
TimerEvent.prototype.constructor = TimerEvent;
69+
70+
module.exports = TimerEvent;

v3/src/time/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Phaser.Time
2+
3+
module.exports = {
4+
5+
Clock: require('./Clock'),
6+
TimerEvent: require('./TimerEvent')
7+
8+
};

0 commit comments

Comments
 (0)