0.1.0-0-pre.3
This commit is contained in:
61
crates/engines/engine-v1-common/src/input.rs
Normal file
61
crates/engines/engine-v1-common/src/input.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
// file: crates/engines/engine-v1-common/src/input.rs
|
||||
// version: 1
|
||||
|
||||
/// Platform-independent snapshot of logical input actions for one update.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub struct InputState {
|
||||
left: bool,
|
||||
right: bool,
|
||||
up: bool,
|
||||
down: bool,
|
||||
primary: bool,
|
||||
secondary: bool,
|
||||
pause: bool,
|
||||
}
|
||||
|
||||
impl InputState {
|
||||
/// Creates a snapshot with no active action.
|
||||
#[must_use]
|
||||
pub const fn none() -> Self {
|
||||
return Self { left: false, right: false, up: false, down: false, primary: false, secondary: false, pause: false };
|
||||
}
|
||||
|
||||
/// Returns a copy with the selected logical action set to the requested state.
|
||||
#[must_use]
|
||||
pub const fn with_action(mut self, action: crate::GameAction, active: bool) -> Self {
|
||||
match action {
|
||||
crate::GameAction::Left => self.left = active,
|
||||
crate::GameAction::Right => self.right = active,
|
||||
crate::GameAction::Up => self.up = active,
|
||||
crate::GameAction::Down => self.down = active,
|
||||
crate::GameAction::Primary => self.primary = active,
|
||||
crate::GameAction::Secondary => self.secondary = active,
|
||||
crate::GameAction::Pause => self.pause = active,
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
/// Reports whether the selected logical action is active.
|
||||
#[must_use]
|
||||
pub const fn is_active(self, action: crate::GameAction) -> bool {
|
||||
return match action {
|
||||
crate::GameAction::Left => self.left,
|
||||
crate::GameAction::Right => self.right,
|
||||
crate::GameAction::Up => self.up,
|
||||
crate::GameAction::Down => self.down,
|
||||
crate::GameAction::Primary => self.primary,
|
||||
crate::GameAction::Secondary => self.secondary,
|
||||
crate::GameAction::Pause => self.pause,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
#[test]
|
||||
fn action_state_is_independent() {
|
||||
let state = crate::InputState::none().with_action(crate::GameAction::Primary, true);
|
||||
assert!(state.is_active(crate::GameAction::Primary));
|
||||
assert!(!state.is_active(crate::GameAction::Secondary));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user