2020-12-13 17:11:55 +00:00
|
|
|
use crate::{uid::Uid, util::Dir};
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-01-07 20:25:12 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage, NullStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2019-07-29 19:54:58 +00:00
|
|
|
use vek::*;
|
2019-01-02 19:22:01 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Position
|
2019-07-29 16:19:08 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2019-01-02 19:22:01 +00:00
|
|
|
pub struct Pos(pub Vec3<f32>);
|
|
|
|
|
|
|
|
impl Component for Pos {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-01-02 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Velocity
|
2019-07-29 16:19:08 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2019-01-02 19:22:01 +00:00
|
|
|
pub struct Vel(pub Vec3<f32>);
|
|
|
|
|
|
|
|
impl Component for Vel {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-01-02 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 18:45:16 +00:00
|
|
|
// Orientation
|
2019-07-29 16:19:08 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2020-03-28 01:31:22 +00:00
|
|
|
pub struct Ori(pub Dir);
|
2019-01-02 19:22:01 +00:00
|
|
|
|
2020-07-15 15:45:47 +00:00
|
|
|
impl Ori {
|
2020-08-04 12:03:01 +00:00
|
|
|
pub fn vec(&self) -> &Vec3<f32> { &*self.0 }
|
2020-07-15 15:45:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 18:45:16 +00:00
|
|
|
impl Component for Ori {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-03-05 00:00:11 +00:00
|
|
|
}
|
2019-04-14 20:30:27 +00:00
|
|
|
|
2020-11-04 10:02:45 +00:00
|
|
|
/// Cache of Velocity (of last tick) * dt (of curent tick)
|
|
|
|
/// It's updated and read in physics sys to speed up entity<->entity collisions
|
|
|
|
/// no need to send it via network
|
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq)]
|
|
|
|
pub struct PreviousVelDtCache(pub Vec3<f32>);
|
|
|
|
|
|
|
|
impl Component for PreviousVelDtCache {
|
|
|
|
type Storage = IdvStorage<Self>;
|
|
|
|
}
|
|
|
|
|
2019-08-02 18:56:37 +00:00
|
|
|
// Scale
|
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Scale(pub f32);
|
|
|
|
|
|
|
|
impl Component for Scale {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-08-02 18:56:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:22:39 +00:00
|
|
|
// Mass
|
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Mass(pub f32);
|
|
|
|
|
|
|
|
impl Component for Mass {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-09-25 20:22:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
// Mass
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum Collider {
|
2020-04-26 14:44:03 +00:00
|
|
|
Box { radius: f32, z_min: f32, z_max: f32 },
|
2020-04-26 14:37:13 +00:00
|
|
|
Point,
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:24:44 +00:00
|
|
|
impl Collider {
|
|
|
|
pub fn get_radius(&self) -> f32 {
|
|
|
|
match self {
|
|
|
|
Collider::Box { radius, .. } => *radius,
|
|
|
|
Collider::Point => 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:22:26 +00:00
|
|
|
pub fn get_z_limits(&self, modifier: f32) -> (f32, f32) {
|
2020-08-24 17:24:44 +00:00
|
|
|
match self {
|
2020-11-06 01:22:26 +00:00
|
|
|
Collider::Box { z_min, z_max, .. } => (*z_min * modifier, *z_max * modifier),
|
2020-08-24 17:24:44 +00:00
|
|
|
Collider::Point => (0.0, 0.0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:37:13 +00:00
|
|
|
impl Component for Collider {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2020-04-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 20:59:36 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Gravity(pub f32);
|
|
|
|
|
|
|
|
impl Component for Gravity {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-10-17 20:59:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 19:28:35 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Sticky;
|
|
|
|
|
|
|
|
impl Component for Sticky {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, NullStorage<Self>>;
|
2019-10-02 19:28:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
// PhysicsState
|
2020-08-24 17:24:44 +00:00
|
|
|
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
2019-08-23 10:11:37 +00:00
|
|
|
pub struct PhysicsState {
|
|
|
|
pub on_ground: bool,
|
2020-04-26 14:37:13 +00:00
|
|
|
pub on_ceiling: bool,
|
2019-09-09 19:11:40 +00:00
|
|
|
pub on_wall: Option<Vec3<f32>>,
|
2020-08-24 17:24:44 +00:00
|
|
|
pub touch_entities: Vec<Uid>,
|
2020-11-06 15:33:20 +00:00
|
|
|
pub in_liquid: Option<f32>, // Depth
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:06:01 +00:00
|
|
|
impl PhysicsState {
|
2020-08-25 10:01:17 +00:00
|
|
|
pub fn reset(&mut self) {
|
2020-08-24 17:24:44 +00:00
|
|
|
// Avoid allocation overhead!
|
|
|
|
let mut touch_entities = std::mem::take(&mut self.touch_entities);
|
|
|
|
touch_entities.clear();
|
|
|
|
*self = Self {
|
|
|
|
touch_entities,
|
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 14:06:01 +00:00
|
|
|
pub fn on_surface(&self) -> Option<Vec3<f32>> {
|
|
|
|
self.on_ground
|
|
|
|
.then_some(-Vec3::unit_z())
|
|
|
|
.or_else(|| self.on_ceiling.then_some(Vec3::unit_z()))
|
|
|
|
.or(self.on_wall)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
impl Component for PhysicsState {
|
2020-08-13 08:12:14 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// ForceUpdate
|
2019-07-30 05:24:36 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
2019-04-14 20:30:27 +00:00
|
|
|
pub struct ForceUpdate;
|
|
|
|
|
|
|
|
impl Component for ForceUpdate {
|
|
|
|
type Storage = NullStorage<Self>;
|
|
|
|
}
|