r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
WaitingRoom.hpp
Go to the documentation of this file.
1///
2/// @file WaitingRoom.hpp
3/// @brief Waiting room scene for R-Type multiplayer lobby system
4/// @details This file contains the waiting room scene where players gather before a game starts.
5/// Players can see other lobby members, wait for the host to start the game,
6/// and leave the lobby if desired. The scene displays real-time lobby updates
7/// and provides controls for lobby management.
8/// @namespace gme
9/// @author R-Type Team
10/// @date 2025
11///
12
13#pragma once
14
15#include <functional>
16#include <vector>
17
20
21namespace gme
22{
23 ///
24 /// @struct InterpolationData
25 /// @brief Data structure for smooth entity interpolation (forward declaration)
26 ///
27
28 ///
29 /// @class WaitingRoomScene
30 /// @brief Pre-game lobby scene where players wait before match starts
31 /// @details This scene provides:
32 /// - Real-time display of lobby information (name, player count, status)
33 /// - List of connected players in the lobby
34 /// - Host controls (start game button)
35 /// - Player controls (ready button, leave button)
36 /// - Animated UI elements for visual feedback
37 /// - Event bus integration for network updates
38 ///
39 /// The scene automatically updates when lobby state changes (players join/leave)
40 /// and transitions to the game scene when the host starts the game.
41 ///
42 /// @namespace gme
43 ///
44 class WaitingRoomScene final : public eng::AScene
45 {
46 public:
47 ///
48 /// @brief Constructor
49 /// @param assignedId Unique scene identifier
50 /// @param renderer Shared pointer to the rendering interface
51 /// @details Initializes the waiting room UI and sets up event subscriptions
52 ///
53 WaitingRoomScene(eng::id assignedId, const std::shared_ptr<eng::IRenderer> &renderer);
54
55 ///
56 /// @brief Destructor
57 ///
58 ~WaitingRoomScene() override = default;
59
60 ///
61 /// @brief Deleted copy constructor (non-copyable)
62 ///
63 WaitingRoomScene(const WaitingRoomScene &other) = delete;
64
65 ///
66 /// @brief Deleted copy assignment operator (non-copyable)
67 ///
69
70 ///
71 /// @brief Deleted move constructor (non-movable)
72 ///
74
75 ///
76 /// @brief Deleted move assignment operator (non-movable)
77 ///
79
80 ///
81 /// @brief Update the waiting room scene (called each frame)
82 /// @param dt Delta time since last frame (in seconds)
83 /// @param size Current window dimensions
84 /// @details Updates animations, processes event bus messages, and refreshes UI
85 ///
86 void update(float dt, const eng::WindowSize &size) override;
87
88 ///
89 /// @brief Handle input events
90 /// @param event Input event (keyboard, mouse)
91 /// @details Handles navigation between buttons and activation of controls
92 ///
93 void event(const eng::Event &event) override;
94
95 ///
96 /// @brief Set the lobby ID
97 /// @param lobbyId Unique lobby identifier
98 ///
99 void setLobbyId(std::uint32_t lobbyId);
100
101 ///
102 /// @brief Update lobby information
103 /// @param lobbyInfo Current lobby state from server
104 /// @details Updates the displayed lobby information and player list
105 ///
106 void setLobbyInfo(const rnp::LobbyInfo &lobbyInfo);
107
108 ///
109 /// @brief Set whether local player is the lobby host
110 /// @param isHost True if local player is host
111 ///
112 void setIsHost(const bool isHost) { m_isHost = isHost; }
113
114 ///
115 /// @brief Check if local player is the lobby host
116 /// @return True if local player is host
117 ///
118 [[nodiscard]] bool isHost() const { return m_isHost; }
119
120 ///
121 /// @brief Get current lobby ID
122 /// @return Lobby identifier
123 ///
124 std::uint32_t getLobbyId() const { return m_lobbyId; };
125
126 ///
127 /// @brief Get reference to music playback flag
128 /// @return Reference to music play state
129 ///
130 bool &playMusic() { return m_playMusic; }
131
132 ///
133 /// @brief Callback invoked when player leaves lobby
134 ///
135 std::function<void()> onLeaveLobby;
136
137 ///
138 /// @brief Callback invoked when game starts
139 ///
140 std::function<void()> onGameStart;
141
142 ///
143 /// @brief Initiate game start sequence
144 /// @details Called when host presses start button
145 ///
146 void startGame();
147
148 private:
149 const std::shared_ptr<eng::IRenderer> &m_renderer; ///< Renderer interface reference
150
151 std::uint32_t m_lobbyId = 0; ///< Current lobby ID
152 rnp::LobbyInfo m_currentLobbyInfo{}; ///< Current lobby state
153 bool m_hasLobbyInfo = false; ///< Whether lobby info has been received
154 float m_animationTime = 0.0f; ///< Timer for UI animations
155
156 int m_selectedButton = 0; ///< Currently selected button index
157 static constexpr int BUTTON_LEAVE = 0; ///< Leave lobby button index
158 static constexpr int BUTTON_READY = 1; ///< Ready button index
159 static constexpr int BUTTON_START = 2; ///< Start game button index (host only)
160 static constexpr int BUTTON_COUNT = 3; ///< Total number of buttons
161
162 // UI Entity IDs
163 ecs::Entity m_lobbyIdEntity = 0; ///< Entity displaying lobby ID
164 ecs::Entity m_playerCountEntity = 0; ///< Entity displaying player count
165 ecs::Entity m_statusEntity = 0; ///< Entity displaying lobby status
166 ecs::Entity m_leaveButtonEntity = 0; ///< Leave button entity
167 ecs::Entity m_startButtonEntity = 0; ///< Start game button entity (host only)
168 ecs::Entity m_readyButtonEntity = 0; ///< Ready button entity
169 std::vector<ecs::Entity> m_playerEntities; ///< Entities for player name displays
170
171 bool m_isHost = false; ///< Whether local player is lobby host
172 bool m_playMusic = false; ///< Music playback state
173
174 ///
175 /// @brief Subscribe to event bus events
176 /// @details Registers for LOBBY_UPDATE and GAME_START events
177 ///
178 void setupEventSubscriptions() const;
179
180 ///
181 /// @brief Process events from event bus
182 /// @details Polls and handles network events
183 ///
184 void processEventBus();
185
186 ///
187 /// @brief Handle lobby update event from server
188 /// @param event Event containing updated lobby information
189 ///
191
192 ///
193 /// @brief Handle game start event from server
194 /// @param event Event indicating game is starting
195 ///
196 void handleGameStart(const utl::Event &event) const;
197
198 ///
199 /// @brief Update the player list display
200 /// @details Recreates player name entities based on current lobby info
201 ///
202 void updatePlayerDisplay();
203
204 ///
205 /// @brief Clear all player display entities
206 ///
207 void clearPlayerEntities();
208
209 ///
210 /// @brief Send leave lobby request to server
211 ///
212 void leaveLobby() const;
213
214 }; // class WaitingRoomScene
215} // namespace gme
This file contains the network protocol.
Abstract class for scene.
Definition IScene.hpp:52
Pre-game lobby scene where players wait before match starts.
void event(const eng::Event &event) override
Handle input events.
bool m_hasLobbyInfo
Whether lobby info has been received.
void processEventBus()
Process events from event bus.
bool isHost() const
Check if local player is the lobby host.
void startGame()
Initiate game start sequence.
int m_selectedButton
Currently selected button index.
static constexpr int BUTTON_START
Start game button index (host only)
void setIsHost(const bool isHost)
Set whether local player is the lobby host.
std::uint32_t m_lobbyId
Current lobby ID.
void handleLobbyUpdate(const utl::Event &event)
Handle lobby update event from server.
void leaveLobby() const
Send leave lobby request to server.
bool m_playMusic
Music playback state.
void setupEventSubscriptions() const
Subscribe to event bus events.
~WaitingRoomScene() override=default
Destructor.
bool m_isHost
Whether local player is lobby host.
static constexpr int BUTTON_LEAVE
Leave lobby button index.
void setLobbyInfo(const rnp::LobbyInfo &lobbyInfo)
Update lobby information.
static constexpr int BUTTON_COUNT
Total number of buttons.
WaitingRoomScene(const WaitingRoomScene &other)=delete
Deleted copy constructor (non-copyable)
WaitingRoomScene(WaitingRoomScene &&other)=delete
Deleted move constructor (non-movable)
void update(float dt, const eng::WindowSize &size) override
Update the waiting room scene (called each frame)
std::function< void()> onGameStart
Callback invoked when game starts.
WaitingRoomScene & operator=(const WaitingRoomScene &other)=delete
Deleted copy assignment operator (non-copyable)
ecs::Entity m_readyButtonEntity
Ready button entity.
void handleGameStart(const utl::Event &event) const
Handle game start event from server.
WaitingRoomScene & operator=(WaitingRoomScene &&other)=delete
Deleted move assignment operator (non-movable)
ecs::Entity m_leaveButtonEntity
Leave button entity.
ecs::Entity m_playerCountEntity
Entity displaying player count.
void updatePlayerDisplay()
Update the player list display.
void setLobbyId(std::uint32_t lobbyId)
Set the lobby ID.
WaitingRoomScene(eng::id assignedId, const std::shared_ptr< eng::IRenderer > &renderer)
Constructor.
std::vector< ecs::Entity > m_playerEntities
Entities for player name displays.
ecs::Entity m_lobbyIdEntity
Entity displaying lobby ID.
std::function< void()> onLeaveLobby
Callback invoked when player leaves lobby.
float m_animationTime
Timer for UI animations.
ecs::Entity m_startButtonEntity
Start game button entity (host only)
static constexpr int BUTTON_READY
Ready button index.
void clearPlayerEntities()
Clear all player display entities.
std::uint32_t getLobbyId() const
Get current lobby ID.
const std::shared_ptr< eng::IRenderer > & m_renderer
Renderer interface reference.
rnp::LobbyInfo m_currentLobbyInfo
Current lobby state.
bool & playMusic()
Get reference to music playback flag.
ecs::Entity m_statusEntity
Entity displaying lobby status.
Event structure for inter-component communication.
Definition Event.hpp:89
This file contains the IScene class.
std::uint32_t Entity
Definition Entity.hpp:13
unsigned int id
Definition IScene.hpp:20
Lobby information structure.
Definition Protocol.hpp:237