This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stateMachine.js
88 lines (82 loc) · 2.22 KB
/
stateMachine.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
/*global ig: true */
ig.module(
'game.system.stateMachine'
)
.requires(
'impact.impact'
)
.defines((function(global) {
'use strict';
var unnamedTransitionCounter = 0;
global.StateMachine = function() {
this.states = {};
this.transitions = {};
// Track states by name.
this.initialState = null;
this.currentState = null;
this.previousState = 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-' + unnamedTransitionCounter;
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 (state.enter) {
state.enter();
}
this.previousState = 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()) {
if (state.exit) {
state.exit();
}
this.currentState = transition.toState;
return;
}
}
};
};
}).bind(this, this));