StateManager moved to GlobalStateManager and a new State level proxy introduced.

You can now start, stop and swap states on the fly.
State.visible now also skips the renderer.
This commit is contained in:
Richard Davey 2017-02-08 01:07:01 +00:00
parent 00185d6193
commit 2c37dc3fda
5 changed files with 126 additions and 36 deletions

View file

@ -14,7 +14,7 @@ var DOMContentLoaded = require('../dom/DOMContentLoaded');
var MainLoop = require('./MainLoop');
var CreateRenderer = require('./CreateRenderer');
var StateManager = require('../state/StateManager');
var GlobalStateManager = require('../state/GlobalStateManager');
var TextureManager = require('../textures/TextureManager');
var Data = require('../components/Data');
var Cache = require('../cache/Cache');
@ -57,9 +57,9 @@ var Game = function (config)
this.input = null;
/**
* @property {Phaser.StateManager} state - The StateManager. Phaser instance specific.
* @property {Phaser.GlobalStateManager} state - The StateManager. Phaser instance specific.
*/
this.state = new StateManager(this, this.config.stateConfig);
this.state = new GlobalStateManager(this, this.config.stateConfig);
/**
* @property {Phaser.Device} device - Contains device information and capabilities (singleton)

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: 'd2813a20-ed80-11e6-9112-31e669c80df9'
build: '13721950-ed9a-11e6-8f4f-afca70274f59'
};
module.exports = CHECKSUM;

View file

@ -18,11 +18,11 @@ var GetContext = require('../canvas/GetContext');
/**
* The State Manager is responsible for loading, setting up and switching game states.
*
* @class Phaser.StateManager
* @class Phaser.GlobalStateManager
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
*/
var StateManager = function (game, stateConfig)
var GlobalStateManager = function (game, stateConfig)
{
this.game = game;
@ -62,15 +62,15 @@ var StateManager = function (game, stateConfig)
}
};
StateManager.prototype.constructor = StateManager;
GlobalStateManager.prototype.constructor = GlobalStateManager;
StateManager.prototype = {
GlobalStateManager.prototype = {
/**
* The Boot handler is called by Phaser.Game when it first starts up.
* The renderer is available by now.
*
* @method Phaser.StateManager#boot
* @method Phaser.GlobalStateManager#boot
* @private
*/
boot: function ()
@ -112,11 +112,11 @@ StateManager.prototype = {
},
/**
* Adds a new State into the StateManager. You must give each State a unique key by which you'll identify it.
* Adds a new State into the GlobalStateManager. 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
* @method Phaser.GlobalStateManager#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.
@ -135,25 +135,25 @@ StateManager.prototype = {
autoStart: autoStart
});
console.log('StateManager not yet booted, adding to list', this._pending.length);
console.log('GlobalStateManager not yet booted, adding to list', this._pending.length);
return;
}
key = this.getKey(key, stateConfig);
// console.log('StateManager.add', key, stateConfig, autoStart);
// console.log('GlobalStateManager.add', key, stateConfig, autoStart);
var newState;
if (stateConfig instanceof State)
{
// console.log('StateManager.add from instance:', key);
// console.log('GlobalStateManager.add from instance:', key);
newState = this.createStateFromInstance(key, stateConfig);
}
else if (typeof stateConfig === 'object')
{
// console.log('StateManager.add from object:', key);
// console.log('GlobalStateManager.add from object:', key);
stateConfig.key = key;
@ -161,7 +161,7 @@ StateManager.prototype = {
}
else if (typeof stateConfig === 'function')
{
// console.log('StateManager.add from function:', key);
// console.log('GlobalStateManager.add from function:', key);
newState = this.createStateFromFunction(key, stateConfig);
}
@ -358,7 +358,7 @@ StateManager.prototype = {
// 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');
// console.log('GlobalStateManager not yet booted, setting autoStart on pending list');
for (var i = 0; i < this._pending.length; i++)
{
@ -440,7 +440,7 @@ StateManager.prototype = {
// Is the loader empty?
if (loader.list.size === 0)
{
this.startCreate(state);
this.create(state);
}
else
{
@ -454,7 +454,7 @@ StateManager.prototype = {
else
{
// No preload? Then there was nothing to load either
this.startCreate(state);
this.create(state);
}
},
@ -464,23 +464,18 @@ StateManager.prototype = {
// console.log('loadComplete', state.sys.settings.key);
this.startCreate(state);
this.create(state);
},
startCreate: function (state)
create: function (state)
{
// console.log('startCreate', state.sys.settings.key);
if (state.create)
{
state.create();
}
console.log('create', state.sys.settings.key);
// Insert at the correct index, or it just all goes wrong :)
var i = this.getStateIndex(state);
// console.log('startCreate.index', state.sys.settings.key, i);
// console.log('create.index', state.sys.settings.key, i);
this.active.push({ index: i, state: state });
@ -488,11 +483,16 @@ StateManager.prototype = {
this.active.sort(this.sortStates);
state.sys.updates.running = true;
if (state.create)
{
state.create();
}
},
pause: function (key)
{
var index = this.getActiveStateIndex(key);
var index = this.getActiveStateIndex(this.getState(key));
if (index > -1)
{
@ -502,7 +502,7 @@ StateManager.prototype = {
this.active.splice(index, 1);
this.active.sort(this.sortStates.bind(this));
this.active.sort(this.sortStates);
}
},
@ -527,4 +527,4 @@ StateManager.prototype = {
};
module.exports = StateManager;
module.exports = GlobalStateManager;

View file

@ -7,10 +7,10 @@
var EventDispatcher = require('../events/EventDispatcher');
var GameObjectFactory = require('./systems/GameObjectFactory');
var GameObjectCreator = require('./systems/GameObjectCreator');
var StateManager = require('./systems/StateManager');
var Loader = require('./systems/Loader');
var UpdateManager = require('./systems/UpdateManager');
var Component = require('../components');
// var Camera = require('../camera/Camera');
var Settings = require('./Settings');
var RTree = require('../structs/RTree');
var Camera = require('../camera/Camera-2')
@ -46,6 +46,7 @@ var Systems = function (state, config)
this.events;
this.updates;
this.tree;
this.stateManager;
// State properties
this.cameras;
@ -77,17 +78,18 @@ Systems.prototype = {
this.tree = RTree(16);
this.events = new EventDispatcher();
this.add = new GameObjectFactory(this.state);
this.make = GameObjectCreator(this.state);
this.make = new GameObjectCreator(this.state);
this.updates = new UpdateManager(this.state);
this.load = new Loader(this.state);
this.stateManager = new StateManager(this.state, game);
// State specific properties (transform, data, children, etc)
// this.camera = new Camera(this.state, 0, 0, this.settings.width, this.settings.height);
this.children = new Component.Children(this.state);
this.color = new Component.Color(this.state);
this.data = new Component.Data(this.state);
this.transform = new Component.Transform(this.state);
this.cameras = [];
this.mainCamera = new Camera(0, 0, this.game.config.width, this.game.config.height);
this.cameras.push(this.mainCamera);
@ -107,11 +109,11 @@ Systems.prototype = {
this.state.color = this.color;
this.state.data = this.data;
this.state.settings = this.settings;
this.state.state = this.stateManager;
// this.state.camera = this.camera;
this.state.transform = this.transform;
this.state.state = this.game.state;
this.state.cache = this.game.cache;
this.state.textures = this.game.textures;
@ -144,16 +146,25 @@ Systems.prototype = {
render: function (interpolation, renderer)
{
if (!this.settings.visible)
{
return;
}
var state = this.state;
var transform = this.transform;
var cameras = this.cameras;
for (var i = 0, l = cameras.length; i < l; ++i)
{
var camera = cameras[i];
camera.preRender();
state.camera = camera;
renderer.render(state, transform.flatRenderArray, interpolation, camera);
//state.render(interpolation);
camera.postRender();
}
},

View file

@ -0,0 +1,79 @@
// A proxy class to the Global State Manager
var StateManager = function (state, game)
{
// The State that owns this StateManager
this.state = state;
this.key = state.sys.settings.key;
// GlobalStateManager
this.manager = game.state;
};
StateManager.prototype.constructor = StateManager;
StateManager.prototype = {
// Start this State (or the one given via key)
start: function (key)
{
if (key === undefined) { key = this.key; }
this.manager.start(key);
},
// Pause this State (or the one given via key)
pause: function (key)
{
if (key === undefined) { key = this.key; }
this.manager.pause(key);
},
// Stop this State and start the one given
swap: function (key)
{
this.manager.pause(this.key);
this.manager.start(key);
},
moveUp: function ()
{
},
moveDown: function ()
{
},
bringToTop: function ()
{
},
sendToBack: function ()
{
},
// TODO
transitionTo: function (key, duration)
{
this.manager.pause(this.key);
this.manager.start(key);
},
isActive: function (key)
{
if (key === undefined) { key = this.key; }
return this.manager.isActive(key);
}
};
module.exports = StateManager;