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;
}
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);

View File

@ -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)
}
}