2019-03-02 03:48:30 +00:00
|
|
|
use crate::{
|
2019-05-28 16:37:49 +00:00
|
|
|
comp::{
|
2019-06-11 19:28:25 +00:00
|
|
|
phys::{Ori, Pos, Vel},
|
|
|
|
Gliding, Jumping, MoveDir, OnGround, Stats,
|
2019-05-28 16:37:49 +00:00
|
|
|
},
|
2019-03-02 03:48:30 +00:00
|
|
|
state::DeltaTime,
|
2019-04-23 22:48:31 +00:00
|
|
|
terrain::TerrainMap,
|
2019-04-29 20:37:19 +00:00
|
|
|
vol::{ReadVol, Vox},
|
2019-03-02 03:48:30 +00:00
|
|
|
};
|
2019-06-09 19:33:20 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
|
2019-04-29 20:37:19 +00:00
|
|
|
use vek::*;
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2019-05-01 16:55:29 +00:00
|
|
|
const GRAVITY: f32 = 9.81 * 4.0;
|
2019-06-04 16:09:34 +00:00
|
|
|
const FRIC_GROUND: f32 = 0.15;
|
|
|
|
const FRIC_AIR: f32 = 0.015;
|
2019-06-11 19:28:25 +00:00
|
|
|
const HUMANOID_ACCEL: f32 = 100.0;
|
|
|
|
const HUMANOID_SPEED: f32 = 500.0;
|
|
|
|
const HUMANOID_AIR_ACCEL: f32 = 10.0;
|
|
|
|
const HUMANOID_AIR_SPEED: f32 = 100.0;
|
|
|
|
const HUMANOID_JUMP_ACCEL: f32 = 16.0;
|
|
|
|
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;
|
2019-04-23 22:48:31 +00:00
|
|
|
|
2019-06-04 15:42:31 +00:00
|
|
|
// Integrates forces, calculates the new velocity based off of the old velocity
|
|
|
|
// dt = delta time
|
|
|
|
// lv = linear velocity
|
|
|
|
// damp = linear damping
|
|
|
|
// Friction is a type of damping.
|
|
|
|
fn integrate_forces(dt: f32, mut lv: Vec3<f32>, damp: f32) -> Vec3<f32> {
|
|
|
|
lv.z -= (GRAVITY * dt).max(-50.0);
|
|
|
|
|
|
|
|
let mut linear_damp = 1.0 - dt * damp;
|
|
|
|
|
|
|
|
if linear_damp < 0.0
|
|
|
|
// reached zero in the given time
|
|
|
|
{
|
|
|
|
linear_damp = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lv *= linear_damp;
|
|
|
|
|
|
|
|
lv
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
/// This system applies forces and calculates new positions and velocities.
|
2019-06-09 19:33:20 +00:00
|
|
|
pub struct Sys;
|
2019-04-16 21:06:33 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2019-03-02 03:48:30 +00:00
|
|
|
type SystemData = (
|
2019-06-09 19:33:20 +00:00
|
|
|
Entities<'a>,
|
2019-04-23 22:48:31 +00:00
|
|
|
ReadExpect<'a, TerrainMap>,
|
2019-03-02 03:48:30 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2019-06-11 19:28:25 +00:00
|
|
|
WriteStorage<'a, OnGround>,
|
2019-04-23 22:48:31 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
2019-06-11 19:28:25 +00:00
|
|
|
WriteStorage<'a, Ori>,
|
|
|
|
ReadStorage<'a, MoveDir>,
|
|
|
|
ReadStorage<'a, Jumping>,
|
|
|
|
ReadStorage<'a, Gliding>,
|
|
|
|
WriteStorage<'a, Stats>,
|
2019-03-02 03:48:30 +00:00
|
|
|
);
|
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-06-11 19:28:25 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
terrain,
|
|
|
|
dt,
|
|
|
|
mut on_grounds,
|
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
|
|
|
move_dirs,
|
|
|
|
jumpings,
|
|
|
|
glidings,
|
|
|
|
stats,
|
|
|
|
): Self::SystemData,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-06-11 19:28:25 +00:00
|
|
|
// Apply movement inputs
|
|
|
|
for (entity, mut pos, mut vel, mut ori, mut on_ground, move_dir, jumping, gliding, stats) in
|
|
|
|
(
|
|
|
|
&entities,
|
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
|
|
|
on_grounds.maybe(),
|
|
|
|
move_dirs.maybe(),
|
|
|
|
jumpings.maybe(),
|
|
|
|
glidings.maybe(),
|
|
|
|
&stats,
|
|
|
|
)
|
|
|
|
.join()
|
2019-06-09 19:33:20 +00:00
|
|
|
{
|
2019-06-11 19:28:25 +00:00
|
|
|
// Disable while dead TODO: Replace with client states?
|
2019-05-28 16:37:49 +00:00
|
|
|
if stats.is_dead {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// Move player according to move_dir
|
|
|
|
if let Some(move_dir) = move_dir {
|
|
|
|
vel.0 += Vec2::broadcast(dt.0)
|
|
|
|
* move_dir.0
|
|
|
|
* match (on_ground.is_some(), gliding.is_some()) {
|
|
|
|
(true, false) if vel.0.magnitude() < HUMANOID_SPEED => HUMANOID_ACCEL,
|
|
|
|
(false, true) if vel.0.magnitude() < GLIDE_SPEED => GLIDE_ACCEL,
|
|
|
|
(false, false) if vel.0.magnitude() < HUMANOID_AIR_SPEED => {
|
|
|
|
HUMANOID_AIR_ACCEL
|
|
|
|
}
|
|
|
|
_ => 0.0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump
|
|
|
|
if jumping.is_some() {
|
|
|
|
vel.0.z = HUMANOID_JUMP_ACCEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Glide
|
|
|
|
if gliding.is_some() && vel.0.magnitude() < GLIDE_SPEED && vel.0.z < 0.0 {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set direction based on velocity
|
|
|
|
if vel.0.magnitude_squared() != 0.0 {
|
|
|
|
ori.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0);
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
// Movement
|
|
|
|
pos.0 += vel.0 * dt.0;
|
|
|
|
|
|
|
|
// Update OnGround component
|
|
|
|
if terrain
|
2019-05-23 14:03:21 +00:00
|
|
|
.get((pos.0 - Vec3::unit_z() * 0.1).map(|e| e.floor() as i32))
|
|
|
|
.map(|vox| !vox.is_empty())
|
|
|
|
.unwrap_or(false)
|
2019-06-09 19:33:20 +00:00
|
|
|
&& vel.0.z <= 0.0
|
|
|
|
{
|
2019-06-11 19:28:25 +00:00
|
|
|
on_ground = Some(&OnGround);
|
2019-06-09 19:33:20 +00:00
|
|
|
} else {
|
2019-06-11 19:28:25 +00:00
|
|
|
on_ground = None;
|
2019-06-09 19:33:20 +00:00
|
|
|
}
|
2019-06-04 18:14:10 +00:00
|
|
|
|
2019-06-04 15:42:31 +00:00
|
|
|
// Integrate forces
|
|
|
|
// Friction is assumed to be a constant dependent on location
|
2019-06-09 19:33:20 +00:00
|
|
|
let friction = 50.0
|
2019-06-11 19:28:25 +00:00
|
|
|
* if on_ground.is_some() {
|
2019-06-09 19:33:20 +00:00
|
|
|
FRIC_GROUND
|
|
|
|
} else {
|
|
|
|
FRIC_AIR
|
|
|
|
};
|
2019-06-04 15:42:31 +00:00
|
|
|
vel.0 = integrate_forces(dt.0, vel.0, friction);
|
2019-04-23 22:48:31 +00:00
|
|
|
|
|
|
|
// Basic collision with terrain
|
2019-05-17 22:42:44 +00:00
|
|
|
let mut i = 0.0;
|
2019-04-23 22:48:31 +00:00
|
|
|
while terrain
|
2019-04-25 15:04:36 +00:00
|
|
|
.get(pos.0.map(|e| e.floor() as i32))
|
2019-04-23 22:48:31 +00:00
|
|
|
.map(|vox| !vox.is_empty())
|
2019-04-29 20:37:19 +00:00
|
|
|
.unwrap_or(false)
|
2019-05-17 22:42:44 +00:00
|
|
|
&& i < 6000.0 * dt.0
|
2019-04-23 22:48:31 +00:00
|
|
|
{
|
2019-05-09 15:15:46 +00:00
|
|
|
pos.0.z += 0.0025;
|
2019-04-23 22:48:31 +00:00
|
|
|
vel.0.z = 0.0;
|
2019-05-17 22:42:44 +00:00
|
|
|
i += 1.0;
|
2019-04-23 22:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
|
|
|
}
|