-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.js
165 lines (141 loc) · 3.9 KB
/
GameObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const uniqid = require('uniqid');
let GameObject = (extend) => {
return class GameObject {
constructor() {
this._dirty = true;
this.systems = new Set();
this.instanceData = {};
this.instances.add(this);
for (let [name, {value, expose, reference}] of this.instanceVariables) {
Object.defineProperty(this, `_${name}`, {
get: () => {
return this.instanceData[name];
},
set: (value) => {
if (value != this.instanceData[name]) {
this.instanceData[name] = value;
this.dirty = true;
}
}
});
if (expose) {
Object.defineProperty(this, name, {
get: () => {
return this[`_${name}`];
},
set: (value) => {
this[`_${name}`] = value;
}
});
}
if (value != undefined) {
this[name] = value;
}
}
}
set dirty(value) {
this._dirty = value;
}
get dirty() {
return this._dirty;
}
static initialize(data) {
this.staticVariables = new Map();
this.instanceVariables = new Map();
this.prototype.staticVariables = this.staticVariables;
this.prototype.instanceVariables = this.instanceVariables;
this.instances = new Set();
this.prototype.instances = this.instances;
this.defineStaticVariable('name', {save: true});
this.defineStaticVariable('id', {save: true});
this.defineStaticVariable('type', {save: true});
this.defineInstanceVariable('uid');
this.modify(data);
}
static modify(data) {
this.baseData = data;
for (let [name, value] of this.staticVariables) {
if (data[name]) {
this.prototype[name] = data[name];
}
}
for (let instance of this.instances) {
instance.modify(data);
}
}
modify(data) {
if (data.instanceData) {
for (let name in data.instanceData) {
this[name] = data.instanceData[name];
}
}
if (data.references) {
for (let name in data.references) {
this[name] = global.objects.get(data.references[name]);
}
}
}
static ready() {
return true;
}
static new(obj) {
obj.uid = uniqid(`${obj.id}-`);
return obj;
}
static load(obj, data) {
obj.modify(data);
return obj;
}
save() {
let data = {instanceData: {}, references: {}};
for (let [name, {save}] of this.staticVariables) {
if (save) {
data[name] = this[name];
}
}
for (let [name, {reference}] of this.instanceVariables) {
if (this[name] != undefined) {
if (reference) {
data.references[name] = this[name].uid;
} else {
data.instanceData[name] = this[name];
}
}
}
return data;
}
delete() {
for (let system of this.systems) {
this.unregister(system);
}
this.instances.delete(this);
global.objects.instances.delete(this.uid);
}
register(name) {
if (!this.systems.has(name)) {
this.systems.add(name);
global.systems.register(name, this);
}
}
unregister(name) {
if (this.systems.has(name)) {
this.systems.delete(name);
global.systems.unregister(name, this);
}
}
runSystem(name, t) {
if (global.systems.has(name)) {
global.systems[name].each(this, t);
}
}
static defineStaticVariable(name, {value, save = false} = {}) {
this.prototype[name] = value;
this.staticVariables.set(name, {name, save});
}
static defineInstanceVariable(name, {value, expose = true, reference = false} = {}) {
this.instanceVariables.set(name, {value, expose, reference});
}
}
};
GameObject.priority = 0;
module.exports = GameObject;