-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.cpp
executable file
·56 lines (47 loc) · 1.13 KB
/
StateMachine.cpp
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
#include "StateMachine.hpp"
//StateMachine() {};
//~StateMachine() {};
void StateMachine::pushState(State* stateRef, bool isChanging)
{
this->m_isAdding = true;
this->m_isChanging = isChanging;
this->m_newState = stateRef;
}
void StateMachine::popState()
{
this->m_isDeleting = true;
}
void StateMachine::applyPendingChanges()
{
if(this->m_isDeleting && !(this->m_stateStack.empty()))
{
this->m_stateStack.pop();
if(!(this->m_stateStack.empty()))
{
//Resume previously stopped state
this->m_stateStack.top()->resume();
}
this->m_isDeleting = false;
}
if(this->m_isAdding)
{
if(!(this-> m_stateStack.empty()))
{
if(this->m_isChanging)
{
this->m_stateStack.pop();
}
else
{
this->m_stateStack.top()->stop();
}
}
this->m_stateStack.push(this->m_newState);
this->m_stateStack.top()->init();
this->m_isAdding = false;
}
}
State* StateMachine::getTopState()
{
return this->m_stateStack.top();
}