Skip to content

Commit d8e0840

Browse files
committed
GameObjects now have a data property again, which is a light-weight DataProxy object which interfaces with the DataStore.
1 parent 62cdad7 commit d8e0840

5 files changed

Lines changed: 283 additions & 3 deletions

File tree

v3/src/gameobjects/GameObject.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Class = require('../utils/Class');
22
var Components = require('./components');
3+
var DataProxy = require('./components/DataProxy');
34

45
var GameObject = new Class({
56

@@ -17,6 +18,8 @@ var GameObject = new Class({
1718

1819
this.tabIndex = -1;
1920

21+
this.data = new DataProxy(scene, this);
22+
2023
// 0001 | 0010 | 0100 | 1000
2124
// Will Render bitmask flags for the components Visible, Alpha, Transform and Texture respectively
2225
this.renderMask = 15;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
var Class = require('../../utils/Class');
2+
3+
var DataProxy = new Class({
4+
5+
initialize:
6+
7+
function DataProxy (scene, gameObject)
8+
{
9+
this.manager = scene.sys.dataStore;
10+
11+
this.gameObject = gameObject;
12+
},
13+
14+
set: function (key, value)
15+
{
16+
return this.manager.set(this.gameObject, key, value);
17+
},
18+
19+
get: function (key)
20+
{
21+
return this.manager.get(this.gameObject, key);
22+
},
23+
24+
getAll: function ()
25+
{
26+
return this.manager.getAll(this.gameObject);
27+
},
28+
29+
query: function (search)
30+
{
31+
return this.manager.query(this.gameObject, search);
32+
},
33+
34+
before: function (key, callback, scope)
35+
{
36+
return this.manager.before(this.gameObject, key, callback, scope);
37+
},
38+
39+
after: function (key, callback, scope)
40+
{
41+
return this.manager.after(this.gameObject, key, callback, scope);
42+
},
43+
44+
each: function (callback, scope)
45+
{
46+
var args = [ this.gameObject, null, undefined ];
47+
48+
for (var i = 2; i < arguments.length; i++)
49+
{
50+
args.push(arguments[i]);
51+
}
52+
53+
return this.manager.each(this.gameObject, callback, scope, args);
54+
},
55+
56+
merge: function (data, overwrite)
57+
{
58+
return this.manager.merge(this.gameObject, data, overwrite);
59+
},
60+
61+
remove: function (key)
62+
{
63+
return this.manager.remove(this.gameObject, key);
64+
},
65+
66+
removeListeners: function (key)
67+
{
68+
return this.manager.removeListeners(this.gameObject, key);
69+
},
70+
71+
pop: function (key)
72+
{
73+
return this.manager.pop(this.gameObject, key);
74+
},
75+
76+
has: function (key)
77+
{
78+
return this.manager.has(this.gameObject, key);
79+
},
80+
81+
reset: function ()
82+
{
83+
return this.manager.reset(this.gameObject);
84+
},
85+
86+
freeze: function ()
87+
{
88+
this.manager.freeze(this.gameObject);
89+
},
90+
91+
unfreeze: function ()
92+
{
93+
this.manager.unfreeze(this.gameObject);
94+
},
95+
96+
destroy: function ()
97+
{
98+
this.manager.kill(this.gameObject);
99+
100+
this.manager = null;
101+
this.gameObject = null;
102+
}
103+
104+
});
105+
106+
module.exports = DataProxy;

v3/src/scene/local/Systems.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var CameraManager = require('../plugins/CameraManager');
22
var Class = require('../../utils/Class');
33
var Clock = require('../../time/Clock');
44
var Data = require('../plugins/Data');
5+
var DataStore = require('../plugins/DataStore');
56
var DisplayList = require('../plugins/DisplayList');
67
var EventDispatcher = require('../../events/EventDispatcher');
78
var GameObjectCreator = require('../plugins/GameObjectCreator');
@@ -46,6 +47,7 @@ var Systems = new Class({
4647
this.add;
4748
this.cameras;
4849
this.data;
50+
this.dataStore;
4951
this.displayList;
5052
this.events;
5153
this.inputManager;
@@ -76,7 +78,12 @@ var Systems = new Class({
7678

7779
this.add = new GameObjectFactory(scene);
7880
this.cameras = new CameraManager(scene);
79-
this.data = new Data(scene);
81+
82+
this.dataStore = new DataStore(scene);
83+
84+
// this.data = new Data(scene);
85+
this.data = this.dataStore.register(this);
86+
8087
this.displayList = new DisplayList(scene);
8188
this.events = new EventDispatcher();
8289
this.inputManager = new InputManager(scene);

v3/src/scene/plugins/Data.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var Data = new Class({
1010

1111
initialize:
1212

13-
function Data (parent)
13+
function Data (parent, eventDispatcher)
1414
{
1515
this.parent = parent;
1616

17-
this.events = new EventDispatcher();
17+
this.events = (eventDispatcher) ? eventDispatcher : new EventDispatcher();
1818

1919
this.list = {};
2020

@@ -233,6 +233,15 @@ var Data = new Class({
233233
this._frozen = false;
234234
},
235235

236+
destroy: function ()
237+
{
238+
this.reset();
239+
240+
this.parent = null;
241+
242+
this.events = null;
243+
},
244+
236245
/**
237246
* Freeze this Data component, so no changes can be written to it.
238247
*

v3/src/scene/plugins/DataStore.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
var Class = require('../../utils/Class');
2+
var Data = require('./Data');
3+
4+
var DataStore = new Class({
5+
6+
initialize:
7+
8+
function DataStore (scene)
9+
{
10+
this.scene = scene;
11+
12+
this.events = scene.sys.events;
13+
14+
this.list = {};
15+
},
16+
17+
register: function (parent)
18+
{
19+
var data = new Data(parent, this.events);
20+
21+
this.list[parent] = data;
22+
23+
return data;
24+
},
25+
26+
getData: function (gameObject)
27+
{
28+
if (!this.list.hasOwnProperty(gameObject))
29+
{
30+
this.list[gameObject] = new Data(gameObject, this.events);
31+
}
32+
33+
return this.list[gameObject];
34+
},
35+
36+
get: function (gameObject, key)
37+
{
38+
var data = this.getData(gameObject);
39+
40+
return data.get(key);
41+
},
42+
43+
set: function (gameObject, key, value)
44+
{
45+
var data = this.getData(gameObject);
46+
47+
return data.set(key, value);
48+
},
49+
50+
getAll: function (gameObject)
51+
{
52+
var data = this.getData(gameObject);
53+
54+
return data.getAll();
55+
},
56+
57+
query: function (gameObject, search)
58+
{
59+
var data = this.getData(gameObject);
60+
61+
return data.query(search);
62+
},
63+
64+
before: function (gameObject, key, callback, scope)
65+
{
66+
var data = this.getData(gameObject);
67+
68+
return data.before(key, callback, scope);
69+
},
70+
71+
after: function (gameObject, key, callback, scope)
72+
{
73+
var data = this.getData(gameObject);
74+
75+
return data.after(key, callback, scope);
76+
},
77+
78+
each: function (gameObject, callback, scope, args)
79+
{
80+
var data = this.getData(gameObject);
81+
82+
return data.each(callback, scope);
83+
},
84+
85+
merge: function (gameObject, data, overwrite)
86+
{
87+
var data = this.getData(gameObject);
88+
89+
return data.merge(data, overwrite);
90+
},
91+
92+
remove: function (gameObject, key)
93+
{
94+
var data = this.getData(gameObject);
95+
96+
return data.remove(key);
97+
},
98+
99+
removeListeners: function (gameObject, key)
100+
{
101+
var data = this.getData(gameObject);
102+
103+
return data.removeListeners(key);
104+
},
105+
106+
pop: function (gameObject, key)
107+
{
108+
var data = this.getData(gameObject);
109+
110+
return data.pop(key);
111+
},
112+
113+
has: function (gameObject, key)
114+
{
115+
var data = this.getData(gameObject);
116+
117+
return data.has(key);
118+
},
119+
120+
reset: function (gameObject)
121+
{
122+
var data = this.getData(gameObject);
123+
124+
return data.reset();
125+
},
126+
127+
freeze: function (gameObject)
128+
{
129+
var data = this.getData(gameObject);
130+
131+
data.freeze = true;
132+
},
133+
134+
unfreeze: function (gameObject)
135+
{
136+
var data = this.getData(gameObject);
137+
138+
data.freeze = false;
139+
},
140+
141+
kill: function (gameObject)
142+
{
143+
if (this.list.hasOwnProperty(gameObject))
144+
{
145+
var data = this.list[gameObject];
146+
147+
data.destroy();
148+
149+
delete this.list[gameObject];
150+
}
151+
}
152+
153+
});
154+
155+
module.exports = DataStore;

0 commit comments

Comments
 (0)