Pacman Torben Petré
Loading...
Searching...
No Matches
StateManager.h
Go to the documentation of this file.
1#ifndef STATEMANAGER_H
2#define STATEMANAGER_H
3
4#include <SFML/Window/Event.hpp>
5#include <stack>
6
7#include "../../Logic/Score.h"
8#include "../SoundManager.h"
9
10
11class StateManager;
12
13class State {
14public:
20 virtual void handleInput(const sf::Event::KeyEvent &keyPressed) = 0;
21
25 virtual void resized() = 0;
26
32 virtual void update(double dt) = 0;
33
40 virtual void render() = 0;
41
42 virtual ~State() = default;
43
44protected:
52 explicit State(StateManager& ctx);
53
55};
56
57
66 std::shared_ptr<logic::Score> scoreSystem;
67 std::shared_ptr<SoundManager> soundManager;
68 std::string username;
69 unsigned int lives;
70
72 const std::shared_ptr<logic::Score>& scoreSystem,
73 const std::shared_ptr<SoundManager>& soundManager,
74 const unsigned int lives
76};
77
78
97public:
98 explicit StateManager();
99
105 [[nodiscard]] State& top();
106
111 [[nodiscard]] bool empty() const;
112
117 [[nodiscard]] GameContext& getGameContext() const;
118
119
130 void swap(const std::shared_ptr<State>& state);
131
136 void push(const std::shared_ptr<State>& state);
137
143 void pop();
144
150 void clear(const std::shared_ptr<State>& state);
151
152
156 void executeCommand();
157
158private:
159 std::unique_ptr<GameContext> gameContext;
160
161 std::stack<std::shared_ptr<State>> states;
162
163 enum Command { NONE, SWAP, PUSH, POP, CLEAR };
164 struct PendingCommand {
165 Command command = Command::NONE;
166 std::shared_ptr<State> state = nullptr;
167 };
168
169 PendingCommand pendingCommand;
170};
171
172#endif //STATEMANAGER_H
Definition StateManager.h:13
virtual void handleInput(const sf::Event::KeyEvent &keyPressed)=0
Handler for KeyPressed events.
State(StateManager &ctx)
Creates the base State object.
Definition StateManager.cpp:4
virtual void render()=0
Render function that is called in the main Game loop.
virtual ~State()=default
virtual void resized()=0
Called when the window is resized.
StateManager & context
Definition StateManager.h:54
virtual void update(double dt)=0
Definition StateManager.h:96
State & top()
Returns the top of the manager stack.
Definition StateManager.cpp:16
StateManager()
Definition StateManager.cpp:7
void push(const std::shared_ptr< State > &state)
Pushes the state to the stack. Does this by updating pending command.
Definition StateManager.cpp:36
void executeCommand()
Applies any pending command, if there is any pending command.
Definition StateManager.cpp:50
GameContext & getGameContext() const
Returns the GameContext struct.
Definition StateManager.cpp:26
void swap(const std::shared_ptr< State > &state)
Helper function for swapping the top state with a new state. Does this by updating the pending comman...
Definition StateManager.cpp:31
void clear(const std::shared_ptr< State > &state)
Clears the state stack and pushes the new state. Does this by updating pending command.
Definition StateManager.cpp:45
void pop()
Pops the top state from the stack. Does this by updating pending command.
Definition StateManager.cpp:40
bool empty() const
Returns whether the manager stack is empty.
Definition StateManager.cpp:21
Definition StateManager.h:65
std::string username
Definition StateManager.h:68
unsigned int lives
Definition StateManager.h:69
std::shared_ptr< logic::Score > scoreSystem
Definition StateManager.h:66
std::shared_ptr< SoundManager > soundManager
Definition StateManager.h:67
GameContext(const std::shared_ptr< logic::Score > &scoreSystem, const std::shared_ptr< SoundManager > &soundManager, const unsigned int lives)
Definition StateManager.h:71