2016-11-29 13:01:16 +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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var CONST = require('../const');
|
|
|
|
var NOOP = require('../utils/NOOP');
|
2016-11-29 15:25:14 +00:00
|
|
|
var State = require('./State');
|
|
|
|
var Systems = require('./Systems');
|
2017-04-26 15:03:14 +00:00
|
|
|
var GetValue = require('../utils/object/GetValue');
|
2017-01-25 17:10:19 +00:00
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2017-02-07 18:44:26 +00:00
|
|
|
var Rectangle = require('../geom/rectangle/Rectangle');
|
|
|
|
var CanvasPool = require('../dom/CanvasPool');
|
|
|
|
var CanvasInterpolation = require('../dom/CanvasInterpolation');
|
|
|
|
var GetContext = require('../canvas/GetContext');
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The State Manager is responsible for loading, setting up and switching game states.
|
|
|
|
*
|
2017-02-08 01:07:01 +00:00
|
|
|
* @class Phaser.GlobalStateManager
|
2016-11-29 13:01:16 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2017-02-08 01:07:01 +00:00
|
|
|
var GlobalStateManager = function (game, stateConfig)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
// Everything kept in here
|
|
|
|
this.keys = {};
|
|
|
|
this.states = [];
|
|
|
|
|
|
|
|
// Only active states are kept in here
|
|
|
|
this.active = [];
|
|
|
|
|
|
|
|
this._pending = [];
|
|
|
|
|
|
|
|
if (stateConfig)
|
|
|
|
{
|
|
|
|
if (Array.isArray(stateConfig))
|
|
|
|
{
|
|
|
|
for (var i = 0; i < stateConfig.length; i++)
|
|
|
|
{
|
|
|
|
// The i === 0 part just starts the first State given
|
2016-11-29 15:25:14 +00:00
|
|
|
this._pending.push({
|
|
|
|
index: i,
|
|
|
|
key: 'default',
|
|
|
|
state: stateConfig[i],
|
2017-02-16 17:18:50 +00:00
|
|
|
autoStart: (i === 0),
|
|
|
|
data: {}
|
2016-11-29 15:25:14 +00:00
|
|
|
});
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-29 15:25:14 +00:00
|
|
|
this._pending.push({
|
|
|
|
index: 0,
|
|
|
|
key: 'default',
|
|
|
|
state: stateConfig,
|
2017-02-16 17:18:50 +00:00
|
|
|
autoStart: true,
|
|
|
|
data: {}
|
2016-11-29 15:25:14 +00:00
|
|
|
});
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-08 01:07:01 +00:00
|
|
|
GlobalStateManager.prototype.constructor = GlobalStateManager;
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-08 01:07:01 +00:00
|
|
|
GlobalStateManager.prototype = {
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Boot handler is called by Phaser.Game when it first starts up.
|
|
|
|
* The renderer is available by now.
|
|
|
|
*
|
2017-02-08 01:07:01 +00:00
|
|
|
* @method Phaser.GlobalStateManager#boot
|
2016-11-29 13:01:16 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this._pending.length; i++)
|
|
|
|
{
|
|
|
|
var entry = this._pending[i];
|
|
|
|
|
|
|
|
this.add(entry.key, entry.state, entry.autoStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the pending list
|
|
|
|
this._pending = [];
|
|
|
|
},
|
|
|
|
|
2017-02-16 17:18:50 +00:00
|
|
|
// private
|
2016-11-29 13:01:16 +00:00
|
|
|
getKey: function (key, stateConfig)
|
|
|
|
{
|
|
|
|
if (!key) { key = 'default'; }
|
|
|
|
|
2017-06-28 01:49:38 +00:00
|
|
|
if (typeof stateConfig === 'function')
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
else if (stateConfig instanceof State)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-06-29 23:32:18 +00:00
|
|
|
key = stateConfig.sys.settings.key;
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
else if (typeof stateConfig === 'object' && stateConfig.hasOwnProperty('key'))
|
|
|
|
{
|
|
|
|
key = stateConfig.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-08 01:07:01 +00:00
|
|
|
* Adds a new State into the GlobalStateManager. You must give each State a unique key by which you'll identify it.
|
2016-11-29 13:01:16 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2017-02-08 01:07:01 +00:00
|
|
|
* @method Phaser.GlobalStateManager#add
|
2016-11-29 13:01:16 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
add: function (key, stateConfig, autoStart)
|
|
|
|
{
|
|
|
|
if (autoStart === undefined) { autoStart = false; }
|
|
|
|
|
|
|
|
// if not booted, then put state into a holding pattern
|
|
|
|
if (!this.game.isBooted)
|
|
|
|
{
|
|
|
|
this._pending.push({
|
|
|
|
index: this._pending.length,
|
|
|
|
key: key,
|
|
|
|
state: stateConfig,
|
|
|
|
autoStart: autoStart
|
|
|
|
});
|
|
|
|
|
2017-06-28 01:49:38 +00:00
|
|
|
// console.log('GlobalStateManager not yet booted, adding to list', this._pending.length);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-03 11:24:18 +00:00
|
|
|
// var ok = key;
|
2016-11-29 13:01:16 +00:00
|
|
|
key = this.getKey(key, stateConfig);
|
|
|
|
|
2017-07-03 11:24:18 +00:00
|
|
|
// console.group('GlobalStateManager.add');
|
|
|
|
// console.log('add key:', ok);
|
|
|
|
// console.log('config key:', key);
|
|
|
|
// console.log('config:', stateConfig);
|
|
|
|
// console.log('autoStart:', autoStart);
|
|
|
|
// console.groupEnd();
|
2017-02-07 18:44:26 +00:00
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
var newState;
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
if (stateConfig instanceof State)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-02-08 01:07:01 +00:00
|
|
|
// console.log('GlobalStateManager.add from instance:', key);
|
2017-06-28 01:49:38 +00:00
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
newState = this.createStateFromInstance(key, stateConfig);
|
|
|
|
}
|
|
|
|
else if (typeof stateConfig === 'object')
|
|
|
|
{
|
2017-02-08 01:07:01 +00:00
|
|
|
// console.log('GlobalStateManager.add from object:', key);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
stateConfig.key = key;
|
|
|
|
|
|
|
|
newState = this.createStateFromObject(key, stateConfig);
|
|
|
|
}
|
|
|
|
else if (typeof stateConfig === 'function')
|
|
|
|
{
|
2017-02-08 01:07:01 +00:00
|
|
|
// console.log('GlobalStateManager.add from function:', key);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
newState = this.createStateFromFunction(key, stateConfig);
|
|
|
|
}
|
|
|
|
|
2017-07-03 11:24:18 +00:00
|
|
|
// Replace key in case the state changed it
|
2017-06-29 23:32:18 +00:00
|
|
|
key = newState.sys.settings.key;
|
2017-06-28 01:49:38 +00:00
|
|
|
|
2017-07-03 11:24:18 +00:00
|
|
|
// console.log('replaced key', key);
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
this.keys[key] = newState;
|
|
|
|
|
|
|
|
this.states.push(newState);
|
|
|
|
|
2017-06-29 23:32:18 +00:00
|
|
|
if (autoStart || newState.sys.settings.active)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
|
|
|
if (this.game.isBooted)
|
|
|
|
{
|
|
|
|
this.start(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._start.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
},
|
|
|
|
|
|
|
|
createStateFromInstance: function (key, newState)
|
|
|
|
{
|
2017-07-03 11:24:18 +00:00
|
|
|
var configKey = newState.sys.settings.key;
|
|
|
|
|
|
|
|
if (configKey !== '')
|
|
|
|
{
|
|
|
|
key = configKey;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newState.sys.settings.key = key;
|
|
|
|
}
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
newState.sys.init(this.game);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.createStateDisplay(newState);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
return newState;
|
|
|
|
},
|
|
|
|
|
|
|
|
createStateFromObject: function (key, stateConfig)
|
|
|
|
{
|
2016-11-29 15:25:14 +00:00
|
|
|
var newState = new State(stateConfig);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-03 11:24:18 +00:00
|
|
|
var configKey = newState.sys.settings.key;
|
|
|
|
|
|
|
|
if (configKey !== '')
|
|
|
|
{
|
|
|
|
key = configKey;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newState.sys.settings.key = key;
|
|
|
|
}
|
2017-07-02 21:27:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
newState.sys.init(this.game);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.createStateDisplay(newState);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
return this.setupCallbacks(newState, stateConfig);
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
createStateFromFunction: function (key, state)
|
|
|
|
{
|
2017-07-03 11:24:18 +00:00
|
|
|
// console.log('createStateFromFunction', key);
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
var newState = new state();
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
if (newState instanceof State)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-07-03 11:24:18 +00:00
|
|
|
// console.log('instanceof State');
|
|
|
|
|
|
|
|
var configKey = newState.sys.settings.key;
|
|
|
|
|
|
|
|
if (configKey !== '')
|
|
|
|
{
|
|
|
|
key = configKey;
|
|
|
|
}
|
2017-06-28 01:49:38 +00:00
|
|
|
|
|
|
|
if (this.keys.hasOwnProperty(key))
|
|
|
|
{
|
|
|
|
throw new Error('Cannot add a State with duplicate key: ' + key);
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
return this.createStateFromInstance(key, newState);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-29 15:25:14 +00:00
|
|
|
newState.sys = new Systems(newState);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-02 21:27:14 +00:00
|
|
|
newState.sys.settings.key = key;
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
newState.sys.init(this.game);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.createStateDisplay(newState);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
// Default required functions
|
2017-02-07 00:41:21 +00:00
|
|
|
|
|
|
|
if (!newState.init)
|
|
|
|
{
|
|
|
|
newState.init = NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.preload)
|
|
|
|
{
|
|
|
|
newState.preload = NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.create)
|
|
|
|
{
|
|
|
|
newState.create = NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.shutdown)
|
|
|
|
{
|
|
|
|
newState.shutdown = NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.update)
|
|
|
|
{
|
|
|
|
newState.update = NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newState.render)
|
|
|
|
{
|
|
|
|
newState.render = NOOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
2016-11-29 15:25:14 +00:00
|
|
|
}
|
|
|
|
},
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
setupCallbacks: function (state, stateConfig)
|
2016-11-29 15:25:14 +00:00
|
|
|
{
|
2017-02-07 18:44:26 +00:00
|
|
|
if (stateConfig === undefined) { stateConfig = state; }
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
// Extract callbacks or set NOOP
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-04-26 15:03:14 +00:00
|
|
|
state.init = GetValue(stateConfig, 'init', NOOP);
|
|
|
|
state.preload = GetValue(stateConfig, 'preload', NOOP);
|
|
|
|
state.create = GetValue(stateConfig, 'create', NOOP);
|
|
|
|
state.shutdown = GetValue(stateConfig, 'shutdown', NOOP);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// Game Loop level callbacks
|
|
|
|
|
2017-04-26 15:03:14 +00:00
|
|
|
state.update = GetValue(stateConfig, 'update', NOOP);
|
|
|
|
state.render = GetValue(stateConfig, 'render', NOOP);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
return state;
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
createStateDisplay: function (state)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-06-29 23:32:18 +00:00
|
|
|
// console.log('createStateDisplay', state.sys.settings.key);
|
2017-01-26 04:06:10 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
var settings = state.sys.settings;
|
2017-01-26 04:06:10 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
// var x = settings.x;
|
|
|
|
// var y = settings.y;
|
|
|
|
var width = settings.width;
|
|
|
|
var height = settings.height;
|
2017-01-26 04:06:10 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
var config = this.game.config;
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
if (config.renderType === CONST.CANVAS)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-02-07 18:44:26 +00:00
|
|
|
if (settings.renderToTexture)
|
|
|
|
{
|
2017-02-07 22:00:55 +00:00
|
|
|
// console.log('renderToTexture', width, height);
|
2017-02-07 18:44:26 +00:00
|
|
|
state.sys.canvas = CanvasPool.create(state, width, height);
|
|
|
|
state.sys.context = GetContext(state.sys.canvas);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
// Pixel Art mode?
|
|
|
|
if (config.pixelArt)
|
|
|
|
{
|
|
|
|
CanvasInterpolation.setCrisp(state.sys.canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-07 22:00:55 +00:00
|
|
|
// console.log('using game canvas');
|
2017-02-07 18:44:26 +00:00
|
|
|
state.sys.mask = new Rectangle(0, 0, width, height);
|
|
|
|
state.sys.canvas = this.game.canvas;
|
|
|
|
state.sys.context = this.game.context;
|
|
|
|
}
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
2017-02-07 18:44:26 +00:00
|
|
|
else if (config.renderType === CONST.WEBGL)
|
2017-01-26 04:06:10 +00:00
|
|
|
{
|
2017-02-07 18:44:26 +00:00
|
|
|
// state.sys.fbo = this.game.renderer.createFBO(state, x, y, width, height);
|
2017-01-26 04:06:10 +00:00
|
|
|
}
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getState: function (key)
|
|
|
|
{
|
|
|
|
return this.keys[key];
|
|
|
|
},
|
|
|
|
|
|
|
|
getStateIndex: function (state)
|
|
|
|
{
|
|
|
|
return this.states.indexOf(state);
|
|
|
|
},
|
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
getActiveState: function (key)
|
|
|
|
{
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
|
|
|
if (this.active[i].state === state)
|
|
|
|
{
|
|
|
|
return this.active[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
getActiveStateIndex: function (state)
|
|
|
|
{
|
2017-02-07 20:47:41 +00:00
|
|
|
var index = -1;
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
for (var i = 0; i < this.active.length; i++)
|
|
|
|
{
|
|
|
|
if (this.active[i].state === state)
|
|
|
|
{
|
2017-02-07 20:47:41 +00:00
|
|
|
index = this.active[i].index;
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 20:47:41 +00:00
|
|
|
return index;
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
isActive: function (key)
|
|
|
|
{
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
2017-06-29 23:32:18 +00:00
|
|
|
return (state && state.sys.settings.active && this.active.indexOf(state) !== -1);
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
2017-02-16 17:18:50 +00:00
|
|
|
start: function (key, data)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-02-16 17:18:50 +00:00
|
|
|
if (data === undefined) { data = {}; }
|
|
|
|
|
2017-07-02 21:27:14 +00:00
|
|
|
// console.log('start:', key);
|
2017-02-17 02:07:56 +00:00
|
|
|
// console.dir(data);
|
2017-02-07 18:44:26 +00:00
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
// if not booted, then put state into a holding pattern
|
|
|
|
if (!this.game.isBooted)
|
|
|
|
{
|
2017-02-08 01:07:01 +00:00
|
|
|
// console.log('GlobalStateManager not yet booted, setting autoStart on pending list');
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < this._pending.length; i++)
|
|
|
|
{
|
|
|
|
var entry = this._pending[i];
|
|
|
|
|
|
|
|
if (entry.key === key)
|
|
|
|
{
|
|
|
|
entry.autoStart = true;
|
2017-02-16 17:18:50 +00:00
|
|
|
entry.data = data;
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
2017-06-28 01:49:38 +00:00
|
|
|
// console.log(state);
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
// Already started? Nothing more to do here ...
|
|
|
|
if (this.isActive(key))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-30 03:06:53 +00:00
|
|
|
state.sys.start(data);
|
2017-02-17 02:07:56 +00:00
|
|
|
|
2017-02-07 12:54:51 +00:00
|
|
|
var loader = state.sys.load;
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-07 12:54:51 +00:00
|
|
|
// Files payload?
|
|
|
|
if (loader && Array.isArray(state.sys.settings.files))
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-02-07 12:54:51 +00:00
|
|
|
loader.reset();
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-07 12:54:51 +00:00
|
|
|
if (loader.loadArray(state.sys.settings.files))
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
loader.events.once('LOADER_COMPLETE_EVENT', this.payloadComplete.bind(this));
|
2017-02-07 12:54:51 +00:00
|
|
|
|
|
|
|
loader.start();
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
this.bootState(state);
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
this.bootState(state);
|
2017-02-07 12:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
payloadComplete: function (event)
|
2017-02-07 12:54:51 +00:00
|
|
|
{
|
|
|
|
var state = event.loader.state;
|
|
|
|
|
2017-02-07 21:49:25 +00:00
|
|
|
// console.log('payloadComplete', state.sys.settings.key);
|
2017-02-07 18:44:26 +00:00
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
this.bootState(state);
|
2017-02-07 12:54:51 +00:00
|
|
|
},
|
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
bootState: function (state)
|
2017-02-07 12:54:51 +00:00
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
// console.log('bootState', state.sys.settings.key);
|
2017-02-07 12:54:51 +00:00
|
|
|
|
|
|
|
if (state.init)
|
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
state.init.call(state, state.sys.settings.data);
|
2017-02-07 12:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var loader = state.sys.load;
|
2017-02-07 18:44:26 +00:00
|
|
|
|
|
|
|
loader.reset();
|
2017-02-07 12:54:51 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
if (state.preload)
|
2017-02-07 12:54:51 +00:00
|
|
|
{
|
2017-02-07 20:47:41 +00:00
|
|
|
state.preload(this.game);
|
2017-02-07 12:54:51 +00:00
|
|
|
|
|
|
|
// Is the loader empty?
|
|
|
|
if (loader.list.size === 0)
|
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
this.create(state);
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
2017-02-07 12:54:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Start the loader going as we have something in the queue
|
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
loader.events.once('LOADER_COMPLETE_EVENT', this.loadComplete.bind(this));
|
2017-02-07 12:54:51 +00:00
|
|
|
|
|
|
|
loader.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No preload? Then there was nothing to load either
|
2017-02-17 02:07:56 +00:00
|
|
|
this.create(state);
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
loadComplete: function (event)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2016-12-06 15:15:42 +00:00
|
|
|
var state = event.loader.state;
|
|
|
|
|
2017-02-07 21:49:25 +00:00
|
|
|
// console.log('loadComplete', state.sys.settings.key);
|
2017-02-07 18:44:26 +00:00
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
this.create(state);
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
2017-02-17 02:07:56 +00:00
|
|
|
create: function (state)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-02-17 02:07:56 +00:00
|
|
|
// console.log('create', state.sys.settings.key);
|
2017-03-14 17:00:14 +00:00
|
|
|
// console.log(state);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
// Insert at the correct index, or it just all goes wrong :)
|
|
|
|
|
|
|
|
var i = this.getStateIndex(state);
|
|
|
|
|
2017-02-08 01:07:01 +00:00
|
|
|
// console.log('create.index', state.sys.settings.key, i);
|
2017-02-07 18:44:26 +00:00
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
this.active.push({ index: i, state: state });
|
|
|
|
|
|
|
|
// Sort the 'active' array based on the index property
|
2017-02-07 19:53:04 +00:00
|
|
|
this.active.sort(this.sortStates);
|
|
|
|
|
2017-02-08 01:07:01 +00:00
|
|
|
if (state.create)
|
|
|
|
{
|
2017-03-14 17:00:14 +00:00
|
|
|
state.create.call(state, state.sys.settings.data);
|
2017-02-08 01:07:01 +00:00
|
|
|
}
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
pause: function (key)
|
|
|
|
{
|
2017-06-30 02:31:31 +00:00
|
|
|
var entry = this.getActiveState(key);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
entry.state.sys.pause();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
resume: function (key)
|
|
|
|
{
|
|
|
|
var entry = this.getActiveState(key);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
entry.state.sys.resume();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sleep: function (key)
|
|
|
|
{
|
|
|
|
var entry = this.getActiveState(key);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
entry.state.sys.sleep();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
wake: function (key)
|
|
|
|
{
|
|
|
|
var entry = this.getActiveState(key);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
entry.state.sys.wake();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
swap: function (from, to)
|
|
|
|
{
|
|
|
|
this.sleep(from);
|
2017-06-30 03:06:53 +00:00
|
|
|
|
|
|
|
if (this.isSleeping(to))
|
|
|
|
{
|
|
|
|
this.wake(to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.start(to);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
isSleeping: function (key)
|
|
|
|
{
|
|
|
|
var entry = this.getActiveState(key);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
return (!entry.state.sys.settings.active && !entry.state.sys.settings.visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function (key)
|
|
|
|
{
|
|
|
|
var entry = this.getActiveState(key);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
if (entry)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-06-30 02:31:31 +00:00
|
|
|
entry.state.sys.shutdown();
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
// Remove from the active list
|
|
|
|
var index = this.active.indexOf(entry);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
if (index !== -1)
|
|
|
|
{
|
|
|
|
this.active.splice(index, 1);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
this.active.sort(this.sortStates);
|
|
|
|
}
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sortStates: function (stateA, stateB)
|
|
|
|
{
|
2017-02-07 19:53:04 +00:00
|
|
|
// console.log('sortStates', stateA.state.sys.settings.key, stateA.index, stateB.state.sys.settings.key, stateB.index);
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
// Sort descending
|
|
|
|
if (stateA.index < stateB.index)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (stateA.index > stateB.index)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-25 17:10:19 +00:00
|
|
|
}
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-02-08 01:07:01 +00:00
|
|
|
module.exports = GlobalStateManager;
|