Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
NetworkSyncSystem.hpp
Go to the documentation of this file.
1
8#ifndef RTYPE_CLIENT_NETWORK_SYSTEM_HPP_
9 #define RTYPE_CLIENT_NETWORK_SYSTEM_HPP_
10
11 #include "RType/ECS/ISystem.hpp"
12 #include "RType/ECS/Registry.hpp"
14
20
21 #include "Game/EntityBuilder.hpp"
22
23 #include <deque>
24 #include <vector>
25 #include <iostream>
26 #include <list>
27 #include <string>
28
33namespace rtp::client {
34
40 {
41 public:
42 enum class State {
44 InLobby,
48 InRoom,
49 InGame
50 };
51 public:
57 NetworkSyncSystem(ClientNetwork& network, ecs::Registry& registry, EntityBuilder builder);
58
63 void update(float dt) override;
64
72 void tryLogin(const std::string& username, const std::string& password, uint8_t weaponKind) const;
73
80 void tryRegister(const std::string& username, const std::string& password) const;
81
86 void requestListRooms(void);
87
99 void tryCreateRoom(const std::string& roomName, uint32_t maxPlayers, float difficulty, float speed, uint32_t duration, uint32_t seed, uint32_t levelId, uint8_t roomType);
100
105 void tryJoinRoom(uint32_t roomId, bool asSpectator = false);
106
110 void tryLeaveRoom(void);
111
116 void trySetReady(bool isReady);
117
121 void tryStartSolo(void);
122
127 void trySendMessage(const std::string& message) const;
128
133 void sendSelectedWeapon(uint8_t weaponKind) const;
134
135 public:
140 bool isInRoom(void) const;
141
146 bool isReady(void) const;
147
152 bool isUdpReady(void) const;
153
158 bool isLoggedIn(void) const;
159
164 bool _isStartingSolo = false;
165
170 std::string getUsername(void) const;
171
173 std::string bestPlayer;
174 int bestScore{0};
176 };
177
178 void setGameOverSummary(const GameOverSummary& summary);
179 GameOverSummary getGameOverSummary(void) const;
180 bool consumeGameOver(void);
181 int getScore(void) const;
182 int getHealthCurrent(void) const;
183 int getHealthMax(void) const;
184
189 std::list<net::RoomInfo> getAvailableRooms(void) const;
190
195 bool isInGame(void) const;
196
201 State getState(void) const;
202
206 uint32_t getCurrentLevelId(void) const;
207
212 std::string getLastChatMessage(void) const;
213
218 const std::deque<std::string>& getChatHistory(void) const;
219
220 uint16_t getAmmoCurrent(void) const;
221 uint16_t getAmmoMax(void) const;
222 bool isReloading(void) const;
223 float getReloadCooldownRemaining(void) const;
224 uint32_t getPingMs(void) const;
225 bool consumeKicked(void);
226
227 private:
230 std::unordered_map<uint32_t, ecs::Entity> _netIdToEntity;
233 private:
234 bool _isInRoom = false;
235 bool _isReady = false;
236 bool _udpReady = false;
237 bool _isLoggedIn = false;
238 std::string _username;
239 std::list<net::RoomInfo> _availableRooms;
241 std::string _lastChatMessage;
242 std::deque<std::string> _chatHistory;
243 std::size_t _chatHistoryLimit = 8;
244 uint16_t _ammoCurrent = 0;
245 uint16_t _ammoMax = 100;
246 bool _ammoReloading = false;
247 float _ammoReloadRemaining = 0.0f;
248 uint32_t _pingMs = 0;
249 float _pingTimer = 0.0f;
250 float _pingInterval = 1.0f;
251 bool _kicked = false;
252 uint32_t _currentLevelId = 1;
254 bool _gameOverPending{false};
255 int _score{0};
257 int _healthMax{0};
259 private:
264 void handleEvent(net::NetworkEvent& event);
265
266 void onLoginResponse(net::Packet& packet);
267
268 void onRegisterResponse(net::Packet& packet);
269
270 void onRoomResponse(net::Packet& packet);
271
272 void onJoinRoomResponse(net::Packet& packet);
273
274 void onCreateRoomResponse(net::Packet& packet);
275
276 void onLeaveRoomResponse(net::Packet& packet);
277
279
280 void onEntityDeath(net::Packet& packet);
281
282 void onRoomUpdate(net::Packet& packet);
283
284 void onRoomChatReceived(net::Packet& packet);
285
286 void pushChatMessage(const std::string& message);
287
288 void onAmmoUpdate(net::Packet& packet);
289 void onBeamState(net::Packet& packet);
290 // Map ownerNetId -> list of (beamEntity, offsetY)
291 std::unordered_map<uint32_t, std::vector<std::pair<ecs::Entity, float>>> _beamEntities;
292 // Map ownerNetId -> list of beam lengths (parallel to _beamEntities entries)
293 std::unordered_map<uint32_t, std::vector<float>> _beamLengths;
294
295 void onPong(net::Packet& packet);
296
297 void onDebugModeUpdate(net::Packet& packet);
298 };
299}
300
301#endif /* !RTYPE_CLIENT_NETWORK_SYSTEM_HPP_ */
Interface for ECS systems.
Network packet implementation for R-Type protocol.
System to handle network-related operations on the client side.
bool _isLoggedIn
Flag indicating if the client is logged in.
void onSpawnEntityFromServer(net::Packet &packet)
std::deque< std::string > _chatHistory
Recent chat messages.
State getState(void) const
Get the current state of the client.
void tryRegister(const std::string &username, const std::string &password) const
Send a request to server attempting to register with provided credentials.
bool isInGame(void) const
Check if the client is in game.
void onEntityDeath(net::Packet &packet)
void trySetReady(bool isReady)
Send a request to set the client's ready status in the current room.
void onLeaveRoomResponse(net::Packet &packet)
void onRoomResponse(net::Packet &packet)
bool isInRoom(void) const
Check if the client is in a room.
ecs::Registry & _registry
Reference to the entity registry.
std::unordered_map< uint32_t, ecs::Entity > _netIdToEntity
Map of network IDs to entities.
void onDebugModeUpdate(net::Packet &packet)
void setGameOverSummary(const GameOverSummary &summary)
void pushChatMessage(const std::string &message)
float _ammoReloadRemaining
Reload time remaining.
bool _isReady
Flag indicating if the client is ready.
bool isReady(void) const
Check if the client is ready.
bool _isInRoom
Flag indicating if the client is in a room.
void onBeamState(net::Packet &packet)
std::list< net::RoomInfo > _availableRooms
List of available rooms from the server.
uint32_t _currentLevelId
Current level id used by client visuals.
float _pingTimer
Ping timer accumulator.
void onPong(net::Packet &packet)
bool _udpReady
Flag indicating if UDP is ready.
void onRegisterResponse(net::Packet &packet)
void tryLogin(const std::string &username, const std::string &password, uint8_t weaponKind) const
Send a request to server attempting to log in with provided credentials.
bool isLoggedIn(void) const
Check if the client is logged in.
float _pingInterval
Ping interval in seconds.
void tryJoinRoom(uint32_t roomId, bool asSpectator=false)
Send a request to join an existing room on the server.
void tryLeaveRoom(void)
Send a request to leave the current room on the server.
void onAmmoUpdate(net::Packet &packet)
void tryStartSolo(void)
Start a solo game by creating a private room and auto-joining/readying.
std::unordered_map< uint32_t, std::vector< std::pair< ecs::Entity, float > > > _beamEntities
std::unordered_map< uint32_t, std::vector< float > > _beamLengths
bool _gameOverPending
Game over pending flag.
void onJoinRoomResponse(net::Packet &packet)
EntityBuilder _builder
Entity builder for spawning entities.
void handleEvent(net::NetworkEvent &event)
Handle a network event.
std::size_t _chatHistoryLimit
Max chat messages to keep.
void onCreateRoomResponse(net::Packet &packet)
uint32_t getCurrentLevelId(void) const
Get the current level ID for client-side visuals (parallax)
bool isUdpReady(void) const
Check if UDP is ready.
void onRoomChatReceived(net::Packet &packet)
void tryCreateRoom(const std::string &roomName, uint32_t maxPlayers, float difficulty, float speed, uint32_t duration, uint32_t seed, uint32_t levelId, uint8_t roomType)
Send a request to create a new room on the server.
void trySendMessage(const std::string &message) const
Send a chat message to the server.
void requestListRooms(void)
Send a request to have the list of available rooms from the server.
std::string _username
The username of the client.
void update(float dt) override
Update system logic for one frame.
ClientNetwork & _network
Reference to the client network manager.
bool _isStartingSolo
Check if the client is starting a solo game.
GameOverSummary _gameOverSummary
Cached game over summary.
std::string getLastChatMessage(void) const
Get the last received room chat message.
std::string getUsername(void) const
Get the username of the client.
void onLoginResponse(net::Packet &packet)
std::string _lastChatMessage
Last received chat message.
const std::deque< std::string > & getChatHistory(void) const
Get chat history buffer (most recent last)
void onRoomUpdate(net::Packet &packet)
void sendSelectedWeapon(uint8_t weaponKind) const
Send currently selected weapon to server to apply immediately.
GameOverSummary getGameOverSummary(void) const
State _currentState
Current state of the client.
std::list< net::RoomInfo > getAvailableRooms(void) const
Get a list of available rooms from the server.
uint32_t _pingMs
Latest ping in ms.
Abstract base class for all ECS systems.
Definition ISystem.hpp:57
Network packet with header and serializable body.
Definition Packet.hpp:471
R-Type client namespace.
Represents a network event containing session ID and packet data.
Definition INetwork.hpp:34