2019-03-02 03:48:30 +00:00
|
|
|
use crate::{
|
2019-06-14 15:27:05 +00:00
|
|
|
comp::{Gliding, Jumping, MoveDir, OnGround, Ori, Pos, Rolling, Stats, Vel},
|
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-16 13:57:01 +00:00
|
|
|
const HUMANOID_ACCEL: f32 = 70.0;
|
|
|
|
const HUMANOID_SPEED: f32 = 120.0;
|
2019-06-11 19:28:25 +00:00
|
|
|
const HUMANOID_AIR_ACCEL: f32 = 10.0;
|
|
|
|
const HUMANOID_AIR_SPEED: f32 = 100.0;
|
|
|
|
const HUMANOID_JUMP_ACCEL: f32 = 16.0;
|
2019-06-27 14:21:41 +00:00
|
|
|
const ROLL_ACCEL: f32 = 120.0;
|
2019-06-16 15:37:31 +00:00
|
|
|
const ROLL_SPEED: f32 = 550.0;
|
2019-06-11 19:28:25 +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;
|
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> {
|
2019-06-27 14:21:41 +00:00
|
|
|
lv.z = (lv.z - GRAVITY * dt).max(-50.0);
|
2019-06-04 15:42:31 +00:00
|
|
|
|
2019-06-27 14:21:41 +00:00
|
|
|
let linear_damp = (1.0 - dt * damp).max(0.0);
|
2019-06-04 15:42:31 +00:00
|
|
|
|
2019-06-27 14:21:41 +00:00
|
|
|
lv * linear_damp
|
2019-06-04 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
ReadStorage<'a, MoveDir>,
|
|
|
|
ReadStorage<'a, Gliding>,
|
2019-06-11 04:08:55 +00:00
|
|
|
ReadStorage<'a, Stats>,
|
2019-06-16 17:00:44 +00:00
|
|
|
WriteStorage<'a, Jumping>,
|
2019-06-16 12:56:07 +00:00
|
|
|
WriteStorage<'a, Rolling>,
|
2019-06-13 18:09:50 +00:00
|
|
|
WriteStorage<'a, OnGround>,
|
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
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,
|
|
|
|
move_dirs,
|
|
|
|
glidings,
|
|
|
|
stats,
|
2019-06-16 17:00:44 +00:00
|
|
|
mut jumpings,
|
2019-06-16 12:56:07 +00:00
|
|
|
mut rollings,
|
2019-06-13 18:09:50 +00:00
|
|
|
mut on_grounds,
|
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
2019-06-09 19:33:20 +00:00
|
|
|
) {
|
2019-06-11 19:28:25 +00:00
|
|
|
// Apply movement inputs
|
2019-06-16 17:00:44 +00:00
|
|
|
for (entity, stats, move_dir, gliding, mut pos, mut vel, mut ori) in (
|
2019-06-13 18:09:50 +00:00
|
|
|
&entities,
|
|
|
|
&stats,
|
|
|
|
move_dirs.maybe(),
|
|
|
|
glidings.maybe(),
|
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
|
|
|
)
|
|
|
|
.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
|
2019-06-13 18:09:50 +00:00
|
|
|
* match (
|
|
|
|
on_grounds.get(entity).is_some(),
|
|
|
|
glidings.get(entity).is_some(),
|
2019-06-16 02:16:22 +00:00
|
|
|
rollings.get(entity).is_some(),
|
2019-06-13 18:09:50 +00:00
|
|
|
) {
|
2019-06-16 02:16:22 +00:00
|
|
|
(true, false, false) if vel.0.magnitude() < HUMANOID_SPEED => {
|
|
|
|
HUMANOID_ACCEL
|
|
|
|
}
|
|
|
|
(false, true, false) if vel.0.magnitude() < GLIDE_SPEED => GLIDE_ACCEL,
|
|
|
|
(false, false, false) if vel.0.magnitude() < HUMANOID_AIR_SPEED => {
|
2019-06-11 19:28:25 +00:00
|
|
|
HUMANOID_AIR_ACCEL
|
|
|
|
}
|
2019-06-16 02:16:22 +00:00
|
|
|
(true, false, true) if vel.0.magnitude() < ROLL_SPEED => ROLL_ACCEL,
|
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump
|
2019-06-16 17:00:44 +00:00
|
|
|
if jumpings.get(entity).is_some() {
|
2019-06-11 19:28:25 +00:00
|
|
|
vel.0.z = HUMANOID_JUMP_ACCEL;
|
2019-06-16 17:00:44 +00:00
|
|
|
jumpings.remove(entity);
|
2019-06-11 19:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-06-16 13:57:01 +00:00
|
|
|
// Roll
|
2019-06-16 12:56:07 +00:00
|
|
|
if let Some(time) = rollings.get_mut(entity).map(|r| &mut r.time) {
|
|
|
|
*time += dt.0;
|
2019-06-29 12:10:52 +00:00
|
|
|
if *time > 0.55 || vel.0.magnitude_squared() < 10.0 {
|
2019-06-16 12:56:07 +00:00
|
|
|
rollings.remove(entity);
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 04:08:55 +00:00
|
|
|
|
2019-06-11 19:28:25 +00:00
|
|
|
// 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-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-13 18:09:50 +00:00
|
|
|
* if on_grounds.get(entity).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-06-27 14:37:33 +00:00
|
|
|
let player_rad = 0.3f32; // half-width of the player's AABB
|
|
|
|
let player_height = 1.55f32;
|
|
|
|
|
|
|
|
// Probe distances
|
|
|
|
let hdist = player_rad.ceil() as i32;
|
|
|
|
let vdist = player_height.ceil() as i32;
|
|
|
|
// Neighbouring blocks iterator
|
|
|
|
let near_iter = (-hdist..=hdist)
|
|
|
|
.map(move |i| (-hdist..=hdist).map(move |j| (0..=vdist).map(move |k| (i, j, k))))
|
2019-06-25 16:13:30 +00:00
|
|
|
.flatten()
|
|
|
|
.flatten();
|
|
|
|
|
2019-06-26 00:28:30 +00:00
|
|
|
// Function for determining whether the player at a specific position collides with the ground
|
2019-06-25 18:57:44 +00:00
|
|
|
let collision_with = |pos: Vec3<f32>, near_iter| {
|
|
|
|
for (i, j, k) in near_iter {
|
|
|
|
let block_pos = pos.map(|e| e.floor() as i32) + Vec3::new(i, j, k);
|
|
|
|
|
2019-06-25 20:26:04 +00:00
|
|
|
if terrain
|
|
|
|
.get(block_pos)
|
|
|
|
.map(|vox| !vox.is_empty())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2019-06-26 09:40:07 +00:00
|
|
|
let player_aabb = Aabb {
|
2019-06-26 00:28:30 +00:00
|
|
|
min: pos + Vec3::new(-player_rad, -player_rad, 0.0),
|
|
|
|
max: pos + Vec3::new(player_rad, player_rad, player_height),
|
|
|
|
};
|
|
|
|
let block_aabb = Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32) + 1.0,
|
|
|
|
};
|
2019-06-25 18:57:44 +00:00
|
|
|
|
2019-06-26 09:40:07 +00:00
|
|
|
if player_aabb.collides_with_aabb(block_aabb) {
|
2019-06-25 20:26:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-25 18:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2019-06-27 14:21:41 +00:00
|
|
|
let was_on_ground = on_grounds.get(entity).is_some();
|
2019-06-26 10:50:55 +00:00
|
|
|
on_grounds.remove(entity); // Assume we're in the air - unless we can prove otherwise
|
2019-06-25 18:57:44 +00:00
|
|
|
pos.0.z -= 0.0001; // To force collision with the floor
|
|
|
|
|
|
|
|
let mut on_ground = false;
|
2019-06-26 10:50:55 +00:00
|
|
|
let mut attempts = 0; // Don't loop infinitely here
|
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Don't jump too far at once
|
|
|
|
let increments = ((vel.0 * dt.0).map(|e| e.abs()).reduce_partial_max() / 0.3)
|
|
|
|
.ceil()
|
|
|
|
.max(1.0);
|
|
|
|
for _ in 0..increments as usize {
|
|
|
|
pos.0 += vel.0 * dt.0 / increments;
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// While the player is colliding with the terrain...
|
|
|
|
while collision_with(pos.0, near_iter.clone()) && attempts < 32 {
|
|
|
|
// Calculate the player's AABB
|
|
|
|
let player_aabb = Aabb {
|
|
|
|
min: pos.0 + Vec3::new(-player_rad, -player_rad, 0.0),
|
|
|
|
max: pos.0 + Vec3::new(player_rad, player_rad, player_height),
|
|
|
|
};
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Determine the block that we are colliding with most (based on minimum collision axis)
|
|
|
|
let (block_pos, block_aabb) = near_iter
|
|
|
|
.clone()
|
|
|
|
// Calculate the block's position in world space
|
|
|
|
.map(|(i, j, k)| pos.0.map(|e| e.floor() as i32) + Vec3::new(i, j, k))
|
|
|
|
// Calculate the AABB of the block
|
|
|
|
.map(|block_pos| {
|
|
|
|
(
|
|
|
|
block_pos,
|
|
|
|
Aabb {
|
|
|
|
min: block_pos.map(|e| e as f32),
|
|
|
|
max: block_pos.map(|e| e as f32) + 1.0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
// Determine whether the block's AABB collides with the player's AABB
|
|
|
|
.filter(|(_, block_aabb)| block_aabb.collides_with_aabb(player_aabb))
|
|
|
|
// Make sure the block is actually solid
|
|
|
|
.filter(|(block_pos, _)| {
|
|
|
|
terrain
|
|
|
|
.get(*block_pos)
|
|
|
|
.map(|vox| !vox.is_empty())
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
// Find the maximum of the minimum collision axes (this bit is weird, trust me that it works)
|
|
|
|
.max_by_key(|(_, block_aabb)| {
|
|
|
|
((player_aabb.collision_vector_with_aabb(*block_aabb) / vel.0)
|
|
|
|
.map(|e| e.abs())
|
|
|
|
.reduce_partial_min()
|
|
|
|
* 1000.0) as i32
|
|
|
|
})
|
|
|
|
.expect("Collision detected, but no colliding blocks found!");
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Find the intrusion vector of the collision
|
|
|
|
let dir = player_aabb.collision_vector_with_aabb(block_aabb);
|
2019-06-25 16:13:30 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// Determine an appropriate resolution vector (i.e: the minimum distance needed to push out of the block)
|
|
|
|
let max_axis = dir.map(|e| e.abs()).reduce_partial_min();
|
|
|
|
let resolve_dir = -dir.map(|e| if e.abs() == max_axis { e } else { 0.0 });
|
2019-06-26 09:40:07 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// When the resolution direction is pointing upwards, we must be on the ground
|
2019-06-27 14:21:41 +00:00
|
|
|
if resolve_dir.z > 0.0 && vel.0.z <= 0.0 {
|
2019-06-26 21:43:47 +00:00
|
|
|
on_ground = true;
|
|
|
|
}
|
2019-06-26 09:43:03 +00:00
|
|
|
|
2019-06-26 21:43:47 +00:00
|
|
|
// When the resolution direction is non-vertical, we must be colliding with a wall
|
|
|
|
// If the space above is free...
|
|
|
|
if resolve_dir.z == 0.0
|
|
|
|
&& !collision_with(pos.0 + Vec3::unit_z() * 1.1, near_iter.clone())
|
|
|
|
{
|
|
|
|
// ...block-hop!
|
|
|
|
pos.0.z = (pos.0.z + 1.0).ceil();
|
|
|
|
on_ground = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Resolve the collision normally
|
|
|
|
pos.0 += resolve_dir;
|
|
|
|
vel.0 = vel
|
|
|
|
.0
|
|
|
|
.map2(resolve_dir, |e, d| if d == 0.0 { e } else { 0.0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
attempts += 1;
|
|
|
|
}
|
2019-04-23 22:48:31 +00:00
|
|
|
}
|
2019-06-25 18:57:44 +00:00
|
|
|
|
|
|
|
if on_ground {
|
|
|
|
on_grounds.insert(entity, OnGround);
|
2019-06-26 10:50:55 +00:00
|
|
|
// If we're not on the ground but the space below us is free, then "snap" to the ground
|
2019-06-26 00:28:30 +00:00
|
|
|
} else if collision_with(pos.0 - Vec3::unit_z() * 1.0, near_iter.clone())
|
|
|
|
&& vel.0.z < 0.0
|
|
|
|
&& vel.0.z > -1.0
|
2019-06-27 14:21:41 +00:00
|
|
|
&& was_on_ground
|
2019-06-26 00:28:30 +00:00
|
|
|
{
|
2019-06-25 20:26:04 +00:00
|
|
|
pos.0.z = (pos.0.z - 0.05).floor();
|
|
|
|
on_grounds.insert(entity, OnGround);
|
|
|
|
}
|
2019-04-23 22:48:31 +00:00
|
|
|
}
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
|
|
|
}
|