Skip to main content

game_toolkit_platformer/sim/
intent.rs

1//! [`Intent`]: what the player wants this step, independent of keys or pads.
2
3/// One step's input. Edges (`*_pressed`) are true for exactly one step; the session
4/// latches presses that land between steps so none get lost in slow motion.
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
6pub struct Intent {
7    /// Horizontal stick, -1 (left) to 1 (right).
8    pub move_x: f32,
9    /// Vertical stick, -1 (up) to 1 (down). Down fast-falls, aims dashes, climbs down and,
10    /// with jump, drops through platforms.
11    pub move_y: f32,
12    /// Jump was pressed since the last step.
13    pub jump_pressed: bool,
14    /// Jump is held.
15    pub jump_held: bool,
16    /// Dash was pressed since the last step.
17    pub dash_pressed: bool,
18    /// Grab is held: hold on to walls.
19    pub grab: bool,
20    /// Dive was pressed since the last step.
21    pub dive_pressed: bool,
22    /// Sprint is held: a higher top speed.
23    pub sprint: bool,
24    /// Super dash is held: charge it (on the ground or a wall); release to launch.
25    pub super_dash: bool,
26    /// Your game's own buttons that are held (attack, interact, ...), one bit each; the
27    /// movement ignores them. Bind keys to them with `controls::Scheme::custom`.
28    pub custom: u32,
29    /// Your game's own buttons pressed since the last step, one bit each: latched by the
30    /// `Platformer` like the built-in presses, so none get lost between steps.
31    pub custom_pressed: u32,
32}
33
34impl Intent {
35    /// The stick points down.
36    pub fn down(&self) -> bool {
37        self.move_y > 0.5
38    }
39
40    /// Whether custom button `bit` (0 to 31) is held.
41    pub fn held(&self, bit: u8) -> bool {
42        self.custom & (1 << bit) != 0
43    }
44
45    /// Whether custom button `bit` (0 to 31) was pressed since the last step.
46    pub fn pressed(&self, bit: u8) -> bool {
47        self.custom_pressed & (1 << bit) != 0
48    }
49}