2019-07-21 12:42:45 +00:00
|
|
|
use crate::{
|
2019-08-23 10:11:37 +00:00
|
|
|
comp::{
|
|
|
|
ActionState::*, CharacterState, Controller, MovementState::*, Ori, PhysicsState, Pos,
|
|
|
|
Stats, Vel,
|
|
|
|
},
|
2019-07-21 12:42:45 +00:00
|
|
|
state::DeltaTime,
|
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-09-04 23:03:49 +00:00
|
|
|
use specs::{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-08-26 18:05:30 +00:00
|
|
|
pub const ROLL_DURATION: Duration = Duration::from_millis(600);
|
|
|
|
|
2019-07-21 12:42:45 +00:00
|
|
|
const HUMANOID_ACCEL: f32 = 70.0;
|
|
|
|
const HUMANOID_SPEED: f32 = 120.0;
|
|
|
|
const HUMANOID_AIR_ACCEL: f32 = 10.0;
|
|
|
|
const HUMANOID_AIR_SPEED: f32 = 100.0;
|
2019-08-25 16:08:00 +00:00
|
|
|
const ROLL_SPEED: f32 = 13.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
|
|
|
|
const GLIDE_ANTIGRAV: f32 = 9.81 * 3.95;
|
|
|
|
|
|
|
|
pub const MOVEMENT_THRESHOLD_VEL: f32 = 3.0;
|
|
|
|
|
|
|
|
/// This system applies forces and calculates new positions and velocities.
|
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
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-07-21 12:42:45 +00:00
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
ReadStorage<'a, Stats>,
|
2019-08-23 10:11:37 +00:00
|
|
|
ReadStorage<'a, Controller>,
|
|
|
|
ReadStorage<'a, PhysicsState>,
|
|
|
|
WriteStorage<'a, CharacterState>,
|
2019-07-21 12:42:45 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
2019-09-04 23:03:49 +00:00
|
|
|
_terrain,
|
2019-07-21 12:42:45 +00:00
|
|
|
dt,
|
|
|
|
stats,
|
2019-08-23 10:11:37 +00:00
|
|
|
controllers,
|
|
|
|
physics_states,
|
|
|
|
mut character_states,
|
2019-07-21 12:42:45 +00:00
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
|
|
|
// Apply movement inputs
|
2019-09-04 23:03:49 +00:00
|
|
|
for (stats, controller, physics, mut character, mut _pos, mut vel, mut ori) in (
|
2019-07-21 12:42:45 +00:00
|
|
|
&stats,
|
2019-08-23 10:11:37 +00:00
|
|
|
&controllers,
|
|
|
|
&physics_states,
|
|
|
|
&mut character_states,
|
2019-07-21 12:42:45 +00:00
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
if stats.is_dead {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-25 16:08:00 +00:00
|
|
|
if character.movement.is_roll() {
|
2019-08-25 16:47:45 +00:00
|
|
|
vel.0 = Vec3::new(0.0, 0.0, vel.0.z)
|
|
|
|
+ controller
|
|
|
|
.move_dir
|
|
|
|
.try_normalized()
|
2019-08-25 19:22:56 +00:00
|
|
|
.unwrap_or(Vec2::from(vel.0).try_normalized().unwrap_or_default())
|
2019-08-25 16:47:45 +00:00
|
|
|
* ROLL_SPEED
|
2019-08-29 02:17:08 +00:00
|
|
|
}
|
2019-08-31 02:37:27 +00:00
|
|
|
if character.action.is_block() || character.action.is_attack() {
|
2019-08-29 02:17:08 +00:00
|
|
|
vel.0 += Vec2::broadcast(dt.0)
|
|
|
|
* controller.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)
|
|
|
|
* controller.move_dir
|
|
|
|
* match (physics.on_ground, &character.movement) {
|
|
|
|
(true, Run) if vel.0.magnitude_squared() < HUMANOID_SPEED.powf(2.0) => {
|
|
|
|
HUMANOID_ACCEL
|
|
|
|
}
|
|
|
|
(false, Glide) if vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0) => {
|
|
|
|
GLIDE_ACCEL
|
|
|
|
}
|
|
|
|
(false, Jump)
|
|
|
|
if vel.0.magnitude_squared() < HUMANOID_AIR_SPEED.powf(2.0) =>
|
|
|
|
{
|
|
|
|
HUMANOID_AIR_ACCEL
|
|
|
|
}
|
|
|
|
_ => 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-08-25 12:29:36 +00:00
|
|
|
let ori_dir = if character.action.is_wield()
|
|
|
|
|| character.action.is_attack()
|
|
|
|
|| character.action.is_block()
|
|
|
|
{
|
2019-08-24 17:58:28 +00:00
|
|
|
Vec2::from(controller.look_dir).normalized()
|
2019-08-25 10:10:42 +00:00
|
|
|
} else {
|
|
|
|
Vec2::from(vel.0)
|
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(),
|
|
|
|
if physics.on_ground { 12.0 } else { 2.0 } * dt.0,
|
|
|
|
);
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Glide
|
2019-08-23 10:11:37 +00:00
|
|
|
if character.movement == Glide
|
|
|
|
&& vel.0.magnitude_squared() < GLIDE_SPEED.powf(2.0)
|
|
|
|
&& vel.0.z < 0.0
|
|
|
|
{
|
|
|
|
character.action = Idle;
|
2019-07-21 12:42:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roll
|
2019-08-23 10:11:37 +00:00
|
|
|
if let Roll { time_left } = &mut character.movement {
|
|
|
|
character.action = Idle;
|
|
|
|
if *time_left == Duration::default() || vel.0.magnitude_squared() < 10.0 {
|
|
|
|
character.movement = Run;
|
|
|
|
} else {
|
|
|
|
*time_left = time_left
|
|
|
|
.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
.unwrap_or_default();
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-24 08:56:28 +00:00
|
|
|
|
|
|
|
if physics.on_ground && (character.movement == Jump || character.movement == Glide) {
|
|
|
|
character.movement = Stand;
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:55:10 +00:00
|
|
|
if !physics.on_ground
|
|
|
|
&& (character.movement == Stand
|
|
|
|
|| character.movement.is_roll()
|
|
|
|
|| character.movement == Run)
|
|
|
|
{
|
2019-08-24 08:56:28 +00:00
|
|
|
character.movement = Jump;
|
|
|
|
}
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|