R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
GameLogic.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** GameLogic.hpp - Server-side game logic implementation
6*/
7
8#pragma once
9
10#include <atomic>
11#include <deque>
12#include <memory>
13#include <mutex>
14#include <unordered_map>
15#include <vector>
20
21namespace scripting {
22 class LuaEngine;
23 class LuaSystemAdapter;
24} // namespace scripting
25
26namespace ecs {
27 class ISystem;
28}
29
30namespace server {
31 class ThreadPool;
32 class EventBus;
33
57 class GameLogic : public IGameLogic {
58 public:
65 explicit GameLogic(std::shared_ptr<ecs::wrapper::ECSWorld> world = nullptr,
66 std::shared_ptr<ThreadPool> threadPool = nullptr,
67 std::shared_ptr<EventBus> eventBus = nullptr);
68
69 ~GameLogic() override;
70
71 bool initialize() override;
72 void update(float deltaTime, uint32_t currentTick) override;
73 uint32_t spawnPlayer(uint32_t playerId, const std::string &playerName) override;
74 void despawnPlayer(uint32_t playerId) override;
75 void processPlayerInput(uint32_t playerId, int inputX, int inputY, bool isShooting,
76 uint32_t sequenceId) override;
77
78 uint32_t getLastProcessedInput(uint32_t playerId) const override {
79 std::lock_guard<std::mutex> lock(_inputMutex);
80 auto it = _lastAppliedSequenceId.find(playerId);
81 return (it != _lastAppliedSequenceId.end()) ? it->second : 0;
82 }
83
84 ecs::Registry &getRegistry() override { return _world->getRegistry(); }
85 bool isGameActive() const override { return _gameActive; }
86 void resetGame() override;
91 void onGameStart();
92
97 std::shared_ptr<ecs::wrapper::ECSWorld> getECSWorld() { return _world; }
98
103 std::shared_ptr<GameStateManager> getStateManager() { return _stateManager; }
104
110
115 const GameRules &getGameRules() const override { return _gameRules; }
116
122 bool loadMap(const std::string &mapFilePath);
123
124 private:
129 void _executeSystems(float deltaTime);
130
134 void _processInput();
135
140
145
146 // ECS World
147 std::shared_ptr<ecs::wrapper::ECSWorld> _world;
148
149 // Lua scripting
150 std::unique_ptr<scripting::LuaEngine> _luaEngine;
151
152 // Player management
153 std::unordered_map<uint32_t, ecs::Address> _playerMap; // playerId -> entityAddress
154
155 // Input queue
156 struct PlayerInput {
157 uint32_t playerId;
161 uint32_t sequenceId;
162 };
163
169 void _applyPlayerInput(uint32_t playerId, const PlayerInput &input);
170
171 // Per-player input queue (FIFO)
172 // Use deque for efficient front removal
173 std::unordered_map<uint32_t, std::deque<PlayerInput>> _pendingInput;
174
175 // Last processed input sequence ID per player (for redundancy)
176 std::unordered_map<uint32_t, uint32_t> _lastReceivedSequenceId;
177 std::unordered_map<uint32_t, uint32_t> _lastAppliedSequenceId;
178
179 // Game state
180 std::shared_ptr<GameStateManager> _stateManager;
181 std::shared_ptr<ThreadPool> _threadPool; // Optional: for parallel system execution
182 std::shared_ptr<EventBus> _eventBus; // Optional: for publishing events
183 bool _gameActive{false};
184 bool _hadPlayers{false}; // Track if any players have joined the game
185 std::atomic<bool> _initialized{false};
186
187 // Thread synchronization
188 mutable std::mutex _inputMutex; // Protects _pendingInput
189 std::mutex _playerMutex; // Protects _playerMap
190
191 // Game rules
193
194 // Constants
195 static constexpr float FIXED_TIMESTEP = 1.0f / 60.0f; // 60 Hz
196 };
197
198} // namespace server
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
Base interface for all ECS systems.
Definition ISystem.hpp:37
Manages entities, their signatures and component type registrations.
Definition Registry.hpp:68
Manages Lua state and script execution for the server.
Definition LuaEngine.hpp:28
ECS system that executes Lua scripts for entities.
Deterministic, authoritative server game logic.
Definition GameLogic.hpp:57
const GameRules & getGameRules() const override
Get the game rules (const version)
uint32_t getLastProcessedInput(uint32_t playerId) const override
Get the last processed input sequence ID for a player.
Definition GameLogic.hpp:78
GameRules & getGameRules()
Get the game rules.
void _applyPlayerInput(uint32_t playerId, const PlayerInput &input)
Apply a single input snapshot to a player entity.
void _executeSystems(float deltaTime)
Execute all systems in order.
uint32_t spawnPlayer(uint32_t playerId, const std::string &playerName) override
Spawn a player entity.
std::shared_ptr< ThreadPool > _threadPool
void resetGame() override
Reset game state (new game)
std::unordered_map< uint32_t, std::deque< PlayerInput > > _pendingInput
void _checkGameOverCondition()
Check if all players are dead and trigger game over.
std::mutex _playerMutex
~GameLogic() override
bool initialize() override
Initialize game logic and ECS systems.
Definition GameLogic.cpp:87
void despawnPlayer(uint32_t playerId) override
Remove a player from the game.
bool loadMap(const std::string &mapFilePath)
Load and activate a new map from a JSON file.
static constexpr float FIXED_TIMESTEP
std::shared_ptr< GameStateManager > getStateManager()
Get the game state manager.
void _cleanupDeadEntities()
Clean up dead entities.
std::unordered_map< uint32_t, ecs::Address > _playerMap
std::shared_ptr< EventBus > _eventBus
std::unique_ptr< scripting::LuaEngine > _luaEngine
ecs::Registry & getRegistry() override
Get reference to the ECS registry.
Definition GameLogic.hpp:84
std::shared_ptr< ecs::wrapper::ECSWorld > _world
bool isGameActive() const override
Check if the game is active.
Definition GameLogic.hpp:85
std::atomic< bool > _initialized
void update(float deltaTime, uint32_t currentTick) override
Update game state for one frame.
std::mutex _inputMutex
GameRules _gameRules
void onGameStart()
Notify Lua scripts that the game has started.
void _processInput()
Process accumulated player input.
std::shared_ptr< GameStateManager > _stateManager
std::unordered_map< uint32_t, uint32_t > _lastReceivedSequenceId
std::unordered_map< uint32_t, uint32_t > _lastAppliedSequenceId
std::shared_ptr< ecs::wrapper::ECSWorld > getECSWorld()
Get the ECS world instance.
Definition GameLogic.hpp:97
void processPlayerInput(uint32_t playerId, int inputX, int inputY, bool isShooting, uint32_t sequenceId) override
Process player input event.
Centralized game rules and configuration.
Definition GameRules.hpp:21
Interface for server-side game logic orchestration.
Maximum number of distinct component types supported by the Registry.
Definition GameLogic.hpp:26