veloren/common/src/sys/movement.rs

160 lines
5.2 KiB
Rust
Raw Normal View History

2019-07-21 12:42:45 +00:00
use crate::{
comp::{
ActionState::*, CharacterState, Controller, MovementState::*, Ori, PhysicsState, Pos,
Stats, Vel,
},
2019-07-21 12:42:45 +00:00
state::DeltaTime,
terrain::TerrainMap,
vol::{ReadVol, Vox},
};
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
use std::time::Duration;
2019-07-21 12:42:45 +00:00
use vek::*;
2019-08-26 18:05:30 +00:00
pub const ROLL_DURATION: Duration = Duration::from_millis(600);
2019-07-21 12:42:45 +00:00
const HUMANOID_ACCEL: f32 = 70.0;
const HUMANOID_SPEED: f32 = 120.0;
const HUMANOID_AIR_ACCEL: f32 = 10.0;
const HUMANOID_AIR_SPEED: f32 = 100.0;
2019-08-25 16:08:00 +00:00
const ROLL_SPEED: f32 = 13.0;
2019-07-21 12:42:45 +00:00
const GLIDE_ACCEL: f32 = 15.0;
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;
/// This system applies forces and calculates new positions and velocities.
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
ReadExpect<'a, TerrainMap>,
Read<'a, DeltaTime>,
ReadStorage<'a, Stats>,
ReadStorage<'a, Controller>,
ReadStorage<'a, PhysicsState>,
WriteStorage<'a, CharacterState>,
2019-07-21 12:42:45 +00:00
WriteStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Ori>,
);
fn run(
&mut self,
(
entities,
terrain,
dt,
stats,
controllers,
physics_states,
mut character_states,
2019-07-21 12:42:45 +00:00
mut positions,
mut velocities,
mut orientations,
): Self::SystemData,
) {
// Apply movement inputs
for (entity, stats, controller, physics, mut character, mut pos, mut vel, mut ori) in (
2019-07-21 12:42:45 +00:00
&entities,
&stats,
&controllers,
&physics_states,
&mut character_states,
2019-07-21 12:42:45 +00:00
&mut positions,
&mut velocities,
&mut orientations,
)
.join()
{
if stats.is_dead {
continue;
}
2019-08-25 16:08:00 +00:00
if character.movement.is_roll() {
2019-08-25 16:47:45 +00:00
vel.0 = Vec3::new(0.0, 0.0, vel.0.z)
+ controller
.move_dir
.try_normalized()
.unwrap_or(Vec2::from(vel.0).try_normalized().unwrap_or_default())
2019-08-25 16:47:45 +00:00
* ROLL_SPEED
2019-08-25 16:08:00 +00:00
} else {
// Move player according to move_dir
vel.0 += Vec2::broadcast(dt.0)
* controller.move_dir
* match (physics.on_ground, &character.movement) {
(true, Run) if vel.0.magnitude_squared() < HUMANOID_SPEED.powf(2.0) => {
HUMANOID_ACCEL
}
(false, Glide) if vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) => {
GLIDE_ACCEL
}
(false, Jump)
if vel.0.magnitude_squared() < HUMANOID_AIR_SPEED.powf(2.0) =>
{
HUMANOID_AIR_ACCEL
}
_ => 0.0,
};
}
2019-07-21 12:42:45 +00:00
// Set direction based on move direction when on the ground
2019-08-25 12:29:36 +00:00
let ori_dir = if character.action.is_wield()
|| character.action.is_attack()
|| character.action.is_block()
{
2019-08-24 17:58:28 +00:00
Vec2::from(controller.look_dir).normalized()
} else {
Vec2::from(vel.0)
};
2019-08-24 17:58:28 +00:00
if ori_dir.magnitude_squared() > 0.0001
&& (ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared()
> 0.001
{
ori.0 = vek::ops::Slerp::slerp(
ori.0,
ori_dir.into(),
if physics.on_ground { 12.0 } else { 2.0 } * dt.0,
);
2019-07-21 12:42:45 +00:00
}
// Glide
if character.movement == Glide
&& vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0)
&& vel.0.z < 0.0
{
character.action = Idle;
2019-07-21 12:42:45 +00:00
let lift = GLIDE_ANTIGRAV + vel.0.z.powf(2.0) * 0.2;
vel.0.z += dt.0 * lift * Vec2::<f32>::from(vel.0 * 0.15).magnitude().min(1.0);
}
// Roll
if let Roll { time_left } = &mut character.movement {
character.action = Idle;
if *time_left == Duration::default() || vel.0.magnitude_squared() < 10.0 {
character.movement = Run;
} else {
*time_left = time_left
.checked_sub(Duration::from_secs_f32(dt.0))
.unwrap_or_default();
2019-07-21 12:42:45 +00:00
}
}
if physics.on_ground && (character.movement == Jump || character.movement == Glide) {
character.movement = Stand;
}
2019-08-26 20:55:10 +00:00
if !physics.on_ground
&& (character.movement == Stand
|| character.movement.is_roll()
|| character.movement == Run)
{
character.movement = Jump;
}
2019-07-21 12:42:45 +00:00
}
}
}