-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstateMachine.js
More file actions
105 lines (95 loc) · 2.9 KB
/
stateMachine.js
File metadata and controls
105 lines (95 loc) · 2.9 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
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
// modified from https://github.com/drhayes/impactjs-statemachine
var StateMachine = function( entity, opts ) {
this.unnamedTransitionCounter = 0;
this.entity = entity;
this.opts = opts || {};
this.states = {};
this.transitions = {};
// Track states by name.
this.initialState = null;
this.currentState = null;
this.previousState = null;
this.timer = null;
this.state = function(name, definition) {
if (!definition) {
return this.states[name];
}
this.states[name] = definition;
if (!this.initialState) {
this.initialState = name;
}
};
this.transition = function(name, fromState, toState, predicate) {
if (!fromState && !toState && !predicate) {
return this.transitions[name];
}
// Transitions don't require names.
if (!predicate) {
predicate = toState;
toState = fromState;
fromState = name;
name = 'transition-' + this.unnamedTransitionCounter;
this.unnamedTransitionCounter += 1;
}
if (!this.states[fromState]) {
throw new Error('Missing from state: ' + fromState);
}
if (!this.states[toState]) {
throw new Error('Missing to state: ' + toState);
}
var transition = {
name: name,
fromState: fromState,
toState: toState,
predicate: predicate
};
this.transitions[name] = transition;
return transition;
};
this.update = function() {
if (!this.currentState) {
this.currentState = this.initialState;
}
var state = this.state(this.currentState);
if (this.previousState !== this.currentState) {
if( this.lastTransition ){
this.entity.animations.play( this.lastTransition.name );
if( this.opts.debug ){
console.log("Play transitional animation: " + this.lastTransition.name );
}
}
if (state.enter) {
this.timer = new Date();
state.enter( this.lastTransition );
}
this.previousState = this.currentState;
}
// Verify the transitional animation has completed before entering update()
if( this.lastTransition &&
( this.entity.animations.currentAnim.name == this.lastTransition.name && this.entity.animations.currentAnim.isPlaying ) ){
return;
}
if( this.entity.animations.currentAnim.name != this.currentState ){
if( this.opts.debug ){
console.log("Play animation: " + this.currentState );
}
this.entity.animations.play( this.currentState );
}
if (state.update) {
state.update();
}
// Iterate through transitions.
for (var name in this.transitions) {
var transition = this.transitions[name];
if (transition.fromState === this.currentState &&
transition.predicate()) {
this.lastTransition = transition;
if (state.exit) {
state.exit();
}
this.currentState = transition.toState;
return;
}
}
};
};