diff options
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/delta.rs | 59 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 157 |
2 files changed, 152 insertions, 64 deletions
diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 41923ffb..c0056411 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,15 +1,41 @@ use crate::EntityPos; pub use azalea_buf::McBuf; -/// Only works for up to 8 blocks -#[derive(Clone, Debug, McBuf)] +pub trait PositionDeltaTrait { + fn x(&self) -> f64; + fn y(&self) -> f64; + fn z(&self) -> f64; +} + +#[derive(Clone, Debug, McBuf, Default)] pub struct PositionDelta { - xa: i16, - ya: i16, - za: i16, + pub xa: f64, + pub ya: f64, + pub za: f64, +} + +/// Only works for up to 8 blocks +#[derive(Clone, Debug, McBuf, Default)] +pub struct PositionDelta8 { + pub xa: i16, + pub ya: i16, + pub za: i16, +} + +impl PositionDeltaTrait for PositionDelta { + fn x(&self) -> f64 { + self.xa + } + fn y(&self) -> f64 { + self.ya + } + fn z(&self) -> f64 { + self.za + } } -impl PositionDelta { +impl PositionDelta8 { + #[deprecated] pub fn float(&self) -> (f64, f64, f64) { ( (self.xa as f64) / 4096.0, @@ -19,13 +45,24 @@ impl PositionDelta { } } +impl PositionDeltaTrait for PositionDelta8 { + fn x(&self) -> f64 { + (self.xa as f64) / 4096.0 + } + fn y(&self) -> f64 { + (self.ya as f64) / 4096.0 + } + fn z(&self) -> f64 { + (self.za as f64) / 4096.0 + } +} + impl EntityPos { - pub fn with_delta(&self, delta: &PositionDelta) -> EntityPos { - let (x, y, z) = delta.float(); + pub fn with_delta(&self, delta: &dyn PositionDeltaTrait) -> EntityPos { EntityPos { - x: self.x + x, - y: self.y + y, - z: self.z + z, + x: self.x + delta.x(), + y: self.y + delta.y(), + z: self.z + delta.z(), } } } diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 8caa5799..de8e2516 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -5,6 +5,12 @@ use std::{ ops::Rem, }; +pub trait PositionXYZ<T> { + fn add_x(&self, n: T) -> Self; + fn add_y(&self, n: T) -> Self; + fn add_z(&self, n: T) -> Self; +} + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct BlockPos { pub x: i32, @@ -30,6 +36,30 @@ impl Rem<i32> for BlockPos { } } +impl PositionXYZ<i32> for BlockPos { + fn add_x(&self, n: i32) -> Self { + BlockPos { + x: self.x + n, + y: self.y, + z: self.z, + } + } + fn add_y(&self, n: i32) -> Self { + BlockPos { + x: self.x, + y: self.y + n, + z: self.z, + } + } + fn add_z(&self, n: i32) -> Self { + BlockPos { + x: self.x, + y: self.y, + z: self.z + n, + } + } +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct ChunkPos { pub x: i32, @@ -42,15 +72,6 @@ impl ChunkPos { } } -impl From<&BlockPos> for ChunkPos { - fn from(pos: &BlockPos) -> Self { - ChunkPos { - x: pos.x.div_floor(16), - z: pos.z.div_floor(16), - } - } -} - /// The coordinates of a chunk section in the world. #[derive(Clone, Copy, Debug, Default)] pub struct ChunkSectionPos { @@ -64,23 +85,6 @@ impl ChunkSectionPos { ChunkSectionPos { x, y, z } } } - -impl From<BlockPos> for ChunkSectionPos { - fn from(pos: BlockPos) -> Self { - ChunkSectionPos { - x: pos.x.div_floor(16), - y: pos.y.div_floor(16), - z: pos.z.div_floor(16), - } - } -} - -impl From<ChunkSectionPos> for ChunkPos { - fn from(pos: ChunkSectionPos) -> Self { - ChunkPos { x: pos.x, z: pos.z } - } -} - /// The coordinates of a block inside a chunk. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct ChunkBlockPos { @@ -94,17 +98,6 @@ impl ChunkBlockPos { ChunkBlockPos { x, y, z } } } - -impl From<&BlockPos> for ChunkBlockPos { - fn from(pos: &BlockPos) -> Self { - ChunkBlockPos { - x: pos.x.rem_euclid(16).abs() as u8, - y: pos.y, - z: pos.z.rem_euclid(16).abs() as u8, - } - } -} - /// The coordinates of a block inside a chunk section. #[derive(Clone, Copy, Debug, Default)] pub struct ChunkSectionBlockPos { @@ -122,6 +115,80 @@ impl ChunkSectionBlockPos { } } +/// A block pos with an attached dimension +#[derive(Debug, Clone)] +pub struct GlobalPos { + pub pos: BlockPos, + // this is actually a ResourceKey in Minecraft, but i don't think it matters? + pub dimension: ResourceLocation, +} + +#[derive(Debug, Clone, Copy, Default)] +pub struct EntityPos { + pub x: f64, + pub y: f64, + pub z: f64, +} + +impl PositionXYZ<f64> for EntityPos { + fn add_x(&self, n: f64) -> Self { + EntityPos { + x: self.x + n, + y: self.y, + z: self.z, + } + } + fn add_y(&self, n: f64) -> Self { + EntityPos { + x: self.x, + y: self.y + n, + z: self.z, + } + } + fn add_z(&self, n: f64) -> Self { + EntityPos { + x: self.x, + y: self.y, + z: self.z + n, + } + } +} + +impl From<&BlockPos> for ChunkPos { + fn from(pos: &BlockPos) -> Self { + ChunkPos { + x: pos.x.div_floor(16), + z: pos.z.div_floor(16), + } + } +} + +impl From<BlockPos> for ChunkSectionPos { + fn from(pos: BlockPos) -> Self { + ChunkSectionPos { + x: pos.x.div_floor(16), + y: pos.y.div_floor(16), + z: pos.z.div_floor(16), + } + } +} + +impl From<ChunkSectionPos> for ChunkPos { + fn from(pos: ChunkSectionPos) -> Self { + ChunkPos { x: pos.x, z: pos.z } + } +} + +impl From<&BlockPos> for ChunkBlockPos { + fn from(pos: &BlockPos) -> Self { + ChunkBlockPos { + x: pos.x.rem_euclid(16).abs() as u8, + y: pos.y, + z: pos.z.rem_euclid(16).abs() as u8, + } + } +} + impl From<&BlockPos> for ChunkSectionBlockPos { fn from(pos: &BlockPos) -> Self { ChunkSectionBlockPos { @@ -141,22 +208,6 @@ impl From<&ChunkBlockPos> for ChunkSectionBlockPos { } } } - -/// A block pos with an attached dimension -#[derive(Debug, Clone)] -pub struct GlobalPos { - pub pos: BlockPos, - // this is actually a ResourceKey in Minecraft, but i don't think it matters? - pub dimension: ResourceLocation, -} - -#[derive(Debug, Clone, Default)] -pub struct EntityPos { - pub x: f64, - pub y: f64, - pub z: f64, -} - impl From<&EntityPos> for BlockPos { fn from(pos: &EntityPos) -> Self { BlockPos { |
