-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStateMachine.cpp
More file actions
62 lines (46 loc) · 1.03 KB
/
StateMachine.cpp
File metadata and controls
62 lines (46 loc) · 1.03 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
61
62
#include "StateMachine.h"
#include "State.h"
#include <iostream>
#include <memory>
StateMachine::StateMachine()
: stateResume{ false }
, stateRunning{ false } {
}
void StateMachine::run(std::unique_ptr<State> state) {
stateRunning = true;
states.push(std::move(state));
}
void StateMachine::nextState() {
if (stateResume) {
//Cleanup current state
if (!states.empty()) { states.pop(); }
stateResume = false;
}
if (!states.empty()) {
std::unique_ptr<State> temp = states.top()->nextState();
if (temp != nullptr) {
if (temp->isReplacing()) {
states.pop();
}
states.push(std::move(temp));
}
}
}
void StateMachine::lastState() {
stateResume = true;
}
void StateMachine::updateEvents() {
states.top()->updateEvents();
}
void StateMachine::update() {
states.top()->update();
}
void StateMachine::render() {
states.top()->render();
}
bool StateMachine::running() {
return stateRunning;
}
void StateMachine::quit() {
stateRunning = false;
}