Struct InputDeviceState

Source
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>

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.