Moved _sys to a StateSystems class to keep things much cleaner, and avoid setter inheritance mess.

This commit is contained in:
Richard Davey 2016-11-06 12:18:08 +00:00
parent 21937a1106
commit d568d2bede
11 changed files with 425 additions and 444 deletions

View file

@ -77,6 +77,7 @@ EOL;
<script src="$path/src/states/State.js"></script>
<script src="$path/src/states/StateManager.js"></script>
<script src="$path/src/states/StateSettings.js"></script>
<script src="$path/src/states/StateSystems.js"></script>
<script src="$path/src/core/Camera.js"></script>
<script src="$path/src/core/Create.js"></script>

View file

@ -480,6 +480,13 @@ var Phaser = Phaser || { // jshint ignore:line
*/
BOTTOM_RIGHT: 12,
/**
* A constant representing a no-operation function
* @constant
* @type {function}
*/
NOOP: function () {},
/**
* Various blend modes supported by Pixi.
*

View file

@ -529,6 +529,233 @@ Phaser.Component.Transform.prototype = {
Object.defineProperties(Phaser.Component.Transform.prototype, {
// Transform getters / setters
x: {
enumerable: true,
get: function ()
{
return this._posX;
},
set: function (value)
{
this._posX = value;
this.dirty = true;
}
},
y: {
enumerable: true,
get: function ()
{
return this._posY;
},
set: function (value)
{
this._posY = value;
this.dirty = true;
}
},
scale: {
enumerable: true,
get: function ()
{
return this._scaleX;
},
set: function (value)
{
this._scaleX = value;
this._scaleY = value;
this.dirty = true;
this.updateCache();
}
},
scaleX: {
enumerable: true,
get: function ()
{
return this._scaleX;
},
set: function (value)
{
this._scaleX = value;
this.dirty = true;
this.updateCache();
}
},
scaleY: {
enumerable: true,
get: function ()
{
return this._scaleY;
},
set: function (value)
{
this._scaleY = value;
this.dirty = true;
this.updateCache();
}
},
anchor: {
enumerable: true,
get: function ()
{
return this._anchorX;
},
set: function (value)
{
this.setAnchor(value);
}
},
anchorX: {
enumerable: true,
get: function ()
{
return this._anchorX;
},
set: function (value)
{
this._anchorX = value;
this.dirty = true;
}
},
anchorY: {
enumerable: true,
get: function ()
{
return this._anchorY;
},
set: function (value)
{
this._anchorY = value;
this.dirty = true;
}
},
pivotX: {
enumerable: true,
get: function ()
{
return this._pivotX;
},
set: function (value)
{
this._pivotX = value;
this.dirty = true;
this.updateCache();
}
},
pivotY: {
enumerable: true,
get: function ()
{
return this._pivotY;
},
set: function (value)
{
this._pivotY = value;
this.dirty = true;
this.updateCache();
}
},
angle: {
enumerable: true,
get: function ()
{
return Phaser.Math.wrapAngle(this.rotation * Phaser.Math.RAD_TO_DEG);
},
set: function (value)
{
this.rotation = Phaser.Math.wrapAngle(value) * Phaser.Math.DEG_TO_RAD;
}
},
rotation: {
enumerable: true,
get: function ()
{
return this._rotation;
},
set: function (value)
{
if (this._rotation === value)
{
return;
}
this._rotation = value;
this.dirty = true;
if (this._rotation % Phaser.Math.PI2)
{
this.cache.sr = Math.sin(this._rotation);
this.cache.cr = Math.cos(this._rotation);
this.updateCache();
this.hasLocalRotation = true;
}
else
{
this.hasLocalRotation = false;
}
}
},
// Sets this *component* as being dirty
dirty: {

View file

@ -14,13 +14,13 @@
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
*/
Phaser.GameObject.Factory = function (game, state)
Phaser.GameObject.Factory = function (state)
{
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
* @protected
*/
this.game = game;
this.game = state.game;
/**
* @property {Phaser.State} state - The State that owns this Factory

View file

@ -46,6 +46,7 @@ Phaser.GameObject.Image.prototype.constructor = Phaser.GameObject.Image;
*/
Phaser.GameObject.Image.prototype.preUpdate = function ()
{
// Would like to get rid of this somehow ...
if (this.parent)
{
this.color.worldAlpha = this.parent.color.worldAlpha;

View file

@ -19,14 +19,14 @@
* @class Phaser.Loader
* @param {Phaser.Game} game - A reference to the currently running game.
*/
Phaser.Loader = function (game, state) {
Phaser.Loader = function (state)
{
/**
* Local reference to game.
* @property {Phaser.Game} game
* @protected
*/
this.game = game;
this.game = state.game;
/**
* Reference to the State that owns this Loader.
@ -40,7 +40,7 @@ Phaser.Loader = function (game, state) {
* @property {Phaser.Cache} cache
* @protected
*/
this.cache = game.cache;
this.cache = this.game.cache;
/**
* If true all calls to Loader.reset will be ignored. Useful if you need to create a load queue before swapping to a preloader state.

View file

@ -459,7 +459,7 @@ Phaser.Renderer.WebGL.prototype = {
var gl = this.gl;
var fbo = state._sys.fbo;
var fbo = state.sys.fbo;
fbo.activate();
@ -842,6 +842,12 @@ Phaser.Renderer.WebGL.prototype = {
return texture;
},
createFBO: function (x, y, width, height)
{
// Store in a local list so we can update size if the canvas size changes?
return new Phaser.Renderer.WebGL.QuadFBO(this, x, y, width, height);
},
destroy: function ()
{
this.projection = null;

View file

@ -7,33 +7,21 @@
/**
* A Base State Class.
*
* Think about how to handle a State that only exists as a Preloader state for example.
* Without needing all the 'loadUpdate' etc functions.
*
* Objective: To avoid the State ever changing shape (unless the dev wants it to)
*
* @class Phaser.State
* @constructor
*/
Phaser.State = function (config)
{
// The properties a State *must* have, that cannot be changed without breaking it:
this.game = null;
this.settings = new Phaser.State.Settings(this, config);
// Could be a StateSystems class (to add protection + jsdocs)
this._sys = {
add: null,
data: null,
input: null,
tweens: null,
load: null,
transform: null,
children: null,
color: null,
time: null,
fbo: null
};
this.sys = new Phaser.State.Systems(this, config);
// Reference to sys.children, set during sys.init only
this.children;
};
Phaser.State.prototype.constructor = Phaser.State;
@ -61,279 +49,3 @@ Phaser.State.prototype = {
}
};
Phaser.State.gs = {
add: {
enumerable: true,
get: function ()
{
return this._sys.add;
},
set: function ()
{
throw Error('Cannot re-assign protected property: add');
}
},
load: {
enumerable: true,
get: function ()
{
return this._sys.load;
},
set: function ()
{
throw Error('Cannot re-assign protected property: load');
}
},
data: {
enumerable: true,
get: function ()
{
return this._sys.data;
},
set: function ()
{
throw Error('Cannot re-assign protected property: data');
}
},
children: {
enumerable: true,
get: function ()
{
return this._sys.children;
},
set: function ()
{
throw Error('Cannot re-assign protected property: children');
}
},
color: {
enumerable: true,
get: function ()
{
return this._sys.color;
},
set: function ()
{
throw Error('Cannot re-assign protected property: color');
}
},
transform: {
enumerable: true,
get: function ()
{
return this._sys.transform;
},
set: function ()
{
throw Error('Cannot re-assign protected property: transform');
}
},
// Just a test
rotation: {
enumerable: true,
get: function ()
{
return this.transform._rotation;
},
set: function (value)
{
if (this.transform._rotation === value)
{
return;
}
this.transform._rotation = value;
this.transform.dirty = true;
if (this.transform._rotation % Phaser.Math.PI2)
{
this.transform.cache.sr = Math.sin(this.transform._rotation);
this.transform.cache.cr = Math.cos(this.transform._rotation);
this.transform.updateCache();
this.transform.hasLocalRotation = true;
}
else
{
this.transform.hasLocalRotation = false;
}
}
}
};
Object.defineProperties(Phaser.State.prototype, Phaser.State.gs);
/*
Object.defineProperties(Phaser.State.prototype, {
add: {
enumerable: true,
get: function ()
{
return this._sys.add;
},
set: function ()
{
throw Error('Cannot re-assign protected property: add');
}
},
load: {
enumerable: true,
get: function ()
{
return this._sys.load;
},
set: function ()
{
throw Error('Cannot re-assign protected property: load');
}
},
data: {
enumerable: true,
get: function ()
{
return this._sys.data;
},
set: function ()
{
throw Error('Cannot re-assign protected property: data');
}
},
children: {
enumerable: true,
get: function ()
{
return this._sys.children;
},
set: function ()
{
throw Error('Cannot re-assign protected property: children');
}
},
color: {
enumerable: true,
get: function ()
{
return this._sys.color;
},
set: function ()
{
throw Error('Cannot re-assign protected property: color');
}
},
transform: {
enumerable: true,
get: function ()
{
return this._sys.transform;
},
set: function ()
{
throw Error('Cannot re-assign protected property: transform');
}
},
// Just a test
rotation: {
enumerable: true,
get: function ()
{
return this.transform._rotation;
},
set: function (value)
{
if (this.transform._rotation === value)
{
return;
}
this.transform._rotation = value;
this.transform.dirty = true;
if (this.transform._rotation % Phaser.Math.PI2)
{
this.transform.cache.sr = Math.sin(this.transform._rotation);
this.transform.cache.cr = Math.cos(this.transform._rotation);
this.transform.updateCache();
this.transform.hasLocalRotation = true;
}
else
{
this.transform.hasLocalRotation = false;
}
}
}
});
*/

View file

@ -157,89 +157,13 @@ Phaser.StateManager.prototype = {
return newState;
},
createStateFromFunction: function (key, state)
{
var newState = new state();
if (newState instanceof Phaser.State)
{
return this.createStateFromInstance(key, newState);
}
else
{
newState.game = null;
newState.settings = new Phaser.State.Settings(newState, key);
// Could be a StateSystems class (to add protection + jsdocs)
newState._sys = {
add: null,
data: null,
input: null,
tweens: null,
load: null,
transform: null,
children: null,
color: null,
time: null,
fbo: null
};
// Messy! But works ...
Object.defineProperties(newState, Phaser.State.gs);
if (!newState.preUpdate)
{
newState.preUpdate = function () {};
}
if (!newState.update)
{
newState.update = function () {};
}
if (!newState.postUpdate)
{
newState.postUpdate = function () {};
}
if (!newState.render)
{
newState.render = function () {};
}
return this.createStateFromInstance(key, newState);
}
},
injectDefaultSystems: function (newState)
{
// Inject the default (non-optional) managers
newState._sys.add = new Phaser.GameObject.Factory(this.game, newState);
// States have their own Loaders
newState._sys.load = new Phaser.Loader(this.game, newState);
newState._sys.transform = new Phaser.Component.Transform(newState);
newState._sys.data = new Phaser.Component.Data(newState);
newState._sys.color = new Phaser.Component.Color(newState);
newState._sys.children = new Phaser.Component.Children(newState);
return newState;
},
createStateFromInstance: function (key, newState)
{
newState.game = this.game;
newState.settings.key = key;
this.injectDefaultSystems(newState);
newState.sys.init();
if (this.game.renderType === Phaser.WEBGL)
{
@ -249,36 +173,18 @@ Phaser.StateManager.prototype = {
return newState;
},
createStateFrameBuffer: function (newState)
{
var x = newState.settings.x;
var y = newState.settings.y;
if (newState.settings.width === -1)
{
newState.settings.width = this.game.width;
}
if (newState.settings.height === -1)
{
newState.settings.height = this.game.height;
}
var width = newState.settings.width;
var height = newState.settings.height;
newState._sys.fbo = new Phaser.Renderer.WebGL.QuadFBO(this.game.renderer, x, y, width, height);
},
createStateFromObject: function (key, state)
{
var newState = new Phaser.State(key);
newState.game = this.game;
this.injectDefaultSystems(newState);
newState.sys.init();
// Inject custom managers
if (this.game.renderType === Phaser.WEBGL)
{
this.createStateFrameBuffer(newState);
}
// Extract callbacks or set NOOP
@ -302,41 +208,84 @@ Phaser.StateManager.prototype = {
newState.shutdown = state.shutdown;
}
newState.preUpdate = (state.hasOwnProperty('preUpdate')) ? state.preUpdate : function () {};
newState.update = (state.hasOwnProperty('update')) ? state.update : function () {};
newState.postUpdate = (state.hasOwnProperty('postUpdate')) ? state.postUpdate : function () {};
newState.render = (state.hasOwnProperty('render')) ? state.render : function () {};
// Settings?
if (state.hasOwnProperty('x'))
{
newState.settings.x = state.x;
}
if (state.hasOwnProperty('y'))
{
newState.settings.y = state.y;
}
if (state.hasOwnProperty('width'))
{
newState.settings.width = state.width;
}
if (state.hasOwnProperty('height'))
{
newState.settings.height = state.height;
}
if (this.game.renderType === Phaser.WEBGL)
{
this.createStateFrameBuffer(newState);
}
newState.preUpdate = (state.hasOwnProperty('preUpdate')) ? state.preUpdate : Phaser.NOOP;
newState.update = (state.hasOwnProperty('update')) ? state.update : Phaser.NOOP;
newState.postUpdate = (state.hasOwnProperty('postUpdate')) ? state.postUpdate : Phaser.NOOP;
newState.render = (state.hasOwnProperty('render')) ? state.render : Phaser.NOOP;
return newState;
},
createStateFromFunction: function (key, state)
{
var newState = new state();
if (newState instanceof Phaser.State)
{
return this.createStateFromInstance(key, newState);
}
else
{
newState.game = this.game;
newState.settings = new Phaser.State.Settings(newState, key);
newState.sys = new Phaser.State.Systems(newState);
newState.sys.init();
if (this.game.renderType === Phaser.WEBGL)
{
this.createStateFrameBuffer(newState);
}
// Default required functions
if (!newState.preUpdate)
{
newState.preUpdate = Phaser.NOOP;
}
if (!newState.update)
{
newState.update = Phaser.NOOP;
}
if (!newState.postUpdate)
{
newState.postUpdate = Phaser.NOOP;
}
if (!newState.render)
{
newState.render = Phaser.NOOP;
}
return newState;
}
},
createStateFrameBuffer: function (newState)
{
var x = newState.settings.x;
var y = newState.settings.y;
if (newState.settings.width === -1)
{
newState.settings.width = this.game.width;
}
if (newState.settings.height === -1)
{
newState.settings.height = this.game.height;
}
var width = newState.settings.width;
var height = newState.settings.height;
newState.sys.fbo = this.game.renderer.createFBO(x, y, width, height);
},
getState: function (key)
{
return this.keys[key];
@ -391,12 +340,12 @@ Phaser.StateManager.prototype = {
if (state.preload)
{
state.load.reset(true);
state.sys.load.reset(true);
state.preload.call(state, this.game);
// Is the loader empty?
if (state.load.totalQueuedFiles() === 0 && state.load.totalQueuedPacks() === 0)
if (state.sys.load.totalQueuedFiles() === 0 && state.sys.load.totalQueuedPacks() === 0)
{
// console.log('empty queue');
this.startCreate(state);
@ -408,7 +357,7 @@ Phaser.StateManager.prototype = {
// Start the loader going as we have something in the queue
// state.load.onLoadComplete.addOnce(this.loadComplete, this, 0, state);
state.load.start();
state.sys.load.start();
}
}
else
@ -454,9 +403,9 @@ Phaser.StateManager.prototype = {
{
var state = this.active[i];
for (var c = 0; c < state._sys.children.list.length; c++)
for (var c = 0; c < state.sys.children.list.length; c++)
{
state._sys.children.list[c].preUpdate();
state.sys.children.list[c].preUpdate();
}
state.preUpdate();
@ -474,9 +423,9 @@ Phaser.StateManager.prototype = {
// This shouldn't be called if the State is still loading
// Have a State.STATUS const in the Settings, dictating what is going on
for (var c = 0; c < state._sys.children.list.length; c++)
for (var c = 0; c < state.sys.children.list.length; c++)
{
var child = state._sys.children.list[c];
var child = state.sys.children.list[c];
if (child.exists)
{
@ -494,9 +443,9 @@ Phaser.StateManager.prototype = {
{
var state = this.active[i];
for (var c = 0; c < state._sys.children.list.length; c++)
for (var c = 0; c < state.sys.children.list.length; c++)
{
state._sys.children.list[c].postUpdate();
state.sys.children.list[c].postUpdate();
}
state.postUpdate();
@ -510,7 +459,7 @@ Phaser.StateManager.prototype = {
var state = this.active[i];
// Can put all kinds of other checks in here, like MainLoop, FPS, etc.
if (!state.settings.visible || state._sys.color.alpha === 0 || state._sys.children.list.length === 0)
if (!state.settings.visible || state.sys.color.alpha === 0 || state.sys.children.list.length === 0)
{
continue;
}
@ -522,9 +471,9 @@ Phaser.StateManager.prototype = {
renderChildren: function (renderer, state)
{
// Populates the display list
for (var c = 0; c < state._sys.children.list.length; c++)
for (var c = 0; c < state.sys.children.list.length; c++)
{
var child = state._sys.children.list[c];
var child = state.sys.children.list[c];
child.render(renderer, child);
}

View file

@ -12,6 +12,12 @@ Phaser.State.Settings = function (state, config)
this.state = state;
this.status = Phaser.State.Settings.PENDING;
// Which part of this State is currently being processed?
// preload, create, update, shutdown, etc
this.op = Phaser.State.Settings.BOOT;
this.key = (config.hasOwnProperty('key')) ? config.key : '';
this.active = (config.hasOwnProperty('active')) ? config.active : false;
@ -30,4 +36,15 @@ Phaser.State.Settings = function (state, config)
this.height = (config.hasOwnProperty('height')) ? config.height : -1;
};
Phaser.State.Settings.PENDING = 0;
Phaser.State.Settings.INSTALLED = 1;
Phaser.State.Settings.BOOT = 0;
Phaser.State.Settings.INIT = 1;
Phaser.State.Settings.PRELOAD = 2;
Phaser.State.Settings.CREATE = 3;
Phaser.State.Settings.UPDATE = 4;
Phaser.State.Settings.RENDER = 5;
Phaser.State.Settings.SHUTDOWN = 6;
Phaser.State.Settings.prototype.constructor = Phaser.State.Settings;

View file

@ -0,0 +1,61 @@
Phaser.State.Systems = function (state, config)
{
this.state = state;
this.config = config;
// State specific managers (Factory, Tweens, Loader, Physics, etc)
this.add;
this.input;
this.load;
this.tweens;
// State specific properties (transform, data, children, etc)
this.children;
this.color;
this.data;
this.fbo;
this.time;
this.transform;
};
Phaser.State.Systems.prototype.constructor = Phaser.State.Systems;
Phaser.State.Systems.prototype = {
init: function ()
{
// State specific managers (Factory, Tweens, Loader, Physics, etc)
this.add = new Phaser.GameObject.Factory(this.state);
this.load = new Phaser.Loader(this.state);
// State specific properties (transform, data, children, etc)
this.children = new Phaser.Component.Children(this.state);
this.color = new Phaser.Component.Color(this.state);
this.data = new Phaser.Component.Data(this.state);
this.transform = new Phaser.Component.Transform(this.state);
// Defaults
this.state.add = this.add;
this.state.load = this.load;
this.state.children = this.children;
this.state.color = this.color;
// this.state.data = this.data;
this.state.transform = this.transform;
// Here we can check which Systems to install as properties into the State object
// (default systems always exist in here, regardless)
var config = this.config;
var t = typeof config;
if (t !== 'object' || (t === 'object' && !t.hasOwnProperty('systems')))
{
return;
}
// this.key = (config.hasOwnProperty('key')) ? config.key : '';
}
};