State indexing added.

This commit is contained in:
Richard Davey 2016-11-06 13:59:48 +00:00
parent 967bb6cc02
commit f2cb384da0
3 changed files with 67 additions and 10 deletions

View file

@ -244,7 +244,7 @@ Phaser.Renderer.WebGL.prototype = {
gl.disable(gl.CULL_FACE); gl.disable(gl.CULL_FACE);
gl.enable(gl.BLEND); gl.enable(gl.BLEND);
// Transparent // Transparent
gl.clearColor(0, 0, 0, 0); gl.clearColor(0, 0, 0, 0);
// Black // Black

View file

@ -20,6 +20,10 @@ Phaser.State = function (config)
this.sys = new Phaser.State.Systems(this, config); this.sys = new Phaser.State.Systems(this, config);
// Needs to have its own Renderer - can make 1 draw call per State if smart enough
// Use a shared canvas though. Or
this.renderer;
// Reference to sys.children, set during sys.init only // Reference to sys.children, set during sys.init only
this.children; this.children;
}; };

View file

@ -31,12 +31,12 @@ Phaser.StateManager = function (game, pending)
for (var i = 0; i < pending.length; i++) for (var i = 0; i < pending.length; i++)
{ {
// The i === 0 part just starts the first State given // The i === 0 part just starts the first State given
this._pending.push({ key: 'default', state: pending[i], autoStart: (i === 0) }); this._pending.push({ index: i, key: 'default', state: pending[i], autoStart: (i === 0) });
} }
} }
else else
{ {
this._pending.push({ key: 'default', state: pending, autoStart: true }); this._pending.push({ index: 0, key: 'default', state: pending, autoStart: true });
} }
} }
}; };
@ -64,6 +64,7 @@ Phaser.StateManager.prototype = {
this.add(entry.key, entry.state, entry.autoStart); this.add(entry.key, entry.state, entry.autoStart);
} }
// Clear the pending list
this._pending = []; this._pending = [];
}, },
@ -109,9 +110,9 @@ Phaser.StateManager.prototype = {
// if not booted, then put state into a holding pattern // if not booted, then put state into a holding pattern
if (!this.game.isBooted) if (!this.game.isBooted)
{ {
console.log('StateManager not yet booted, adding to list'); this._pending.push({ index: this._pending.length, key: key, state: state, autoStart: autoStart });
this._pending.push({ key: key, state: state, autoStart: autoStart }); console.log('StateManager not yet booted, adding to list', this._pending.length);
return; return;
} }
@ -291,6 +292,24 @@ Phaser.StateManager.prototype = {
return this.keys[key]; return this.keys[key];
}, },
getStateIndex: function (state)
{
return this.states.indexOf(state);
},
getActiveStateIndex: function (state)
{
for (var i = 0; i < this.active.length; i++)
{
if (this.active[i].state === state)
{
return this.active[i].index;
}
}
return -1;
},
isActive: function (key) isActive: function (key)
{ {
var state = this.getState(key); var state = this.getState(key);
@ -390,18 +409,52 @@ Phaser.StateManager.prototype = {
state.create.call(state); state.create.call(state);
} }
this.active.push(state); // Insert at the correct index, or it just all goes wrong :)
var i = this.getStateIndex(state);
console.log('startCreate', i);
console.log(state);
this.active.push({ index: i, state: state });
// Sort the 'active' array based on the index property
// FIX: Cannot sort because it contains objects?
this.active.sort(this.sortStates.bind(this));
this.game.updates.running = true; this.game.updates.running = true;
}, },
sortStates: function (stateA, stateB)
{
// var indexA = this.getActiveStateIndex(stateA.state);
// var indexB = this.getActiveStateIndex(stateB);
console.log('sorting states', stateA, stateB);
// Sort descending
if (stateA.index < stateB.index)
{
return -1;
}
else if (stateA.index > stateB.index)
{
return 1;
}
else
{
return 0;
}
},
// See if we can reduce this down to just update and render // See if we can reduce this down to just update and render
preUpdate: function () preUpdate: function ()
{ {
for (var i = 0; i < this.active.length; i++) for (var i = 0; i < this.active.length; i++)
{ {
var state = this.active[i]; var state = this.active[i].state;
for (var c = 0; c < state.sys.children.list.length; c++) for (var c = 0; c < state.sys.children.list.length; c++)
{ {
@ -416,7 +469,7 @@ Phaser.StateManager.prototype = {
{ {
for (var i = 0; i < this.active.length; i++) for (var i = 0; i < this.active.length; i++)
{ {
var state = this.active[i]; var state = this.active[i].state;
// Invoke State Main Loop here - updating all of its systems (tweens, physics, etc) // Invoke State Main Loop here - updating all of its systems (tweens, physics, etc)
@ -441,7 +494,7 @@ Phaser.StateManager.prototype = {
{ {
for (var i = 0; i < this.active.length; i++) for (var i = 0; i < this.active.length; i++)
{ {
var state = this.active[i]; var state = this.active[i].state;
for (var c = 0; c < state.sys.children.list.length; c++) for (var c = 0; c < state.sys.children.list.length; c++)
{ {
@ -456,7 +509,7 @@ Phaser.StateManager.prototype = {
{ {
for (var i = 0; i < this.active.length; i++) for (var i = 0; i < this.active.length; i++)
{ {
var state = this.active[i]; var state = this.active[i].state;
// Can put all kinds of other checks in here, like MainLoop, FPS, etc. // Can put all kinds of other checks in here, like MainLoop, FPS, etc.
if (!state.settings.visible || state.sys.color.alpha === 0 || state.sys.children.list.length === 0) if (!state.settings.visible || state.sys.color.alpha === 0 || state.sys.children.list.length === 0)