From 1e1a8de252191ca9336e03c1fe766558c38b403b Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 15 Oct 2018 10:10:53 +0200 Subject: [PATCH] Manage running key --- src/engine/character/mod.rs | 7 +++---- src/engine/controls/mod.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/engine/character/mod.rs b/src/engine/character/mod.rs index 15bfa43..7eed504 100644 --- a/src/engine/character/mod.rs +++ b/src/engine/character/mod.rs @@ -197,10 +197,9 @@ impl Updatable for Character { self.speed.y = physics::MAXIMUM_VERTICAL_SPEED; } - let limit = if Key::Space.is_pressed() { - physics::MAXIMUM_RUNNING_SPEED - } else { - physics::MAXIMUM_WALKING_SPEED + let limit = match self.controls { + Some(controls) if controls.is_running() => physics::MAXIMUM_RUNNING_SPEED, + _ => physics::MAXIMUM_WALKING_SPEED, }; self.speed.x = clamp(self.speed.x, -limit, limit); diff --git a/src/engine/controls/mod.rs b/src/engine/controls/mod.rs index 42269a8..81099fc 100644 --- a/src/engine/controls/mod.rs +++ b/src/engine/controls/mod.rs @@ -3,6 +3,7 @@ use sfml::window::joystick::{ Axis, axis_position, is_connected, + is_button_pressed, COUNT, }; @@ -73,6 +74,14 @@ impl Controls { } } + /// Returns whether the running key is pressed. + pub fn is_running(&self) -> bool { + match self { + Controls::Keyboard(ref map) => map.is_running(), + Controls::Gamepad(ref map) => map.is_running(), + } + } + } /// A map between keyboard keys and actions. @@ -82,6 +91,9 @@ pub struct KeyboardMap { /// The key corresponding to the jump button. jump_key: Key, + /// The key corresponding to the run button. + run_key: Key, + /// The key corresponding to the up button. up_key: Key, @@ -102,6 +114,7 @@ impl KeyboardMap { pub fn default() -> KeyboardMap { KeyboardMap { jump_key: Key::Space, + run_key: Key::LAlt, up_key: Key::Up, left_key: Key::Left, right_key: Key::Right, @@ -152,6 +165,11 @@ impl KeyboardMap { } + /// Returns whether the running button is held down. + pub fn is_running(&self) -> bool { + self.run_key.is_pressed() + } + } /// A map between gamepad buttons and actions. @@ -164,6 +182,9 @@ pub struct GamepadMap { /// Number of the jump button. jump_button: u32, + /// Number of the run button. + run_button: u32, + /// Left / Right axis. left_right_axis: Axis, @@ -183,6 +204,7 @@ impl GamepadMap { Some(GamepadMap { id: id, jump_button: 1, + run_button: 2, left_right_axis: Axis::X, }) } @@ -229,4 +251,9 @@ impl GamepadMap { 0.0 ) } + + /// Returns whether the run button is held down. + pub fn is_running(&self) -> bool { + is_button_pressed(self.id, self.run_button) + } }