-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.cpp
43 lines (35 loc) · 1013 Bytes
/
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
#include "StateMachine.hpp"
namespace Checkers {
void StateMachine::addState(StateRef newState, bool isReplacing) {
_isAdding = true;
_isReplacing = isReplacing;
_newState = std::move(newState);
}
void StateMachine::removeState() {
_isRemoving = true;
}
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;
}
}
StateRef& StateMachine::getActiveState() {
return _states.top();
}
}