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) | |
| PlayerControllerMulti & | operator= (const PlayerControllerMulti &)=delete |
| Deleted copy assignment operator (non-copyable) | |
| PlayerControllerMulti (PlayerControllerMulti &&)=delete | |
| Deleted move constructor (non-movable) | |
| PlayerControllerMulti & | operator= (PlayerControllerMulti &&)=delete |
| Deleted move assignment operator (non-movable) | |
| void | update (ecs::Registry ®istry, float dt) override |
| Update the player controller system (called each frame) | |
| void | handleInput (ecs::Registry ®istry, const eng::Event &event) |
| Handle input events from the window. | |
| ecs::Entity | createPlayer (ecs::Registry ®istry, 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::EventBus & | m_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. | |
Manages local player input and network communication for multiplayer.
This system handles:
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.
|
inlineexplicit |
Constructor.
| renderer | Shared pointer to the renderer interface |
| sessionId | Player'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:
|
overridedefault |
Destructor.
|
delete |
Deleted copy constructor (non-copyable)
|
delete |
Deleted move constructor (non-movable)
| ecs::Entity gme::PlayerControllerMulti::createPlayer | ( | ecs::Registry & | registry, |
| float | x, | ||
| float | y ) |
Create the local player entity.
| registry | ECS registry to create entity in |
| x | Initial X position |
| y | Initial Y position |
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:| void gme::PlayerControllerMulti::handleInput | ( | ecs::Registry & | registry, |
| const eng::Event & | event ) |
Handle input events from the window.
| registry | ECS registry |
| event | Input 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.
| bool gme::PlayerControllerMulti::isSpacePressed | ( | ) | const |
Check if space bar is currently pressed.
Definition at line 212 of file playerControllerMulti.cpp.
References eng::Space.
|
delete |
Deleted copy assignment operator (non-copyable)
|
delete |
Deleted move assignment operator (non-movable)
|
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.
|
private |
Send input command to server via event bus.
| up | Move up key pressed |
| down | Move down key pressed |
| left | Move left key pressed |
| right | Move right key pressed |
| shoot | Shoot 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:
|
overridevirtual |
Update the player controller system (called each frame)
| registry | ECS registry containing all entities and components |
| dt | Delta 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:
|
private |
Maximum input send rate (144 Hz)
Definition at line 138 of file PlayerControllerMulti.hpp.
|
private |
Event bus component ID.
Definition at line 135 of file PlayerControllerMulti.hpp.
Referenced by PlayerControllerMulti().
|
private |
Event bus reference.
Definition at line 136 of file PlayerControllerMulti.hpp.
Referenced by PlayerControllerMulti().
|
private |
Current state of all keys.
Definition at line 133 of file PlayerControllerMulti.hpp.
|
private |
Last acknowledged sequence ID from server.
Definition at line 141 of file PlayerControllerMulti.hpp.
|
private |
Next sequence ID for input packets.
Definition at line 140 of file PlayerControllerMulti.hpp.
|
private |
Local player entity ID.
Definition at line 134 of file PlayerControllerMulti.hpp.
Referenced by createPlayer().
|
private |
Renderer interface reference.
Definition at line 132 of file PlayerControllerMulti.hpp.