diff --git a/v3/src/gameobjects/GameObject.js b/v3/src/gameobjects/GameObject.js index 4d1695ee1..b31dd2392 100644 --- a/v3/src/gameobjects/GameObject.js +++ b/v3/src/gameobjects/GameObject.js @@ -1,5 +1,6 @@ var Class = require('../utils/Class'); var Components = require('./components'); +var DataProxy = require('./components/DataProxy'); var GameObject = new Class({ @@ -17,6 +18,8 @@ var GameObject = new Class({ this.tabIndex = -1; + this.data = new DataProxy(scene, this); + // 0001 | 0010 | 0100 | 1000 // Will Render bitmask flags for the components Visible, Alpha, Transform and Texture respectively this.renderMask = 15; diff --git a/v3/src/gameobjects/components/DataProxy.js b/v3/src/gameobjects/components/DataProxy.js new file mode 100644 index 000000000..d1c5dc564 --- /dev/null +++ b/v3/src/gameobjects/components/DataProxy.js @@ -0,0 +1,106 @@ +var Class = require('../../utils/Class'); + +var DataProxy = new Class({ + + initialize: + + function DataProxy (scene, gameObject) + { + this.manager = scene.sys.dataStore; + + this.gameObject = gameObject; + }, + + set: function (key, value) + { + return this.manager.set(this.gameObject, key, value); + }, + + get: function (key) + { + return this.manager.get(this.gameObject, key); + }, + + getAll: function () + { + return this.manager.getAll(this.gameObject); + }, + + query: function (search) + { + return this.manager.query(this.gameObject, search); + }, + + before: function (key, callback, scope) + { + return this.manager.before(this.gameObject, key, callback, scope); + }, + + after: function (key, callback, scope) + { + return this.manager.after(this.gameObject, key, callback, scope); + }, + + each: function (callback, scope) + { + var args = [ this.gameObject, null, undefined ]; + + for (var i = 2; i < arguments.length; i++) + { + args.push(arguments[i]); + } + + return this.manager.each(this.gameObject, callback, scope, args); + }, + + merge: function (data, overwrite) + { + return this.manager.merge(this.gameObject, data, overwrite); + }, + + remove: function (key) + { + return this.manager.remove(this.gameObject, key); + }, + + removeListeners: function (key) + { + return this.manager.removeListeners(this.gameObject, key); + }, + + pop: function (key) + { + return this.manager.pop(this.gameObject, key); + }, + + has: function (key) + { + return this.manager.has(this.gameObject, key); + }, + + reset: function () + { + return this.manager.reset(this.gameObject); + }, + + freeze: function () + { + this.manager.freeze(this.gameObject); + }, + + unfreeze: function () + { + this.manager.unfreeze(this.gameObject); + }, + + destroy: function () + { + this.manager.kill(this.gameObject); + + this.manager = null; + this.gameObject = null; + } + +}); + +module.exports = DataProxy; diff --git a/v3/src/scene/local/Systems.js b/v3/src/scene/local/Systems.js index b92705495..de82ed9d6 100644 --- a/v3/src/scene/local/Systems.js +++ b/v3/src/scene/local/Systems.js @@ -2,6 +2,7 @@ var CameraManager = require('../plugins/CameraManager'); var Class = require('../../utils/Class'); var Clock = require('../../time/Clock'); var Data = require('../plugins/Data'); +var DataStore = require('../plugins/DataStore'); var DisplayList = require('../plugins/DisplayList'); var EventDispatcher = require('../../events/EventDispatcher'); var GameObjectCreator = require('../plugins/GameObjectCreator'); @@ -46,6 +47,7 @@ var Systems = new Class({ this.add; this.cameras; this.data; + this.dataStore; this.displayList; this.events; this.inputManager; @@ -76,7 +78,12 @@ var Systems = new Class({ this.add = new GameObjectFactory(scene); this.cameras = new CameraManager(scene); - this.data = new Data(scene); + + this.dataStore = new DataStore(scene); + + // this.data = new Data(scene); + this.data = this.dataStore.register(this); + this.displayList = new DisplayList(scene); this.events = new EventDispatcher(); this.inputManager = new InputManager(scene); diff --git a/v3/src/scene/plugins/Data.js b/v3/src/scene/plugins/Data.js index d2e9da864..f66c9390e 100644 --- a/v3/src/scene/plugins/Data.js +++ b/v3/src/scene/plugins/Data.js @@ -10,11 +10,11 @@ var Data = new Class({ initialize: - function Data (parent) + function Data (parent, eventDispatcher) { this.parent = parent; - this.events = new EventDispatcher(); + this.events = (eventDispatcher) ? eventDispatcher : new EventDispatcher(); this.list = {}; @@ -233,6 +233,15 @@ var Data = new Class({ this._frozen = false; }, + destroy: function () + { + this.reset(); + + this.parent = null; + + this.events = null; + }, + /** * Freeze this Data component, so no changes can be written to it. * diff --git a/v3/src/scene/plugins/DataStore.js b/v3/src/scene/plugins/DataStore.js new file mode 100644 index 000000000..6b2851155 --- /dev/null +++ b/v3/src/scene/plugins/DataStore.js @@ -0,0 +1,155 @@ +var Class = require('../../utils/Class'); +var Data = require('./Data'); + +var DataStore = new Class({ + + initialize: + + function DataStore (scene) + { + this.scene = scene; + + this.events = scene.sys.events; + + this.list = {}; + }, + + register: function (parent) + { + var data = new Data(parent, this.events); + + this.list[parent] = data; + + return data; + }, + + getData: function (gameObject) + { + if (!this.list.hasOwnProperty(gameObject)) + { + this.list[gameObject] = new Data(gameObject, this.events); + } + + return this.list[gameObject]; + }, + + get: function (gameObject, key) + { + var data = this.getData(gameObject); + + return data.get(key); + }, + + set: function (gameObject, key, value) + { + var data = this.getData(gameObject); + + return data.set(key, value); + }, + + getAll: function (gameObject) + { + var data = this.getData(gameObject); + + return data.getAll(); + }, + + query: function (gameObject, search) + { + var data = this.getData(gameObject); + + return data.query(search); + }, + + before: function (gameObject, key, callback, scope) + { + var data = this.getData(gameObject); + + return data.before(key, callback, scope); + }, + + after: function (gameObject, key, callback, scope) + { + var data = this.getData(gameObject); + + return data.after(key, callback, scope); + }, + + each: function (gameObject, callback, scope, args) + { + var data = this.getData(gameObject); + + return data.each(callback, scope); + }, + + merge: function (gameObject, data, overwrite) + { + var data = this.getData(gameObject); + + return data.merge(data, overwrite); + }, + + remove: function (gameObject, key) + { + var data = this.getData(gameObject); + + return data.remove(key); + }, + + removeListeners: function (gameObject, key) + { + var data = this.getData(gameObject); + + return data.removeListeners(key); + }, + + pop: function (gameObject, key) + { + var data = this.getData(gameObject); + + return data.pop(key); + }, + + has: function (gameObject, key) + { + var data = this.getData(gameObject); + + return data.has(key); + }, + + reset: function (gameObject) + { + var data = this.getData(gameObject); + + return data.reset(); + }, + + freeze: function (gameObject) + { + var data = this.getData(gameObject); + + data.freeze = true; + }, + + unfreeze: function (gameObject) + { + var data = this.getData(gameObject); + + data.freeze = false; + }, + + kill: function (gameObject) + { + if (this.list.hasOwnProperty(gameObject)) + { + var data = this.list[gameObject]; + + data.destroy(); + + delete this.list[gameObject]; + } + } + +}); + +module.exports = DataStore;