-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachine.cs
More file actions
executable file
·55 lines (48 loc) · 1.16 KB
/
StateMachine.cs
File metadata and controls
executable file
·55 lines (48 loc) · 1.16 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
using System;
using System.Collections.Generic;
namespace SimpleState {
public class StateMachine
{
public List<State> states = new List<State>();
State current;
public void Update(float dt) {
if(current != null) {
current.Update(dt);
}
}
public bool startState(State state) {
if(current == null) {
current = state;
current.enter("");
return true;
}
return false;
}
public bool changeState(string name) {
if(validNextState(name)) {
string previous = current.name;
current.Exit(name);
State next = this.getState(name);
if(next != null) {
current = next;
current.Enter(previous);
return true;
}
}
return false;
}
private State getState(string name)
{
foreach(State state in states) {
if ( state.name == name ) {
return state;
}
}
return null;
}
private bool validNextState(string next)
{
return current.next_states.Contains(next);
}
}
}