R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::GameLogic Class Reference

Deterministic, authoritative server game logic. More...

#include <GameLogic.hpp>

Inheritance diagram for server::GameLogic:
Inheritance graph
Collaboration diagram for server::GameLogic:
Collaboration graph

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::RegistrygetRegistry () 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::ECSWorldgetECSWorld ()
 Get the ECS world instance.
 
std::shared_ptr< GameStateManagergetStateManager ()
 Get the game state manager.
 
GameRulesgetGameRules ()
 Get the game rules.
 
const GameRulesgetGameRules () 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
 

Detailed Description

Deterministic, authoritative server game logic.

Coordinates all ECS systems in a strict order:

  1. Input processing
  2. Movement system
  3. Collision detection
  4. Health/damage system
  5. Spawning system
  6. AI system
  7. Projectile system
  8. Boundary system
  9. State serialization

Features:

  • Fixed timestep (60 Hz / 1/60s per frame)
  • Deterministic updates (same input = same output)
  • Multi-threaded safe (internal synchronization)
  • Player entity management
  • Game state snapshots
  • Uses World abstraction layer for entity management

Definition at line 57 of file GameLogic.hpp.

Constructor & Destructor Documentation

◆ GameLogic()

server::GameLogic::GameLogic ( std::shared_ptr< ecs::wrapper::ECSWorld world = nullptr,
std::shared_ptr< ThreadPool threadPool = nullptr,
std::shared_ptr< EventBus eventBus = nullptr 
)
explicit

Constructor.

Parameters
worldOptional ECSWorld instance (creates one if not provided)
threadPoolOptional ThreadPool for parallel system execution
eventBusOptional EventBus for publishing game events

Definition at line 54 of file GameLogic.cpp.

References _eventBus, _luaEngine, _threadPool, _world, LOG_DEBUG, and LOG_INFO.

◆ ~GameLogic()

server::GameLogic::~GameLogic ( )
overridedefault

Member Function Documentation

◆ _applyPlayerInput()

◆ _checkGameOverCondition()

void server::GameLogic::_checkGameOverCondition ( )
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().

Here is the call graph for this function:

◆ _cleanupDeadEntities()

void server::GameLogic::_cleanupDeadEntities ( )
private

Clean up dead entities.

Definition at line 459 of file GameLogic.cpp.

References _playerMap, _playerMutex, _world, and LOG_INFO.

Referenced by update().

◆ _executeSystems()

void server::GameLogic::_executeSystems ( float  deltaTime)
private

Execute all systems in order.

Parameters
deltaTimeFrame delta time

Definition at line 402 of file GameLogic.cpp.

References _threadPool, and _world.

Referenced by update().

◆ _processInput()

void server::GameLogic::_processInput ( )
private

Process accumulated player input.

Definition at line 272 of file GameLogic.cpp.

References _applyPlayerInput(), _inputMutex, and _pendingInput.

Referenced by update().

Here is the call graph for this function:

◆ despawnPlayer()

void server::GameLogic::despawnPlayer ( uint32_t  playerId)
overridevirtual

Remove a player from the game.

Parameters
playerIdPlayer 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.

◆ getECSWorld()

std::shared_ptr< ecs::wrapper::ECSWorld > server::GameLogic::getECSWorld ( )
inline

Get the ECS world instance.

Returns
Shared pointer to ECSWorld

Definition at line 97 of file GameLogic.hpp.

References _world.

◆ getGameRules() [1/2]

GameRules & server::GameLogic::getGameRules ( )
inline

Get the game rules.

Returns
Reference to game rules

Definition at line 109 of file GameLogic.hpp.

References _gameRules.

◆ getGameRules() [2/2]

const GameRules & server::GameLogic::getGameRules ( ) const
inlineoverridevirtual

Get the game rules (const version)

Returns
Const reference to game rules

Implements server::IGameLogic.

Definition at line 115 of file GameLogic.hpp.

References _gameRules.

◆ getLastProcessedInput()

uint32_t server::GameLogic::getLastProcessedInput ( uint32_t  playerId) const
inlineoverridevirtual

Get the last processed input sequence ID for a player.

Parameters
playerIdPlayer ID
Returns
Sequence ID or 0 if none

Implements server::IGameLogic.

Definition at line 78 of file GameLogic.hpp.

References _inputMutex, and _lastAppliedSequenceId.

◆ getRegistry()

ecs::Registry & server::GameLogic::getRegistry ( )
inlineoverridevirtual

Get reference to the ECS registry.

Returns
Registry reference

Implements server::IGameLogic.

Definition at line 84 of file GameLogic.hpp.

References _world.

◆ getStateManager()

std::shared_ptr< GameStateManager > server::GameLogic::getStateManager ( )
inline

Get the game state manager.

Returns
Shared pointer to GameStateManager

Definition at line 103 of file GameLogic.hpp.

References _stateManager.

◆ initialize()

bool server::GameLogic::initialize ( )
overridevirtual

Initialize game logic and ECS systems.

Returns
true if successful

Implements server::IGameLogic.

Definition at line 87 of file GameLogic.cpp.

References _eventBus, _gameActive, _initialized, _luaEngine, _stateManager, _threadPool, _world, LOG_ERROR, and LOG_INFO.

◆ isGameActive()

bool server::GameLogic::isGameActive ( ) const
inlineoverridevirtual

Check if the game is active.

Returns
true if game is running

Implements server::IGameLogic.

Definition at line 85 of file GameLogic.hpp.

References _gameActive.

◆ loadMap()

bool server::GameLogic::loadMap ( const std::string &  mapFilePath)

Load and activate a new map from a JSON file.

Parameters
mapFilePathPath to the map JSON file
Returns
True if map was loaded successfully

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().

Here is the call graph for this function:

◆ onGameStart()

void server::GameLogic::onGameStart ( )

Notify Lua scripts that the game has started.

Parameters
roomIdThe ID of the room where the game started

Definition at line 588 of file GameLogic.cpp.

References loadMap(), and LOG_INFO.

Here is the call graph for this function:

◆ processPlayerInput()

void server::GameLogic::processPlayerInput ( uint32_t  playerId,
int  inputX,
int  inputY,
bool  isShooting,
uint32_t  sequenceId 
)
overridevirtual

Process player input event.

Parameters
playerIdPlayer ID
inputXHorizontal input (-1, 0, 1)
inputYVertical input (-1, 0, 1)
isShootingWhether player is shooting
sequenceIdInput sequence ID (for redundancy handling)

Implements server::IGameLogic.

Definition at line 254 of file GameLogic.cpp.

References _inputMutex, _lastReceivedSequenceId, and _pendingInput.

◆ resetGame()

void server::GameLogic::resetGame ( )
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.

◆ spawnPlayer()

uint32_t server::GameLogic::spawnPlayer ( uint32_t  playerId,
const std::string &  playerName 
)
overridevirtual

◆ update()

void server::GameLogic::update ( float  deltaTime,
uint32_t  currentTick 
)
overridevirtual

Update game state for one frame.

Parameters
deltaTimeTime since last frame in seconds
currentTickCurrent server tick number (from ServerLoop)

Implements server::IGameLogic.

Definition at line 150 of file GameLogic.cpp.

References _checkGameOverCondition(), _cleanupDeadEntities(), _executeSystems(), _gameActive, _processInput(), and _stateManager.

Here is the call graph for this function:

Member Data Documentation

◆ _eventBus

std::shared_ptr<EventBus> server::GameLogic::_eventBus
private

Definition at line 182 of file GameLogic.hpp.

Referenced by _checkGameOverCondition(), GameLogic(), and initialize().

◆ _gameActive

bool server::GameLogic::_gameActive {false}
private

Definition at line 183 of file GameLogic.hpp.

Referenced by _checkGameOverCondition(), initialize(), isGameActive(), resetGame(), and update().

◆ _gameRules

GameRules server::GameLogic::_gameRules
private

Definition at line 192 of file GameLogic.hpp.

Referenced by getGameRules(), getGameRules(), and spawnPlayer().

◆ _hadPlayers

bool server::GameLogic::_hadPlayers {false}
private

Definition at line 184 of file GameLogic.hpp.

Referenced by _checkGameOverCondition(), resetGame(), and spawnPlayer().

◆ _initialized

std::atomic<bool> server::GameLogic::_initialized {false}
private

Definition at line 185 of file GameLogic.hpp.

Referenced by initialize().

◆ _inputMutex

std::mutex server::GameLogic::_inputMutex
mutableprivate

Definition at line 188 of file GameLogic.hpp.

Referenced by _processInput(), getLastProcessedInput(), and processPlayerInput().

◆ _lastAppliedSequenceId

std::unordered_map<uint32_t, uint32_t> server::GameLogic::_lastAppliedSequenceId
private

Definition at line 177 of file GameLogic.hpp.

Referenced by _applyPlayerInput(), getLastProcessedInput(), and resetGame().

◆ _lastReceivedSequenceId

std::unordered_map<uint32_t, uint32_t> server::GameLogic::_lastReceivedSequenceId
private

Definition at line 176 of file GameLogic.hpp.

Referenced by processPlayerInput(), and resetGame().

◆ _luaEngine

std::unique_ptr<scripting::LuaEngine> server::GameLogic::_luaEngine
private

Definition at line 150 of file GameLogic.hpp.

Referenced by GameLogic(), initialize(), and loadMap().

◆ _pendingInput

std::unordered_map<uint32_t, std::deque<PlayerInput> > server::GameLogic::_pendingInput
private

Definition at line 173 of file GameLogic.hpp.

Referenced by _processInput(), processPlayerInput(), and resetGame().

◆ _playerMap

std::unordered_map<uint32_t, ecs::Address> server::GameLogic::_playerMap
private

◆ _playerMutex

std::mutex server::GameLogic::_playerMutex
private

◆ _stateManager

std::shared_ptr<GameStateManager> server::GameLogic::_stateManager
private

Definition at line 180 of file GameLogic.hpp.

Referenced by _checkGameOverCondition(), getStateManager(), initialize(), and update().

◆ _threadPool

std::shared_ptr<ThreadPool> server::GameLogic::_threadPool
private

Definition at line 181 of file GameLogic.hpp.

Referenced by _executeSystems(), GameLogic(), and initialize().

◆ _world

◆ FIXED_TIMESTEP

constexpr float server::GameLogic::FIXED_TIMESTEP = 1.0f / 60.0f
staticconstexprprivate

Definition at line 195 of file GameLogic.hpp.

Referenced by _applyPlayerInput().


The documentation for this class was generated from the following files: