43 ShipControls() : ids_{declare(actions_)}, auto_fire_{
id(ecs::Action::Fire)} {}
56 const auto index = std::countr_zero(
static_cast<unsigned>(action));
57 return *std::next(ids_.begin(),
static_cast<std::ptrdiff_t
>(index));
85 for (
const ecs::Action action : {Up, Down, Left, Right}) {
86 input.
set(action, actions_.
held(
id(action), keyboard, gamepad));
88 input.
set(Fire, auto_fire_.
update(actions_, keyboard, gamepad));
95 [[nodiscard]] std::uint8_t
held() const noexcept {
return held_; }
105 std::string_view gamepad = {}) {
106 actions_.
load(settings, input);
107 if (!gamepad.empty()) {
120 std::string_view gamepad = {})
const {
121 actions_.
save(settings, input);
122 if (!gamepad.empty()) {
130 static std::array<platform::ActionId, 5> declare(platform::ActionMap&
actions) {
134 actions.
add(
"down", {Key::Down}, {DpadDown, LeftStickDown}),
136 actions.
add(
"right", {Key::Right}, {DpadRight, LeftStickRight}),
140 platform::ActionMap actions_;
141 std::array<platform::ActionId, 5> ids_;
142 platform::LatchedAction auto_fire_;
143 std::uint8_t held_ = 0;
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
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
Shoot'em up building blocks on top of the ECS: player ships, their controls, weapons and their shots....
Definition components.hpp:5