2013-11-25 03:13:04 +00:00
|
|
|
/* jshint newcap: false */
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-10-02 11:11:22 +00:00
|
|
|
* The State Manager is responsible for loading, setting up and switching game states.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.StateManager
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2013-10-02 11:11:22 +00:00
|
|
|
* @param {Phaser.State|Object} [pendingState=null] - A State object to seed the manager with.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 13:38:51 +00:00
|
|
|
Phaser.StateManager = function (game, pendingState) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Object} states - The object containing Phaser.States.
|
|
|
|
*/
|
|
|
|
this.states = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.State} _pendingState - The state to be switched to in the next frame.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._pendingState = null;
|
|
|
|
|
2013-12-24 03:18:55 +00:00
|
|
|
if (typeof pendingState !== 'undefined' && pendingState !== null)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
this._pendingState = pendingState;
|
|
|
|
}
|
|
|
|
|
2014-02-25 14:46:48 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _clearWorld - Clear the world when we switch state?
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._clearWorld = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _clearCache - Clear the cache when we switch state?
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._clearCache = false;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _created - Flag that sets if the State has been created or not.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._created = false;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2014-02-25 11:56:57 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _args - Temporary container when you pass vars from one State to another.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._args = [];
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {string} current - The current active State object (defaults to null).
|
|
|
|
*/
|
|
|
|
this.current = '';
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onInitCallback - This is called when the state is set as the active state.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onInitCallback = null;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onPreloadCallback - This is called when the state starts to load assets.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onPreloadCallback = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onCreateCallback - This is called when the state preload has finished and creation begins.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onCreateCallback = null;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onUpdateCallback - This is called when the state is updated, every game loop. It doesn't happen during preload (@see onLoadUpdateCallback).
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onUpdateCallback = null;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onRenderCallback - This is called post-render. It doesn't happen during preload (see onLoadRenderCallback).
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onRenderCallback = null;
|
|
|
|
|
2014-09-05 05:21:53 +00:00
|
|
|
/**
|
|
|
|
* @property {function} onResizeCallback - This is called if ScaleManager.scalemode is RESIZE and a resize event occurs. It's passed the new width and height.
|
|
|
|
*/
|
|
|
|
this.onResizeCallback = null;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onPreRenderCallback - This is called before the state is rendered and before the stage is cleared.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onPreRenderCallback = null;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onLoadUpdateCallback - This is called when the State is updated during the preload phase.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onLoadUpdateCallback = null;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onLoadRenderCallback - This is called when the State is rendered during the preload phase.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onLoadRenderCallback = null;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onPausedCallback - This is called when the game is paused.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onPausedCallback = null;
|
|
|
|
|
2014-02-22 00:01:19 +00:00
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onResumedCallback - This is called when the game is resumed from a paused state.
|
2014-02-22 00:01:19 +00:00
|
|
|
*/
|
|
|
|
this.onResumedCallback = null;
|
|
|
|
|
2014-04-27 23:07:40 +00:00
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onPauseUpdateCallback - This is called every frame while the game is paused.
|
2014-04-27 23:07:40 +00:00
|
|
|
*/
|
|
|
|
this.onPauseUpdateCallback = null;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-09-01 01:59:24 +00:00
|
|
|
* @property {function} onShutDownCallback - This is called when the state is shut down (i.e. swapped to another state).
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.onShutDownCallback = null;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.StateManager.prototype = {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Boot handler is called by Phaser.Game when it first starts up.
|
|
|
|
* @method Phaser.StateManager#boot
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
boot: function () {
|
|
|
|
|
2013-11-28 05:43:35 +00:00
|
|
|
this.game.onPause.add(this.pause, this);
|
|
|
|
this.game.onResume.add(this.resume, this);
|
2014-02-25 16:26:11 +00:00
|
|
|
this.game.load.onLoadComplete.add(this.loadComplete, this);
|
2013-11-28 05:43:35 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this._pendingState !== null)
|
|
|
|
{
|
|
|
|
if (typeof this._pendingState === 'string')
|
|
|
|
{
|
|
|
|
// State was already added, so just start it
|
2014-03-13 16:49:52 +00:00
|
|
|
this.start(this._pendingState, false, false);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.add('default', this._pendingState, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-26 01:32:38 +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.
|
|
|
|
*
|
2013-10-02 11:11:22 +00:00
|
|
|
* @method Phaser.StateManager#add
|
2014-02-26 01:32:38 +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.
|
2013-08-29 13:38:51 +00:00
|
|
|
*/
|
|
|
|
add: function (key, state, autoStart) {
|
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
if (typeof autoStart === "undefined") { autoStart = false; }
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
var newState;
|
|
|
|
|
|
|
|
if (state instanceof Phaser.State)
|
|
|
|
{
|
|
|
|
newState = state;
|
|
|
|
}
|
|
|
|
else if (typeof state === 'object')
|
|
|
|
{
|
|
|
|
newState = state;
|
|
|
|
newState.game = this.game;
|
|
|
|
}
|
|
|
|
else if (typeof state === 'function')
|
|
|
|
{
|
|
|
|
newState = new state(this.game);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.states[key] = newState;
|
|
|
|
|
|
|
|
if (autoStart)
|
|
|
|
{
|
|
|
|
if (this.game.isBooted)
|
|
|
|
{
|
|
|
|
this.start(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._pendingState = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* Delete the given state.
|
|
|
|
* @method Phaser.StateManager#remove
|
|
|
|
* @param {string} key - A unique key you use to reference this state, i.e. "MainMenu", "Level1".
|
|
|
|
*/
|
2013-08-29 13:38:51 +00:00
|
|
|
remove: function (key) {
|
|
|
|
|
2014-04-01 10:25:04 +00:00
|
|
|
if (this.current === key)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
this.callbackContext = null;
|
|
|
|
|
|
|
|
this.onInitCallback = null;
|
|
|
|
this.onShutDownCallback = null;
|
|
|
|
|
|
|
|
this.onPreloadCallback = null;
|
|
|
|
this.onLoadRenderCallback = null;
|
|
|
|
this.onLoadUpdateCallback = null;
|
|
|
|
this.onCreateCallback = null;
|
|
|
|
this.onUpdateCallback = null;
|
|
|
|
this.onRenderCallback = null;
|
2014-09-05 05:21:53 +00:00
|
|
|
this.onResizeCallback = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
this.onPausedCallback = null;
|
2014-02-22 00:01:19 +00:00
|
|
|
this.onResumedCallback = null;
|
2014-04-27 23:07:40 +00:00
|
|
|
this.onPauseUpdateCallback = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-08-29 14:14:56 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
delete this.states[key];
|
2013-08-29 14:14:56 +00:00
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-13 15:03:46 +00:00
|
|
|
* Start the given State. If a State is already running then State.shutDown will be called (if it exists) before switching to the new State.
|
|
|
|
*
|
2013-10-02 11:11:22 +00:00
|
|
|
* @method Phaser.StateManager#start
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {string} key - The key of the state you want to start.
|
2014-02-13 15:03:46 +00:00
|
|
|
* @param {boolean} [clearWorld=true] - Clear everything in the world? This clears the World display list fully (but not the Stage, so if you've added your own objects to the Stage they will need managing directly)
|
|
|
|
* @param {boolean} [clearCache=false] - Clear the Game.Cache? This purges out all loaded assets. The default is false and you must have clearWorld=true if you want to clearCache as well.
|
2014-02-25 11:56:57 +00:00
|
|
|
* @param {...*} parameter - Additional parameters that will be passed to the State.init function (if it has one).
|
2013-08-29 13:38:51 +00:00
|
|
|
*/
|
|
|
|
start: function (key, clearWorld, clearCache) {
|
|
|
|
|
|
|
|
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
|
|
|
if (typeof clearCache === "undefined") { clearCache = false; }
|
|
|
|
|
2014-02-25 14:46:48 +00:00
|
|
|
if (this.checkState(key))
|
|
|
|
{
|
|
|
|
// Place the state in the queue. It will be started the next time the game loop starts.
|
|
|
|
this._pendingState = key;
|
|
|
|
this._clearWorld = clearWorld;
|
|
|
|
this._clearCache = clearCache;
|
|
|
|
|
|
|
|
if (arguments.length > 3)
|
|
|
|
{
|
|
|
|
this._args = Array.prototype.splice.call(arguments, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
},
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-04-01 10:25:04 +00:00
|
|
|
/**
|
|
|
|
* Restarts the current State. State.shutDown will be called (if it exists) before the State is restarted.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#restart
|
|
|
|
* @param {boolean} [clearWorld=true] - Clear everything in the world? This clears the World display list fully (but not the Stage, so if you've added your own objects to the Stage they will need managing directly)
|
|
|
|
* @param {boolean} [clearCache=false] - Clear the Game.Cache? This purges out all loaded assets. The default is false and you must have clearWorld=true if you want to clearCache as well.
|
|
|
|
* @param {...*} parameter - Additional parameters that will be passed to the State.init function if it has one.
|
|
|
|
*/
|
|
|
|
restart: function (clearWorld, clearCache) {
|
|
|
|
|
|
|
|
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
|
|
|
if (typeof clearCache === "undefined") { clearCache = false; }
|
|
|
|
|
|
|
|
// Place the state in the queue. It will be started the next time the game loop starts.
|
|
|
|
this._pendingState = this.current;
|
|
|
|
this._clearWorld = clearWorld;
|
|
|
|
this._clearCache = clearCache;
|
|
|
|
|
2014-04-14 22:34:13 +00:00
|
|
|
if (arguments.length > 2)
|
2014-04-01 10:25:04 +00:00
|
|
|
{
|
2014-04-14 22:34:13 +00:00
|
|
|
this._args = Array.prototype.splice.call(arguments, 2);
|
2014-04-01 10:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Used by onInit and onShutdown when those functions don't exist on the state
|
2013-10-02 11:11:22 +00:00
|
|
|
* @method Phaser.StateManager#dummy
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2013-08-29 13:38:51 +00:00
|
|
|
dummy: function () {
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-25 14:54:56 +00:00
|
|
|
* preUpdate is called right at the start of the game loop. It is responsible for changing to a new state that was requested previously.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#preUpdate
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-02-25 14:46:48 +00:00
|
|
|
preUpdate: function () {
|
|
|
|
|
|
|
|
if (this._pendingState && this.game.isBooted)
|
|
|
|
{
|
|
|
|
// Already got a state running?
|
2014-08-29 10:37:47 +00:00
|
|
|
this.clearCurrentState();
|
2014-02-25 14:46:48 +00:00
|
|
|
|
|
|
|
this.setCurrentState(this._pendingState);
|
|
|
|
|
|
|
|
if (this.onPreloadCallback)
|
|
|
|
{
|
|
|
|
this.game.load.reset();
|
|
|
|
this.onPreloadCallback.call(this.callbackContext, this.game);
|
|
|
|
|
|
|
|
// Is the loader empty?
|
2014-05-29 14:57:01 +00:00
|
|
|
if (this.game.load.totalQueuedFiles() === 0 && this.game.load.totalQueuedPacks() === 0)
|
2014-02-25 14:46:48 +00:00
|
|
|
{
|
2014-02-25 16:26:11 +00:00
|
|
|
this.loadComplete();
|
2014-02-25 14:46:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Start the loader going as we have something in the queue
|
|
|
|
this.game.load.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No init? Then there was nothing to load either
|
2014-02-25 16:26:11 +00:00
|
|
|
this.loadComplete();
|
2014-02-25 14:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
if (this.current === this._pendingState)
|
|
|
|
{
|
|
|
|
this._pendingState = null;
|
|
|
|
}
|
2014-02-25 14:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-08-29 10:37:47 +00:00
|
|
|
/**
|
|
|
|
* This method clears the current State, calling its shutdown callback. The process also removes any active tweens,
|
|
|
|
* resets the camera, resets input, clears physics, removes timers and if set clears the world and cache too.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#clearCurrentState
|
|
|
|
*/
|
|
|
|
clearCurrentState: function () {
|
|
|
|
|
|
|
|
if (this.current)
|
|
|
|
{
|
|
|
|
if (this.onShutDownCallback)
|
|
|
|
{
|
|
|
|
this.onShutDownCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.game.tweens.removeAll();
|
|
|
|
|
|
|
|
this.game.camera.reset();
|
|
|
|
|
|
|
|
this.game.input.reset(true);
|
|
|
|
|
|
|
|
this.game.physics.clear();
|
|
|
|
|
|
|
|
this.game.time.removeAll();
|
|
|
|
|
2014-09-05 05:21:53 +00:00
|
|
|
this.game.scale.reset(this._clearWorld);
|
|
|
|
|
2014-08-29 10:37:47 +00:00
|
|
|
if (this._clearWorld)
|
|
|
|
{
|
|
|
|
this.game.world.shutdown();
|
|
|
|
|
|
|
|
if (this._clearCache === true)
|
|
|
|
{
|
|
|
|
this.game.cache.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-25 14:46:48 +00:00
|
|
|
/**
|
|
|
|
* Checks if a given phaser state is valid. A State is considered valid if it has at least one of the core functions: preload, create, update or render.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#checkState
|
|
|
|
* @param {string} key - The key of the state you want to check.
|
|
|
|
* @return {boolean} true if the State has the required functions, otherwise false.
|
|
|
|
*/
|
2013-08-29 13:38:51 +00:00
|
|
|
checkState: function (key) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this.states[key])
|
|
|
|
{
|
|
|
|
var valid = false;
|
|
|
|
|
|
|
|
if (this.states[key]['preload']) { valid = true; }
|
2014-02-25 14:46:48 +00:00
|
|
|
if (this.states[key]['create']) { valid = true; }
|
|
|
|
if (this.states[key]['update']) { valid = true; }
|
|
|
|
if (this.states[key]['render']) { valid = true; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
|
|
|
if (valid === false)
|
|
|
|
{
|
2014-02-25 14:46:48 +00:00
|
|
|
console.warn("Invalid Phaser State object given. Must contain at least a one of the required functions: preload, create, update or render");
|
2013-11-25 03:13:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.warn("Phaser.StateManager - No state found with the key: " + key);
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-09-18 05:34:56 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-02 11:11:22 +00:00
|
|
|
* Links game properties to the State given by the key.
|
2014-04-27 23:07:40 +00:00
|
|
|
*
|
2013-10-02 11:11:22 +00:00
|
|
|
* @method Phaser.StateManager#link
|
|
|
|
* @param {string} key - State key.
|
|
|
|
* @protected
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-18 05:34:56 +00:00
|
|
|
link: function (key) {
|
|
|
|
|
|
|
|
this.states[key].game = this.game;
|
|
|
|
this.states[key].add = this.game.add;
|
2014-02-21 14:50:18 +00:00
|
|
|
this.states[key].make = this.game.make;
|
2013-09-18 05:34:56 +00:00
|
|
|
this.states[key].camera = this.game.camera;
|
|
|
|
this.states[key].cache = this.game.cache;
|
|
|
|
this.states[key].input = this.game.input;
|
|
|
|
this.states[key].load = this.game.load;
|
|
|
|
this.states[key].math = this.game.math;
|
|
|
|
this.states[key].sound = this.game.sound;
|
2014-02-13 12:55:58 +00:00
|
|
|
this.states[key].scale = this.game.scale;
|
2014-02-25 14:46:48 +00:00
|
|
|
this.states[key].state = this;
|
2013-09-18 05:34:56 +00:00
|
|
|
this.states[key].stage = this.game.stage;
|
|
|
|
this.states[key].time = this.game.time;
|
|
|
|
this.states[key].tweens = this.game.tweens;
|
|
|
|
this.states[key].world = this.game.world;
|
|
|
|
this.states[key].particles = this.game.particles;
|
|
|
|
this.states[key].rnd = this.game.rnd;
|
2014-03-05 02:36:08 +00:00
|
|
|
this.states[key].physics = this.game.physics;
|
2014-02-27 17:00:14 +00:00
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-02 11:11:22 +00:00
|
|
|
* Sets the current State. Should not be called directly (use StateManager.start)
|
2014-04-27 23:07:40 +00:00
|
|
|
*
|
2013-10-02 11:11:22 +00:00
|
|
|
* @method Phaser.StateManager#setCurrentState
|
|
|
|
* @param {string} key - State key.
|
2014-01-08 21:57:13 +00:00
|
|
|
* @private
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 03:13:04 +00:00
|
|
|
setCurrentState: function (key) {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
this.callbackContext = this.states[key];
|
|
|
|
|
2013-09-18 05:34:56 +00:00
|
|
|
this.link(key);
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
// Used when the state is set as being the current active state
|
2013-08-29 13:38:51 +00:00
|
|
|
this.onInitCallback = this.states[key]['init'] || this.dummy;
|
|
|
|
|
|
|
|
this.onPreloadCallback = this.states[key]['preload'] || null;
|
|
|
|
this.onLoadRenderCallback = this.states[key]['loadRender'] || null;
|
|
|
|
this.onLoadUpdateCallback = this.states[key]['loadUpdate'] || null;
|
|
|
|
this.onCreateCallback = this.states[key]['create'] || null;
|
|
|
|
this.onUpdateCallback = this.states[key]['update'] || null;
|
|
|
|
this.onPreRenderCallback = this.states[key]['preRender'] || null;
|
|
|
|
this.onRenderCallback = this.states[key]['render'] || null;
|
2014-09-05 05:21:53 +00:00
|
|
|
this.onResizeCallback = this.states[key]['resize'] || null;
|
2013-08-29 13:38:51 +00:00
|
|
|
this.onPausedCallback = this.states[key]['paused'] || null;
|
2014-02-22 00:01:19 +00:00
|
|
|
this.onResumedCallback = this.states[key]['resumed'] || null;
|
2014-04-27 23:07:40 +00:00
|
|
|
this.onPauseUpdateCallback = this.states[key]['pauseUpdate'] || null;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
// Used when the state is no longer the current active state
|
2013-08-29 13:38:51 +00:00
|
|
|
this.onShutDownCallback = this.states[key]['shutdown'] || this.dummy;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.current = key;
|
|
|
|
this._created = false;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2014-02-25 11:56:57 +00:00
|
|
|
this.onInitCallback.apply(this.callbackContext, this._args);
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2014-02-25 14:54:56 +00:00
|
|
|
this._args = [];
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
},
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2014-01-08 21:57:13 +00:00
|
|
|
/**
|
|
|
|
* Gets the current State.
|
|
|
|
*
|
|
|
|
* @method Phaser.StateManager#getCurrentState
|
|
|
|
* @return Phaser.State
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
getCurrentState: function() {
|
|
|
|
return this.states[this.current];
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#loadComplete
|
2013-10-02 11:11:22 +00:00
|
|
|
* @protected
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-29 13:38:51 +00:00
|
|
|
loadComplete: function () {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this._created === false && this.onCreateCallback)
|
2013-08-30 17:56:10 +00:00
|
|
|
{
|
2013-11-25 03:13:04 +00:00
|
|
|
this._created = true;
|
2013-10-30 16:09:06 +00:00
|
|
|
this.onCreateCallback.call(this.callbackContext, this.game);
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
2013-09-23 21:23:17 +00:00
|
|
|
else
|
|
|
|
{
|
2013-11-25 03:13:04 +00:00
|
|
|
this._created = true;
|
2013-09-23 21:23:17 +00:00
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-28 05:43:35 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#pause
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
pause: function () {
|
|
|
|
|
|
|
|
if (this._created && this.onPausedCallback)
|
|
|
|
{
|
2014-02-22 00:01:19 +00:00
|
|
|
this.onPausedCallback.call(this.callbackContext, this.game);
|
2013-11-28 05:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#resume
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
resume: function () {
|
|
|
|
|
2014-02-22 00:01:19 +00:00
|
|
|
if (this._created && this.onResumedCallback)
|
2013-11-28 05:43:35 +00:00
|
|
|
{
|
2014-02-22 00:01:19 +00:00
|
|
|
this.onResumedCallback.call(this.callbackContext, this.game);
|
2013-11-28 05:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#update
|
2013-10-02 11:11:22 +00:00
|
|
|
* @protected
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-29 14:14:56 +00:00
|
|
|
update: function () {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this._created && this.onUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onUpdateCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.onLoadUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onLoadUpdateCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
},
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2014-04-27 23:07:40 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#pauseUpdate
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
pauseUpdate: function () {
|
|
|
|
|
|
|
|
if (this._created && this.onPauseUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onPauseUpdateCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.onLoadUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onLoadUpdateCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#preRender
|
2013-10-02 11:11:22 +00:00
|
|
|
* @protected
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-29 14:14:56 +00:00
|
|
|
preRender: function () {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this.onPreRenderCallback)
|
|
|
|
{
|
|
|
|
this.onPreRenderCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
},
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2014-09-05 05:21:53 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#resize
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
resize: function (width, height) {
|
|
|
|
|
|
|
|
if (this.onResizeCallback)
|
|
|
|
{
|
|
|
|
this.onResizeCallback.call(this.callbackContext, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.StateManager#render
|
2013-10-02 11:11:22 +00:00
|
|
|
* @protected
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-29 14:14:56 +00:00
|
|
|
render: function () {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this._created && this.onRenderCallback)
|
|
|
|
{
|
|
|
|
if (this.game.renderType === Phaser.CANVAS)
|
|
|
|
{
|
|
|
|
this.game.context.save();
|
|
|
|
this.game.context.setTransform(1, 0, 0, 1, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onRenderCallback.call(this.callbackContext, this.game);
|
|
|
|
|
|
|
|
if (this.game.renderType === Phaser.CANVAS)
|
|
|
|
{
|
|
|
|
this.game.context.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.onLoadRenderCallback)
|
|
|
|
{
|
|
|
|
this.onLoadRenderCallback.call(this.callbackContext, this.game);
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-04-08 02:31:13 +00:00
|
|
|
* Removes all StateManager callback references to the State object, nulls the game reference and clears the States object.
|
|
|
|
* You don't recover from this without rebuilding the Phaser instance again.
|
2013-10-02 11:11:22 +00:00
|
|
|
* @method Phaser.StateManager#destroy
|
2013-08-29 13:38:51 +00:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
2014-08-29 10:37:47 +00:00
|
|
|
this.clearCurrentState();
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
this.callbackContext = null;
|
2013-08-29 14:14:56 +00:00
|
|
|
|
|
|
|
this.onInitCallback = null;
|
|
|
|
this.onShutDownCallback = null;
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
this.onPreloadCallback = null;
|
|
|
|
this.onLoadRenderCallback = null;
|
|
|
|
this.onLoadUpdateCallback = null;
|
|
|
|
this.onCreateCallback = null;
|
|
|
|
this.onUpdateCallback = null;
|
|
|
|
this.onRenderCallback = null;
|
|
|
|
this.onPausedCallback = null;
|
2014-02-22 00:01:19 +00:00
|
|
|
this.onResumedCallback = null;
|
2014-04-27 23:07:40 +00:00
|
|
|
this.onPauseUpdateCallback = null;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
this.game = null;
|
|
|
|
this.states = {};
|
|
|
|
this._pendingState = null;
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 21:23:00 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.StateManager.prototype.constructor = Phaser.StateManager;
|