Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
ship_controls.hpp
Go to the documentation of this file.
1
7#pragma once
8
15
16#include <array>
17#include <bit>
18#include <cstddef>
19#include <cstdint>
20#include <iterator>
21#include <string_view>
22
23namespace aegis2d::shmup {
24
41public:
43 ShipControls() : ids_{declare(actions_)}, auto_fire_{id(ecs::Action::Fire)} {}
44
47 [[nodiscard]] platform::ActionMap& actions() noexcept { return actions_; }
48
50 [[nodiscard]] const platform::ActionMap& actions() const noexcept { return actions_; }
51
55 [[nodiscard]] platform::ActionId id(ecs::Action action) const noexcept {
56 const auto index = std::countr_zero(static_cast<unsigned>(action));
57 return *std::next(ids_.begin(), static_cast<std::ptrdiff_t>(index));
58 }
59
62 [[nodiscard]] platform::LatchedAction& auto_fire() noexcept { return auto_fire_; }
63
65 [[nodiscard]] const platform::LatchedAction& auto_fire() const noexcept { return auto_fire_; }
66
72 std::uint8_t update(const platform::Keyboard& keyboard) {
73 return update(keyboard, platform::Gamepad{});
74 }
75
82 std::uint8_t update(const platform::Keyboard& keyboard, const platform::Gamepad& gamepad) {
83 using enum ecs::Action;
84 ecs::PlayerInput input;
85 for (const ecs::Action action : {Up, Down, Left, Right}) {
86 input.set(action, actions_.held(id(action), keyboard, gamepad));
87 }
88 input.set(Fire, auto_fire_.update(actions_, keyboard, gamepad));
89 held_ = input.held;
90 return held_;
91 }
92
95 [[nodiscard]] std::uint8_t held() const noexcept { return held_; }
96
104 void load(const core::Config& settings, std::string_view input, std::string_view auto_fire,
105 std::string_view gamepad = {}) {
106 actions_.load(settings, input);
107 if (!gamepad.empty()) {
108 actions_.load_buttons(settings, gamepad);
109 }
110 auto_fire_.load(settings, auto_fire);
111 }
112
119 void save(core::Config& settings, std::string_view input, std::string_view auto_fire,
120 std::string_view gamepad = {}) const {
121 actions_.save(settings, input);
122 if (!gamepad.empty()) {
123 actions_.save_buttons(settings, gamepad);
124 }
125 auto_fire_.save(settings, auto_fire);
126 }
127
128private:
129 // In the order of the ecs::Action bits, so id() finds an action by its bit.
130 static std::array<platform::ActionId, 5> declare(platform::ActionMap& actions) {
131 using enum platform::GamepadButton;
132 using platform::Key;
133 return {actions.add("up", {Key::Up}, {DpadUp, LeftStickUp}),
134 actions.add("down", {Key::Down}, {DpadDown, LeftStickDown}),
135 actions.add("left", {Key::Left}, {DpadLeft, LeftStickLeft}),
136 actions.add("right", {Key::Right}, {DpadRight, LeftStickRight}),
137 actions.add("fire", {Key::Space}, {South, RightTrigger})};
138 }
139
140 platform::ActionMap actions_;
141 std::array<platform::ActionId, 5> ids_;
142 platform::LatchedAction auto_fire_;
143 std::uint8_t held_ = 0;
144};
145
146} // namespace aegis2d::shmup
Named actions (move, fire...) bound to keys and gamepad buttons, so gameplay never reads raw input.
JSON configuration file (server port, window size...) read through typed getters.
Definition config.hpp:58
Actions of the game and the keys and gamepad buttons that trigger them.
Definition actions.hpp:38
bool held(ActionId action, const Keyboard &keyboard) const
Tells whether an action is held.
ActionId add(std::string name, std::span< const Key > defaults)
Declares an action, bound to its default keys.
std::size_t load(const core::Config &settings, std::string_view section)
Reads the keys written by save(): each action found gets the keys listed.
void save(core::Config &settings, std::string_view section) const
Writes the keys of every action into the player's settings.
std::size_t load_buttons(const core::Config &settings, std::string_view section)
Reads the gamepad buttons written by save_buttons(), as load() reads keys.
void save_buttons(core::Config &settings, std::string_view section) const
Writes the gamepad buttons of every action into the player's settings.
Buttons held down, and those pressed or released since the start of the frame, with the position of t...
Definition gamepad.hpp:139
Keys held down, and those pressed or released since the start of the frame.
Definition keyboard.hpp:150
An action the player can hold without holding a key, such as fire.
Definition latched_action.hpp:28
void load(const core::Config &settings, std::string_view path)
Reads whether the option is on, as written by save().
void save(core::Config &settings, std::string_view path) const
Writes whether the option is on into the player's settings.
bool update(const ActionMap &actions, const Keyboard &keyboard)
Reads the frame and tells whether the action counts as held.
The controls of a player ship: arrows move it and Space fires, or a gamepad's D-pad or left stick and...
Definition ship_controls.hpp:40
platform::ActionId id(ecs::Action action) const noexcept
Identifier of one of the ship's actions in actions().
Definition ship_controls.hpp:55
void load(const core::Config &settings, std::string_view input, std::string_view auto_fire, std::string_view gamepad={})
Reads the player's key and button bindings and auto-fire option.
Definition ship_controls.hpp:104
ShipControls()
Declares the ship's actions with their default keys, auto-fire off.
Definition ship_controls.hpp:43
void save(core::Config &settings, std::string_view input, std::string_view auto_fire, std::string_view gamepad={}) const
Writes the key and button bindings and the auto-fire option into the player's settings.
Definition ship_controls.hpp:119
const platform::ActionMap & actions() const noexcept
The ship's actions, to rebind them or show them (platform::control_lines()).
Definition ship_controls.hpp:50
std::uint8_t update(const platform::Keyboard &keyboard)
Reads the frame and tells which actions are held.
Definition ship_controls.hpp:72
const platform::LatchedAction & auto_fire() const noexcept
The auto-fire option of the fire action.
Definition ship_controls.hpp:65
platform::LatchedAction & auto_fire() noexcept
The auto-fire option of the fire action.
Definition ship_controls.hpp:62
std::uint8_t update(const platform::Keyboard &keyboard, const platform::Gamepad &gamepad)
Reads the frame on the keyboard and the gamepad, and tells which actions are held on either.
Definition ship_controls.hpp:82
platform::ActionMap & actions() noexcept
The ship's actions, to rebind them or show them (platform::control_lines()).
Definition ship_controls.hpp:47
std::uint8_t held() const noexcept
The result of the last update().
Definition ship_controls.hpp:95
JSON configuration with dotted-path lookups, which can be changed and saved.
Components shared by the client and the server.
Gamepad buttons, sticks and triggers, with no SDL type.
Keyboard keys, their names, and the state of the keyboard during a frame.
Hold-free actions: pressed once to start, once more to stop, such as auto-fire.
Action
Actions a player can hold, one bit each in PlayerInput.
Definition components.hpp:168
GamepadButton
Buttons of a gamepad, named by their position on the pad.
Definition gamepad.hpp:23
@ LeftStickUp
Left stick pushed up.
@ LeftStickLeft
Left stick pushed left.
@ South
Bottom face button.
@ RightTrigger
Right trigger pulled.
Key
Keyboard key, independent of SDL.
Definition keyboard.hpp:24
std::size_t ActionId
Identifier of an action in its ActionMap, given by ActionMap::add().
Definition actions.hpp:22
Shoot'em up building blocks on top of the ECS: player ships, their controls, weapons and their shots....
Definition components.hpp:5
Actions a player holds during the tick, as received from its client.
Definition components.hpp:184
std::uint8_t held
Held actions, one Action bit each.
Definition components.hpp:186
constexpr void set(Action action, bool pressed) noexcept
Holds or releases an action.
Definition components.hpp:198