-
Notifications
You must be signed in to change notification settings - Fork 0
/
finite_state_machine.h
49 lines (37 loc) · 1.13 KB
/
finite_state_machine.h
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
#ifndef FINITE_STATE_MACHINE_H
#define FINITE_STATE_MACHINE_H
typedef void (*stateFunctions_t)();
class State {
public:
// constructors
State(stateFunctions_t updateFunction); // for a state woth only update
State(int id, stateFunctions_t updateFunction); // same as above but with id
State(int id, stateFunctions_t enterFunction,
stateFunctions_t updateFunction,
stateFunctions_t exitFnction);
void init(int id, stateFunctions_t enterFunction,
stateFunctions_t updateFunction,
stateFunctions_t exitFunction);
int id(); // id acessor
void enter();
void update();
void exit();
private:
int _id;
stateFunctions_t _enterFunction;
stateFunctions_t _updateFunction;
stateFunctions_t _exitFunction;
};
// FINITE STATE MACHINE
class FiniteStateMachine {
public:
FiniteStateMachine(State* current);
void update();
void transitionTo(State* state);
void immediateTransitionTo(State* state);
State* getCurrentState();
private:
State* _currentState; // pointer to the current state
State* _nextState; // poiter to the next state
};
#endif