2019-04-23 22:48:31 +00:00
|
|
|
use vek::*;
|
|
|
|
use specs::{Join, Read, ReadStorage, System, WriteStorage, ReadExpect};
|
2019-03-02 03:48:30 +00:00
|
|
|
use crate::{
|
|
|
|
comp::phys::{Pos, Vel},
|
|
|
|
state::DeltaTime,
|
2019-04-23 22:48:31 +00:00
|
|
|
terrain::TerrainMap,
|
|
|
|
vol::{Vox, ReadVol},
|
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-04-23 22:48:31 +00:00
|
|
|
const GRAVITY: f32 = 9.81;
|
|
|
|
|
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-04-23 22:48:31 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
2019-03-02 03:48:30 +00:00
|
|
|
);
|
|
|
|
|
2019-04-23 22:48:31 +00:00
|
|
|
fn run(&mut self, (terrain, dt, mut positions, mut velocities): Self::SystemData) {
|
|
|
|
for (pos, vel) in (&mut positions, &mut velocities).join() {
|
|
|
|
// Gravity
|
|
|
|
vel.0.z -= GRAVITY * dt.0 as f32;
|
|
|
|
|
|
|
|
// Movement
|
|
|
|
pos.0 += vel.0 * dt.0 as f32;
|
|
|
|
|
|
|
|
// Basic collision with terrain
|
|
|
|
while terrain
|
|
|
|
.get(pos.0.map(|e| e as i32))
|
|
|
|
.map(|vox| !vox.is_empty())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
pos.0.z += 0.05;
|
|
|
|
vel.0.z = 0.0;
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
|
|
|
}
|