parent
7c235d6d61
commit
536ff0ebb6
5 changed files with 145 additions and 21 deletions
@ -0,0 +1,110 @@ |
||||
use std::ops::{Add, AddAssign, Sub, SubAssign}; |
||||
|
||||
use bevy::prelude::*; |
||||
|
||||
use serde::{Deserialize, Serialize}; |
||||
|
||||
#[derive(Component, Reflect, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Hash, Debug)] |
||||
pub struct BlockPos { |
||||
pub x: i32, |
||||
pub y: i32, |
||||
pub z: i32, |
||||
} |
||||
|
||||
impl BlockPos { |
||||
pub fn new(x: i32, y: i32, z: i32) -> Self { |
||||
Self { x, y, z } |
||||
} |
||||
|
||||
pub fn distance_squared(self, other: BlockPos) -> i32 { |
||||
(self - other).length_squared() |
||||
} |
||||
|
||||
pub fn distance(self, other: BlockPos) -> f32 { |
||||
(self.distance_squared(other) as f32).sqrt() |
||||
} |
||||
|
||||
pub fn center(self) -> Vec3 { |
||||
IVec3::from(self).as_vec3() + Vec3::ONE / 2. |
||||
} |
||||
} |
||||
|
||||
impl std::fmt::Display for BlockPos { |
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
||||
write!(f, "[{}, {}, {}]", self.x, self.y, self.z) |
||||
} |
||||
} |
||||
|
||||
// conversion
|
||||
|
||||
impl From<(i32, i32, i32)> for BlockPos { |
||||
fn from((x, y, z): (i32, i32, i32)) -> Self { |
||||
Self::new(x, y, z) |
||||
} |
||||
} |
||||
impl From<BlockPos> for (i32, i32, i32) { |
||||
fn from(BlockPos { x, y, z }: BlockPos) -> Self { |
||||
(x, y, z) |
||||
} |
||||
} |
||||
|
||||
impl From<[i32; 3]> for BlockPos { |
||||
fn from([x, y, z]: [i32; 3]) -> Self { |
||||
Self::new(x, y, z) |
||||
} |
||||
} |
||||
impl From<BlockPos> for [i32; 3] { |
||||
fn from(BlockPos { x, y, z }: BlockPos) -> Self { |
||||
[x, y, z] |
||||
} |
||||
} |
||||
|
||||
impl From<IVec3> for BlockPos { |
||||
fn from(IVec3 { x, y, z }: IVec3) -> Self { |
||||
Self::new(x, y, z) |
||||
} |
||||
} |
||||
impl From<BlockPos> for IVec3 { |
||||
fn from(BlockPos { x, y, z }: BlockPos) -> Self { |
||||
IVec3::new(x, y, z) |
||||
} |
||||
} |
||||
|
||||
// addition / subtraction
|
||||
|
||||
impl Add<IVec3> for BlockPos { |
||||
type Output = BlockPos; |
||||
fn add(self, rhs: IVec3) -> Self::Output { |
||||
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) |
||||
} |
||||
} |
||||
|
||||
impl AddAssign<IVec3> for BlockPos { |
||||
fn add_assign(&mut self, rhs: IVec3) { |
||||
self.x += rhs.x; |
||||
self.y += rhs.y; |
||||
self.z += rhs.z; |
||||
} |
||||
} |
||||
|
||||
impl Sub<IVec3> for BlockPos { |
||||
type Output = BlockPos; |
||||
fn sub(self, rhs: IVec3) -> Self::Output { |
||||
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) |
||||
} |
||||
} |
||||
|
||||
impl SubAssign<IVec3> for BlockPos { |
||||
fn sub_assign(&mut self, rhs: IVec3) { |
||||
self.x -= rhs.x; |
||||
self.y -= rhs.y; |
||||
self.z -= rhs.z; |
||||
} |
||||
} |
||||
|
||||
impl Sub<BlockPos> for BlockPos { |
||||
type Output = IVec3; |
||||
fn sub(self, rhs: BlockPos) -> Self::Output { |
||||
IVec3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) |
||||
} |
||||
} |
||||
@ -0,0 +1,3 @@ |
||||
mod block_pos; |
||||
|
||||
pub use block_pos::BlockPos; |
||||
Loading…
Reference in new issue