Skip to content

Commit ca2097f

Browse files
committed
Recoded how the boot data and processing works to allow for more flexibility when setting-up scenes. Updates phaserjs#3470
1 parent 66a9bec commit ca2097f

1 file changed

Lines changed: 103 additions & 46 deletions

File tree

src/scene/SceneManager.js

Lines changed: 103 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,36 @@ var SceneManager = new Class({
9191
this._queue = [];
9292

9393
/**
94-
* The number of Scenes to process.
94+
* Boot time data to merge.
9595
*
96-
* @name Phaser.Scenes.SceneManager#_processing
97-
* @type {integer}
96+
* @name Phaser.Scenes.SceneManager#_data
97+
* @type {object}
9898
* @private
99+
* @since 3.4.0
100+
*/
101+
this._data = {};
102+
103+
/**
104+
* Is the Scene Manager actively processing the Scenes list?
105+
*
106+
* @name Phaser.Scenes.SceneManager#isProcessing
107+
* @type {boolean}
108+
* @default false
109+
* @readOnly
99110
* @since 3.0.0
100111
*/
101-
this._processing = 0;
112+
this.isProcessing = false;
113+
114+
/**
115+
* Has the Scene Manager properly started?
116+
*
117+
* @name Phaser.Scenes.SceneManager#isBooted
118+
* @type {boolean}
119+
* @default false
120+
* @readOnly
121+
* @since 3.4.0
122+
*/
123+
this.isBooted = false;
102124

103125
if (sceneConfig)
104126
{
@@ -132,6 +154,11 @@ var SceneManager = new Class({
132154
*/
133155
bootQueue: function ()
134156
{
157+
if (this.isBooted)
158+
{
159+
return;
160+
}
161+
135162
var i;
136163
var entry;
137164
var key;
@@ -166,6 +193,17 @@ var SceneManager = new Class({
166193

167194
this.scenes.push(newScene);
168195

196+
// Any data to inject?
197+
if (this._data[key])
198+
{
199+
newScene.sys.settings.data = this._data[key].data;
200+
201+
if (this._data[key].autoStart)
202+
{
203+
entry.autoStart = true;
204+
}
205+
}
206+
169207
if (entry.autoStart || newScene.sys.settings.active)
170208
{
171209
this._start.push(key);
@@ -175,12 +213,16 @@ var SceneManager = new Class({
175213
// Clear the pending lists
176214
this._pending.length = 0;
177215

216+
this._data = {};
217+
218+
this.isBooted = true;
219+
178220
// _start might have been populated by the above
179221
for (i = 0; i < this._start.length; i++)
180222
{
181223
entry = this._start[i];
182224

183-
this.start(entry, entry.scene.data);
225+
this.start(entry);
184226
}
185227

186228
this._start.length = 0;
@@ -211,15 +253,15 @@ var SceneManager = new Class({
211253
{
212254
entry = this._pending[i];
213255

214-
this.add(entry.key, entry.scene, entry.autoStart);
256+
this.add(entry.key, entry.scene, entry.autoStart, entry.data);
215257
}
216258

217259
// _start might have been populated by this.add
218260
for (i = 0; i < this._start.length; i++)
219261
{
220262
entry = this._start[i];
221263

222-
this.start(entry, entry.scene.data);
264+
this.start(entry);
223265
}
224266

225267
// Clear the pending lists
@@ -259,21 +301,23 @@ var SceneManager = new Class({
259301
* @param {string} key - A unique key used to reference the Scene, i.e. `MainMenu` or `Level1`.
260302
* @param {(Phaser.Scene|SettingsConfig|function)} sceneConfig - The config for the Scene
261303
* @param {boolean} [autoStart=false] - If `true` the Scene will be started immediately after being added.
304+
* @param {object} [data] - Optional data object. This will be set as Scene.settings.data and passed to `Scene.init`.
262305
*
263-
* @return {?Phaser.Scene} The added Scene, if it was added immediately.
306+
* @return {?Phaser.Scene} The added Scene, if it was added immediately, otherwise `null`.
264307
*/
265-
add: function (key, sceneConfig, autoStart)
308+
add: function (key, sceneConfig, autoStart, data)
266309
{
267310
if (autoStart === undefined) { autoStart = false; }
311+
if (data === undefined) { data = {}; }
268312

269-
// if not booted, then put scene into a holding pattern
270-
if (this._processing === 1 || !this.game.isBooted)
313+
// If processing or not booted then put scene into a holding pattern
314+
if (this.isProcessing || !this.isBooted)
271315
{
272316
this._pending.push({
273317
key: key,
274318
scene: sceneConfig,
275319
autoStart: autoStart,
276-
data: {}
320+
data: data
277321
});
278322

279323
return null;
@@ -298,6 +342,9 @@ var SceneManager = new Class({
298342
newScene = this.createSceneFromFunction(key, sceneConfig);
299343
}
300344

345+
// Any data to inject?
346+
newScene.sys.settings.data = data;
347+
301348
// Replace key in case the scene changed it
302349
key = newScene.sys.settings.key;
303350

@@ -307,13 +354,13 @@ var SceneManager = new Class({
307354

308355
if (autoStart || newScene.sys.settings.active)
309356
{
310-
if (this.game.isBooted)
357+
if (this._pending.length)
311358
{
312-
this.start(key, newScene.sys.settings.data);
359+
this._start.push(key);
313360
}
314361
else
315362
{
316-
this._start.push(key);
363+
this.start(key);
317364
}
318365
}
319366

@@ -338,7 +385,7 @@ var SceneManager = new Class({
338385
*/
339386
remove: function (key)
340387
{
341-
if (this._processing)
388+
if (this.isProcessing)
342389
{
343390
this._queue.push({ op: 'remove', keyA: key, keyB: null });
344391
}
@@ -468,7 +515,7 @@ var SceneManager = new Class({
468515
{
469516
this.processQueue();
470517

471-
this._processing = 1;
518+
this.isProcessing = true;
472519

473520
// Loop through the active scenes in reverse order
474521
for (var i = this.scenes.length - 1; i >= 0; i--)
@@ -523,7 +570,7 @@ var SceneManager = new Class({
523570
}
524571
}
525572

526-
this._processing = 0;
573+
this.isProcessing = false;
527574
},
528575

529576
/**
@@ -693,13 +740,18 @@ var SceneManager = new Class({
693740
{
694741
for (var propertyKey in sceneConfig.extend)
695742
{
696-
newScene[propertyKey] = sceneConfig.extend[propertyKey];
697-
}
698-
}
743+
var value = sceneConfig.extend[propertyKey];
699744

700-
if (sceneConfig.hasOwnProperty('data'))
701-
{
702-
newScene.data = sceneConfig.data;
745+
if (propertyKey === 'data' && newScene.hasOwnProperty('data') && typeof value === 'object')
746+
{
747+
// Populate the DataManager
748+
newScene.data.merge(value);
749+
}
750+
else if (propertyKey !== 'sys')
751+
{
752+
newScene[propertyKey] = value;
753+
}
754+
}
703755
}
704756

705757
return newScene;
@@ -940,27 +992,19 @@ var SceneManager = new Class({
940992
* @since 3.0.0
941993
*
942994
* @param {string} key - The Scene to start.
943-
* @param {object} [data] - The Scene data.
995+
* @param {object} [data] - Optional data object to pass to Scene.Settings and Scene.init.
944996
*
945997
* @return {Phaser.Scenes.SceneManager} This SceneManager.
946998
*/
947999
start: function (key, data)
9481000
{
949-
if (data === undefined) { data = {}; }
950-
951-
// If the Game is not booted, then put the Scene into a holding pattern
952-
if (!this.game.isBooted)
1001+
// If the Scene Manager is not running, then put the Scene into a holding pattern
1002+
if (!this.isBooted)
9531003
{
954-
for (var i = 0; i < this._pending.length; i++)
955-
{
956-
var entry = this._pending[i];
957-
958-
if (entry.key === key)
959-
{
960-
entry.autoStart = true;
961-
entry.scene.data = data;
962-
}
963-
}
1004+
this._data[key] = {
1005+
autoStart: true,
1006+
data: data
1007+
};
9641008

9651009
return this;
9661010
}
@@ -1102,7 +1146,7 @@ var SceneManager = new Class({
11021146
*/
11031147
bringToTop: function (key)
11041148
{
1105-
if (this._processing)
1149+
if (this.isProcessing)
11061150
{
11071151
this._queue.push({ op: 'bringToTop', keyA: key, keyB: null });
11081152
}
@@ -1136,7 +1180,7 @@ var SceneManager = new Class({
11361180
*/
11371181
sendToBack: function (key)
11381182
{
1139-
if (this._processing)
1183+
if (this.isProcessing)
11401184
{
11411185
this._queue.push({ op: 'sendToBack', keyA: key, keyB: null });
11421186
}
@@ -1168,7 +1212,7 @@ var SceneManager = new Class({
11681212
*/
11691213
moveDown: function (key)
11701214
{
1171-
if (this._processing)
1215+
if (this.isProcessing)
11721216
{
11731217
this._queue.push({ op: 'moveDown', keyA: key, keyB: null });
11741218
}
@@ -1202,7 +1246,7 @@ var SceneManager = new Class({
12021246
*/
12031247
moveUp: function (key)
12041248
{
1205-
if (this._processing)
1249+
if (this.isProcessing)
12061250
{
12071251
this._queue.push({ op: 'moveUp', keyA: key, keyB: null });
12081252
}
@@ -1244,7 +1288,7 @@ var SceneManager = new Class({
12441288
return this;
12451289
}
12461290

1247-
if (this._processing)
1291+
if (this.isProcessing)
12481292
{
12491293
this._queue.push({ op: 'moveAbove', keyA: keyA, keyB: keyB });
12501294
}
@@ -1288,7 +1332,7 @@ var SceneManager = new Class({
12881332
return this;
12891333
}
12901334

1291-
if (this._processing)
1335+
if (this.isProcessing)
12921336
{
12931337
this._queue.push({ op: 'moveBelow', keyA: keyA, keyB: keyB });
12941338
}
@@ -1312,6 +1356,19 @@ var SceneManager = new Class({
13121356
return this;
13131357
},
13141358

1359+
/**
1360+
* Queue a Scene operation for the next update.
1361+
*
1362+
* @method Phaser.Scenes.SceneManager#queueOp
1363+
* @private
1364+
* @since 3.0.0
1365+
*
1366+
* @param {string} op - The operation to perform.
1367+
* @param {(string|Phaser.Scene)} keyA - Scene A.
1368+
* @param {(string|Phaser.Scene)} [keyB] - Scene B.
1369+
*
1370+
* @return {Phaser.Scenes.SceneManager} This SceneManager.
1371+
*/
13151372
queueOp: function (op, keyA, keyB)
13161373
{
13171374
this._queue.push({ op: op, keyA: keyA, keyB: keyB });
@@ -1337,7 +1394,7 @@ var SceneManager = new Class({
13371394
return this;
13381395
}
13391396

1340-
if (this._processing)
1397+
if (this.isProcessing)
13411398
{
13421399
this._queue.push({ op: 'swapPosition', keyA: keyA, keyB: keyB });
13431400
}

0 commit comments

Comments
 (0)