pub struct InputDeviceState<const N: usize> {
pub device: InputDevice,
pub actions: [ActionState; N],
}
Expand description
The main input interface for the game, created, and maintained in game code.
N
should be the amount of actions that can be triggered by the player to
be detected by the game. Every frame, InputDeviceState::update
should be
called, and then the ActionState::pressed
status of the values in
InputDeviceState::actions
should be used to trigger any relevant events
in the game based on the actions’ index.
§Example
use engine::input::{InputDeviceState, ActionState, ActionKind};
#[repr(usize)]
enum PlayerAction {
Jump,
Run,
Select,
_Count,
}
let mut input_device_state = InputDeviceState {
device: a_device_from_platform,
actions: [ActionState::default(); PlayerAction::_Count as usize],
};
// Maybe bind the actions:
let jump_action = &mut input_device_state.actions[PlayerAction::Jump as usize];
jump_action.kind = ActionKind::Instant;
jump_action.mapping = Some(button_from_platform);
let run_action = &mut input_device_state.actions[PlayerAction::Run as usize];
run_action.kind = ActionKind::Held;
run_action.mapping = Some(another_button_from_platform);
// Somewhere early in a frame:
input_device_state.update(&mut event_queue);
if input_device_state.actions[PlayerAction::Jump as usize].pressed {
// Jump!
}
Fields§
§device: InputDevice
The device this InputDeviceState
tracks.
actions: [ActionState; N]
Each action’s current state, updated based on events in
InputDeviceState::update
.
Implementations§
Source§impl<const N: usize> InputDeviceState<N>
impl<const N: usize> InputDeviceState<N>
Sourcepub fn update(&mut self, event_queue: &mut EventQueue)
pub fn update(&mut self, event_queue: &mut EventQueue)
Checks the event queue for any events that could be consumed by this
InputDeviceState
, and consumes any such events to trigger actions.
Also resets the ActionState::pressed
status of
ActionKind::Instant
actions.
Auto Trait Implementations§
impl<const N: usize> Freeze for InputDeviceState<N>
impl<const N: usize> RefUnwindSafe for InputDeviceState<N>
impl<const N: usize> Send for InputDeviceState<N>
impl<const N: usize> Sync for InputDeviceState<N>
impl<const N: usize> Unpin for InputDeviceState<N>
impl<const N: usize> UnwindSafe for InputDeviceState<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more