diff --git a/common/src/sys/action_state.rs b/common/src/sys/action_state.rs index 93dc4607e5..08c1a3d514 100644 --- a/common/src/sys/action_state.rs +++ b/common/src/sys/action_state.rs @@ -4,6 +4,7 @@ use crate::{ Jumping, OnGround, Ori, Pos, Rolling, Vel, }, state::DeltaTime, + sys::phys::MOVEMENT_THRESHOLD_VEL, }; use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage}; @@ -63,7 +64,7 @@ impl<'a> System<'a> for Sys { { *action_state = ActionState { on_ground: on_ground.is_some(), - moving: vel.0.magnitude_squared() > 10.0, + moving: vel.0.magnitude_squared() > MOVEMENT_THRESHOLD_VEL.powf(2.0), attacking: attacking.is_some(), rolling: rolling.is_some(), gliding: gliding.is_some(), diff --git a/common/src/sys/controller.rs b/common/src/sys/controller.rs index 9dc84325c4..58edf00140 100644 --- a/common/src/sys/controller.rs +++ b/common/src/sys/controller.rs @@ -1,7 +1,7 @@ use crate::{ comp::{ - Animation, AnimationInfo, Attacking, Controller, Gliding, HealthSource, Jumping, MoveDir, - OnGround, Respawning, Rolling, Stats, {ForceUpdate, Ori, Pos, Vel}, + ActionState, Animation, AnimationInfo, Attacking, Controller, Gliding, HealthSource, + Jumping, MoveDir, OnGround, Respawning, Rolling, Stats, {ForceUpdate, Ori, Pos, Vel}, }, state::DeltaTime, }; @@ -18,7 +18,7 @@ impl<'a> System<'a> for Sys { ReadStorage<'a, Pos>, ReadStorage<'a, Vel>, ReadStorage<'a, Ori>, - ReadStorage<'a, OnGround>, + ReadStorage<'a, ActionState>, WriteStorage<'a, MoveDir>, WriteStorage<'a, Jumping>, WriteStorage<'a, Attacking>, @@ -37,7 +37,7 @@ impl<'a> System<'a> for Sys { positions, velocities, orientations, - on_grounds, + action_states, mut move_dirs, mut jumpings, mut attackings, @@ -46,14 +46,14 @@ impl<'a> System<'a> for Sys { mut glidings, ): Self::SystemData, ) { - for (entity, controller, stats, pos, vel, ori, on_ground) in ( + for (entity, controller, stats, pos, vel, ori, a) in ( &entities, &controllers, &stats, &positions, &velocities, &orientations, - on_grounds.maybe(), + &action_states, ) .join() { @@ -66,10 +66,10 @@ impl<'a> System<'a> for Sys { } // Move dir - if rollings.get(entity).is_none() { + if !a.rolling { move_dirs.insert( entity, - MoveDir(if controller.move_dir.magnitude() > 1.0 { + MoveDir(if controller.move_dir.magnitude_squared() > 1.0 { controller.move_dir.normalized() } else { controller.move_dir @@ -78,38 +78,24 @@ impl<'a> System<'a> for Sys { } // Glide - if controller.glide - && on_ground.is_none() - && attackings.get(entity).is_none() - && rollings.get(entity).is_none() - { + if controller.glide && !a.on_ground && !a.attacking && !a.rolling { glidings.insert(entity, Gliding); } else { glidings.remove(entity); } // Attack - if controller.attack - && attackings.get(entity).is_none() - && glidings.get(entity).is_none() - && rollings.get(entity).is_none() - { + if controller.attack && !a.attacking && !a.gliding && !a.rolling { attackings.insert(entity, Attacking::start()); } // Jump - if controller.jump && on_ground.is_some() && vel.0.z <= 0.0 { + if controller.jump && a.on_ground && vel.0.z <= 0.0 { jumpings.insert(entity, Jumping); } // Roll - if controller.roll - && rollings.get(entity).is_none() - && attackings.get(entity).is_none() - && glidings.get(entity).is_none() - && on_ground.is_some() - && vel.0.magnitude_squared() > 25.0 - { + if controller.roll && !a.rolling && a.on_ground && a.moving && !a.attacking && !a.gliding { rollings.insert(entity, Rolling::start()); } } diff --git a/common/src/sys/phys.rs b/common/src/sys/phys.rs index 4da571fb1b..bf41fc2775 100644 --- a/common/src/sys/phys.rs +++ b/common/src/sys/phys.rs @@ -1,5 +1,5 @@ use crate::{ - comp::{Gliding, Jumping, MoveDir, OnGround, Ori, Pos, Rolling, Stats, Vel}, + comp::{ActionState, Gliding, Jumping, MoveDir, OnGround, Ori, Pos, Rolling, Stats, Vel}, state::DeltaTime, terrain::TerrainMap, vol::{ReadVol, Vox}, @@ -22,6 +22,8 @@ const GLIDE_SPEED: f32 = 45.0; // Gravity is 9.81 * 4, so this makes gravity equal to .15 const GLIDE_ANTIGRAV: f32 = 9.81 * 3.95; +pub const MOVEMENT_THRESHOLD_VEL: f32 = 3.0; + // Integrates forces, calculates the new velocity based off of the old velocity // dt = delta time // lv = linear velocity @@ -45,6 +47,7 @@ impl<'a> System<'a> for Sys { ReadStorage<'a, MoveDir>, ReadStorage<'a, Gliding>, ReadStorage<'a, Stats>, + ReadStorage<'a, ActionState>, WriteStorage<'a, Jumping>, WriteStorage<'a, Rolling>, WriteStorage<'a, OnGround>, @@ -62,6 +65,7 @@ impl<'a> System<'a> for Sys { move_dirs, glidings, stats, + action_states, mut jumpings, mut rollings, mut on_grounds, @@ -71,11 +75,11 @@ impl<'a> System<'a> for Sys { ): Self::SystemData, ) { // Apply movement inputs - for (entity, stats, move_dir, gliding, mut pos, mut vel, mut ori) in ( + for (entity, stats, a, move_dir, mut pos, mut vel, mut ori) in ( &entities, &stats, + &action_states, move_dirs.maybe(), - glidings.maybe(), &mut positions, &mut velocities, &mut orientations, @@ -91,19 +95,25 @@ impl<'a> System<'a> for Sys { if let Some(move_dir) = move_dir { vel.0 += Vec2::broadcast(dt.0) * move_dir.0 - * match ( - on_grounds.get(entity).is_some(), - glidings.get(entity).is_some(), - rollings.get(entity).is_some(), - ) { - (true, false, false) if vel.0.magnitude() < HUMANOID_SPEED => { + * match (a.on_ground, a.gliding, a.rolling) { + (true, false, false) + if vel.0.magnitude_squared() < HUMANOID_SPEED.powf(2.0) => + { HUMANOID_ACCEL } - (false, true, false) if vel.0.magnitude() < GLIDE_SPEED => GLIDE_ACCEL, - (false, false, false) if vel.0.magnitude() < HUMANOID_AIR_SPEED => { + (false, true, false) + if vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) => + { + GLIDE_ACCEL + } + (false, false, false) + if vel.0.magnitude_squared() < HUMANOID_AIR_SPEED.powf(2.0) => + { HUMANOID_AIR_ACCEL } - (true, false, true) if vel.0.magnitude() < ROLL_SPEED => ROLL_ACCEL, + (true, false, true) if vel.0.magnitude_squared() < ROLL_SPEED.powf(2.0) => { + ROLL_ACCEL + } _ => 0.0, }; @@ -116,7 +126,10 @@ impl<'a> System<'a> for Sys { } // Glide - if gliding.is_some() && vel.0.magnitude() < GLIDE_SPEED && vel.0.z < 0.0 { + if a.gliding + && vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) + && vel.0.z < 0.0 + { let lift = GLIDE_ANTIGRAV + vel.0.z.powf(2.0) * 0.2; vel.0.z += dt.0 * lift * Vec2::::from(vel.0 * 0.15).magnitude().min(1.0); } @@ -124,7 +137,7 @@ impl<'a> System<'a> for Sys { // Roll if let Some(time) = rollings.get_mut(entity).map(|r| &mut r.time) { *time += dt.0; - if *time > 0.55 || vel.0.magnitude_squared() < 10.0 { + if *time > 0.55 || !a.moving { rollings.remove(entity); } } @@ -136,12 +149,7 @@ impl<'a> System<'a> for Sys { // Integrate forces // Friction is assumed to be a constant dependent on location - let friction = 50.0 - * if on_grounds.get(entity).is_some() { - FRIC_GROUND - } else { - FRIC_AIR - }; + let friction = 50.0 * if a.on_ground { FRIC_GROUND } else { FRIC_AIR }; vel.0 = integrate_forces(dt.0, vel.0, friction); // Basic collision with terrain @@ -184,7 +192,7 @@ impl<'a> System<'a> for Sys { false }; - let was_on_ground = on_grounds.get(entity).is_some(); + let was_on_ground = a.on_ground; on_grounds.remove(entity); // Assume we're in the air - unless we can prove otherwise pos.0.z -= 0.0001; // To force collision with the floor