Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::platform::LatchedAction Class Reference

An action the player can hold without holding a key, such as fire. More...

#include <aegis2d/platform/latched_action.hpp>

Public Member Functions

 LatchedAction (ActionId action, bool enabled=false) noexcept
 Follows an action of an ActionMap.
 
ActionId action () const noexcept
 Action followed.
 
void set_enabled (bool enabled) noexcept
 Turns the option on or off; either way the action is released.
 
bool enabled () const noexcept
 Tells whether the option is on.
 
bool update (const ActionMap &actions, const Keyboard &keyboard)
 Reads the frame and tells whether the action counts as held.
 
bool update (const ActionMap &actions, const Keyboard &keyboard, const Gamepad &gamepad)
 Reads the frame on the keyboard and the gamepad, as update(const ActionMap&, const Keyboard&) does on the keyboard: a press of a key or a button of the action switches it.
 
bool held () const noexcept
 Whether the action counts as held.
 
void release () noexcept
 Releases a latched action, for example when the player dies or the game pauses.
 
void save (core::Config &settings, std::string_view path) const
 Writes whether the option is on into the player's settings.
 
void load (const core::Config &settings, std::string_view path)
 Reads whether the option is on, as written by save().
 

Detailed Description

An action the player can hold without holding a key, such as fire.

Players who cannot hold a key down (limited strength, tremor, a switch or a mouth stick) turn this option on: the first press of the action holds it, the next press releases it. With the option off, the action is held while its key is, as usual. The client sends the result in its ecs::PlayerInput, so the server fires at the weapon's rate for as long as the action is held:

platform::LatchedAction auto_fire{fire};
auto_fire.load(settings, "accessibility.auto_fire");
...
window.poll_events();
input.set(ecs::Action::Fire, auto_fire.update(actions, window.keyboard()));
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().

Constructor & Destructor Documentation

◆ LatchedAction()

aegis2d::platform::LatchedAction::LatchedAction ( ActionId  action,
bool  enabled = false 
)
explicitnoexcept

Follows an action of an ActionMap.

Parameters
actionAction to latch, declared in the ActionMap given to update().
enabledWhether the option is on at first.

Member Function Documentation

◆ action()

ActionId aegis2d::platform::LatchedAction::action ( ) const
noexcept

Action followed.

Returns
The action given to the constructor.

◆ enabled()

bool aegis2d::platform::LatchedAction::enabled ( ) const
noexcept

Tells whether the option is on.

Returns
The last value given to the constructor or to set_enabled().

◆ held()

bool aegis2d::platform::LatchedAction::held ( ) const
noexcept

Whether the action counts as held.

Returns
The result of the last update(); false before the first one.

◆ load()

void aegis2d::platform::LatchedAction::load ( const core::Config &  settings,
std::string_view  path 
)

Reads whether the option is on, as written by save().

A missing value keeps the option as it is; a value that is not a boolean is logged as a warning and ignored. The action is released.

Parameters
settingsSettings read with read_settings() or core::Config.
pathDotted path given to save().

◆ release()

void aegis2d::platform::LatchedAction::release ( )
noexcept

Releases a latched action, for example when the player dies or the game pauses.

◆ save()

void aegis2d::platform::LatchedAction::save ( core::Config &  settings,
std::string_view  path 
) const

Writes whether the option is on into the player's settings.

Parameters
settingsSettings to update, then saved with core::Config::save().
pathDotted path of the value, such as "accessibility.auto_fire".

◆ set_enabled()

void aegis2d::platform::LatchedAction::set_enabled ( bool  enabled)
noexcept

Turns the option on or off; either way the action is released.

Parameters
enabledtrue for hold-free, false to hold the key as usual.

◆ update() [1/2]

bool aegis2d::platform::LatchedAction::update ( const ActionMap &  actions,
const Keyboard &  keyboard 
)

Reads the frame and tells whether the action counts as held.

Call it once per frame, after Window::poll_events(). With the option on, each frame in which the action is pressed (ActionMap::pressed()) switches it between held and released.

Parameters
actionsActions of the game.
keyboardState of the keyboard, usually Window::keyboard().
Returns
Whether the action is held this frame, as held() then returns.
Exceptions
std::out_of_rangeif the action is not declared in actions.

◆ update() [2/2]

bool aegis2d::platform::LatchedAction::update ( const ActionMap &  actions,
const Keyboard &  keyboard,
const Gamepad &  gamepad 
)

Reads the frame on the keyboard and the gamepad, as update(const ActionMap&, const Keyboard&) does on the keyboard: a press of a key or a button of the action switches it.

Parameters
actionsActions of the game.
keyboardState of the keyboard, usually Window::keyboard().
gamepadState of the gamepads, usually Window::gamepad().
Returns
Whether the action is held this frame, as held() then returns.
Exceptions
std::out_of_rangeif the action is not declared in actions.

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