Skip to content

Commit 6024720

Browse files
committed
Lots of new State Manager features (sleep, wake, pause, resume, etc)
1 parent a27d42b commit 6024720

6 files changed

Lines changed: 314 additions & 35 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: '0d93fb90-5d23-11e7-a642-d974c901baec'
2+
build: 'f20e3110-5d2c-11e7-9a51-cfbdba7f8363'
33
};
44
module.exports = CHECKSUM;

v3/src/state/GlobalStateManager.js

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,19 @@ GlobalStateManager.prototype = {
347347
return this.states.indexOf(state);
348348
},
349349

350+
getActiveState: function (key)
351+
{
352+
var state = this.getState(key);
353+
354+
for (var i = 0; i < this.active.length; i++)
355+
{
356+
if (this.active[i].state === state)
357+
{
358+
return this.active[i];
359+
}
360+
}
361+
},
362+
350363
getActiveStateIndex: function (state)
351364
{
352365
var index = -1;
@@ -373,7 +386,7 @@ GlobalStateManager.prototype = {
373386
{
374387
if (data === undefined) { data = {}; }
375388

376-
// console.log('start:', key);
389+
console.log('start:', key);
377390
// console.dir(data);
378391

379392
// if not booted, then put state into a holding pattern
@@ -518,17 +531,67 @@ GlobalStateManager.prototype = {
518531

519532
pause: function (key)
520533
{
521-
var index = this.getActiveStateIndex(this.getState(key));
534+
var entry = this.getActiveState(key);
535+
536+
if (entry)
537+
{
538+
entry.state.sys.pause();
539+
}
540+
},
541+
542+
resume: function (key)
543+
{
544+
var entry = this.getActiveState(key);
545+
546+
if (entry)
547+
{
548+
entry.state.sys.resume();
549+
}
550+
},
551+
552+
sleep: function (key)
553+
{
554+
var entry = this.getActiveState(key);
555+
556+
if (entry)
557+
{
558+
entry.state.sys.sleep();
559+
}
560+
},
561+
562+
wake: function (key)
563+
{
564+
var entry = this.getActiveState(key);
565+
566+
if (entry)
567+
{
568+
entry.state.sys.wake();
569+
}
570+
},
571+
572+
swap: function (from, to)
573+
{
574+
this.sleep(from);
575+
this.start(to);
576+
},
577+
578+
stop: function (key)
579+
{
580+
var entry = this.getActiveState(key);
522581

523-
if (index > -1)
582+
if (entry)
524583
{
525-
var state = this.getState(key);
584+
entry.state.sys.shutdown();
526585

527-
state.sys.settings.active = false;
586+
// Remove from the active list
587+
var index = this.active.indexOf(entry);
528588

529-
this.active.splice(index, 1);
589+
if (index !== -1)
590+
{
591+
this.active.splice(index, 1);
530592

531-
this.active.sort(this.sortStates);
593+
this.active.sort(this.sortStates);
594+
}
532595
}
533596
},
534597

v3/src/state/Systems.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ var Systems = function (state, config)
4545
this.stateManager;
4646
this.time;
4747
this.tweens;
48-
this.updates;
48+
// this.updates;
4949

5050
// State properties
5151
this.children;
5252
this.color;
5353
this.data;
5454
};
5555

56-
Systems.prototype.constructor = Systems;
57-
5856
Systems.prototype = {
5957

6058
init: function (game)
@@ -85,7 +83,7 @@ Systems.prototype = {
8583
this.stateManager = new StateManager(this.state, game);
8684
this.time = new Clock(this.state);
8785
this.tweens = new TweenManager(this.state);
88-
this.updates = new UpdateManager(this.state);
86+
// this.updates = new UpdateManager(this.state);
8987

9088
this.inject();
9189
},
@@ -107,6 +105,11 @@ Systems.prototype = {
107105

108106
step: function (time, delta)
109107
{
108+
if (!this.settings.active)
109+
{
110+
return;
111+
}
112+
110113
this.time.begin(time);
111114

112115
this.tweens.begin(time);
@@ -143,12 +146,87 @@ Systems.prototype = {
143146
}
144147

145148
this.cameras.render(renderer, this.children, interpolation);
149+
150+
// After everything has rendered are there any pending StateManager actions?
151+
this.stateManager.update();
146152
},
147153

148154
sortZ: function (childA, childB)
149155
{
150156
return childA._z - childB._z;
157+
},
158+
159+
// A paused State still renders, it just doesn't run ANY of its update handlers or systems
160+
pause: function ()
161+
{
162+
// Was paused by the GlobalStateManager
163+
164+
this.settings.active = false;
165+
166+
// Notify the State callback or dispatch an event
167+
},
168+
169+
resume: function ()
170+
{
171+
// Was resumed by the GlobalStateManager
172+
173+
this.settings.active = true;
174+
175+
// Notify the State callback or dispatch an event
176+
},
177+
178+
sleep: function ()
179+
{
180+
// Was sent to sleep by the GlobalStateManager
181+
182+
this.settings.active = false;
183+
this.settings.visible = false;
184+
185+
// Notify the State callback or dispatch an event
186+
},
187+
188+
wake: function ()
189+
{
190+
// Was woken up by the GlobalStateManager
191+
192+
this.settings.active = true;
193+
this.settings.visible = true;
194+
195+
// Notify the State callback or dispatch an event
196+
},
197+
198+
shutdown: function ()
199+
{
200+
// Was stopped by the GlobalStateManager
201+
202+
this.settings.active = false;
203+
204+
// If all State level managers followed the same pattern then we could just iterate
205+
// the map and call shutdown on all of them, same for destroy
206+
207+
// Move to a Plugin based system? Then plugins can look-up other plugins via the State
208+
// and store their own references to them to avoid constant look-ups.
209+
210+
this.children.removeAll();
211+
212+
this.time.shutdown();
213+
this.tweens.shutdown();
214+
215+
this.state.shutdown.call(this.state);
216+
},
217+
218+
// Game level nuke
219+
destroy: function ()
220+
{
221+
// TODO
222+
223+
this.time.destroy();
224+
this.tweens.destroy();
225+
226+
// etc
151227
}
152228
};
153229

230+
Systems.prototype.constructor = Systems;
231+
154232
module.exports = Systems;

0 commit comments

Comments
 (0)