GameObjects now have a data property again, which is a light-weight DataProxy object which interfaces with the DataStore.

This commit is contained in:
Richard Davey 2017-09-08 01:59:53 +01:00
parent 62cdad7114
commit d8e08406ea
5 changed files with 283 additions and 3 deletions

View file

@ -1,5 +1,6 @@
var Class = require('../utils/Class'); var Class = require('../utils/Class');
var Components = require('./components'); var Components = require('./components');
var DataProxy = require('./components/DataProxy');
var GameObject = new Class({ var GameObject = new Class({
@ -17,6 +18,8 @@ var GameObject = new Class({
this.tabIndex = -1; this.tabIndex = -1;
this.data = new DataProxy(scene, this);
// 0001 | 0010 | 0100 | 1000 // 0001 | 0010 | 0100 | 1000
// Will Render bitmask flags for the components Visible, Alpha, Transform and Texture respectively // Will Render bitmask flags for the components Visible, Alpha, Transform and Texture respectively
this.renderMask = 15; this.renderMask = 15;

View file

@ -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;

View file

@ -2,6 +2,7 @@ var CameraManager = require('../plugins/CameraManager');
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var Clock = require('../../time/Clock'); var Clock = require('../../time/Clock');
var Data = require('../plugins/Data'); var Data = require('../plugins/Data');
var DataStore = require('../plugins/DataStore');
var DisplayList = require('../plugins/DisplayList'); var DisplayList = require('../plugins/DisplayList');
var EventDispatcher = require('../../events/EventDispatcher'); var EventDispatcher = require('../../events/EventDispatcher');
var GameObjectCreator = require('../plugins/GameObjectCreator'); var GameObjectCreator = require('../plugins/GameObjectCreator');
@ -46,6 +47,7 @@ var Systems = new Class({
this.add; this.add;
this.cameras; this.cameras;
this.data; this.data;
this.dataStore;
this.displayList; this.displayList;
this.events; this.events;
this.inputManager; this.inputManager;
@ -76,7 +78,12 @@ var Systems = new Class({
this.add = new GameObjectFactory(scene); this.add = new GameObjectFactory(scene);
this.cameras = new CameraManager(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.displayList = new DisplayList(scene);
this.events = new EventDispatcher(); this.events = new EventDispatcher();
this.inputManager = new InputManager(scene); this.inputManager = new InputManager(scene);

View file

@ -10,11 +10,11 @@ var Data = new Class({
initialize: initialize:
function Data (parent) function Data (parent, eventDispatcher)
{ {
this.parent = parent; this.parent = parent;
this.events = new EventDispatcher(); this.events = (eventDispatcher) ? eventDispatcher : new EventDispatcher();
this.list = {}; this.list = {};
@ -233,6 +233,15 @@ var Data = new Class({
this._frozen = false; 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. * Freeze this Data component, so no changes can be written to it.
* *

View file

@ -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;