-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.js
More file actions
59 lines (48 loc) · 1.27 KB
/
GameObject.js
File metadata and controls
59 lines (48 loc) · 1.27 KB
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
var GameObject = EventTarget.$extend({
id: null,
components: null,
angle: 0,
parent: null,
children: null,
__init__: function(id) {
if (arguments.length < 2)
throw "Need at least one ID and one component to initialize game object.";
this.id = id;
this.components = Array.prototype.slice.call(arguments);
this.components.shift(1);
for (var i = 0; i < this.components.length; i++) {
this.components[i].container = this;
}
for (var i = 0; i < this.components.length; i++) {
this.components[i].onReady();
}
this.children = [];
log('Created GO ID: ' + id + ' with ' + this.components.length + ' components.');
},
getComponent: function(type) {
for (var i = 0; i < this.components.length; i++) {
if (this.components[i].type == type)
return this.components[i];
}
return null;
},
add: function (obj) {
this.children.push(obj);
obj.parent = this;
obj.onAdded();
},
remove: function (obj) {
obj.onRemoving();
this.children = this.children.filter(function (item) {
return item === obj;
});
obj.parent = null;
},
destroy: function () {
this.parent.remove(this);
},
// Virtual
onAdded: function () { },
// Virtual
onRemoving: function () { }
});