R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
GameStateManager.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by hugo on 06/12/2025
4** File description:
5** GameStateManager.cpp
6*/
7
12
13namespace server {
14
16 if (_currentStateID == stateID) {
17 return;
18 }
19
20 if (_currentStateID >= 0 && _currentStateID < static_cast<int>(_states.size()) &&
22 _states[_currentStateID]->exit();
23 }
24
25 _currentStateID = stateID;
26
27 // Enter new state
28 if (_currentStateID >= 0 && _currentStateID < static_cast<int>(_states.size())) {
30 _states[_currentStateID]->enter();
31 LOG_INFO("✓ Changed to state ", _currentStateID);
32 // Note: GameEndedEvent is now published by GameLogic with the correct reason
33 }
34 } else {
35 LOG_ERROR("Invalid state ID: ", stateID);
36 }
37 }
38
42
43 void GameStateManager::registerState(int stateID, std::shared_ptr<GameState> state) {
44 // Ensure vector is large enough
45 if (stateID >= static_cast<int>(_states.size())) {
46 _states.resize(stateID + 1);
47 }
48 _states[stateID] = state;
49 }
50
51 void GameStateManager::update(float dt) {
52 if (_currentStateID >= 0 && _currentStateID < static_cast<int>(_states.size()) &&
54 _states[_currentStateID]->update(dt);
55 }
56 }
57
58 void GameStateManager::setEventBus(std::shared_ptr<EventBus> eventBus) {
59 _eventBus = eventBus;
60 }
61
62} // namespace server
#define LOG_INFO(...)
Definition Logger.hpp:181
#define LOG_ERROR(...)
Definition Logger.hpp:183
int getCurrentState() const override
std::vector< std::shared_ptr< GameState > > _states
void registerState(int stateID, std::shared_ptr< GameState > state)
Register a game state.
std::shared_ptr< EventBus > _eventBus
void changeState(int stateID) override
void setEventBus(std::shared_ptr< EventBus > eventBus)
Set EventBus for publishing state change events.
void update(float dt)
Update current state.