diff --git a/crates/core/block/Cargo.toml b/crates/core/block/Cargo.toml index be5861d..4fdac45 100644 --- a/crates/core/block/Cargo.toml +++ b/crates/core/block/Cargo.toml @@ -13,14 +13,9 @@ maintenance = { status = "passively-maintained" } [dependencies] rimecraft-registry = { path = "../registry" } -glam = "0.25" rimecraft-state = { path = "../state" } -rimecraft-edcode = { path = "../../util/edcode", optional = true } -serde = { version = "1.0", optional = true } [features] -serde = ["dep:serde"] -edcode = ["dep:rimecraft-edcode"] [lints] workspace = true diff --git a/crates/core/block/src/lib.rs b/crates/core/block/src/lib.rs index 325b7a9..09de6d5 100644 --- a/crates/core/block/src/lib.rs +++ b/crates/core/block/src/lib.rs @@ -5,10 +5,6 @@ use rimecraft_state::{States, StatesMut}; use std::marker::PhantomData; -mod pos; - -pub use pos::BlockPos; - pub use rimecraft_state as state; /// Block containing settings and the state manager. diff --git a/crates/flare3d/src/state.rs b/crates/flare3d/src/state.rs index 232164f..f9b3386 100644 --- a/crates/flare3d/src/state.rs +++ b/crates/flare3d/src/state.rs @@ -354,7 +354,6 @@ pub struct State<'s> { instances: Vec, instance_buffer: wgpu::Buffer, depth_texture: Texture, - } impl<'s> State<'s> { @@ -674,8 +673,5 @@ impl<'s> State<'s> { pub fn input(&mut self, event: &WindowEvent) -> bool { self.camera_controller.process_events(event) - } - - } diff --git a/crates/util/serde-update/src/erased.rs b/crates/util/serde-update/src/erased.rs index f29c161..b048eda 100644 --- a/crates/util/serde-update/src/erased.rs +++ b/crates/util/serde-update/src/erased.rs @@ -12,11 +12,11 @@ macro_rules! __internal_update_from_erased { fn update( &mut self, deserializer: D, - ) -> Result<(), >::Error> + ) -> Result<(), >::Error> where - D: serde::Deserializer<'de>, + D: ::serde::Deserializer<'de>, { - use serde::de::Error; + use ::serde::de::Error; self.erased_update(&mut >::erase( deserializer, )) @@ -49,7 +49,7 @@ macro_rules! update_trait_object { } /// [`Update`] but type erased. -pub trait ErasedUpdate<'de>: erased_serde::Serialize { +pub trait ErasedUpdate<'de> { /// Update this type from an erased deserializer. /// /// # Errors @@ -62,8 +62,6 @@ pub trait ErasedUpdate<'de>: erased_serde::Serialize { ) -> Result<(), erased_serde::Error>; } -erased_serde::serialize_trait_object!(<'de> ErasedUpdate<'de>); - crate::update_trait_object!(ErasedUpdate<'de>); impl<'de, T> ErasedUpdate<'de> for T @@ -81,16 +79,6 @@ where struct ErasedWrapper<'a, 'de>(pub &'a mut dyn ErasedUpdate<'de>); -impl serde::Serialize for ErasedWrapper<'_, '_> { - #[inline] - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - self.0.serialize(serializer) - } -} - impl<'de> Update<'de> for ErasedWrapper<'_, 'de> { #[inline] fn update(&mut self, deserializer: D) -> Result<(), >::Error> diff --git a/crates/util/voxel-math/Cargo.toml b/crates/util/voxel-math/Cargo.toml new file mode 100644 index 0000000..20bb2b6 --- /dev/null +++ b/crates/util/voxel-math/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "rimecraft-voxel-math" +version = "0.1.0" +edition = "2021" +authors = ["JieningYu "] +description = "Voxel math library for rimecraft" +repository = "https://github.com/rimecraft-rs/rimecraft/" +license = "AGPL-3.0-or-later" +categories = ["game-development"] + +[badges] +maintenance = { status = "passively-maintained" } + +[dependencies] +rimecraft-edcode = { path = "../../util/edcode", optional = true } +serde = { version = "1.0", optional = true } +glam = "0.25" + +[features] +default = ["serde", "edcode"] +serde = ["dep:serde"] +edcode = ["dep:rimecraft-edcode"] + +[lints] +workspace = true diff --git a/crates/core/block/src/pos.rs b/crates/util/voxel-math/src/block_pos.rs similarity index 89% rename from crates/core/block/src/pos.rs rename to crates/util/voxel-math/src/block_pos.rs index eff8da6..f02ebcf 100644 --- a/crates/core/block/src/pos.rs +++ b/crates/util/voxel-math/src/block_pos.rs @@ -1,3 +1,5 @@ +//! Position types. + use glam::IVec3; /// A position of a block in a three-dimensional volume. @@ -12,25 +14,25 @@ impl BlockPos { /// Creates a new block position. #[inline] - pub fn new(x: i32, y: i32, z: i32) -> Self { + pub const fn new(x: i32, y: i32, z: i32) -> Self { Self(IVec3::new(x, y, z)) } /// Returns the x-coordinate of the position. #[inline] - pub fn x(&self) -> i32 { + pub const fn x(&self) -> i32 { self.0.x } /// Returns the y-coordinate of the position. #[inline] - pub fn y(&self) -> i32 { + pub const fn y(&self) -> i32 { self.0.y } /// Returns the z-coordinate of the position. #[inline] - pub fn z(&self) -> i32 { + pub const fn z(&self) -> i32 { self.0.z } } @@ -95,38 +97,6 @@ impl std::ops::SubAssign for BlockPos { } } -impl std::ops::Add for BlockPos { - type Output = BlockPos; - - #[inline] - fn add(self, rhs: BlockPos) -> BlockPos { - BlockPos(self.0 + rhs.0) - } -} - -impl std::ops::AddAssign for BlockPos { - #[inline] - fn add_assign(&mut self, rhs: BlockPos) { - self.0 += rhs.0; - } -} - -impl std::ops::Sub for BlockPos { - type Output = BlockPos; - - #[inline] - fn sub(self, rhs: BlockPos) -> BlockPos { - BlockPos(self.0 - rhs.0) - } -} - -impl std::ops::SubAssign for BlockPos { - #[inline] - fn sub_assign(&mut self, rhs: BlockPos) { - self.0 -= rhs.0; - } -} - const LEN_BITS_X: i32 = 1 + (1i32 << (32 - (30000000i32 - 1).leading_zeros())).ilog2() as i32; const LEN_BITS_Y: i32 = 64 - LEN_BITS_X - LEN_BITS_Z; const LEN_BITS_Z: i32 = LEN_BITS_X; @@ -160,7 +130,7 @@ impl From for BlockPos { } #[cfg(feature = "serde")] -mod serde { +mod _serde { use ::serde::{Deserialize, Serialize}; use super::*; @@ -243,8 +213,6 @@ mod serde { #[cfg(feature = "edcode")] mod edcode { - use std::convert::Infallible; - use rimecraft_edcode::{Decode, Encode}; use super::*; diff --git a/crates/util/voxel-math/src/chunk_pos.rs b/crates/util/voxel-math/src/chunk_pos.rs new file mode 100644 index 0000000..f9dfb57 --- /dev/null +++ b/crates/util/voxel-math/src/chunk_pos.rs @@ -0,0 +1,101 @@ +/// A pair of two integers representing the X and Z coordinates of a chunk. +/// +/// Chunk positions are usually serialized as an [`u64`]. +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] +pub struct ChunkPos { + /// The X coordinate of the chunk. + pub x: i32, + /// The Z coordinate of the chunk. + pub z: i32, +} + +impl ChunkPos { + /// Creates a new `ChunkPos` with the given X and Z coordinates. + #[inline] + pub const fn new(x: i32, z: i32) -> Self { + Self { x, z } + } + + /// Creates a new `ChunkPos` from the given region coordinates. + #[inline] + pub const fn from_region(x: i32, z: i32) -> Self { + Self::new(x << 5, z << 5) + } + + /// Creates a new `ChunkPos` from the given region center coordinates. + #[inline] + pub const fn from_region_center(x: i32, z: i32) -> Self { + Self::new((x << 5) + 31, (z << 5) + 31) + } + + /// Returns the x-coordinate of the position. + #[inline] + pub const fn x(&self) -> i32 { + self.x + } + + /// Returns the z-coordinate of the position. + #[inline] + pub const fn z(&self) -> i32 { + self.z + } +} + +impl From<(i32, i32)> for ChunkPos { + #[inline] + fn from((x, z): (i32, i32)) -> Self { + Self::new(x, z) + } +} + +impl From for u64 { + #[inline] + fn from(ChunkPos { x, z }: ChunkPos) -> Self { + x as u64 & 0xFFFF_FFFF_u64 | (z as u64 & 0xFFFF_FFFF_u64) << 32 + } +} + +impl From for ChunkPos { + #[inline] + fn from(value: u64) -> Self { + Self { + x: (value & 0xFFFF_FFFF_u64) as i32, + z: (value >> 32u64 & 0xFFFF_FFFF_u64) as i32, + } + } +} + +impl From for (i32, i32) { + #[inline] + fn from(ChunkPos { x, z }: ChunkPos) -> Self { + (x, z) + } +} + +#[cfg(feature = "edcode")] +mod _edcode { + use rimecraft_edcode::{Decode, Encode}; + + use crate::ChunkPos; + + impl Encode for ChunkPos { + #[inline] + fn encode(&self, mut buf: B) -> Result<(), std::io::Error> + where + B: rimecraft_edcode::bytes::BufMut, + { + buf.put_u64((*self).into()); + Ok(()) + } + } + + impl Decode for ChunkPos { + #[inline] + fn decode(mut buf: B) -> Result + where + B: rimecraft_edcode::bytes::Buf, + { + Ok(buf.get_u64().into()) + } + } +} diff --git a/crates/util/voxel-math/src/chunk_section_pos.rs b/crates/util/voxel-math/src/chunk_section_pos.rs new file mode 100644 index 0000000..f1e5d04 --- /dev/null +++ b/crates/util/voxel-math/src/chunk_section_pos.rs @@ -0,0 +1,91 @@ +use glam::IVec3; + +/// Position of a chunk section. +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] +pub struct ChunkSectionPos(pub IVec3); + +impl ChunkSectionPos { + /// Creates a new `ChunkSectionPos` with the given X, Y, and Z coordinates. + #[inline] + pub const fn new(x: i32, y: i32, z: i32) -> Self { + Self(IVec3::new(x, y, z)) + } + + /// Returns the x-coordinate of the position. + #[inline] + pub const fn x(&self) -> i32 { + self.0.x + } + + /// Returns the y-coordinate of the position. + #[inline] + pub const fn y(&self) -> i32 { + self.0.y + } + + /// Returns the z-coordinate of the position. + #[inline] + pub const fn z(&self) -> i32 { + self.0.z + } +} + +impl From for ChunkSectionPos { + #[inline] + fn from(pos: IVec3) -> Self { + Self(pos) + } +} + +impl From for IVec3 { + #[inline] + fn from(pos: ChunkSectionPos) -> Self { + pos.0 + } +} + +impl From<(i32, i32, i32)> for ChunkSectionPos { + #[inline] + fn from((x, y, z): (i32, i32, i32)) -> Self { + Self::new(x, y, z) + } +} + +impl From for (i32, i32, i32) { + #[inline] + fn from(pos: ChunkSectionPos) -> (i32, i32, i32) { + (pos.x(), pos.y(), pos.z()) + } +} + +impl std::ops::Add for ChunkSectionPos { + type Output = Self; + + #[inline] + fn add(self, rhs: IVec3) -> Self { + Self(self.0 + rhs) + } +} + +impl std::ops::AddAssign for ChunkSectionPos { + #[inline] + fn add_assign(&mut self, rhs: IVec3) { + self.0 += rhs; + } +} + +impl std::ops::Sub for ChunkSectionPos { + type Output = Self; + + #[inline] + fn sub(self, rhs: IVec3) -> Self { + Self(self.0 - rhs) + } +} + +impl std::ops::SubAssign for ChunkSectionPos { + #[inline] + fn sub_assign(&mut self, rhs: IVec3) { + self.0 -= rhs; + } +} diff --git a/crates/util/voxel-math/src/lib.rs b/crates/util/voxel-math/src/lib.rs new file mode 100644 index 0000000..3d5467a --- /dev/null +++ b/crates/util/voxel-math/src/lib.rs @@ -0,0 +1,9 @@ +//! Voxel math library. + +mod block_pos; +mod chunk_pos; +mod chunk_section_pos; + +pub use block_pos::BlockPos; +pub use chunk_pos::ChunkPos; +pub use chunk_section_pos::ChunkSectionPos;