2019-03-02 03:48:30 +00:00
|
|
|
use crate::{
|
2019-05-28 16:37:49 +00:00
|
|
|
comp::{
|
|
|
|
phys::{Pos, Vel},
|
|
|
|
Stats,
|
|
|
|
},
|
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-04-29 20:37:19 +00:00
|
|
|
use specs::{Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
|
|
|
|
use vek::*;
|
2019-03-02 03:48:30 +00:00
|
|
|
|
|
|
|
// Basic ECS physics system
|
2019-04-16 21:06:33 +00:00
|
|
|
pub struct Sys;
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2019-05-01 16:55:29 +00:00
|
|
|
const GRAVITY: f32 = 9.81 * 4.0;
|
2019-04-23 22:48:31 +00:00
|
|
|
|
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-04-23 22:48:31 +00:00
|
|
|
ReadExpect<'a, TerrainMap>,
|
2019-03-02 03:48:30 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2019-05-28 16:37:49 +00:00
|
|
|
ReadStorage<'a, Stats>,
|
2019-04-23 22:48:31 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
2019-03-02 03:48:30 +00:00
|
|
|
);
|
|
|
|
|
2019-05-28 16:37:49 +00:00
|
|
|
fn run(&mut self, (terrain, dt, stats, mut positions, mut velocities): Self::SystemData) {
|
|
|
|
for (stats, pos, vel) in (&stats, &mut positions, &mut velocities).join() {
|
|
|
|
// Disable while dead TODO: Replace with client states
|
|
|
|
if stats.is_dead {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-23 14:03:21 +00:00
|
|
|
// Handle held-down control
|
|
|
|
let on_ground = terrain
|
|
|
|
.get((pos.0 - Vec3::unit_z() * 0.1).map(|e| e.floor() as i32))
|
|
|
|
.map(|vox| !vox.is_empty())
|
|
|
|
.unwrap_or(false)
|
|
|
|
&& vel.0.z <= 0.0;
|
|
|
|
|
|
|
|
// Friction
|
|
|
|
// Will never make the character go backwards since it is interpolating towards 0
|
|
|
|
let friction = if on_ground { 0.15 } else { 0.015 };
|
|
|
|
let mul = 50.0;
|
|
|
|
let z = vel.0.z;
|
|
|
|
vel.0 = Vec3::lerp(vel.0, Vec3::new(0.0, 0.0, 0.0), friction * dt.0 * mul);
|
|
|
|
vel.0.z = z;
|
|
|
|
|
2019-04-23 22:48:31 +00:00
|
|
|
// Gravity
|
2019-05-01 11:28:26 +00:00
|
|
|
vel.0.z = (vel.0.z - GRAVITY * dt.0).max(-50.0);
|
2019-04-23 22:48:31 +00:00
|
|
|
|
|
|
|
// Movement
|
2019-05-01 11:28:26 +00:00
|
|
|
pos.0 += vel.0 * dt.0;
|
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
|
|
|
}
|
|
|
|
}
|