2013-08-29 13:38:51 +00:00
|
|
|
Phaser.StateManager = function (game, pendingState) {
|
|
|
|
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
this.states = {};
|
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
if (pendingState !== null)
|
|
|
|
{
|
|
|
|
this._pendingState = pendingState;
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.StateManager.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Phaser.Game}
|
|
|
|
*/
|
|
|
|
game: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The state to be switched to in the next frame.
|
|
|
|
* @type {State}
|
|
|
|
*/
|
|
|
|
_pendingState: null,
|
|
|
|
|
2013-08-30 17:56:10 +00:00
|
|
|
/**
|
|
|
|
* Flag that sets if the State has been created or not.
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
_created: false,
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
/**
|
|
|
|
* The state to be switched to in the next frame.
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
states: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current active State object (defaults to null)
|
|
|
|
* @type {String}
|
|
|
|
*/
|
|
|
|
current: '',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when the state is started (i.e. set as the current active state)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onInitCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when init states. (loading assets...)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onPreloadCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when create states. (setup states...)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onCreateCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when State is updated, this doesn't happen during load (see onLoadUpdateCallback)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onUpdateCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when the State is rendered, this doesn't happen during load (see onLoadRenderCallback)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onRenderCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called before the State is rendered and before the stage is cleared
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onPreRenderCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when the State is updated but only during the load process
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onLoadUpdateCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when the State is rendered but only during the load process
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onLoadRenderCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when states paused.
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onPausedCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will be called when the state is shut down (i.e. swapped to another state)
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
onShutDownCallback: null,
|
|
|
|
|
|
|
|
boot: function () {
|
|
|
|
|
2013-08-29 18:20:04 +00:00
|
|
|
// console.log('Phaser.StateManager.boot');
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
if (this._pendingState !== null)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('_pendingState found');
|
|
|
|
// console.log(typeof this._pendingState);
|
2013-08-29 14:14:56 +00:00
|
|
|
|
|
|
|
if (typeof this._pendingState === 'string')
|
|
|
|
{
|
|
|
|
// State was already added, so just start it
|
|
|
|
this.start(this._pendingState, false, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.add('default', this._pendingState, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new State.
|
|
|
|
* @param key {String} A unique key you use to reference this state, i.e. "MainMenu", "Level1".
|
|
|
|
* @param state {State} The state you want to switch to.
|
|
|
|
* @param autoStart {Boolean} Start the state immediately after creating it? (default true)
|
|
|
|
*/
|
|
|
|
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-08-29 21:53:55 +00:00
|
|
|
// console.log('Phaser.StateManager.addState', key);
|
|
|
|
// console.log(typeof state);
|
|
|
|
// console.log('autoStart?', autoStart);
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
var newState;
|
|
|
|
|
|
|
|
if (state instanceof Phaser.State)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Phaser.StateManager.addState: Phaser.State given');
|
2013-08-29 13:38:51 +00:00
|
|
|
newState = state;
|
|
|
|
newState.link(this.game);
|
|
|
|
}
|
|
|
|
else if (typeof state === 'object')
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Phaser.StateManager.addState: Object given');
|
2013-08-29 13:38:51 +00:00
|
|
|
newState = state;
|
2013-09-13 04:44:04 +00:00
|
|
|
newState.game = this.game;
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
|
|
|
else if (typeof state === 'function')
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Phaser.StateManager.addState: Function given');
|
2013-08-29 13:38:51 +00:00
|
|
|
newState = new state(this.game);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.states[key] = newState;
|
|
|
|
|
|
|
|
if (autoStart)
|
|
|
|
{
|
2013-08-29 14:14:56 +00:00
|
|
|
if (this.game.isBooted)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Game is booted, so we can start the state now');
|
2013-08-29 14:14:56 +00:00
|
|
|
this.start(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Game is NOT booted, so set the current state as pending');
|
2013-08-29 14:14:56 +00:00
|
|
|
this._pendingState = key;
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function (key) {
|
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
if (this.current == key)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
this.onPausedCallback = null;
|
|
|
|
this.onDestroyCallback = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this.states[key];
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the given state
|
|
|
|
* @param key {String} The key of the state you want to start.
|
|
|
|
* @param [clearWorld] {bool} clear everything in the world? (Default to true)
|
|
|
|
* @param [clearCache] {bool} clear asset cache? (Default to false and ONLY available when clearWorld=true)
|
|
|
|
*/
|
|
|
|
start: function (key, clearWorld, clearCache) {
|
|
|
|
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Phaser.StateManager.start', key);
|
2013-08-29 13:38:51 +00:00
|
|
|
// console.log(this);
|
|
|
|
// console.log(this.callbackContext);
|
|
|
|
|
|
|
|
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
|
|
|
if (typeof clearCache === "undefined") { clearCache = false; }
|
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
if (this.game.isBooted == false)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Game is NOT booted, so set the requested state as pending');
|
2013-08-29 14:14:56 +00:00
|
|
|
this._pendingState = key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
if (this.checkState(key) == false)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Already got a state running?
|
|
|
|
if (this.current)
|
|
|
|
{
|
|
|
|
this.onShutDownCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clearWorld) {
|
|
|
|
|
2013-09-13 04:44:04 +00:00
|
|
|
this.game.world.destroy();
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-09-13 04:44:04 +00:00
|
|
|
if (clearCache == true)
|
|
|
|
{
|
2013-08-29 13:38:51 +00:00
|
|
|
this.game.cache.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setCurrentState(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.onPreloadCallback)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Preload Callback found');
|
2013-08-29 13:38:51 +00:00
|
|
|
this.game.load.reset();
|
|
|
|
this.onPreloadCallback.call(this.callbackContext);
|
|
|
|
|
|
|
|
// Is the loader empty?
|
|
|
|
if (this.game.load.queueSize == 0)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Loader queue empty');
|
2013-08-29 13:38:51 +00:00
|
|
|
this.game.loadComplete();
|
|
|
|
|
|
|
|
if (this.onCreateCallback)
|
|
|
|
{
|
|
|
|
this.onCreateCallback.call(this.callbackContext);
|
|
|
|
}
|
2013-08-30 17:56:10 +00:00
|
|
|
|
|
|
|
this._created = true;
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Loader started');
|
2013-08-29 13:38:51 +00:00
|
|
|
// Start the loader going as we have something in the queue
|
|
|
|
this.game.load.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Preload callback not found');
|
2013-08-29 13:38:51 +00:00
|
|
|
// No init? Then there was nothing to load either
|
|
|
|
if (this.onCreateCallback)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Create callback found');
|
2013-08-29 13:38:51 +00:00
|
|
|
this.onCreateCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:56:10 +00:00
|
|
|
this._created = true;
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
this.game.loadComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
// Used by onInit and onShutdown when those functions don't exist on the state
|
|
|
|
dummy: function () {
|
|
|
|
},
|
|
|
|
|
|
|
|
checkState: function (key) {
|
|
|
|
|
|
|
|
if (this.states[key])
|
|
|
|
{
|
|
|
|
var valid = false;
|
|
|
|
|
|
|
|
if (this.states[key]['preload']) { valid = true; }
|
|
|
|
|
|
|
|
if (valid == false && this.states[key]['loadRender']) { valid = true; }
|
|
|
|
if (valid == false && this.states[key]['loadUpdate']) { valid = true; }
|
|
|
|
if (valid == false && this.states[key]['create']) { valid = true; }
|
|
|
|
if (valid == false && this.states[key]['update']) { valid = true; }
|
|
|
|
if (valid == false && this.states[key]['preRender']) { valid = true; }
|
|
|
|
if (valid == false && this.states[key]['render']) { valid = true; }
|
|
|
|
if (valid == false && this.states[key]['paused']) { valid = true; }
|
|
|
|
|
|
|
|
if (valid == false)
|
|
|
|
{
|
|
|
|
console.warn("Invalid Phaser State object given. Must contain at least a one of the required functions.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.warn("Phaser.StateManager - No state found with the key: " + key);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
setCurrentState: function (key) {
|
|
|
|
|
|
|
|
this.callbackContext = this.states[key];
|
|
|
|
|
|
|
|
// Used when the state is set as being the current active state
|
|
|
|
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;
|
|
|
|
this.onPausedCallback = this.states[key]['paused'] || null;
|
|
|
|
|
|
|
|
// Used when the state is no longer the current active state
|
|
|
|
this.onShutDownCallback = this.states[key]['shutdown'] || this.dummy;
|
|
|
|
|
|
|
|
this.current = key;
|
2013-08-30 17:56:10 +00:00
|
|
|
this._created = false;
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
this.onInitCallback.call(this.callbackContext);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
loadComplete: function () {
|
|
|
|
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Phaser.StateManager.loadComplete');
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-30 17:56:10 +00:00
|
|
|
if (this._created == false && this.onCreateCallback)
|
|
|
|
{
|
2013-08-29 21:53:55 +00:00
|
|
|
// console.log('Create callback found');
|
2013-08-29 13:38:51 +00:00
|
|
|
this.onCreateCallback.call(this.callbackContext);
|
2013-08-30 17:56:10 +00:00
|
|
|
this._created = true;
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
update: function () {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-30 17:56:10 +00:00
|
|
|
if (this._created && this.onUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onUpdateCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.onLoadUpdateCallback)
|
|
|
|
{
|
|
|
|
this.onLoadUpdateCallback.call(this.callbackContext);
|
|
|
|
}
|
2013-08-29 14:14:56 +00:00
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
},
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
preRender: function () {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
if (this.onPreRenderCallback)
|
|
|
|
{
|
|
|
|
this.onPreRenderCallback.call(this.callbackContext);
|
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
},
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
render: function () {
|
2013-08-29 13:38:51 +00:00
|
|
|
|
2013-08-31 20:50:34 +00:00
|
|
|
if (this._created && this.onRenderCallback)
|
|
|
|
{
|
|
|
|
this.onRenderCallback.call(this.callbackContext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.onLoadRenderCallback)
|
|
|
|
{
|
|
|
|
this.onLoadRenderCallback.call(this.callbackContext);
|
|
|
|
}
|
2013-08-29 14:14:56 +00:00
|
|
|
}
|
2013-08-29 13:38:51 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nuke the entire game from orbit
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
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;
|
|
|
|
this.onDestroyCallback = null;
|
|
|
|
|
2013-08-29 14:14:56 +00:00
|
|
|
this.game = null;
|
|
|
|
this.states = {};
|
|
|
|
this._pendingState = null;
|
|
|
|
|
2013-08-29 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|