-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachine.cpp
More file actions
60 lines (48 loc) · 1.55 KB
/
StateMachine.cpp
File metadata and controls
60 lines (48 loc) · 1.55 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
56
57
58
59
60
#include "StateMachine.hpp"
namespace engine{
//AddState function for adding a state to the StateMachine.
void StateMachine::AddState(StateRef newState, bool IsReplacing) {
_isAdding=true;
_isReplacing = IsReplacing;
_newState = std::move(newState);
}
//RemoveState function for removing the top state on ProcessStateChanges.
void StateMachine::RemoveState() {
_isRemoving = true;
}
//ProcessStateChanges function for updating the StateMachine.
void StateMachine::ProcessStateChanges() {
if(_isRemoving && !_states.empty()){
_states.pop();
if(!_states.empty()){
_states.top()->Resume();
}
_isRemoving = false;
}
if(_isAdding){
if(!_states.empty()){
if(_isReplacing){
_states.pop();
}
else{
_states.top()->Pause();
}
}
_states.push(std::move(_newState));
_states.top()->Init();
_isAdding = false;
}
}
//GetActiveState function for returning the top state.
StateRef& StateMachine::GetActiveState() {
return _states.top();
}
//clean_states function for cleaning the stateMachine to only the top state.
void StateMachine::clean_states() {
if(!_states.empty()){
StateRef ns = std::move(_states.top());
_states = std::stack<StateRef>{};
_states.push(std::move(ns));
}
}
}