Skip to content

Commit 2c37dc3

Browse files
committed
StateManager moved to GlobalStateManager and a new State level proxy introduced.
You can now start, stop and swap states on the fly. State.visible now also skips the renderer.
1 parent 00185d6 commit 2c37dc3

5 files changed

Lines changed: 126 additions & 36 deletions

File tree

v3/src/boot/Game.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var DOMContentLoaded = require('../dom/DOMContentLoaded');
1414

1515
var MainLoop = require('./MainLoop');
1616
var CreateRenderer = require('./CreateRenderer');
17-
var StateManager = require('../state/StateManager');
17+
var GlobalStateManager = require('../state/GlobalStateManager');
1818
var TextureManager = require('../textures/TextureManager');
1919
var Data = require('../components/Data');
2020
var Cache = require('../cache/Cache');
@@ -57,9 +57,9 @@ var Game = function (config)
5757
this.input = null;
5858

5959
/**
60-
* @property {Phaser.StateManager} state - The StateManager. Phaser instance specific.
60+
* @property {Phaser.GlobalStateManager} state - The StateManager. Phaser instance specific.
6161
*/
62-
this.state = new StateManager(this, this.config.stateConfig);
62+
this.state = new GlobalStateManager(this, this.config.stateConfig);
6363

6464
/**
6565
* @property {Phaser.Device} device - Contains device information and capabilities (singleton)

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: 'd2813a20-ed80-11e6-9112-31e669c80df9'
2+
build: '13721950-ed9a-11e6-8f4f-afca70274f59'
33
};
44
module.exports = CHECKSUM;
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ var GetContext = require('../canvas/GetContext');
1818
/**
1919
* The State Manager is responsible for loading, setting up and switching game states.
2020
*
21-
* @class Phaser.StateManager
21+
* @class Phaser.GlobalStateManager
2222
* @constructor
2323
* @param {Phaser.Game} game - A reference to the currently running game.
2424
*/
25-
var StateManager = function (game, stateConfig)
25+
var GlobalStateManager = function (game, stateConfig)
2626
{
2727
this.game = game;
2828

@@ -62,15 +62,15 @@ var StateManager = function (game, stateConfig)
6262
}
6363
};
6464

65-
StateManager.prototype.constructor = StateManager;
65+
GlobalStateManager.prototype.constructor = GlobalStateManager;
6666

67-
StateManager.prototype = {
67+
GlobalStateManager.prototype = {
6868

6969
/**
7070
* The Boot handler is called by Phaser.Game when it first starts up.
7171
* The renderer is available by now.
7272
*
73-
* @method Phaser.StateManager#boot
73+
* @method Phaser.GlobalStateManager#boot
7474
* @private
7575
*/
7676
boot: function ()
@@ -112,11 +112,11 @@ StateManager.prototype = {
112112
},
113113

114114
/**
115-
* Adds a new State into the StateManager. You must give each State a unique key by which you'll identify it.
115+
* Adds a new State into the GlobalStateManager. You must give each State a unique key by which you'll identify it.
116116
* The State can be either a Phaser.State object (or an object that extends it), a plain JavaScript object or a function.
117117
* If a function is given a new state object will be created by calling it.
118118
*
119-
* @method Phaser.StateManager#add
119+
* @method Phaser.GlobalStateManager#add
120120
* @param {string} key - A unique key you use to reference this state, i.e. "MainMenu", "Level1".
121121
* @param {Phaser.State|object|function} state - The state you want to switch to.
122122
* @param {boolean} [autoStart=false] - If true the State will be started immediately after adding it.
@@ -135,33 +135,33 @@ StateManager.prototype = {
135135
autoStart: autoStart
136136
});
137137

138-
console.log('StateManager not yet booted, adding to list', this._pending.length);
138+
console.log('GlobalStateManager not yet booted, adding to list', this._pending.length);
139139

140140
return;
141141
}
142142

143143
key = this.getKey(key, stateConfig);
144144

145-
// console.log('StateManager.add', key, stateConfig, autoStart);
145+
// console.log('GlobalStateManager.add', key, stateConfig, autoStart);
146146

147147
var newState;
148148

149149
if (stateConfig instanceof State)
150150
{
151-
// console.log('StateManager.add from instance:', key);
151+
// console.log('GlobalStateManager.add from instance:', key);
152152
newState = this.createStateFromInstance(key, stateConfig);
153153
}
154154
else if (typeof stateConfig === 'object')
155155
{
156-
// console.log('StateManager.add from object:', key);
156+
// console.log('GlobalStateManager.add from object:', key);
157157

158158
stateConfig.key = key;
159159

160160
newState = this.createStateFromObject(key, stateConfig);
161161
}
162162
else if (typeof stateConfig === 'function')
163163
{
164-
// console.log('StateManager.add from function:', key);
164+
// console.log('GlobalStateManager.add from function:', key);
165165

166166
newState = this.createStateFromFunction(key, stateConfig);
167167
}
@@ -358,7 +358,7 @@ StateManager.prototype = {
358358
// if not booted, then put state into a holding pattern
359359
if (!this.game.isBooted)
360360
{
361-
// console.log('StateManager not yet booted, setting autoStart on pending list');
361+
// console.log('GlobalStateManager not yet booted, setting autoStart on pending list');
362362

363363
for (var i = 0; i < this._pending.length; i++)
364364
{
@@ -440,7 +440,7 @@ StateManager.prototype = {
440440
// Is the loader empty?
441441
if (loader.list.size === 0)
442442
{
443-
this.startCreate(state);
443+
this.create(state);
444444
}
445445
else
446446
{
@@ -454,7 +454,7 @@ StateManager.prototype = {
454454
else
455455
{
456456
// No preload? Then there was nothing to load either
457-
this.startCreate(state);
457+
this.create(state);
458458
}
459459
},
460460

@@ -464,35 +464,35 @@ StateManager.prototype = {
464464

465465
// console.log('loadComplete', state.sys.settings.key);
466466

467-
this.startCreate(state);
467+
this.create(state);
468468
},
469469

470-
startCreate: function (state)
470+
create: function (state)
471471
{
472-
// console.log('startCreate', state.sys.settings.key);
473-
474-
if (state.create)
475-
{
476-
state.create();
477-
}
472+
console.log('create', state.sys.settings.key);
478473

479474
// Insert at the correct index, or it just all goes wrong :)
480475

481476
var i = this.getStateIndex(state);
482477

483-
// console.log('startCreate.index', state.sys.settings.key, i);
478+
// console.log('create.index', state.sys.settings.key, i);
484479

485480
this.active.push({ index: i, state: state });
486481

487482
// Sort the 'active' array based on the index property
488483
this.active.sort(this.sortStates);
489484

490485
state.sys.updates.running = true;
486+
487+
if (state.create)
488+
{
489+
state.create();
490+
}
491491
},
492492

493493
pause: function (key)
494494
{
495-
var index = this.getActiveStateIndex(key);
495+
var index = this.getActiveStateIndex(this.getState(key));
496496

497497
if (index > -1)
498498
{
@@ -502,7 +502,7 @@ StateManager.prototype = {
502502

503503
this.active.splice(index, 1);
504504

505-
this.active.sort(this.sortStates.bind(this));
505+
this.active.sort(this.sortStates);
506506
}
507507
},
508508

@@ -527,4 +527,4 @@ StateManager.prototype = {
527527

528528
};
529529

530-
module.exports = StateManager;
530+
module.exports = GlobalStateManager;

v3/src/state/Systems.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
var EventDispatcher = require('../events/EventDispatcher');
88
var GameObjectFactory = require('./systems/GameObjectFactory');
99
var GameObjectCreator = require('./systems/GameObjectCreator');
10+
var StateManager = require('./systems/StateManager');
1011
var Loader = require('./systems/Loader');
1112
var UpdateManager = require('./systems/UpdateManager');
1213
var Component = require('../components');
13-
// var Camera = require('../camera/Camera');
1414
var Settings = require('./Settings');
1515
var RTree = require('../structs/RTree');
1616
var Camera = require('../camera/Camera-2')
@@ -46,6 +46,7 @@ var Systems = function (state, config)
4646
this.events;
4747
this.updates;
4848
this.tree;
49+
this.stateManager;
4950

5051
// State properties
5152
this.cameras;
@@ -77,17 +78,18 @@ Systems.prototype = {
7778
this.tree = RTree(16);
7879
this.events = new EventDispatcher();
7980
this.add = new GameObjectFactory(this.state);
80-
this.make = GameObjectCreator(this.state);
81+
this.make = new GameObjectCreator(this.state);
8182
this.updates = new UpdateManager(this.state);
8283
this.load = new Loader(this.state);
84+
this.stateManager = new StateManager(this.state, game);
8385

8486
// State specific properties (transform, data, children, etc)
8587

86-
// this.camera = new Camera(this.state, 0, 0, this.settings.width, this.settings.height);
8788
this.children = new Component.Children(this.state);
8889
this.color = new Component.Color(this.state);
8990
this.data = new Component.Data(this.state);
9091
this.transform = new Component.Transform(this.state);
92+
9193
this.cameras = [];
9294
this.mainCamera = new Camera(0, 0, this.game.config.width, this.game.config.height);
9395
this.cameras.push(this.mainCamera);
@@ -107,11 +109,11 @@ Systems.prototype = {
107109
this.state.color = this.color;
108110
this.state.data = this.data;
109111
this.state.settings = this.settings;
112+
this.state.state = this.stateManager;
110113

111114
// this.state.camera = this.camera;
112115
this.state.transform = this.transform;
113116

114-
this.state.state = this.game.state;
115117
this.state.cache = this.game.cache;
116118
this.state.textures = this.game.textures;
117119

@@ -144,16 +146,25 @@ Systems.prototype = {
144146

145147
render: function (interpolation, renderer)
146148
{
149+
if (!this.settings.visible)
150+
{
151+
return;
152+
}
153+
147154
var state = this.state;
148155
var transform = this.transform;
149156
var cameras = this.cameras;
157+
150158
for (var i = 0, l = cameras.length; i < l; ++i)
151159
{
152160
var camera = cameras[i];
161+
153162
camera.preRender();
163+
154164
state.camera = camera;
165+
155166
renderer.render(state, transform.flatRenderArray, interpolation, camera);
156-
//state.render(interpolation);
167+
157168
camera.postRender();
158169
}
159170
},
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// A proxy class to the Global State Manager
2+
3+
var StateManager = function (state, game)
4+
{
5+
// The State that owns this StateManager
6+
this.state = state;
7+
8+
this.key = state.sys.settings.key;
9+
10+
// GlobalStateManager
11+
this.manager = game.state;
12+
};
13+
14+
StateManager.prototype.constructor = StateManager;
15+
16+
StateManager.prototype = {
17+
18+
// Start this State (or the one given via key)
19+
start: function (key)
20+
{
21+
if (key === undefined) { key = this.key; }
22+
23+
this.manager.start(key);
24+
},
25+
26+
// Pause this State (or the one given via key)
27+
pause: function (key)
28+
{
29+
if (key === undefined) { key = this.key; }
30+
31+
this.manager.pause(key);
32+
},
33+
34+
// Stop this State and start the one given
35+
swap: function (key)
36+
{
37+
this.manager.pause(this.key);
38+
39+
this.manager.start(key);
40+
},
41+
42+
moveUp: function ()
43+
{
44+
45+
},
46+
47+
moveDown: function ()
48+
{
49+
50+
},
51+
52+
bringToTop: function ()
53+
{
54+
55+
},
56+
57+
sendToBack: function ()
58+
{
59+
60+
},
61+
62+
// TODO
63+
transitionTo: function (key, duration)
64+
{
65+
this.manager.pause(this.key);
66+
67+
this.manager.start(key);
68+
},
69+
70+
isActive: function (key)
71+
{
72+
if (key === undefined) { key = this.key; }
73+
74+
return this.manager.isActive(key);
75+
}
76+
77+
};
78+
79+
module.exports = StateManager;

0 commit comments

Comments
 (0)