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 Settings = require('./Settings');
|
|
|
|
var Systems = require('./Systems');
|
2017-02-04 05:39:37 +00:00
|
|
|
var GetObjectValue = require('../utils/object/GetObjectValue');
|
2017-01-25 17:10:19 +00:00
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
var StateManager = function (game, stateConfig)
|
|
|
|
{
|
|
|
|
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],
|
|
|
|
autoStart: (i === 0)
|
|
|
|
});
|
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,
|
|
|
|
autoStart: true
|
|
|
|
});
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
StateManager.prototype.constructor = StateManager;
|
|
|
|
|
|
|
|
StateManager.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Boot handler is called by Phaser.Game when it first starts up.
|
|
|
|
* The renderer is available by now.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#boot
|
|
|
|
* @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 = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
getKey: function (key, stateConfig)
|
|
|
|
{
|
|
|
|
if (!key) { key = 'default'; }
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
if (stateConfig instanceof State)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
|
|
|
key = stateConfig.settings.key;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2016-12-06 15:25:24 +00:00
|
|
|
// console.log('StateManager not yet booted, adding to list', this._pending.length);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = this.getKey(key, stateConfig);
|
|
|
|
|
|
|
|
var newState;
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
if (stateConfig instanceof State)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2016-12-06 15:25:24 +00:00
|
|
|
// console.log('StateManager.add from instance', key);
|
2016-11-29 13:01:16 +00:00
|
|
|
newState = this.createStateFromInstance(key, stateConfig);
|
|
|
|
}
|
|
|
|
else if (typeof stateConfig === 'object')
|
|
|
|
{
|
2016-12-06 15:25:24 +00:00
|
|
|
// console.log('StateManager.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')
|
|
|
|
{
|
2016-12-06 15:25:24 +00:00
|
|
|
// console.log('StateManager.add from function', key);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
newState = this.createStateFromFunction(key, stateConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.keys[key] = newState;
|
|
|
|
|
|
|
|
this.states.push(newState);
|
|
|
|
|
|
|
|
if (autoStart || newState.settings.active)
|
|
|
|
{
|
|
|
|
if (this.game.isBooted)
|
|
|
|
{
|
|
|
|
this.start(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._start.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
},
|
|
|
|
|
|
|
|
createStateFromInstance: function (key, newState)
|
|
|
|
{
|
|
|
|
newState.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
|
|
|
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
var newState = new state();
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
if (newState instanceof State)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
|
|
|
return this.createStateFromInstance(key, newState);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newState.game = this.game;
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
newState.settings = new Settings(newState, key);
|
|
|
|
newState.sys = new Systems(newState);
|
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
|
|
|
|
|
|
|
// Default required functions
|
2016-11-29 15:25:14 +00:00
|
|
|
return this.setupCallbacks(newState);
|
|
|
|
}
|
|
|
|
},
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
setupCallbacks: function (newState, stateConfig)
|
|
|
|
{
|
|
|
|
if (stateConfig === undefined) { stateConfig = newState; }
|
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
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
newState.init = GetObjectValue(stateConfig, 'init', NOOP);
|
|
|
|
newState.preload = GetObjectValue(stateConfig, 'preload', NOOP);
|
|
|
|
newState.create = GetObjectValue(stateConfig, 'create', NOOP);
|
|
|
|
newState.shutdown = GetObjectValue(stateConfig, 'shutdown', NOOP);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// Game Loop level callbacks
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
newState.update = GetObjectValue(stateConfig, 'update', NOOP);
|
|
|
|
newState.render = GetObjectValue(stateConfig, 'render', NOOP);
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
return newState;
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
createStateDisplay: function (newState)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-01-26 04:06:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
var settings = newState.sys.settings;
|
|
|
|
|
|
|
|
var x = settings.x;
|
|
|
|
var y = settings.y;
|
|
|
|
|
|
|
|
// Too late to do all this?
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
if (settings.width === -1)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-01-26 04:06:10 +00:00
|
|
|
settings.width = this.game.config.width;
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
if (settings.height === -1)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-01-26 04:06:10 +00:00
|
|
|
settings.height = this.game.config.height;
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
if (this.game.config.renderType === CONST.WEBGL)
|
|
|
|
{
|
|
|
|
var width = settings.width;
|
|
|
|
var height = settings.height;
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
newState.sys.fbo = this.game.renderer.createFBO(newState, x, y, width, height);
|
|
|
|
}
|
|
|
|
*/
|
2016-11-29 13:01:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getState: function (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)
|
|
|
|
{
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
|
|
|
return (state && state.settings.active && this.active.indexOf(state) !== -1);
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function (key)
|
|
|
|
{
|
|
|
|
// if not booted, then put state into a holding pattern
|
|
|
|
if (!this.game.isBooted)
|
|
|
|
{
|
2016-12-06 15:25:24 +00:00
|
|
|
// console.log('StateManager 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
// Already started? Nothing more to do here ...
|
|
|
|
if (this.isActive(key))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.settings.active = true;
|
|
|
|
|
|
|
|
// + arguments
|
|
|
|
if (state.init)
|
|
|
|
{
|
|
|
|
state.init.call(state);
|
|
|
|
}
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
if (state.preload && state.sys.load)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2016-11-30 17:16:45 +00:00
|
|
|
state.sys.load.reset();
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
state.preload.call(state, this.game);
|
|
|
|
|
|
|
|
// Is the loader empty?
|
2016-11-30 17:16:45 +00:00
|
|
|
if (state.sys.load.list.size === 0)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
|
|
|
this.startCreate(state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Start the loader going as we have something in the queue
|
2016-12-06 15:15:42 +00:00
|
|
|
|
2016-12-06 15:25:24 +00:00
|
|
|
state.sys.load.events.once('LOADER_COMPLETE_EVENT', this.loadComplete.bind(this));
|
2016-11-29 13:01:16 +00:00
|
|
|
|
|
|
|
state.sys.load.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No preload? Then there was nothing to load either
|
|
|
|
this.startCreate(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-06 15:15:42 +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;
|
|
|
|
|
2016-11-29 13:01:16 +00:00
|
|
|
// Make sure to do load-update one last time before state is set to _created
|
|
|
|
|
2016-11-30 17:16:45 +00:00
|
|
|
// Stop doing this ...
|
2016-11-29 13:01:16 +00:00
|
|
|
if (state.hasOwnProperty('loadUpdate'))
|
|
|
|
{
|
|
|
|
state.loadUpdate.call(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.startCreate(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
startCreate: function (state)
|
|
|
|
{
|
|
|
|
if (state.create)
|
|
|
|
{
|
|
|
|
state.create.call(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
state.sys.updates.running = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
pause: function (key)
|
|
|
|
{
|
|
|
|
var index = this.getActiveStateIndex(key);
|
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
{
|
|
|
|
var state = this.getState(key);
|
|
|
|
|
|
|
|
state.settings.active = false;
|
|
|
|
|
|
|
|
this.active.splice(index, 1);
|
|
|
|
|
|
|
|
this.active.sort(this.sortStates.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sortStates: function (stateA, stateB)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = StateManager;
|