Manage running key

This commit is contained in:
Thomas Forgione 2018-10-15 10:10:53 +02:00
parent 20dddbd461
commit 1e1a8de252
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
2 changed files with 30 additions and 4 deletions

View File

@ -197,10 +197,9 @@ impl Updatable for Character {
self.speed.y = physics::MAXIMUM_VERTICAL_SPEED; self.speed.y = physics::MAXIMUM_VERTICAL_SPEED;
} }
let limit = if Key::Space.is_pressed() { let limit = match self.controls {
physics::MAXIMUM_RUNNING_SPEED Some(controls) if controls.is_running() => physics::MAXIMUM_RUNNING_SPEED,
} else { _ => physics::MAXIMUM_WALKING_SPEED,
physics::MAXIMUM_WALKING_SPEED
}; };
self.speed.x = clamp(self.speed.x, -limit, limit); self.speed.x = clamp(self.speed.x, -limit, limit);

View File

@ -3,6 +3,7 @@ use sfml::window::joystick::{
Axis, Axis,
axis_position, axis_position,
is_connected, is_connected,
is_button_pressed,
COUNT, 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. /// A map between keyboard keys and actions.
@ -82,6 +91,9 @@ pub struct KeyboardMap {
/// The key corresponding to the jump button. /// The key corresponding to the jump button.
jump_key: Key, jump_key: Key,
/// The key corresponding to the run button.
run_key: Key,
/// The key corresponding to the up button. /// The key corresponding to the up button.
up_key: Key, up_key: Key,
@ -102,6 +114,7 @@ impl KeyboardMap {
pub fn default() -> KeyboardMap { pub fn default() -> KeyboardMap {
KeyboardMap { KeyboardMap {
jump_key: Key::Space, jump_key: Key::Space,
run_key: Key::LAlt,
up_key: Key::Up, up_key: Key::Up,
left_key: Key::Left, left_key: Key::Left,
right_key: Key::Right, 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. /// A map between gamepad buttons and actions.
@ -164,6 +182,9 @@ pub struct GamepadMap {
/// Number of the jump button. /// Number of the jump button.
jump_button: u32, jump_button: u32,
/// Number of the run button.
run_button: u32,
/// Left / Right axis. /// Left / Right axis.
left_right_axis: Axis, left_right_axis: Axis,
@ -183,6 +204,7 @@ impl GamepadMap {
Some(GamepadMap { Some(GamepadMap {
id: id, id: id,
jump_button: 1, jump_button: 1,
run_button: 2,
left_right_axis: Axis::X, left_right_axis: Axis::X,
}) })
} }
@ -229,4 +251,9 @@ impl GamepadMap {
0.0 0.0
) )
} }
/// Returns whether the run button is held down.
pub fn is_running(&self) -> bool {
is_button_pressed(self.id, self.run_button)
}
} }