EchoTrio
 
Loading...
Searching...
No Matches
FiniteStateMachine.cs
Go to the documentation of this file.
1namespace FSM {
2 /// Finite state machine class to handle state transitions and updates.
3 public class FiniteStateMachine {
4 public const int INVALID_STATE = -1; // Use negative value to denote an invalid state.
5
6 public readonly int NumStates = 0;
8 private int nextState = INVALID_STATE;
9
10 /// Define a delegate that takes in 0 arguments and returns void.
11 public delegate void FuncPtr();
12 /// The entry callback function of states.
13 private FuncPtr[] stateEntries = null;
14 /// The update callback function of states.
15 private FuncPtr[] stateUpdates = null;
16 /// The late update callback function of states.
17 private FuncPtr[] stateLateUpdates = null;
18 /// The exit callback function of states.
19 private FuncPtr[] stateExits = null;
20
21 public FiniteStateMachine(int numStates) {
22 // Initialise arrays to hold the function pointers to the entry, update, late update and exit callback functions of the states.
23 NumStates = numStates;
24 stateEntries = new FuncPtr[numStates];
25 stateUpdates = new FuncPtr[numStates];
26 stateLateUpdates = new FuncPtr[numStates];
27 stateExits = new FuncPtr[numStates];
28 }
29
30 public int GetCurrentState() { return currentState; }
31 public int GetNextState() { return nextState; }
32
33 public void SetStateEntry(int state, FuncPtr funcPtr = null) { stateEntries[state] = funcPtr; }
34 public void SetStateUpdate(int state, FuncPtr funcPtr = null) { stateUpdates[state] = funcPtr; }
35 public void SetStateLateUpdate(int state, FuncPtr funcPtr = null) { stateLateUpdates[state] = funcPtr; }
36 public void SetStateExit(int state, FuncPtr funcPtr = null) { stateExits[state] = funcPtr; }
37
38 /// Sets the next state to transit to.
39 /// <param name="nextState">The index of the next state.</param>
40 public void ChangeState(int nextState) { this.nextState = nextState; }
41
42 public void Update() {
43 // Check if the next state is different from the current state.
44 if (nextState != currentState) {
45 // Exit current state.
46 if (0 <= currentState) { stateExits[currentState]?.Invoke(); }
47
48 // Enter next state.
50 if (0 <= currentState) { stateEntries[currentState]?.Invoke(); }
51 }
52
53 // Update current state every frame.
54 if (0 <= currentState) { stateUpdates[currentState]?.Invoke(); }
55 }
56
57 public void LateUpdate() {
58 // Late update current state every frame.
59 if (0 <= currentState) { stateLateUpdates[currentState]?.Invoke(); }
60 }
61 }
62}
Finite state machine class to handle state transitions and updates.
void SetStateUpdate(int state, FuncPtr funcPtr=null)
delegate void FuncPtr()
Define a delegate that takes in 0 arguments and returns void.
void ChangeState(int nextState)
void SetStateExit(int state, FuncPtr funcPtr=null)
FuncPtr[] stateLateUpdates
The late update callback function of states.
FuncPtr[] stateUpdates
The update callback function of states.
FuncPtr[] stateExits
The exit callback function of states.
void SetStateEntry(int state, FuncPtr funcPtr=null)
FuncPtr[] stateEntries
The entry callback function of states.
void SetStateLateUpdate(int state, FuncPtr funcPtr=null)
FiniteStateMachine(int numStates)