aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/position.rs
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-core/src/position.rs')
-rw-r--r--azalea-core/src/position.rs157
1 files changed, 104 insertions, 53 deletions
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 {