Skip to main content

embedded_3dgfx/
input.rs

1//! Platform-agnostic input state.
2//!
3//! Fill an [`InputState`] each frame from your hardware (GPIO buttons,
4//! I²C gamepad, USB HID, SDL2 keyboard, …) and pass it to
5//! [`CharacterController::tick`](crate::character::CharacterController::tick).
6//!
7//! All fields default to zero / false so you only need to set the ones
8//! relevant to your hardware.
9
10/// Normalised per-frame input fed into the character controller.
11///
12/// Analog axes are in `[-1.0, 1.0]`; the controller applies its own speed
13/// scaling so you do not need to multiply by delta-time here.
14#[derive(Clone, Copy, Default, Debug)]
15#[cfg_attr(feature = "std", derive(PartialEq))]
16pub struct InputState {
17    /// Forward / backward motion.  `+1.0` = walk forward, `-1.0` = backward.
18    pub forward: f32,
19    /// Lateral motion.  `+1.0` = strafe right, `-1.0` = strafe left.
20    pub strafe: f32,
21    /// Yaw (horizontal look) delta this frame, in radians.
22    /// Positive rotates the view to the right.
23    pub look_yaw: f32,
24    /// Pitch (vertical look) delta this frame, in radians.
25    /// Positive tilts the view upward.
26    pub look_pitch: f32,
27    /// `true` on any frame the jump button is held while the character is
28    /// grounded.  The controller self-resets this internally via `on_ground`.
29    pub jump: bool,
30    /// Sprint modifier — held to move at `run_speed` instead of `walk_speed`.
31    pub sprint: bool,
32}
33
34#[cfg(test)]
35mod tests {
36    use super::InputState;
37
38    #[test]
39    fn default_input_state_is_neutral() {
40        let input = InputState::default();
41        assert_eq!(input.forward, 0.0);
42        assert_eq!(input.strafe, 0.0);
43        assert_eq!(input.look_yaw, 0.0);
44        assert_eq!(input.look_pitch, 0.0);
45        assert!(!input.jump);
46        assert!(!input.sprint);
47    }
48
49    #[test]
50    fn input_state_can_store_full_analog_range() {
51        let input = InputState {
52            forward: 1.0,
53            strafe: -1.0,
54            look_yaw: 0.75,
55            look_pitch: -0.5,
56            jump: true,
57            sprint: true,
58        };
59        assert_eq!(input.forward, 1.0);
60        assert_eq!(input.strafe, -1.0);
61        assert_eq!(input.look_yaw, 0.75);
62        assert_eq!(input.look_pitch, -0.5);
63        assert!(input.jump);
64        assert!(input.sprint);
65    }
66}