From a27d42bc1356b1eb9d8229801ca0c028e40865f9 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 30 Jun 2017 00:32:18 +0100 Subject: [PATCH] Added State Injection Map State level properties can now be set and modified via the State config. State.settings removed and all accesses to it moved to sys.settings. --- v3/src/checksum.js | 2 +- v3/src/loader/BaseLoader.js | 4 ++-- v3/src/state/GlobalStateManager.js | 18 +++++++++--------- v3/src/state/InjectionMap.js | 30 ++++++++++++++++++++++++++++++ v3/src/state/Settings.js | 7 ++++++- v3/src/state/State.js | 6 ++---- v3/src/state/Systems.js | 29 +++++++++-------------------- 7 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 v3/src/state/InjectionMap.js diff --git a/v3/src/checksum.js b/v3/src/checksum.js index dca477188..0a116faad 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '0a9c3370-5ce3-11e7-886b-43134c9cdc6e' +build: '0d93fb90-5d23-11e7-a642-d974c901baec' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/loader/BaseLoader.js b/v3/src/loader/BaseLoader.js index 7d0e7617a..c3178779e 100644 --- a/v3/src/loader/BaseLoader.js +++ b/v3/src/loader/BaseLoader.js @@ -67,7 +67,7 @@ BaseLoader.prototype = { start: function () { - console.log(this.state.settings.key, '- BaseLoader start. Files to load:', this.list.size); + console.log(this.state.sys.settings.key, '- BaseLoader start. Files to load:', this.list.size); if (!this.isReady()) { @@ -247,7 +247,7 @@ BaseLoader.prototype = { processComplete: function () { - console.log(this.state.settings.key, '- Loader Complete. Loaded:', this.storage.size, 'Failed:', this.failed.size); + console.log(this.state.sys.settings.key, '- Loader Complete. Loaded:', this.storage.size, 'Failed:', this.failed.size); this.list.clear(); this.inflight.clear(); diff --git a/v3/src/state/GlobalStateManager.js b/v3/src/state/GlobalStateManager.js index 17ad37213..3ae7a90d5 100644 --- a/v3/src/state/GlobalStateManager.js +++ b/v3/src/state/GlobalStateManager.js @@ -99,7 +99,7 @@ GlobalStateManager.prototype = { } else if (stateConfig instanceof State) { - key = stateConfig.settings.key; + key = stateConfig.sys.settings.key; } else if (typeof stateConfig === 'object' && stateConfig.hasOwnProperty('key')) { @@ -175,13 +175,13 @@ GlobalStateManager.prototype = { } // Replace key incase the state changed it - key = newState.settings.key; + key = newState.sys.settings.key; this.keys[key] = newState; this.states.push(newState); - if (autoStart || newState.settings.active) + if (autoStart || newState.sys.settings.active) { if (this.game.isBooted) { @@ -198,7 +198,7 @@ GlobalStateManager.prototype = { createStateFromInstance: function (key, newState) { - newState.settings.key = key; + newState.sys.settings.key = key; newState.sys.init(this.game); @@ -298,7 +298,7 @@ GlobalStateManager.prototype = { createStateDisplay: function (state) { - // console.log('createStateDisplay', state.settings.key); + // console.log('createStateDisplay', state.sys.settings.key); var settings = state.sys.settings; @@ -366,7 +366,7 @@ GlobalStateManager.prototype = { { var state = this.getState(key); - return (state && state.settings.active && this.active.indexOf(state) !== -1); + return (state && state.sys.settings.active && this.active.indexOf(state) !== -1); }, start: function (key, data) @@ -407,9 +407,9 @@ GlobalStateManager.prototype = { return; } - state.settings.active = true; + state.sys.settings.active = true; - state.settings.data = data; + state.sys.settings.data = data; var loader = state.sys.load; @@ -524,7 +524,7 @@ GlobalStateManager.prototype = { { var state = this.getState(key); - state.settings.active = false; + state.sys.settings.active = false; this.active.splice(index, 1); diff --git a/v3/src/state/InjectionMap.js b/v3/src/state/InjectionMap.js new file mode 100644 index 000000000..e92a1e220 --- /dev/null +++ b/v3/src/state/InjectionMap.js @@ -0,0 +1,30 @@ +// These properties get injected into the State and map to local systems +// The key is the local system reference, the value is the property that is added to the State +// These can be modified via the config object + +var InjectionMap = { + + game: 'game', + + anims: 'anims', + cache: 'cache', + input: 'input', + registry: 'registry', + textures: 'textures', + + add: 'add', + cameras: 'cameras', + events: 'events', + load: 'load', + make: 'make', + stateManager: 'state', + time: 'time', + tweens: 'tweens', + + children: 'children', + color: 'color', + data: 'data' + +}; + +module.exports = InjectionMap; diff --git a/v3/src/state/Settings.js b/v3/src/state/Settings.js index b2696159d..853d14e71 100644 --- a/v3/src/state/Settings.js +++ b/v3/src/state/Settings.js @@ -1,6 +1,7 @@ var CONST = require('./const'); var ScaleModes = require('../renderer/ScaleModes'); var GetValue = require('../utils/object/GetValue'); +var InjectionMap = require('./InjectionMap'); var Settings = { @@ -22,7 +23,7 @@ var Settings = { op: CONST.BOOT, - key: GetValue(config, 'key', ''), + key: GetValue(config, 'key', 'default'), active: GetValue(config, 'active', false), visible: GetValue(config, 'visible', true), @@ -36,6 +37,10 @@ var Settings = { cameras: GetValue(config, 'cameras', null), + // State Property Injection Map + + map: GetValue(config, 'map', InjectionMap), + // State Render Settings (applies only to this State) scaleMode: GetValue(config, 'scaleMode', ScaleModes.DEFAULT), diff --git a/v3/src/state/State.js b/v3/src/state/State.js index bb02070c3..cc3ff4aed 100644 --- a/v3/src/state/State.js +++ b/v3/src/state/State.js @@ -16,12 +16,8 @@ var State = function (config) { // The State Systems. You must never overwrite this property, or all hell will break lose. this.sys = new Systems(this, config); - - this.settings = this.sys.settings; }; -State.prototype.constructor = State; - State.prototype = { // Should be overridden by your own States @@ -36,4 +32,6 @@ State.prototype = { }; +State.prototype.constructor = State; + module.exports = State; diff --git a/v3/src/state/Systems.js b/v3/src/state/Systems.js index 29af8c883..50dab5a69 100644 --- a/v3/src/state/Systems.js +++ b/v3/src/state/Systems.js @@ -92,28 +92,17 @@ Systems.prototype = { inject: function () { - // Defaults properties injected into the State + var map = this.settings.map; - this.state.game = this.game; + for (var key in map) + { + if (key === 'sys') + { + continue; + } - this.state.anims = this.anims; - this.state.cache = this.cache; - this.state.input = this.input; - this.state.registry = this.registry; - this.state.textures = this.textures; - - this.state.add = this.add; - this.state.cameras = this.cameras; - this.state.events = this.events; - this.state.load = this.load; - this.state.make = this.make; - this.state.state = this.stateManager; - this.state.time = this.time; - this.state.tweens = this.tweens; - - this.state.children = this.children; - this.state.color = this.color; - this.state.data = this.data; + this.state[map[key]] = this[key]; + } }, step: function (time, delta)