-
Notifications
You must be signed in to change notification settings - Fork 22
/
Trait.js
executable file
·65 lines (55 loc) · 1.73 KB
/
Trait.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
Engine.Trait = function()
{
this._host = undefined;
/* Bind on instanciation so that we
they can be found when unbound. */
for (var method in this.MAGIC_METHODS) {
if (this[method]) {
this[method] = this[method].bind(this);
}
}
}
Engine.Trait.prototype.MAGIC_METHODS = {
'__collides': Engine.Object.prototype.EVENT_COLLIDE,
'__obstruct': Engine.Object.prototype.EVENT_OBSTRUCT,
'__uncollides': Engine.Object.prototype.EVENT_UNCOLLIDE,
'__timeshift': Engine.Object.prototype.EVENT_TIMESHIFT,
}
Engine.Trait.prototype.NAME = undefined;
Engine.Trait.prototype.__attach = function(object)
{
if (object instanceof Engine.Object === false) {
throw new TypeError('Invalid object');
}
if (this._host) {
this.__detach();
}
for (var method in this.MAGIC_METHODS) {
if (this[method]) {
object.bind(this.MAGIC_METHODS[method],
this[method]);
}
}
this._host = object;
}
Engine.Trait.prototype.__detach = function()
{
for (var method in this.MAGIC_METHODS) {
this._host.unbind(this.MAGIC_METHODS[method],
this[method]);
}
this._host = undefined;
}
Engine.Trait.prototype.__require = function(host, traitReference)
{
var trait = host.getTrait(traitReference);
if (trait !== false) {
return trait;
}
console.error("%s depends on %s which could not be found on %s", this, new traitReference(), host);
throw new Error("Required trait not found");
}
Engine.Trait.prototype.__collides = undefined;
Engine.Trait.prototype.__obstruct = undefined;
Engine.Trait.prototype.__uncollides = undefined;
Engine.Trait.prototype.__timeshift = undefined;