-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateManager.hpp
52 lines (43 loc) · 1.26 KB
/
StateManager.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
#pragma once
#include <vector>
#include <unordered_map>
#include "State_Intro.hpp"
#include "State_MainMenu.hpp"
#include "State_Game.hpp"
#include "State_Paused.hpp"
#include "SharedContext.hpp"
enum class StateType{ Intro = 1, MainMenu, Game, Paused, GameOver, Credits };
// State container.
using StateContainer = std::vector<std::pair<StateType, BaseState*>>;
// Type container.
using TypeContainer = std::vector<StateType>;
// State factory.
using StateFactory = std::unordered_map<StateType, std::function<BaseState*(void)>>;
class StateManager{
public:
StateManager(SharedContext* l_shared);
~StateManager();
void Update(const sf::Time& l_time);
void Draw();
void ProcessRequests();
SharedContext* GetContext();
bool HasState(const StateType& l_type);
void SwitchTo(const StateType& l_type);
void Remove(const StateType& l_type);
private:
// Methods.
void CreateState(const StateType& l_type);
void RemoveState(const StateType& l_type);
template<class T>
void RegisterState(const StateType& l_type){
m_stateFactory[l_type] = [this]() -> BaseState*
{
return new T(this);
};
}
// Members.
SharedContext* m_shared;
StateContainer m_states;
TypeContainer m_toRemove;
StateFactory m_stateFactory;
};