Commit c95ce74f authored by Simon Goller's avatar Simon Goller Committed by neosam
Browse files

Add ability to run with shift key

The stamina system now allows the player to run for two seconds
until the stamina runs out.  It takes four seconds to fill the
stamina again.

To implment running, the `movable` component got a new `running`
attribute.  If this is true, the velocity is doubled im the
`move_movable` system.  This `running` flag is set to true when
the player presses the Shift button and is set to false when
either the Shift button is released or by the `empty_stamina_running`
system if no stamina is left.
parent edbd3622
pub struct Movable {
pub velocity: nalgebra::Vector2<f32>,
pub running: bool,
}
impl Movable {
pub fn new() -> Movable {
Movable {
velocity: nalgebra::Vector2::new(0.0, 0.0),
running: false,
}
}
}
......@@ -14,6 +14,6 @@ impl Progress {
}
pub fn modify(&mut self, delta: f32) {
self.value = (self.value + delta).min(self.max).max(0.0);
self.value = (self.value + delta).max(0.0).min(self.max);
}
}
......@@ -22,6 +22,6 @@ pub fn create_player(
components::Movable::new(),
colliders::Collider::new_player(50.0),
components::Sprite::new(sprites::Sprite::Player),
components::Stamina::new(30.0, 5.0),
components::Stamina::new(10.0, 5.0),
))
}
......@@ -54,6 +54,7 @@ impl InGameState {
.flush()
.add_system(colliders::update_collision_world_system())
.flush()
.add_thread_local(systems::empty_stamina_running_system())
.add_thread_local(systems::move_movable_system())
.flush();
......@@ -119,32 +120,39 @@ impl gamestate::State for InGameState {
.get_mut(&mut self.world, player)
.unwrap();
match event {
gamestate::Event::KeyDown("w") | gamestate::Event::KeyDown("ArrowUp") => {
gamestate::Event::KeyDown("w") | gamestate::Event::KeyDown("W") | gamestate::Event::KeyDown("ArrowUp") => {
player_movable.velocity.y = -300.0
}
gamestate::Event::KeyUp("w") | gamestate::Event::KeyUp("ArrowUp") => {
gamestate::Event::KeyUp("w") | gamestate::Event::KeyUp("W") | gamestate::Event::KeyUp("ArrowUp") => {
player_movable.velocity.y = 0.0
}
gamestate::Event::KeyDown("a") | gamestate::Event::KeyDown("ArrowLeft") => {
gamestate::Event::KeyDown("a") | gamestate::Event::KeyDown("A") | gamestate::Event::KeyDown("ArrowLeft") => {
player_movable.velocity.x = -300.0
}
gamestate::Event::KeyUp("a") | gamestate::Event::KeyUp("ArrowLeft") => {
gamestate::Event::KeyUp("a") | gamestate::Event::KeyUp("A") | gamestate::Event::KeyUp("ArrowLeft") => {
player_movable.velocity.x = 0.0
}
gamestate::Event::KeyDown("s") | gamestate::Event::KeyDown("ArrowDown") => {
gamestate::Event::KeyDown("s") | gamestate::Event::KeyDown("S") | gamestate::Event::KeyDown("ArrowDown") => {
player_movable.velocity.y = 300.0
}
gamestate::Event::KeyUp("s") | gamestate::Event::KeyUp("ArrowDown") => {
gamestate::Event::KeyUp("s") | gamestate::Event::KeyUp("S") | gamestate::Event::KeyUp("ArrowDown") => {
player_movable.velocity.y = 0.0
}
gamestate::Event::KeyDown("d") | gamestate::Event::KeyDown("ArrowRight") => {
gamestate::Event::KeyDown("d") | gamestate::Event::KeyDown("D") | gamestate::Event::KeyDown("ArrowRight") => {
player_movable.velocity.x = 300.0
}
gamestate::Event::KeyUp("d") | gamestate::Event::KeyUp("ArrowRight") => {
gamestate::Event::KeyUp("d") | gamestate::Event::KeyUp("D") | gamestate::Event::KeyUp("ArrowRight") => {
player_movable.velocity.x = 0.0
}
gamestate::Event::KeyDown("Shift") => {
player_movable.running = true
}
gamestate::Event::KeyUp("Shift") => {
player_movable.running = false
}
_ => (),
}
if player_movable.velocity.x != 0.0 || player_movable.velocity.y != 0.0 {
player_movable.velocity = player_movable.velocity.normalize() * 300.0;
}
......
......@@ -18,7 +18,7 @@ pub use player::{
update_stamina_bar_system
};
pub use sprite::draw_sprites_system;
pub use stamina::stamina_regeneration_system;
pub use stamina::{stamina_regeneration_system, empty_stamina_running_system};
pub use tmp_stationary_obstacles::{
draw_tmp_stationary_obstacles_barrier_system, reduce_sanity_obstacle_system,
};
......@@ -29,6 +29,10 @@ pub fn move_movable(
return;
}
if movable.running {
actual_velocity *= 2.0
}
// Collect the normals of all collisions we're currently involved in. These define which
// directions we can move: No movement is allowed which has any component pointing towards any
// of the normals. In mathematical terms: The dot-product of the final velocity and each of
......
......@@ -8,5 +8,22 @@ pub fn stamina_regeneration(
#[resource] clock: &resources::Clock,
stamina: &mut components::Stamina,
) {
stamina.progress.modify(stamina.progress.value * clock.frame_delta());
stamina.progress.modify(stamina.regeneration * clock.frame_delta());
}
#[legion::system(for_each)]
#[write_component(components::Stamina)]
#[write_component(components::Movable)]
pub fn empty_stamina_running(
#[resource] clock: &resources::Clock,
stamina: &mut components::Stamina,
movalbe: &mut components::Movable,
) {
if movalbe.running {
stamina.progress.modify(-clock.frame_delta() * 10.0);
}
if stamina.progress.value == 0.0 {
movalbe.running = false;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment