diff options
Diffstat (limited to 'azalea-entity/src')
| -rwxr-xr-x | azalea-entity/src/dimensions.rs | 2 | ||||
| -rw-r--r-- | azalea-entity/src/lib.rs | 61 | ||||
| -rw-r--r-- | azalea-entity/src/plugin/mod.rs | 2 | ||||
| -rw-r--r-- | azalea-entity/src/vec_delta_codec.rs | 60 |
4 files changed, 95 insertions, 30 deletions
diff --git a/azalea-entity/src/dimensions.rs b/azalea-entity/src/dimensions.rs index b5a3f310..5236b80f 100755 --- a/azalea-entity/src/dimensions.rs +++ b/azalea-entity/src/dimensions.rs @@ -7,7 +7,7 @@ pub struct EntityDimensions { } impl EntityDimensions { - pub fn make_bounding_box(&self, pos: &Vec3) -> AABB { + pub fn make_bounding_box(&self, pos: Vec3) -> AABB { let radius = (self.width / 2.0) as f64; let height = self.height as f64; AABB { diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 1334ee0f..95a2ed3c 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -9,6 +9,7 @@ pub mod metadata; pub mod mining; pub mod particle; mod plugin; +pub mod vec_delta_codec; use std::{ fmt::Debug, @@ -17,6 +18,7 @@ use std::{ pub use attributes::Attributes; use azalea_block::BlockState; +use azalea_buf::AzBuf; use azalea_core::{ aabb::AABB, math, @@ -30,6 +32,7 @@ use derive_more::{Deref, DerefMut}; pub use dimensions::EntityDimensions; use plugin::indexing::EntityChunkPos; use uuid::Uuid; +use vec_delta_codec::VecDeltaCodec; use self::attributes::AttributeInstance; pub use crate::plugin::*; @@ -210,7 +213,7 @@ impl From<&LastSentPosition> for BlockPos { pub struct Jumping(bool); /// A component that contains the direction an entity is looking. -#[derive(Debug, Component, Copy, Clone, Default, PartialEq)] +#[derive(Debug, Component, Copy, Clone, Default, PartialEq, AzBuf)] pub struct LookDirection { /// Left and right. Aka yaw. pub y_rot: f32, @@ -245,7 +248,7 @@ impl Eq for LookDirection {} /// The physics data relating to the entity, such as position, velocity, and /// bounding box. -#[derive(Debug, Component, Clone)] +#[derive(Debug, Component, Clone, Default)] pub struct Physics { /// How fast the entity is moving. pub velocity: Vec3, @@ -257,8 +260,10 @@ pub struct Physics { /// Z acceleration. pub zza: f32, - pub on_ground: bool, - pub last_on_ground: bool, + on_ground: bool, + last_on_ground: bool, + + pub vec_delta_codec: VecDeltaCodec, /// The width and height of the entity. pub dimensions: EntityDimensions, @@ -274,7 +279,7 @@ pub struct Physics { } impl Physics { - pub fn new(dimensions: EntityDimensions, pos: &Vec3) -> Self { + pub fn new(dimensions: EntityDimensions, pos: Vec3) -> Self { Self { velocity: Vec3::default(), @@ -292,8 +297,30 @@ impl Physics { horizontal_collision: false, vertical_collision: false, + + vec_delta_codec: VecDeltaCodec::new(pos), } } + + pub fn on_ground(&self) -> bool { + self.on_ground + } + /// Updates [`Self::on_ground`] and [`Self::last_on_ground`]. + pub fn set_on_ground(&mut self, on_ground: bool) { + self.last_on_ground = self.on_ground; + self.on_ground = on_ground; + } + + /// The last value of the on_ground value. + /// + /// This is used by Azalea internally for physics, it might not work as you + /// expect since it can be influenced by packets sent by the server. + pub fn last_on_ground(&self) -> bool { + self.last_on_ground + } + pub fn set_last_on_ground(&mut self, last_on_ground: bool) { + self.last_on_ground = last_on_ground; + } } /// Marker component for entities that are dead. @@ -384,7 +411,7 @@ impl EntityBundle { position: Position(pos), chunk_pos: EntityChunkPos(ChunkPos::from(&pos)), last_sent_position: LastSentPosition(pos), - physics: Physics::new(dimensions, &pos), + physics: Physics::new(dimensions, pos), eye_height: EyeHeight(eye_height), direction: LookDirection::default(), @@ -427,25 +454,3 @@ impl FluidOnEyes { #[derive(Component, Clone, Debug, PartialEq, Deref, DerefMut)] pub struct OnClimbable(bool); - -// #[cfg(test)] -// mod tests { -// use super::*; -// use crate::PartialWorld; - -// #[test] -// fn from_mut_entity_to_ref_entity() { -// let mut world = PartialWorld::default(); -// let uuid = Uuid::from_u128(100); -// world.add_entity( -// 0, -// EntityData::new( -// uuid, -// Vec3::default(), -// EntityMetadata::Player(metadata::Player::default()), -// ), -// ); -// let entity: Entity = world.entity_mut(0).unwrap(); -// assert_eq!(entity.uuid, uuid); -// } -// } diff --git a/azalea-entity/src/plugin/mod.rs b/azalea-entity/src/plugin/mod.rs index 67763484..90d7f1c5 100644 --- a/azalea-entity/src/plugin/mod.rs +++ b/azalea-entity/src/plugin/mod.rs @@ -198,7 +198,7 @@ pub fn clamp_look_direction(mut query: Query<&mut LookDirection>) { /// Cached position in the world must be updated. pub fn update_bounding_box(mut query: Query<(&Position, &mut Physics), Changed<Position>>) { for (position, mut physics) in query.iter_mut() { - let bounding_box = physics.dimensions.make_bounding_box(position); + let bounding_box = physics.dimensions.make_bounding_box(**position); physics.bounding_box = bounding_box; } } diff --git a/azalea-entity/src/vec_delta_codec.rs b/azalea-entity/src/vec_delta_codec.rs new file mode 100644 index 00000000..51aa7cea --- /dev/null +++ b/azalea-entity/src/vec_delta_codec.rs @@ -0,0 +1,60 @@ +use azalea_core::position::Vec3; + +#[derive(Debug, Clone, Default)] +pub struct VecDeltaCodec { + base: Vec3, +} + +impl VecDeltaCodec { + pub fn new(base: Vec3) -> Self { + Self { base } + } + + pub fn decode(&self, x: i64, y: i64, z: i64) -> Vec3 { + if x == 0 && y == 0 && z == 0 { + return self.base; + } + + let new_x = if x == 0 { + self.base.x + } else { + decode(encode(self.base.x) + x) + }; + let new_y = if y == 0 { + self.base.y + } else { + decode(encode(self.base.y) + y) + }; + let new_z = if z == 0 { + self.base.z + } else { + decode(encode(self.base.z) + z) + }; + + Vec3::new(new_x, new_y, new_z) + } + + pub fn encode_x(&self, pos: Vec3) -> i64 { + encode(pos.x) - encode(self.base.x) + } + pub fn encode_y(&self, pos: Vec3) -> i64 { + encode(pos.y) - encode(self.base.y) + } + pub fn encode_z(&self, pos: Vec3) -> i64 { + encode(pos.z) - encode(self.base.z) + } + + pub fn set_base(&mut self, pos: Vec3) { + self.base = pos; + } + pub fn base(&self) -> Vec3 { + self.base + } +} + +fn encode(value: f64) -> i64 { + (value * 4096.).round() as i64 +} +fn decode(value: i64) -> f64 { + (value as f64) / 4096. +} |
