Skip to content

Commit 89a737a

Browse files
committed
State Manager holds all given states until booted before creating them.
1 parent a5736f1 commit 89a737a

4 files changed

Lines changed: 141 additions & 60 deletions

File tree

src/components/Transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Phaser.Component.Transform = function (gameObject, x, y, scaleX, scaleY)
5555
this._worldScaleY = scaleY;
5656

5757
this._dirty = true;
58-
58+
5959
this.game.updates.add(this);
6060

6161
// The parent Transform (NOT the parent GameObject, although very often they are related)

src/core/Game.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Phaser.Game = function (width, height, renderer, parent, state, pixelArt)
184184
/**
185185
* @property {Phaser.StateManager} state - The StateManager.
186186
*/
187-
this.state = null;
187+
this.state = new Phaser.StateManager(this, state);
188188

189189
/**
190190
* @property {boolean} isBooted - Whether the game engine is booted, aka available.
@@ -484,8 +484,6 @@ Phaser.Game = function (width, height, renderer, parent, state, pixelArt)
484484
}
485485

486486
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
487-
488-
this.state = new Phaser.StateManager(this, state);
489487
}
490488

491489
this.device.whenReady(this.boot, this);
@@ -565,15 +563,10 @@ Phaser.Game.prototype = {
565563

566564
this.rnd = new Phaser.RandomDataGenerator(seed);
567565

568-
var state = null;
569-
570566
if (config['state'])
571567
{
572-
state = config['state'];
568+
this.state.add(null, config['state'], true);
573569
}
574-
575-
this.state = new Phaser.StateManager(this, state);
576-
577570
},
578571

579572
/**
@@ -589,6 +582,19 @@ Phaser.Game.prototype = {
589582
return;
590583
}
591584

585+
// Inject any new Factory helpers that exist in the build
586+
for (var gameobject in Phaser.GameObject)
587+
{
588+
if (Phaser.GameObject[gameobject].hasOwnProperty('FACTORY_KEY'))
589+
{
590+
var key = Phaser.GameObject[gameobject]['FACTORY_KEY'];
591+
592+
// console.log('found', key);
593+
594+
Phaser.GameObject.Factory.prototype[key] = Phaser.GameObject[gameobject]['FACTORY_ADD'];
595+
}
596+
}
597+
592598
this.onPause = new Phaser.Signal();
593599
this.onResume = new Phaser.Signal();
594600
this.onBlur = new Phaser.Signal();
@@ -605,9 +611,8 @@ Phaser.Game.prototype = {
605611
}
606612
};
607613

608-
this.updates = new Phaser.UpdateManager(this);
609-
610614
this.scale = new Phaser.ScaleManager(this, this._width, this._height);
615+
611616
// this.stage = new Phaser.Stage(this);
612617

613618
this.setUpRenderer();
@@ -616,18 +621,9 @@ Phaser.Game.prototype = {
616621

617622
// this.world = new Phaser.World(this);
618623

619-
// Inject any new Factory helpers that exist in the build
620-
for (var gameobject in Phaser.GameObject)
621-
{
622-
if (Phaser.GameObject[gameobject].hasOwnProperty('FACTORY_KEY'))
623-
{
624-
var key = Phaser.GameObject[gameobject]['FACTORY_KEY'];
625-
626-
// console.log('found', key);
624+
this.updates = new Phaser.UpdateManager(this);
627625

628-
Phaser.GameObject.Factory.prototype[key] = Phaser.GameObject[gameobject]['FACTORY_ADD'];
629-
}
630-
}
626+
// this.state = new Phaser.StateManager(this, this._pendingState);
631627

632628
this.add = new Phaser.GameObject.Factory(this);
633629

@@ -650,6 +646,7 @@ Phaser.Game.prototype = {
650646
this.scale.boot();
651647
this.input.boot();
652648
this.sound.boot();
649+
653650
this.state.boot();
654651

655652
// if (this.config['enableDebug'])

src/states/State.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,22 @@ Phaser.State.prototype.constructor = Phaser.State;
4040

4141
Phaser.State.prototype = {
4242

43+
// Can be overridden by your own States
4344
preUpdate: function ()
4445
{
4546
},
4647

48+
// Can be overridden by your own States
4749
update: function ()
4850
{
4951
},
5052

53+
// Can be overridden by your own States
5154
postUpdate: function ()
5255
{
5356
},
5457

58+
// Can be overridden by your own States
5559
render: function ()
5660
{
5761
}
@@ -155,6 +159,41 @@ Object.defineProperties(Phaser.State.prototype, {
155159
throw Error('Cannot re-assign protected property: transform');
156160
}
157161

162+
},
163+
164+
// Just a test
165+
rotation: {
166+
167+
enumerable: true,
168+
169+
get: function ()
170+
{
171+
return this.transform._rotation;
172+
},
173+
174+
set: function (value)
175+
{
176+
if (this.transform._rotation === value)
177+
{
178+
return;
179+
}
180+
181+
this.transform._rotation = value;
182+
this.transform.dirty = true;
183+
184+
if (this.transform._rotation % Phaser.Math.PI2)
185+
{
186+
this.transform.cache.sr = Math.sin(this.transform._rotation);
187+
this.transform.cache.cr = Math.cos(this.transform._rotation);
188+
this.transform.updateCache();
189+
this.transform.hasLocalRotation = true;
190+
}
191+
else
192+
{
193+
this.transform.hasLocalRotation = false;
194+
}
195+
}
196+
158197
}
159198

160199
});

src/states/StateManager.js

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,23 @@ Phaser.StateManager = function (game, pending)
2222
// Only active states are kept in here
2323
this.active = [];
2424

25-
this._pending = pending;
25+
this._pending = [];
2626

27-
this._start = [];
27+
if (pending)
28+
{
29+
if (Array.isArray(pending))
30+
{
31+
for (var i = 0; i < pending.length; i++)
32+
{
33+
// The i === 0 part just starts the first State given
34+
this._pending.push({ key: 'default', state: pending[i], autoStart: (i === 0) });
35+
}
36+
}
37+
else
38+
{
39+
this._pending.push({ key: 'default', state: pending, autoStart: true });
40+
}
41+
}
2842
};
2943

3044
Phaser.StateManager.prototype = {
@@ -41,28 +55,21 @@ Phaser.StateManager.prototype = {
4155
// this.game.onPause.add(this.pause, this);
4256
// this.game.onResume.add(this.resume, this);
4357

44-
if (this._pending)
58+
console.log('StateManager.boot');
59+
60+
for (var i = 0; i < this._pending.length; i++)
4561
{
46-
if (Array.isArray(this._pending))
47-
{
48-
for (var i = 0; i < this._pending.length; i++)
49-
{
50-
// The i === 0 part just starts the first State given
51-
this.add('default', this._pending[i], (i === 0));
52-
}
53-
}
54-
else
55-
{
56-
this.add('default', this._pending, true);
57-
}
62+
var entry = this._pending[i];
5863

59-
this._pending = null;
64+
this.add(entry.key, entry.state, entry.autoStart);
6065
}
66+
67+
this._pending = [];
6168
},
6269

6370
getKey: function (key, state)
6471
{
65-
if (key === undefined) { key = 'default'; }
72+
if (!key) { key = 'default'; }
6673

6774
if (state instanceof Phaser.State)
6875
{
@@ -99,24 +106,34 @@ Phaser.StateManager.prototype = {
99106
{
100107
if (autoStart === undefined) { autoStart = false; }
101108

102-
// if not booted, then do something else - put state into a holding pattern?
103-
// if (this.game.isBooted)
109+
// if not booted, then put state into a holding pattern
110+
if (!this.game.isBooted)
111+
{
112+
console.log('StateManager not yet booted, adding to list');
113+
114+
this._pending.push({ key: key, state: state, autoStart: autoStart });
115+
116+
return;
117+
}
104118

105119
key = this.getKey(key, state);
106120

107121
var newState;
108122

109123
if (state instanceof Phaser.State)
110124
{
125+
console.log('StateManager.add from instance', key);
111126
newState = this.createStateFromInstance(key, state);
112127
}
113128
else if (typeof state === 'object')
114129
{
130+
console.log('StateManager.add from object', key);
115131
newState = this.createStateFromObject(key, state);
116132
}
117133
else if (typeof state === 'function')
118134
{
119-
newState = new state(this.game);
135+
console.log('StateManager.add from function', key);
136+
newState = this.createStateFromFunction(key, state);
120137
}
121138

122139
this.keys[key] = newState;
@@ -140,12 +157,22 @@ Phaser.StateManager.prototype = {
140157
return newState;
141158
},
142159

143-
createStateFromInstance: function (key, newState)
160+
createStateFromFunction: function (key, state)
144161
{
145-
newState.game = this.game;
162+
var newState = new state();
146163

147-
newState.settings.key = key;
164+
if (newState instanceof Phaser.State)
165+
{
166+
return this.createStateFromInstance(key, newState);
167+
}
168+
else
169+
{
170+
throw new Error('Given State object must extend Phaser.State');
171+
}
172+
},
148173

174+
injectDefaultSystems: function (newState)
175+
{
149176
// Inject the default (non-optional) managers
150177

151178
newState._sys.add = new Phaser.GameObject.Factory(this.game, newState);
@@ -161,6 +188,17 @@ Phaser.StateManager.prototype = {
161188

162189
newState._sys.children = new Phaser.Component.Children(newState);
163190

191+
return newState;
192+
},
193+
194+
createStateFromInstance: function (key, newState)
195+
{
196+
newState.game = this.game;
197+
198+
newState.settings.key = key;
199+
200+
this.injectDefaultSystems(newState);
201+
164202
if (this.game.renderType === Phaser.WEBGL)
165203
{
166204
this.createStateFrameBuffer(newState);
@@ -192,22 +230,11 @@ Phaser.StateManager.prototype = {
192230

193231
createStateFromObject: function (key, state)
194232
{
195-
var newState = new Phaser.State(key, this.game);
196-
197-
// Inject the default (non-optional) managers
198-
199-
newState._sys.add = new Phaser.GameObject.Factory(this.game, newState);
200-
201-
// States have their own Loaders? Would make a lot of sense actually
202-
// newState._sys.load = this.game.load;
203-
204-
newState._sys.transform = new Phaser.Component.Transform(newState);
205-
206-
newState._sys.data = new Phaser.Component.Data(newState);
233+
var newState = new Phaser.State(key);
207234

208-
newState._sys.color = new Phaser.Component.Color(newState);
235+
newState.game = this.game;
209236

210-
newState._sys.children = new Phaser.Component.Children(newState);
237+
this.injectDefaultSystems(newState);
211238

212239
// Inject custom managers
213240

@@ -282,6 +309,24 @@ Phaser.StateManager.prototype = {
282309

283310
start: function (key)
284311
{
312+
// if not booted, then put state into a holding pattern
313+
if (!this.game.isBooted)
314+
{
315+
console.log('StateManager not yet booted, setting autoStart on pending list');
316+
317+
for (var i = 0; i < this._pending.length; i++)
318+
{
319+
var entry = this._pending[i];
320+
321+
if (entry.key === key)
322+
{
323+
entry.autoStart = true;
324+
}
325+
}
326+
327+
return;
328+
}
329+
285330
var state = this.getState(key);
286331

287332
if (state)

0 commit comments

Comments
 (0)