-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActorComponent.js
More file actions
53 lines (44 loc) · 1.13 KB
/
ActorComponent.js
File metadata and controls
53 lines (44 loc) · 1.13 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
var ActorComponent = Component.$extend({
states: [],
state: null,
actions: [],
input: null,
__init__: function() {
},
changeState: function(state) {
if (this.state) {
this.state.onExit(this);
}
this.state = state;
this.state.onEnter(this);
},
subscribeToActions: function() {
for (var i = 0, l = actions.length; i < l; i++) {
var action = actions[i];
action.fn = bind(function() {
this.state.onAction(this, action.name);
}, this);
this.container.addListener(action.name, action.fn);
}
},
unsubscribeFromActions: function() {
for (var i = 0, l = actions.length; i < l; i++) {
this.container.removeListener(actions[i].name, actions[i].fn);
delete actions[i].fn;
}
},
onAdded: function() {
this.subscribeToActions();
},
onRemoved: function() {
this.unsubscribeFromActions();
}
})
var State = Class.$extend({
// Virtual
onEnter: function(actor) {},
// Virtual
onExit: function(actor) {},
// Virtual
onAction: function(actor, action) {}
})