-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateMachine.h
62 lines (55 loc) · 1.15 KB
/
StateMachine.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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include"State.h"
#include<cassert>
template<typename Entity_type>
class StateMachine
{
private:
Entity_type &entity;
State<Entity_type> *cur_state;
State<Entity_type> *pre_state;
State<Entity_type> *glo_state;
public:
StateMachine(Entity_type &_entity): entity(_entity),
cur_state(nullptr),
pre_state(nullptr),
glo_state(nullptr)
{}
void InitState(State<Entity_type> *_cur, State<Entity_type> *_pre, State<Entity_type> *_glo)
{
cur_state = _cur;
pre_state = _pre;
glo_state = _glo;
}
void excute()
{
assert(cur_state != nullptr);
cur_state->excute(&entity);
assert(glo_state != nullptr);
glo_state->excute(&entity);
}
void change_state(State<Entity_type> *state)
{
assert(cur_state != nullptr);
cur_state->exit(&entity);
pre_state = cur_state;
cur_state = state;
assert(state != nullptr);
cur_state->enter(&entity);
}
void recover_pre_state()
{
change_state(pre_state);
}
State<Entity_type> * get_curent_state() const
{
return cur_state;
}
State<Entity_type> * get_global_state() const
{
return glo_state;
}
State<Entity_type> * get_previous_state() const
{
return pre_state;
}
};