/
StateMachine.as
67 lines (61 loc) · 1.37 KB
/
StateMachine.as
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
package Mono.Utilities
{
import Mono.Mono;
import Mono.Utilities.Complements.State;
import flash.utils.Dictionary;
public class StateMachine
{
private var _currentState:State;
private var _states:Dictionary;
public function StateMachine()
{
_states = new Dictionary();
}
/** The update that must be called each time per frame.
*
* */
public function update():void
{
_currentState.update();
}
/** Adds a state.
*
* @param state State to be added
* @param name Name of the state
*
* */
public function addState(state:State, name:String):void
{
_states[name] = state;
if(_currentState==null)
{
_currentState = state;
}
}
/** Changes to other state.
*
* @param name Name of the state.
*
* */
public function setState(name:String):void
{
if(_states[name]!=null)
{
if(_states[name] != _currentState)
{
_currentState.sleep();
_currentState = _states[name];
_currentState.awake();
}
else
{
Main.mono.reportWarning("It was already in the state "+name+" so it won't change state", "Utilities", "StateMachine", "setState");
}
}
else
{
Main.mono.reportWarning("The state didn't exist, so no changes were made", "Utilities", "StateMachine", "setState");
}
}
}
}