|
R-Type
Distributed multiplayer game engine in C++
|
Deterministic, authoritative server game logic. More...
#include <GameLogic.hpp>


Classes | |
| struct | PlayerInput |
Public Member Functions | |
| GameLogic (std::shared_ptr< ecs::wrapper::ECSWorld > world=nullptr, std::shared_ptr< ThreadPool > threadPool=nullptr, std::shared_ptr< EventBus > eventBus=nullptr) | |
| Constructor. | |
| ~GameLogic () override | |
| bool | initialize () override |
| Initialize game logic and ECS systems. | |
| void | update (float deltaTime, uint32_t currentTick) override |
| Update game state for one frame. | |
| uint32_t | spawnPlayer (uint32_t playerId, const std::string &playerName) override |
| Spawn a player entity. | |
| void | despawnPlayer (uint32_t playerId) override |
| Remove a player from the game. | |
| void | processPlayerInput (uint32_t playerId, int inputX, int inputY, bool isShooting, uint32_t sequenceId) override |
| Process player input event. | |
| uint32_t | getLastProcessedInput (uint32_t playerId) const override |
| Get the last processed input sequence ID for a player. | |
| ecs::Registry & | getRegistry () override |
| Get reference to the ECS registry. | |
| bool | isGameActive () const override |
| Check if the game is active. | |
| void | resetGame () override |
| Reset game state (new game) | |
| void | onGameStart () |
| Notify Lua scripts that the game has started. | |
| std::shared_ptr< ecs::wrapper::ECSWorld > | getECSWorld () |
| Get the ECS world instance. | |
| std::shared_ptr< GameStateManager > | getStateManager () |
| Get the game state manager. | |
| GameRules & | getGameRules () |
| Get the game rules. | |
| const GameRules & | getGameRules () const override |
| Get the game rules (const version) | |
| bool | loadMap (const std::string &mapFilePath) |
| Load and activate a new map from a JSON file. | |
Public Member Functions inherited from server::IGameLogic | |
| virtual | ~IGameLogic ()=default |
Private Member Functions | |
| void | _executeSystems (float deltaTime) |
| Execute all systems in order. | |
| void | _processInput () |
| Process accumulated player input. | |
| void | _cleanupDeadEntities () |
| Clean up dead entities. | |
| void | _checkGameOverCondition () |
| Check if all players are dead and trigger game over. | |
| void | _applyPlayerInput (uint32_t playerId, const PlayerInput &input) |
| Apply a single input snapshot to a player entity. | |
Private Attributes | |
| std::shared_ptr< ecs::wrapper::ECSWorld > | _world |
| std::unique_ptr< scripting::LuaEngine > | _luaEngine |
| std::unordered_map< uint32_t, ecs::Address > | _playerMap |
| std::unordered_map< uint32_t, std::deque< PlayerInput > > | _pendingInput |
| std::unordered_map< uint32_t, uint32_t > | _lastReceivedSequenceId |
| std::unordered_map< uint32_t, uint32_t > | _lastAppliedSequenceId |
| std::shared_ptr< GameStateManager > | _stateManager |
| std::shared_ptr< ThreadPool > | _threadPool |
| std::shared_ptr< EventBus > | _eventBus |
| bool | _gameActive {false} |
| bool | _hadPlayers {false} |
| std::atomic< bool > | _initialized {false} |
| std::mutex | _inputMutex |
| std::mutex | _playerMutex |
| GameRules | _gameRules |
Static Private Attributes | |
| static constexpr float | FIXED_TIMESTEP = 1.0f / 60.0f |
Deterministic, authoritative server game logic.
Coordinates all ECS systems in a strict order:
Features:
Definition at line 57 of file GameLogic.hpp.
|
explicit |
Constructor.
| world | Optional ECSWorld instance (creates one if not provided) |
| threadPool | Optional ThreadPool for parallel system execution |
| eventBus | Optional EventBus for publishing game events |
Definition at line 54 of file GameLogic.cpp.
References _eventBus, _luaEngine, _threadPool, _world, LOG_DEBUG, and LOG_INFO.
|
overridedefault |
|
private |
Apply a single input snapshot to a player entity.
| playerId | Player ID |
| input | Input snapshot to apply |
Definition at line 300 of file GameLogic.cpp.
References _lastAppliedSequenceId, _playerMap, _world, FIXED_TIMESTEP, ecs::wrapper::Entity::get(), ecs::Animation::getCurrentClipName(), ecs::Velocity::getSpeed(), ecs::wrapper::Entity::has(), server::GameLogic::PlayerInput::inputX, server::GameLogic::PlayerInput::inputY, server::GameLogic::PlayerInput::isShooting, LOG_ERROR, LOG_WARNING, server::GameLogic::PlayerInput::sequenceId, ecs::Animation::setCurrentClipName(), ecs::Animation::setCurrentFrameIndex(), ecs::Velocity::setDirection(), ecs::Weapon::setShouldShoot(), and ecs::Animation::setTimer().
Referenced by _processInput().

|
private |
Check if all players are dead and trigger game over.
Definition at line 484 of file GameLogic.cpp.
References _eventBus, _gameActive, _hadPlayers, _playerMap, _playerMutex, _stateManager, _world, ecs::wrapper::Entity::get(), ecs::Health::getCurrentHealth(), ecs::wrapper::Entity::has(), ecs::Spawner::isActive, and LOG_INFO.
Referenced by update().

|
private |
Clean up dead entities.
Definition at line 459 of file GameLogic.cpp.
References _playerMap, _playerMutex, _world, and LOG_INFO.
Referenced by update().
|
private |
Execute all systems in order.
| deltaTime | Frame delta time |
Definition at line 402 of file GameLogic.cpp.
References _threadPool, and _world.
Referenced by update().
|
private |
Process accumulated player input.
Definition at line 272 of file GameLogic.cpp.
References _applyPlayerInput(), _inputMutex, and _pendingInput.
Referenced by update().

|
overridevirtual |
Remove a player from the game.
| playerId | Player ID to remove |
Implements server::IGameLogic.
Definition at line 227 of file GameLogic.cpp.
References _playerMap, _playerMutex, _world, LOG_ERROR, LOG_INFO, LOG_WARNING, and ecs::Manual.
|
inline |
Get the ECS world instance.
Definition at line 97 of file GameLogic.hpp.
References _world.
|
inline |
Get the game rules.
Definition at line 109 of file GameLogic.hpp.
References _gameRules.
|
inlineoverridevirtual |
Get the game rules (const version)
Implements server::IGameLogic.
Definition at line 115 of file GameLogic.hpp.
References _gameRules.
|
inlineoverridevirtual |
Get the last processed input sequence ID for a player.
| playerId | Player ID |
Implements server::IGameLogic.
Definition at line 78 of file GameLogic.hpp.
References _inputMutex, and _lastAppliedSequenceId.
|
inlineoverridevirtual |
Get reference to the ECS registry.
Implements server::IGameLogic.
Definition at line 84 of file GameLogic.hpp.
References _world.
|
inline |
Get the game state manager.
Definition at line 103 of file GameLogic.hpp.
References _stateManager.
|
overridevirtual |
Initialize game logic and ECS systems.
Implements server::IGameLogic.
Definition at line 87 of file GameLogic.cpp.
References _eventBus, _gameActive, _initialized, _luaEngine, _stateManager, _threadPool, _world, LOG_ERROR, and LOG_INFO.
|
inlineoverridevirtual |
Check if the game is active.
Implements server::IGameLogic.
Definition at line 85 of file GameLogic.hpp.
References _gameActive.
| bool server::GameLogic::loadMap | ( | const std::string & | mapFilePath | ) |
Load and activate a new map from a JSON file.
| mapFilePath | Path to the map JSON file |
Definition at line 597 of file GameLogic.cpp.
References _luaEngine, _world, ecs::MapData::getDuration(), ecs::MapData::getMapId(), ecs::MapData::getName(), ecs::MapData::getScrollSpeed(), ecs::MapData::getSpawnScript(), map::MapLoader::loadFromFile(), LOG_ERROR, and LOG_INFO.
Referenced by onGameStart().

| void server::GameLogic::onGameStart | ( | ) |
Notify Lua scripts that the game has started.
| roomId | The ID of the room where the game started |
Definition at line 588 of file GameLogic.cpp.
References loadMap(), and LOG_INFO.

|
overridevirtual |
Process player input event.
| playerId | Player ID |
| inputX | Horizontal input (-1, 0, 1) |
| inputY | Vertical input (-1, 0, 1) |
| isShooting | Whether player is shooting |
| sequenceId | Input sequence ID (for redundancy handling) |
Implements server::IGameLogic.
Definition at line 254 of file GameLogic.cpp.
References _inputMutex, _lastReceivedSequenceId, and _pendingInput.
|
overridevirtual |
Reset game state (new game)
Implements server::IGameLogic.
Definition at line 573 of file GameLogic.cpp.
References _gameActive, _hadPlayers, _lastAppliedSequenceId, _lastReceivedSequenceId, _pendingInput, _playerMap, _world, and LOG_INFO.
|
overridevirtual |
Spawn a player entity.
| playerId | Unique player ID |
| playerName | Player's display name |
Implements server::IGameLogic.
Definition at line 170 of file GameLogic.cpp.
References _gameRules, _hadPlayers, _playerMap, _playerMutex, _world, ecs::PrefabFactory::createOrbitalModule(), AnimDB::createPlayerAnimations(), ecs::wrapper::Entity::getAddress(), server::GameRules::getDefaultPlayerDamage(), server::GameRules::getDefaultPlayerFireRate(), server::GameRules::getDefaultPlayerHealth(), server::GameRules::getDefaultPlayerSpeed(), server::GameRules::getPlayerSpawnX(), server::GameRules::getPlayerSpawnY(), LOG_ERROR, LOG_INFO, and ecs::wrapper::Entity::with().

|
overridevirtual |
Update game state for one frame.
| deltaTime | Time since last frame in seconds |
| currentTick | Current server tick number (from ServerLoop) |
Implements server::IGameLogic.
Definition at line 150 of file GameLogic.cpp.
References _checkGameOverCondition(), _cleanupDeadEntities(), _executeSystems(), _gameActive, _processInput(), and _stateManager.

|
private |
Definition at line 182 of file GameLogic.hpp.
Referenced by _checkGameOverCondition(), GameLogic(), and initialize().
|
private |
Definition at line 183 of file GameLogic.hpp.
Referenced by _checkGameOverCondition(), initialize(), isGameActive(), resetGame(), and update().
|
private |
Definition at line 192 of file GameLogic.hpp.
Referenced by getGameRules(), getGameRules(), and spawnPlayer().
|
private |
Definition at line 184 of file GameLogic.hpp.
Referenced by _checkGameOverCondition(), resetGame(), and spawnPlayer().
|
private |
Definition at line 185 of file GameLogic.hpp.
Referenced by initialize().
|
mutableprivate |
Definition at line 188 of file GameLogic.hpp.
Referenced by _processInput(), getLastProcessedInput(), and processPlayerInput().
|
private |
Definition at line 177 of file GameLogic.hpp.
Referenced by _applyPlayerInput(), getLastProcessedInput(), and resetGame().
|
private |
Definition at line 176 of file GameLogic.hpp.
Referenced by processPlayerInput(), and resetGame().
|
private |
Definition at line 150 of file GameLogic.hpp.
Referenced by GameLogic(), initialize(), and loadMap().
|
private |
Definition at line 173 of file GameLogic.hpp.
Referenced by _processInput(), processPlayerInput(), and resetGame().
|
private |
Definition at line 153 of file GameLogic.hpp.
Referenced by _applyPlayerInput(), _checkGameOverCondition(), _cleanupDeadEntities(), despawnPlayer(), resetGame(), and spawnPlayer().
|
private |
Definition at line 189 of file GameLogic.hpp.
Referenced by _checkGameOverCondition(), _cleanupDeadEntities(), despawnPlayer(), and spawnPlayer().
|
private |
Definition at line 180 of file GameLogic.hpp.
Referenced by _checkGameOverCondition(), getStateManager(), initialize(), and update().
|
private |
Definition at line 181 of file GameLogic.hpp.
Referenced by _executeSystems(), GameLogic(), and initialize().
|
private |
Definition at line 147 of file GameLogic.hpp.
Referenced by _applyPlayerInput(), _checkGameOverCondition(), _cleanupDeadEntities(), _executeSystems(), despawnPlayer(), GameLogic(), getECSWorld(), getRegistry(), initialize(), loadMap(), resetGame(), and spawnPlayer().
|
staticconstexprprivate |
Definition at line 195 of file GameLogic.hpp.
Referenced by _applyPlayerInput().