R-Type 0.1
Loading...
Searching...
No Matches
PlayerSystem.hpp
1#pragma once
2
3#include <ISystem.hpp>
4
5#include "Logger/Logger.hpp"
6#include "inputAction/InputAction.hpp"
7
8#include "input/action.hpp"
9
10#include "components/Animator.hpp"
11#include "components/Bullet.hpp"
12#include "components/Controllable.hpp"
13#include "components/NetworkEntity.hpp"
14#include "components/Transform.hpp"
15#include "components/Velocity.hpp"
16
17#include "NetworkClient.hpp"
18#include "factoryDataDriven/factoryDataDriven.hpp"
19
20namespace rtk::systems {
30class PlayerSystem final : public rtk::ecs::ISystem {
31private:
32 rtk::InputState &_inputState;
33 busEvent &_eventBus;
34 rtk::factory::EntityFactory &_entityFactory;
35 rtype::client::NetworkClient &_networkClient;
36 std::uint32_t _clientTick{0};
37
38 static constexpr float SQUARE_2_DIVIDE_BY_2 = 0.7071f;
39
40public:
48 PlayerSystem(rtk::InputState &inputState, busEvent &eventBus,
49 rtk::factory::EntityFactory &entityFactory,
50 rtype::client::NetworkClient &networkClient)
51 : _inputState(inputState), _eventBus(eventBus),
52 _entityFactory(entityFactory), _networkClient(networkClient) {}
53
54 ~PlayerSystem() override = default;
55
59 void onStart(rtk::ecs::Registry &) override {
60 LOG_INFO("Load Player System");
61 }
62
66 void update(rtk::ecs::Registry &, float) override {
67 sendInputsToServer();
68 handleShootingAudio();
69 _clientTick++;
70 }
71
72private:
73 bool _isChargingShoot{false};
74
75 void sendInputsToServer() {
76 if (!_networkClient.isConnected())
77 return;
78
79 rtype::protocol::InputAction networkActions =
81
83 networkActions = networkActions | rtype::protocol::InputAction::Up;
84 }
86 networkActions = networkActions | rtype::protocol::InputAction::Down;
87 }
89 networkActions = networkActions | rtype::protocol::InputAction::Left;
90 }
92 networkActions = networkActions | rtype::protocol::InputAction::Right;
93 }
95 networkActions = networkActions | rtype::protocol::InputAction::Shoot;
96 }
97
98 _networkClient.sendInput(_clientTick, networkActions);
99 }
100
101 void handleShootingAudio() {
102 bool isShootPressed =
104
105 if (isShootPressed) {
106 _isChargingShoot = true;
107 } else if (_isChargingShoot) {
108 _isChargingShoot = false;
110 }
111 }
112
116 void onStop(rtk::ecs::Registry &) override {}
117};
118
120} // namespace rtk::systems
Type indexed publish and subscribe event bus.
Definition busEvents.hpp:20
void publish(const E &event)
Calls every handler registered for the event type.
Definition busEvents.hpp:52
Builds entities from JSON blueprints and registered component builders.
Definition factoryDataDriven.hpp:54
Sends the local player actions to the server every frame and triggers the shot sound.
Definition PlayerSystem.hpp:30
void update(rtk::ecs::Registry &, float) override
Sends an Input message and publishes a shot on button release.
Definition PlayerSystem.hpp:66
void onStart(rtk::ecs::Registry &) override
Logs that the system is loaded.
Definition PlayerSystem.hpp:59
PlayerSystem(rtk::InputState &inputState, busEvent &eventBus, rtk::factory::EntityFactory &entityFactory, rtype::client::NetworkClient &networkClient)
Creates the system.
Definition PlayerSystem.hpp:48
UDP client that performs the handshake, sends inputs and decodes server messages.
Definition NetworkClient.hpp:71
bool isConnected() const
Tells whether the server accepted the handshake.
Definition NetworkClient.cpp:332
void sendInput(std::uint32_t clientTick, rtype::protocol::InputAction actions)
Sends an Input message with the next input sequence number.
Definition NetworkClient.cpp:79
constexpr bool hasAction(uint32_t engineMask, Action action)
Tells whether an action is active in a mask.
Definition action.hpp:40
@ Down
Move down.
@ Up
Move up.
@ Right
Move right.
@ Left
Move left.
InputAction
Bit flags of the actions held by a player.
Definition InputPayload.hpp:16
@ NoneAction
No action held.
@ Shoot
Shoot button held.
Player input sampled during the current frame.
Definition InputAction.hpp:14
uint32_t actionMask
Bit n is set while action n is active.
Definition InputAction.hpp:15
Published when the local player releases the shoot button.
Definition game.hpp:30