2016-11-04 02:08:17 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The State Manager is responsible for loading, setting up and switching game states.
|
|
|
|
*
|
|
|
|
* @class Phaser.StateManager
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2016-11-09 14:52:53 +00:00
|
|
|
Phaser.StateManager = function (game, stateConfig)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
// Everything kept in here
|
|
|
|
this.keys = {};
|
|
|
|
this.states = [];
|
|
|
|
|
|
|
|
// Only active states are kept in here
|
|
|
|
this.active = [];
|
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
this._pending = [];
|
2016-11-04 03:09:48 +00:00
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig)
|
2016-11-04 05:41:00 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
if (Array.isArray(stateConfig))
|
2016-11-04 05:41:00 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
for (var i = 0; i < stateConfig.length; i++)
|
2016-11-04 05:41:00 +00:00
|
|
|
{
|
|
|
|
// The i === 0 part just starts the first State given
|
2016-11-09 14:52:53 +00:00
|
|
|
this._pending.push({ index: i, key: 'default', state: stateConfig[i], autoStart: (i === 0) });
|
2016-11-04 05:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
this._pending.push({ index: 0, key: 'default', state: stateConfig, autoStart: true });
|
2016-11-04 05:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 02:08:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.StateManager.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Boot handler is called by Phaser.Game when it first starts up.
|
2016-11-04 02:24:49 +00:00
|
|
|
* The renderer is available by now.
|
2016-11-04 04:12:18 +00:00
|
|
|
*
|
2016-11-04 02:08:17 +00:00
|
|
|
* @method Phaser.StateManager#boot
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
// this.game.onPause.add(this.pause, this);
|
|
|
|
// this.game.onResume.add(this.resume, this);
|
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
console.log('StateManager.boot');
|
|
|
|
|
|
|
|
for (var i = 0; i < this._pending.length; i++)
|
2016-11-04 03:09:48 +00:00
|
|
|
{
|
2016-11-04 05:41:00 +00:00
|
|
|
var entry = this._pending[i];
|
2016-11-04 03:09:48 +00:00
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
this.add(entry.key, entry.state, entry.autoStart);
|
2016-11-04 03:09:48 +00:00
|
|
|
}
|
2016-11-04 05:41:00 +00:00
|
|
|
|
2016-11-06 13:59:48 +00:00
|
|
|
// Clear the pending list
|
2016-11-04 05:41:00 +00:00
|
|
|
this._pending = [];
|
2016-11-04 03:09:48 +00:00
|
|
|
},
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
getKey: function (key, stateConfig)
|
2016-11-04 03:09:48 +00:00
|
|
|
{
|
2016-11-04 05:41:00 +00:00
|
|
|
if (!key) { key = 'default'; }
|
2016-11-04 03:09:48 +00:00
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig instanceof Phaser.State)
|
2016-11-04 03:09:48 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
key = stateConfig.settings.key;
|
2016-11-04 03:09:48 +00:00
|
|
|
}
|
2016-11-09 14:52:53 +00:00
|
|
|
else if (typeof stateConfig === 'object' && stateConfig.hasOwnProperty('key'))
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
key = stateConfig.key;
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 03:09:48 +00:00
|
|
|
// By this point it's either 'default' or extracted from the State
|
|
|
|
|
|
|
|
if (this.keys.hasOwnProperty(key))
|
|
|
|
{
|
|
|
|
throw new Error('Cannot add a State with duplicate key: ' + key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
2016-11-04 02:08:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new State into the StateManager. You must give each State a unique key by which you'll identify it.
|
|
|
|
* The State can be either a Phaser.State object (or an object that extends it), a plain JavaScript object or a function.
|
|
|
|
* If a function is given a new state object will be created by calling it.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#add
|
|
|
|
* @param {string} key - A unique key you use to reference this state, i.e. "MainMenu", "Level1".
|
|
|
|
* @param {Phaser.State|object|function} state - The state you want to switch to.
|
|
|
|
* @param {boolean} [autoStart=false] - If true the State will be started immediately after adding it.
|
|
|
|
*/
|
2016-11-09 14:52:53 +00:00
|
|
|
add: function (key, stateConfig, autoStart)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
|
|
|
if (autoStart === undefined) { autoStart = false; }
|
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
// if not booted, then put state into a holding pattern
|
|
|
|
if (!this.game.isBooted)
|
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
this._pending.push({ index: this._pending.length, key: key, state: stateConfig, autoStart: autoStart });
|
2016-11-04 05:41:00 +00:00
|
|
|
|
2016-11-06 13:59:48 +00:00
|
|
|
console.log('StateManager not yet booted, adding to list', this._pending.length);
|
2016-11-04 05:41:00 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
key = this.getKey(key, stateConfig);
|
2016-11-04 03:09:48 +00:00
|
|
|
|
2016-11-04 02:08:17 +00:00
|
|
|
var newState;
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig instanceof Phaser.State)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-04 05:41:00 +00:00
|
|
|
console.log('StateManager.add from instance', key);
|
2016-11-09 14:52:53 +00:00
|
|
|
newState = this.createStateFromInstance(key, stateConfig);
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
2016-11-09 14:52:53 +00:00
|
|
|
else if (typeof stateConfig === 'object')
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-04 05:41:00 +00:00
|
|
|
console.log('StateManager.add from object', key);
|
2016-11-09 14:52:53 +00:00
|
|
|
|
|
|
|
stateConfig.key = key;
|
|
|
|
|
|
|
|
newState = this.createStateFromObject(key, stateConfig);
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
2016-11-09 14:52:53 +00:00
|
|
|
else if (typeof stateConfig === 'function')
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-04 05:41:00 +00:00
|
|
|
console.log('StateManager.add from function', key);
|
2016-11-09 14:52:53 +00:00
|
|
|
|
|
|
|
newState = this.createStateFromFunction(key, stateConfig);
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.keys[key] = newState;
|
|
|
|
|
|
|
|
this.states.push(newState);
|
|
|
|
|
2016-11-04 03:09:48 +00:00
|
|
|
// window.console.dir(newState);
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-04 04:12:18 +00:00
|
|
|
if (autoStart || newState.settings.active)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
|
|
|
if (this.game.isBooted)
|
|
|
|
{
|
|
|
|
this.start(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-04 03:09:48 +00:00
|
|
|
this._start.push(key);
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 03:09:48 +00:00
|
|
|
|
|
|
|
return newState;
|
|
|
|
},
|
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
createStateFromInstance: function (key, newState)
|
|
|
|
{
|
|
|
|
newState.game = this.game;
|
|
|
|
|
|
|
|
newState.settings.key = key;
|
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
newState.sys.init();
|
2016-11-04 05:41:00 +00:00
|
|
|
|
2016-11-04 03:09:48 +00:00
|
|
|
if (this.game.renderType === Phaser.WEBGL)
|
|
|
|
{
|
2016-11-04 04:12:18 +00:00
|
|
|
this.createStateFrameBuffer(newState);
|
2016-11-04 03:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
2016-11-04 02:08:17 +00:00
|
|
|
},
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
createStateFromObject: function (key, stateConfig)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
var newState = new Phaser.State(stateConfig);
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-04 05:41:00 +00:00
|
|
|
newState.game = this.game;
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
newState.sys.init();
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
if (this.game.renderType === Phaser.WEBGL)
|
|
|
|
{
|
|
|
|
this.createStateFrameBuffer(newState);
|
|
|
|
}
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
// Extract callbacks or set NOOP
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig.hasOwnProperty('init'))
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
newState.init = stateConfig.init;
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig.hasOwnProperty('preload'))
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
newState.preload = stateConfig.preload;
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig.hasOwnProperty('create'))
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
newState.create = stateConfig.create;
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
if (stateConfig.hasOwnProperty('shutdown'))
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-09 14:52:53 +00:00
|
|
|
newState.shutdown = stateConfig.shutdown;
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 14:52:53 +00:00
|
|
|
newState.preUpdate = (stateConfig.hasOwnProperty('preUpdate')) ? stateConfig.preUpdate : Phaser.NOOP;
|
|
|
|
newState.update = (stateConfig.hasOwnProperty('update')) ? stateConfig.update : Phaser.NOOP;
|
|
|
|
newState.postUpdate = (stateConfig.hasOwnProperty('postUpdate')) ? stateConfig.postUpdate : Phaser.NOOP;
|
|
|
|
newState.render = (stateConfig.hasOwnProperty('render')) ? stateConfig.render : Phaser.NOOP;
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
return newState;
|
|
|
|
},
|
2016-11-04 02:24:49 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
createStateFromFunction: function (key, state)
|
|
|
|
{
|
|
|
|
var newState = new state();
|
2016-11-04 02:24:49 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
if (newState instanceof Phaser.State)
|
2016-11-04 02:24:49 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
return this.createStateFromInstance(key, newState);
|
2016-11-04 02:24:49 +00:00
|
|
|
}
|
2016-11-06 12:18:08 +00:00
|
|
|
else
|
2016-11-04 02:24:49 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
newState.game = this.game;
|
|
|
|
|
|
|
|
newState.settings = new Phaser.State.Settings(newState, key);
|
|
|
|
|
|
|
|
newState.sys = new Phaser.State.Systems(newState);
|
|
|
|
|
|
|
|
newState.sys.init();
|
|
|
|
|
|
|
|
if (this.game.renderType === Phaser.WEBGL)
|
|
|
|
{
|
|
|
|
this.createStateFrameBuffer(newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default required functions
|
|
|
|
|
|
|
|
if (!newState.preUpdate)
|
|
|
|
{
|
|
|
|
newState.preUpdate = Phaser.NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.update)
|
|
|
|
{
|
|
|
|
newState.update = Phaser.NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.postUpdate)
|
|
|
|
{
|
|
|
|
newState.postUpdate = Phaser.NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.render)
|
|
|
|
{
|
|
|
|
newState.render = Phaser.NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
2016-11-04 02:24:49 +00:00
|
|
|
}
|
2016-11-06 12:18:08 +00:00
|
|
|
},
|
2016-11-04 02:24:49 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
createStateFrameBuffer: function (newState)
|
|
|
|
{
|
|
|
|
var x = newState.settings.x;
|
|
|
|
var y = newState.settings.y;
|
|
|
|
|
|
|
|
if (newState.settings.width === -1)
|
2016-11-04 02:24:49 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
newState.settings.width = this.game.width;
|
2016-11-04 02:24:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
if (newState.settings.height === -1)
|
2016-11-04 02:24:49 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
newState.settings.height = this.game.height;
|
2016-11-04 02:24:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
var width = newState.settings.width;
|
|
|
|
var height = newState.settings.height;
|
|
|
|
|
2016-11-10 17:04:55 +00:00
|
|
|
newState.sys.fbo = this.game.renderer.createFBO(newState, x, y, width, height);
|
2016-11-04 02:08:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getState: function (key)
|
|
|
|
{
|
|
|
|
return this.keys[key];
|
|
|
|
},
|
|
|
|
|
2016-11-06 13:59:48 +00:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2016-11-04 04:12:18 +00:00
|
|
|
isActive: function (key)
|
|
|
|
{
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
|
|
|
return (state && state.settings.active && this.active.indexOf(state) !== -1);
|
|
|
|
},
|
|
|
|
|
2016-11-04 02:08:17 +00:00
|
|
|
start: function (key)
|
|
|
|
{
|
2016-11-04 05:41:00 +00:00
|
|
|
// if not booted, then put state into a holding pattern
|
|
|
|
if (!this.game.isBooted)
|
|
|
|
{
|
|
|
|
console.log('StateManager not yet booted, setting autoStart on pending list');
|
|
|
|
|
|
|
|
for (var i = 0; i < this._pending.length; i++)
|
|
|
|
{
|
|
|
|
var entry = this._pending[i];
|
|
|
|
|
|
|
|
if (entry.key === key)
|
|
|
|
{
|
|
|
|
entry.autoStart = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 02:08:17 +00:00
|
|
|
var state = this.getState(key);
|
|
|
|
|
2016-11-04 04:12:18 +00:00
|
|
|
if (state)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-04 04:12:18 +00:00
|
|
|
// Already started? Nothing more to do here ...
|
|
|
|
if (this.isActive(key))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 02:08:17 +00:00
|
|
|
state.settings.active = true;
|
|
|
|
|
|
|
|
// + arguments
|
|
|
|
if (state.init)
|
|
|
|
{
|
|
|
|
state.init.call(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.preload)
|
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
state.sys.load.reset(true);
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
state.preload.call(state, this.game);
|
|
|
|
|
|
|
|
// Is the loader empty?
|
2016-11-06 12:18:08 +00:00
|
|
|
if (state.sys.load.totalQueuedFiles() === 0 && state.sys.load.totalQueuedPacks() === 0)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-04 03:09:48 +00:00
|
|
|
// console.log('empty queue');
|
2016-11-04 02:08:17 +00:00
|
|
|
this.startCreate(state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-04 03:09:48 +00:00
|
|
|
// console.log('load start');
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
// Start the loader going as we have something in the queue
|
2016-11-04 03:21:04 +00:00
|
|
|
// state.load.onLoadComplete.addOnce(this.loadComplete, this, 0, state);
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
state.sys.load.start();
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-04 03:09:48 +00:00
|
|
|
// console.log('no preload');
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
// No preload? Then there was nothing to load either
|
|
|
|
this.startCreate(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
loadComplete: function (state)
|
|
|
|
{
|
2016-11-04 03:09:48 +00:00
|
|
|
// console.log('loadComplete');
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
// Make sure to do load-update one last time before state is set to _created
|
|
|
|
|
|
|
|
if (state.hasOwnProperty('loadUpdate'))
|
|
|
|
{
|
|
|
|
state.loadUpdate.call(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.startCreate(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
startCreate: function (state)
|
|
|
|
{
|
|
|
|
if (state.create)
|
|
|
|
{
|
|
|
|
state.create.call(state);
|
|
|
|
}
|
|
|
|
|
2016-11-06 13:59:48 +00:00
|
|
|
// Insert at the correct index, or it just all goes wrong :)
|
|
|
|
|
|
|
|
var i = this.getStateIndex(state);
|
|
|
|
|
|
|
|
this.active.push({ index: i, state: state });
|
|
|
|
|
|
|
|
// Sort the 'active' array based on the index property
|
|
|
|
this.active.sort(this.sortStates.bind(this));
|
2016-11-06 13:04:45 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
state.sys.updates.running = true;
|
|
|
|
|
|
|
|
state.sys.mainloop.start();
|
2016-11-04 02:08:17 +00:00
|
|
|
},
|
|
|
|
|
2016-11-06 16:50:45 +00:00
|
|
|
pause: function (key)
|
2016-11-06 13:59:48 +00:00
|
|
|
{
|
2016-11-06 16:50:45 +00:00
|
|
|
var index = this.getActiveStateIndex(key);
|
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
{
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
|
|
|
state.settings.active = false;
|
2016-11-06 13:59:48 +00:00
|
|
|
|
2016-11-06 16:50:45 +00:00
|
|
|
this.active.splice(index, 1);
|
2016-11-06 13:59:48 +00:00
|
|
|
|
2016-11-06 16:50:45 +00:00
|
|
|
this.active.sort(this.sortStates.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sortStates: function (stateA, stateB)
|
|
|
|
{
|
2016-11-06 13:59:48 +00:00
|
|
|
// Sort descending
|
|
|
|
if (stateA.index < stateB.index)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (stateA.index > stateB.index)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-04 02:08:17 +00:00
|
|
|
// See if we can reduce this down to just update and render
|
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
step: function (timestamp)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
|
|
|
var state = this.active[i].state;
|
|
|
|
|
|
|
|
if (state.sys.mainloop.running)
|
|
|
|
{
|
|
|
|
state.sys.mainloop.step(timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2016-11-04 02:08:17 +00:00
|
|
|
preUpdate: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
2016-11-06 13:59:48 +00:00
|
|
|
var state = this.active[i].state;
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
for (var c = 0; c < state.sys.children.list.length; c++)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
state.sys.children.list[c].preUpdate();
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state.preUpdate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
2016-11-06 13:59:48 +00:00
|
|
|
var state = this.active[i].state;
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-04 04:12:18 +00:00
|
|
|
// Invoke State Main Loop here - updating all of its systems (tweens, physics, etc)
|
|
|
|
|
2016-11-04 06:25:07 +00:00
|
|
|
// This shouldn't be called if the State is still loading
|
|
|
|
// Have a State.STATUS const in the Settings, dictating what is going on
|
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
for (var c = 0; c < state.sys.children.list.length; c++)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
var child = state.sys.children.list[c];
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
if (child.exists)
|
|
|
|
{
|
|
|
|
child.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
postUpdate: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
2016-11-06 13:59:48 +00:00
|
|
|
var state = this.active[i].state;
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-06 12:18:08 +00:00
|
|
|
for (var c = 0; c < state.sys.children.list.length; c++)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
state.sys.children.list[c].postUpdate();
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state.postUpdate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
2016-11-06 13:59:48 +00:00
|
|
|
var state = this.active[i].state;
|
2016-11-04 02:08:17 +00:00
|
|
|
|
|
|
|
// Can put all kinds of other checks in here, like MainLoop, FPS, etc.
|
2016-11-06 12:18:08 +00:00
|
|
|
if (!state.settings.visible || state.sys.color.alpha === 0 || state.sys.children.list.length === 0)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.game.renderer.render(state);
|
|
|
|
}
|
|
|
|
},
|
2016-11-08 01:50:57 +00:00
|
|
|
*/
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
renderChildren: function (renderer, state, interpolationPercentage)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
|
|
|
// Populates the display list
|
2016-11-06 12:18:08 +00:00
|
|
|
for (var c = 0; c < state.sys.children.list.length; c++)
|
2016-11-04 02:08:17 +00:00
|
|
|
{
|
2016-11-06 12:18:08 +00:00
|
|
|
var child = state.sys.children.list[c];
|
2016-11-04 02:08:17 +00:00
|
|
|
|
2016-11-08 01:50:57 +00:00
|
|
|
child.render(renderer, child, interpolationPercentage);
|
2016-11-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|