2019-09-09 19:11:40 +00:00
|
|
|
use super::phys::GRAVITY;
|
2019-07-21 12:42:45 +00:00
|
|
|
use crate::{
|
2019-08-23 10:11:37 +00:00
|
|
|
comp::{
|
2019-12-30 15:16:21 +00:00
|
|
|
ActionState, CharacterState, Controller, Mounting, MovementState::*, Ori, PhysicsState,
|
|
|
|
Pos, Stats, Vel,
|
2019-08-23 10:11:37 +00:00
|
|
|
},
|
2019-12-03 06:30:08 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2019-07-21 12:42:45 +00:00
|
|
|
state::DeltaTime,
|
2019-11-24 20:12:03 +00:00
|
|
|
sync::Uid,
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
terrain::TerrainGrid,
|
2019-07-21 12:42:45 +00:00
|
|
|
};
|
2019-11-24 20:12:03 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
|
2019-08-23 10:11:37 +00:00
|
|
|
use std::time::Duration;
|
2019-07-21 12:42:45 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-12-09 14:45:10 +00:00
|
|
|
pub const ROLL_DURATION: Duration = Duration::from_millis(600);
|
2019-08-26 18:05:30 +00:00
|
|
|
|
2019-10-14 10:22:48 +00:00
|
|
|
const HUMANOID_ACCEL: f32 = 50.0;
|
2019-07-21 12:42:45 +00:00
|
|
|
const HUMANOID_SPEED: f32 = 120.0;
|
|
|
|
const HUMANOID_AIR_ACCEL: f32 = 10.0;
|
|
|
|
const HUMANOID_AIR_SPEED: f32 = 100.0;
|
2019-09-09 19:11:40 +00:00
|
|
|
const HUMANOID_WATER_ACCEL: f32 = 70.0;
|
|
|
|
const HUMANOID_WATER_SPEED: f32 = 120.0;
|
|
|
|
const HUMANOID_CLIMB_ACCEL: f32 = 5.0;
|
2019-12-03 06:30:08 +00:00
|
|
|
const ROLL_SPEED: f32 = 17.0;
|
|
|
|
const CHARGE_SPEED: f32 = 20.0;
|
2019-07-21 12:42:45 +00:00
|
|
|
const GLIDE_ACCEL: f32 = 15.0;
|
|
|
|
const GLIDE_SPEED: f32 = 45.0;
|
2019-08-29 02:17:08 +00:00
|
|
|
const BLOCK_ACCEL: f32 = 30.0;
|
|
|
|
const BLOCK_SPEED: f32 = 75.0;
|
2019-07-21 12:42:45 +00:00
|
|
|
// Gravity is 9.81 * 4, so this makes gravity equal to .15
|
2019-09-09 19:11:40 +00:00
|
|
|
const GLIDE_ANTIGRAV: f32 = GRAVITY * 0.96;
|
|
|
|
const CLIMB_SPEED: f32 = 5.0;
|
2019-07-21 12:42:45 +00:00
|
|
|
|
|
|
|
pub const MOVEMENT_THRESHOLD_VEL: f32 = 3.0;
|
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
/// # Movement System
|
|
|
|
/// #### Applies forces, calculates new positions and velocities,7
|
|
|
|
/// #### based on Controller(Inputs) and CharacterState.
|
|
|
|
/// ----
|
|
|
|
///
|
|
|
|
/// **Writes:**
|
|
|
|
/// Pos, Vel, Ori
|
|
|
|
///
|
|
|
|
/// **Reads:**
|
|
|
|
/// Uid, Stats, Controller, PhysicsState, CharacterState, Mounting
|
2019-07-21 12:42:45 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
2019-09-09 19:11:40 +00:00
|
|
|
Entities<'a>,
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
ReadExpect<'a, TerrainGrid>,
|
2019-12-03 06:30:08 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2019-07-21 12:42:45 +00:00
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
2019-12-03 06:30:08 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
|
|
|
ReadStorage<'a, Stats>,
|
|
|
|
ReadStorage<'a, Controller>,
|
|
|
|
ReadStorage<'a, PhysicsState>,
|
|
|
|
ReadStorage<'a, CharacterState>,
|
2019-09-09 19:11:40 +00:00
|
|
|
ReadStorage<'a, Mounting>,
|
2019-07-21 12:42:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
2019-09-09 19:11:40 +00:00
|
|
|
entities,
|
2019-09-04 23:03:49 +00:00
|
|
|
_terrain,
|
2019-12-03 06:30:08 +00:00
|
|
|
_server_bus,
|
2019-07-21 12:42:45 +00:00
|
|
|
dt,
|
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
2019-12-03 06:30:08 +00:00
|
|
|
uids,
|
|
|
|
stats,
|
|
|
|
controllers,
|
|
|
|
physics_states,
|
|
|
|
character_states,
|
2019-09-09 19:11:40 +00:00
|
|
|
mountings,
|
2019-07-21 12:42:45 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
|
|
|
// Apply movement inputs
|
2019-09-09 19:11:40 +00:00
|
|
|
for (
|
|
|
|
_entity,
|
|
|
|
mut _pos,
|
|
|
|
mut vel,
|
|
|
|
mut ori,
|
2019-12-03 06:30:08 +00:00
|
|
|
_uid,
|
|
|
|
stats,
|
|
|
|
controller,
|
|
|
|
physics,
|
|
|
|
character,
|
|
|
|
mount,
|
2019-09-09 19:11:40 +00:00
|
|
|
) in (
|
|
|
|
&entities,
|
2019-07-21 12:42:45 +00:00
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
2019-12-03 06:30:08 +00:00
|
|
|
&uids,
|
|
|
|
&stats,
|
|
|
|
&controllers,
|
|
|
|
&physics_states,
|
|
|
|
&character_states,
|
2019-09-09 19:11:40 +00:00
|
|
|
mountings.maybe(),
|
2019-07-21 12:42:45 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
if stats.is_dead {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
if mount.is_some() {
|
2019-09-09 19:11:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
let inputs = &controller.inputs;
|
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
if character.action.is_roll() {
|
2019-08-25 16:47:45 +00:00
|
|
|
vel.0 = Vec3::new(0.0, 0.0, vel.0.z)
|
2019-10-17 11:21:22 +00:00
|
|
|
+ (vel.0 * Vec3::new(1.0, 1.0, 0.0)
|
2019-10-26 15:36:43 +00:00
|
|
|
+ 1.5 * inputs.move_dir.try_normalized().unwrap_or_default())
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or_default()
|
2019-10-17 11:21:22 +00:00
|
|
|
* ROLL_SPEED;
|
2019-12-03 06:30:08 +00:00
|
|
|
} else if character.action.is_charge() {
|
|
|
|
vel.0 = Vec3::new(0.0, 0.0, vel.0.z)
|
|
|
|
+ (vel.0 * Vec3::new(1.0, 1.0, 0.0)
|
|
|
|
+ 1.5 * inputs.move_dir.try_normalized().unwrap_or_default())
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or_default()
|
|
|
|
* CHARGE_SPEED;
|
|
|
|
} else if character.action.is_block() {
|
2019-08-29 02:17:08 +00:00
|
|
|
vel.0 += Vec2::broadcast(dt.0)
|
2019-10-15 04:06:14 +00:00
|
|
|
* inputs.move_dir
|
2019-09-04 23:03:49 +00:00
|
|
|
* match physics.on_ground {
|
|
|
|
true if vel.0.magnitude_squared() < BLOCK_SPEED.powf(2.0) => BLOCK_ACCEL,
|
2019-08-29 02:17:08 +00:00
|
|
|
_ => 0.0,
|
|
|
|
}
|
2019-08-25 16:08:00 +00:00
|
|
|
} else {
|
|
|
|
// Move player according to move_dir
|
|
|
|
vel.0 += Vec2::broadcast(dt.0)
|
2019-10-15 04:06:14 +00:00
|
|
|
* inputs.move_dir
|
2019-08-25 16:08:00 +00:00
|
|
|
* match (physics.on_ground, &character.movement) {
|
|
|
|
(true, Run) if vel.0.magnitude_squared() < HUMANOID_SPEED.powf(2.0) => {
|
|
|
|
HUMANOID_ACCEL
|
|
|
|
}
|
2019-09-09 19:11:40 +00:00
|
|
|
(false, Climb) if vel.0.magnitude_squared() < HUMANOID_SPEED.powf(2.0) => {
|
|
|
|
HUMANOID_CLIMB_ACCEL
|
|
|
|
}
|
2019-08-25 16:08:00 +00:00
|
|
|
(false, Glide) if vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) => {
|
|
|
|
GLIDE_ACCEL
|
|
|
|
}
|
2019-12-09 14:45:10 +00:00
|
|
|
(false, Fall) | (false, Jump)
|
2019-08-25 16:08:00 +00:00
|
|
|
if vel.0.magnitude_squared() < HUMANOID_AIR_SPEED.powf(2.0) =>
|
|
|
|
{
|
|
|
|
HUMANOID_AIR_ACCEL
|
|
|
|
}
|
2019-09-09 19:11:40 +00:00
|
|
|
(false, Swim)
|
|
|
|
if vel.0.magnitude_squared() < HUMANOID_WATER_SPEED.powf(2.0) =>
|
|
|
|
{
|
|
|
|
HUMANOID_WATER_ACCEL
|
|
|
|
}
|
2019-08-25 16:08:00 +00:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
|
|
|
}
|
2019-07-21 12:42:45 +00:00
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
// Set direction based on move direction when on the ground
|
2019-12-03 06:30:08 +00:00
|
|
|
let ori_dir = if
|
|
|
|
//character.action.is_wield() ||
|
|
|
|
character.action.is_attack() || character.action.is_block() {
|
2019-10-15 04:06:14 +00:00
|
|
|
Vec2::from(inputs.look_dir).normalized()
|
2019-09-09 19:11:40 +00:00
|
|
|
} else if let (Climb, Some(wall_dir)) = (character.movement, physics.on_wall) {
|
|
|
|
if Vec2::<f32>::from(wall_dir).magnitude_squared() > 0.001 {
|
|
|
|
Vec2::from(wall_dir).normalized()
|
|
|
|
} else {
|
2019-12-01 23:40:05 +00:00
|
|
|
Vec2::from(inputs.move_dir)
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
2019-12-20 04:28:37 +00:00
|
|
|
} else if let Glide = character.movement {
|
|
|
|
// Note: non-gliding forces will also affect velocity and thus orientation
|
|
|
|
// producing potentially unexpected changes in direction
|
|
|
|
Vec2::from(vel.0)
|
2019-08-25 10:10:42 +00:00
|
|
|
} else {
|
2019-12-30 15:16:21 +00:00
|
|
|
if let ActionState::Roll { .. } = character.action {
|
|
|
|
// So can can't spin/slip around while rolling
|
|
|
|
Vec2::from(vel.0)
|
|
|
|
} else {
|
|
|
|
Vec2::from(inputs.move_dir)
|
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
};
|
2019-08-24 17:58:28 +00:00
|
|
|
|
2019-08-23 10:11:37 +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(),
|
2019-10-14 10:22:48 +00:00
|
|
|
if physics.on_ground { 9.0 } else { 2.0 } * dt.0,
|
2019-08-23 10:11:37 +00:00
|
|
|
);
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Glide
|
2019-08-23 10:11:37 +00:00
|
|
|
if character.movement == Glide
|
2019-09-09 19:11:40 +00:00
|
|
|
&& Vec2::<f32>::from(vel.0).magnitude_squared() < GLIDE_SPEED.powf(2.0)
|
2019-08-23 10:11:37 +00:00
|
|
|
&& vel.0.z < 0.0
|
|
|
|
{
|
2019-09-09 19:11:40 +00:00
|
|
|
let lift = GLIDE_ANTIGRAV + vel.0.z.abs().powf(2.0) * 0.15;
|
|
|
|
vel.0.z += dt.0
|
|
|
|
* lift
|
|
|
|
* (Vec2::<f32>::from(vel.0).magnitude() * 0.075)
|
|
|
|
.min(1.0)
|
|
|
|
.max(0.2);
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:11:40 +00:00
|
|
|
// Climb
|
|
|
|
if let (true, Some(_wall_dir)) = (
|
2019-11-29 15:20:35 +00:00
|
|
|
(inputs.climb.is_pressed() | inputs.climb_down.is_pressed())
|
|
|
|
&& vel.0.z <= CLIMB_SPEED,
|
2019-09-09 19:11:40 +00:00
|
|
|
physics.on_wall,
|
|
|
|
) {
|
2019-11-29 15:20:35 +00:00
|
|
|
if inputs.climb_down.is_pressed() && !inputs.climb.is_pressed() {
|
2019-09-09 19:11:40 +00:00
|
|
|
vel.0 -= dt.0 * vel.0.map(|e| e.abs().powf(1.5) * e.signum() * 6.0);
|
2019-11-29 15:20:35 +00:00
|
|
|
} else if inputs.climb.is_pressed() && !inputs.climb_down.is_pressed() {
|
2019-09-09 19:11:40 +00:00
|
|
|
vel.0.z = (vel.0.z + dt.0 * GRAVITY * 1.25).min(CLIMB_SPEED);
|
|
|
|
} else {
|
|
|
|
vel.0.z = vel.0.z + dt.0 * GRAVITY * 1.5;
|
|
|
|
vel.0 = Lerp::lerp(
|
|
|
|
vel.0,
|
|
|
|
Vec3::zero(),
|
|
|
|
30.0 * dt.0 / (1.0 - vel.0.z.min(0.0) * 5.0),
|
|
|
|
);
|
|
|
|
}
|
2019-08-24 08:56:28 +00:00
|
|
|
}
|
2019-09-09 19:11:40 +00:00
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
if character.movement == Swim && inputs.jump.is_pressed() {
|
|
|
|
vel.0.z = (vel.0.z + dt.0 * GRAVITY * 1.25).min(HUMANOID_WATER_SPEED);
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|