R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
GameLoop.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by samuelBleau on 26/11/2025.
4** File description:
5** GameLoop.hpp
6*/
7
8#ifndef GAMELOOP_HPP
9#define GAMELOOP_HPP
10
11#include <raylib.h>
12#include <chrono>
13#include <deque>
14#include <iostream>
15#include <memory>
16#include <optional>
17#include <unordered_set>
18#include "../common/Logger/Logger.hpp"
21#include "Core/EventBus/EventBus.hpp"
22#include "Events/NetworkEvent/NetworkEvent.hpp"
23#include "Events/UIEvent.hpp"
24#include "Input/InputBuffer.hpp"
27
29
57class GameLoop {
58 public:
69 GameLoop(EventBus &eventBus, Replicator &replicator, const std::string &playerName);
70
76 ~GameLoop();
77
94 bool initialize();
95
127 void run();
128
141 void shutdown();
142
152 void stop();
153
172 void setReconciliationThreshold(float threshold);
173
180 float getReconciliationThreshold() const;
181
182 private:
190 void update(float deltaTime);
191
199 void fixedUpdate(float fixedDeltaTime);
200
206 void render();
207
213 void processInput();
214
220 float calculateDeltaTime();
221
229 void handleNetworkMessage(const NetworkEvent &event);
230 void handleUIEvent(const UIEvent &event);
231
232 // Network message handlers
233 void handleGameStart(const std::vector<uint8_t> &payload);
234 void handleGameState(const std::vector<uint8_t> &payload);
235 void handleGameruleUpdate(const std::vector<uint8_t> &payload);
236 void handleRoomList(const std::vector<uint8_t> &payload);
237 void handleRoomState(const std::vector<uint8_t> &payload);
238 void handleEntityDestroyed(const std::vector<uint8_t> &payload);
239 void handleChatMessage(const std::vector<uint8_t> &payload);
240 void handleLeftRoom(const std::vector<uint8_t> &payload);
241 void handleGameOver(const std::vector<uint8_t> &payload);
242
243 // Helpers
245 void simulateInputHistory(float &x, float &y);
246
247 EventBus *_eventBus; // Non-owning pointer (owned by Client)
248 std::unique_ptr<InputBuffer> _inputBuffer;
249 Replicator *_replicator; // Non-owning pointer (owned by Client)
250 std::unique_ptr<Rendering> _rendering;
251
252 bool _running = false;
253 bool _initialized = false;
254
255 float _fixedTimestep = 1.0f / 60.0f;
256 float _accumulator = 0.0f;
257 uint32_t _currentFrame = 0;
258
259 // Input tracking
260 uint32_t _inputSequenceId = 0;
261
262 // Input redundancy history
263 static constexpr size_t INPUT_HISTORY_SIZE = 12; // Send last 12 inputs (~200ms)
264 std::deque<RType::Messages::C2S::PlayerInput::InputSnapshot> _inputHistory;
265
266 // Player entity tracking
267 std::optional<uint32_t> _myEntityId; // Local player's entity ID (std::nullopt if not yet assigned)
268 uint32_t _myPlayerId = 0; // Local player's ID assigned by server
269 bool _entityInitialized = false;
271 false; // Flag to track if we just created the current room // True after first server update received
272 std::string _playerName; // Local player's display name (for host detection)
273 bool _isMoving = false; // True when player is actively moving
274 float _playerSpeed = 300.0f; // pixels per second (MUST MATCH SERVER!)
275 bool _clientSidePredictionEnabled = true; // Client-side prediction for smooth movement
276 float _gameSpeedMultiplier = 1.0f; // Game speed multiplier from server
277
278 // Entity tracking for cleanup
279 std::unordered_set<uint32_t> _knownEntityIds; // Track all entities we've seen for proper cleanup
280};
281
282#endif
GameScene
Definition GameLoop.hpp:28
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
Main game loop orchestrating all subsystems.
Definition GameLoop.hpp:57
std::unique_ptr< Rendering > _rendering
Definition GameLoop.hpp:250
EventBus * _eventBus
Definition GameLoop.hpp:247
float _playerSpeed
Definition GameLoop.hpp:274
void processInput()
Process player inputs.
Definition GameLoop.cpp:362
static constexpr size_t INPUT_HISTORY_SIZE
Definition GameLoop.hpp:263
void handleEntityDestroyed(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:654
uint32_t _currentFrame
Definition GameLoop.hpp:257
void handleLeftRoom(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:822
void handleNetworkMessage(const NetworkEvent &event)
Handle incoming network messages.
Definition GameLoop.cpp:492
void handleGameruleUpdate(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:777
bool _clientSidePredictionEnabled
Definition GameLoop.hpp:275
uint32_t _inputSequenceId
Definition GameLoop.hpp:260
std::unique_ptr< InputBuffer > _inputBuffer
Definition GameLoop.hpp:248
float _gameSpeedMultiplier
Definition GameLoop.hpp:276
void update(float deltaTime)
Update game logic (variable timestep)
Definition GameLoop.cpp:298
bool _entityInitialized
Definition GameLoop.hpp:269
void handleGameState(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:678
void handleGameOver(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:855
bool _initialized
Definition GameLoop.hpp:253
float _fixedTimestep
Definition GameLoop.hpp:255
Replicator * _replicator
Definition GameLoop.hpp:249
std::deque< RType::Messages::C2S::PlayerInput::InputSnapshot > _inputHistory
Definition GameLoop.hpp:264
void run()
Start the main game loop.
Definition GameLoop.cpp:161
void setReconciliationThreshold(float threshold)
Set the reconciliation threshold for client-side prediction.
Definition GameLoop.cpp:284
std::string _playerName
Definition GameLoop.hpp:272
bool _isMoving
Definition GameLoop.hpp:273
void handleRoomState(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:606
float getReconciliationThreshold() const
Get the current reconciliation threshold.
Definition GameLoop.cpp:291
void simulateInputHistory(float &x, float &y)
Definition GameLoop.cpp:742
bool _justCreatedRoom
Definition GameLoop.hpp:270
void handleRoomList(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:575
void processServerReconciliation(const RType::Messages::S2C::EntityState &entity)
Definition GameLoop.cpp:723
bool initialize()
Initialize all game subsystems.
Definition GameLoop.cpp:22
float _accumulator
Definition GameLoop.hpp:256
void shutdown()
Stop and clean up all subsystems.
Definition GameLoop.cpp:258
bool _running
Definition GameLoop.hpp:252
void handleGameStart(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:529
void handleUIEvent(const UIEvent &event)
Definition GameLoop.cpp:51
void render()
Perform rendering of current frame.
Definition GameLoop.cpp:347
std::optional< uint32_t > _myEntityId
Definition GameLoop.hpp:267
std::unordered_set< uint32_t > _knownEntityIds
Definition GameLoop.hpp:279
float calculateDeltaTime()
Calculate time elapsed since last frame.
Definition GameLoop.cpp:482
uint32_t _myPlayerId
Definition GameLoop.hpp:268
~GameLoop()
Destructor.
Definition GameLoop.cpp:18
void stop()
Stop the game loop.
Definition GameLoop.cpp:279
void fixedUpdate(float fixedDeltaTime)
Update physics simulation (fixed timestep)
Definition GameLoop.cpp:338
void handleChatMessage(const std::vector< uint8_t > &payload)
Definition GameLoop.cpp:806
Event representing a network message.
State of a single entity.
Client-server network replication manager with dedicated network thread.