From e84893b07a65ebca8d4027b386d3dfd87bcfd009 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 19 Jun 2022 13:42:01 -0500 Subject: Fix issues with entity pos conversion to block pos --- azalea-core/src/position.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'azalea-core/src') diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 43881f1c..7b2c01da 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -2,7 +2,7 @@ use std::ops::Rem; use crate::resource_location::ResourceLocation; -#[derive(Clone, Copy, Debug, Default)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct BlockPos { pub x: i32, pub y: i32, @@ -157,9 +157,9 @@ pub struct EntityPos { impl From<&EntityPos> for BlockPos { fn from(pos: &EntityPos) -> Self { BlockPos { - x: pos.x as i32, - y: pos.y as i32, - z: pos.z as i32, + x: pos.x.floor() as i32, + y: pos.y.floor() as i32, + z: pos.z.floor() as i32, } } } @@ -188,14 +188,25 @@ mod tests { assert_eq!(chunk_block_pos, ChunkBlockPos::new(5, 78, 14)); } + #[test] + fn test_from_entity_pos_to_block_pos() { + let entity_pos = EntityPos { + x: 31.5, + y: 80.0, + z: -16.1, + }; + let block_pos = BlockPos::from(&entity_pos); + assert_eq!(block_pos, BlockPos::new(31, 80, -17)); + } + #[test] fn test_from_entity_pos_to_chunk_pos() { let entity_pos = EntityPos { - x: 33.5, - y: 77.0, - z: -19.6, + x: 31.5, + y: 80.0, + z: -16.1, }; let chunk_pos = ChunkPos::from(&entity_pos); - assert_eq!(chunk_pos, ChunkPos::new(2, -2)); + assert_eq!(chunk_pos, ChunkPos::new(1, -2)); } } -- cgit v1.2.3