r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
gme::PlayerControllerMulti Class Referencefinal

Manages local player input and network communication for multiplayer. More...

#include <PlayerControllerMulti.hpp>

+ Inheritance diagram for gme::PlayerControllerMulti:
+ Collaboration diagram for gme::PlayerControllerMulti:

Classes

struct  PendingInput
 Represents an input command awaiting server acknowledgment. More...
 

Public Member Functions

 PlayerControllerMulti (const std::shared_ptr< eng::IRenderer > &renderer, uint32_t sessionId)
 Constructor.
 
 ~PlayerControllerMulti () override=default
 Destructor.
 
 PlayerControllerMulti (const PlayerControllerMulti &)=delete
 Deleted copy constructor (non-copyable)
 
PlayerControllerMultioperator= (const PlayerControllerMulti &)=delete
 Deleted copy assignment operator (non-copyable)
 
 PlayerControllerMulti (PlayerControllerMulti &&)=delete
 Deleted move constructor (non-movable)
 
PlayerControllerMultioperator= (PlayerControllerMulti &&)=delete
 Deleted move assignment operator (non-movable)
 
void update (ecs::Registry &registry, float dt) override
 Update the player controller system (called each frame)
 
void handleInput (ecs::Registry &registry, const eng::Event &event)
 Handle input events from the window.
 
ecs::Entity createPlayer (ecs::Registry &registry, float x, float y)
 Create the local player entity.
 
bool isSpacePressed () const
 Check if space bar is currently pressed.
 
- Public Member Functions inherited from ecs::ASystem
bool isEnable () override
 
void setEnable (const bool enable) override
 
- Public Member Functions inherited from ecs::ISystem
virtual ~ISystem ()=default
 

Private Member Functions

void sendInputToServer (bool up, bool down, bool left, bool right, bool shoot) const
 Send input command to server via event bus.
 
void sendInputsIfChanged ()
 Send input update if state has changed.
 

Private Attributes

const std::shared_ptr< eng::IRenderer > & m_renderer
 Renderer interface reference.
 
std::unordered_map< eng::Key, bool > m_keysPressed
 Current state of all keys.
 
ecs::Entity m_playerEntity
 Local player entity ID.
 
uint32_t m_componentId
 Event bus component ID.
 
utl::EventBusm_eventBus
 Event bus reference.
 
const float INPUT_THROTTLE_INTERVAL = 1.0f / 144.0f
 Maximum input send rate (144 Hz)
 
uint32_t m_nextSeqId = 1
 Next sequence ID for input packets.
 
uint32_t m_lastAckSeqId = 0
 Last acknowledged sequence ID from server.
 

Detailed Description

Manages local player input and network communication for multiplayer.

This system handles:

  • Capturing and processing player keyboard input
  • Sending input commands to the game server
  • Client-side prediction for responsive controls
  • Input throttling to reduce network bandwidth
  • Sequence numbering for input acknowledgment

The controller sends input updates to the server at a maximum rate of 144 Hz and maintains a history of pending inputs for reconciliation.

Definition at line 41 of file PlayerControllerMulti.hpp.

Constructor & Destructor Documentation

◆ PlayerControllerMulti() [1/3]

gme::PlayerControllerMulti::PlayerControllerMulti ( const std::shared_ptr< eng::IRenderer > & renderer,
uint32_t sessionId )
inlineexplicit

Constructor.

Parameters
rendererShared pointer to the renderer interface
sessionIdPlayer's network session ID

Initializes the controller and registers with the event bus

Definition at line 50 of file PlayerControllerMulti.hpp.

References m_componentId, m_eventBus, and utl::EventBus::registerComponent().

+ Here is the call graph for this function:

◆ ~PlayerControllerMulti()

gme::PlayerControllerMulti::~PlayerControllerMulti ( )
overridedefault

Destructor.

◆ PlayerControllerMulti() [2/3]

gme::PlayerControllerMulti::PlayerControllerMulti ( const PlayerControllerMulti & )
delete

Deleted copy constructor (non-copyable)

◆ PlayerControllerMulti() [3/3]

gme::PlayerControllerMulti::PlayerControllerMulti ( PlayerControllerMulti && )
delete

Deleted move constructor (non-movable)

Member Function Documentation

◆ createPlayer()

ecs::Entity gme::PlayerControllerMulti::createPlayer ( ecs::Registry & registry,
float x,
float y )

Create the local player entity.

Parameters
registryECS registry to create entity in
xInitial X position
yInitial Y position
Returns
Created player entity ID

Creates a player entity with all necessary components for gameplay

Definition at line 8 of file playerControllerMulti.cpp.

References utl::calculateHitboxOffsets(), ecs::Registry::createEntity(), m_playerEntity, utl::GameConfig::Beam::MAX_CHARGE, utl::GameConfig::Hitbox::PLAYER_RADIUS, utl::GameConfig::Player::SCALE, utl::GameConfig::Player::SPRITE_HEIGHT, utl::GameConfig::Player::SPRITE_WIDTH, utl::Path::Texture::TEXTURE_PLAYER, and ecs::Registry::EntityBuilder::with().

+ Here is the call graph for this function:

◆ handleInput()

void gme::PlayerControllerMulti::handleInput ( ecs::Registry & registry,
const eng::Event & event )

Handle input events from the window.

Parameters
registryECS registry
eventInput event (keyboard press/release)

Updates internal key state for the player's controls

Definition at line 29 of file playerControllerMulti.cpp.

References eng::Down, eng::Event::key, eng::KeyPressed, eng::KeyReleased, eng::Left, eng::Right, eng::Space, eng::Event::type, and eng::Up.

◆ isSpacePressed()

bool gme::PlayerControllerMulti::isSpacePressed ( ) const

Check if space bar is currently pressed.

Returns
True if space is pressed (shooting)

Definition at line 212 of file playerControllerMulti.cpp.

References eng::Space.

◆ operator=() [1/2]

PlayerControllerMulti & gme::PlayerControllerMulti::operator= ( const PlayerControllerMulti & )
delete

Deleted copy assignment operator (non-copyable)

◆ operator=() [2/2]

PlayerControllerMulti & gme::PlayerControllerMulti::operator= ( PlayerControllerMulti && )
delete

Deleted move assignment operator (non-movable)

◆ sendInputsIfChanged()

void gme::PlayerControllerMulti::sendInputsIfChanged ( )
private

Send input update if state has changed.

Only sends updates when input state changes to reduce network traffic

Definition at line 86 of file playerControllerMulti.cpp.

References eng::Down, eng::Left, eng::Right, eng::Space, and eng::Up.

◆ sendInputToServer()

void gme::PlayerControllerMulti::sendInputToServer ( bool up,
bool down,
bool left,
bool right,
bool shoot ) const
private

Send input command to server via event bus.

Parameters
upMove up key pressed
downMove down key pressed
leftMove left key pressed
rightMove right key pressed
shootShoot key pressed

Serializes input state and sends to server as INPUT event

Definition at line 106 of file playerControllerMulti.cpp.

References rnp::EventRecord::data, rnp::EventRecord::entityId, rnp::Serializer::getData(), rnp::INPUT, utl::NETWORK_CLIENT, utl::SEND_ENTITY_EVENT, rnp::Serializer::serializeEntityEvents(), and rnp::EventRecord::type.

+ Here is the call graph for this function:

◆ update()

void gme::PlayerControllerMulti::update ( ecs::Registry & registry,
float dt )
overridevirtual

Update the player controller system (called each frame)

Parameters
registryECS registry containing all entities and components
dtDelta time since last frame (in seconds)

Processes input state and sends updates to server if inputs changed

Implements ecs::ISystem.

Definition at line 131 of file playerControllerMulti.cpp.

References ecs::Registry::createEntity(), eng::Down, ecs::Registry::getAll(), ecs::Registry::getComponent(), ecs::INVALID_ENTITY, eng::Left, eng::Right, eng::Space, eng::Up, and ecs::Registry::EntityBuilder::with().

+ Here is the call graph for this function:

Member Data Documentation

◆ INPUT_THROTTLE_INTERVAL

const float gme::PlayerControllerMulti::INPUT_THROTTLE_INTERVAL = 1.0f / 144.0f
private

Maximum input send rate (144 Hz)

Definition at line 138 of file PlayerControllerMulti.hpp.

◆ m_componentId

uint32_t gme::PlayerControllerMulti::m_componentId
private

Event bus component ID.

Definition at line 135 of file PlayerControllerMulti.hpp.

Referenced by PlayerControllerMulti().

◆ m_eventBus

utl::EventBus& gme::PlayerControllerMulti::m_eventBus
private

Event bus reference.

Definition at line 136 of file PlayerControllerMulti.hpp.

Referenced by PlayerControllerMulti().

◆ m_keysPressed

std::unordered_map<eng::Key, bool> gme::PlayerControllerMulti::m_keysPressed
private

Current state of all keys.

Definition at line 133 of file PlayerControllerMulti.hpp.

◆ m_lastAckSeqId

uint32_t gme::PlayerControllerMulti::m_lastAckSeqId = 0
private

Last acknowledged sequence ID from server.

Definition at line 141 of file PlayerControllerMulti.hpp.

◆ m_nextSeqId

uint32_t gme::PlayerControllerMulti::m_nextSeqId = 1
private

Next sequence ID for input packets.

Definition at line 140 of file PlayerControllerMulti.hpp.

◆ m_playerEntity

ecs::Entity gme::PlayerControllerMulti::m_playerEntity
private

Local player entity ID.

Definition at line 134 of file PlayerControllerMulti.hpp.

Referenced by createPlayer().

◆ m_renderer

const std::shared_ptr<eng::IRenderer>& gme::PlayerControllerMulti::m_renderer
private

Renderer interface reference.

Definition at line 132 of file PlayerControllerMulti.hpp.


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