-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.hpp
75 lines (58 loc) · 1.75 KB
/
state.hpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef ARITHMETIX_STATE_HPP
#define ARITHMETIX_STATE_HPP
#include "sdlw.hpp"
#include "transition.hpp"
#include <map>
#include <memory>
#include <string>
class state {
public:
state() = default;
state(const state &) = delete;
state &operator=(const state &) = delete;
virtual ~state() {}
virtual void activate_state(const state_arg &) {}
virtual void deactivate_state() {}
virtual void on_event(const SDL_Event &) = 0;
virtual void on_draw(sdl_surface &) = 0;
};
class state_machine {
public:
state_machine() { m_current = m_next = m_states.end(); }
void add_state(const std::string &name, std::unique_ptr<state> s) {
m_states.emplace(name, std::move(s));
}
void set_next_state(const std::string &name) {
set_next_state(name, std::monostate());
}
void set_next_state(const std::string &name, const state_arg &args) {
// TODO: Verify that the state exists.
m_next = m_states.find(name);
m_next_args = args;
// If there's no active state, activate this right away
if (m_current == m_states.end()) {
m_current = m_next;
m_current->second->activate_state(m_next_args);
}
}
void send_event(const SDL_Event &e) {
if (m_current != m_states.end()) {
m_current->second->on_event(e);
}
if (m_current != m_next && m_next != m_states.end()) {
m_current->second->deactivate_state();
m_current = m_next;
m_current->second->activate_state(m_next_args);
}
}
void draw(sdl_surface &screen) {
if (m_current != m_states.end()) m_current->second->on_draw(screen);
}
private:
using state_map = std::map<std::string, std::unique_ptr<state>>;
state_map m_states;
state_map::iterator m_current;
state_map::iterator m_next;
state_arg m_next_args;
};
#endif