-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
237 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "rimecraft-voxel-math" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["JieningYu <[email protected]>"] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ChunkPos> 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<u64> 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<ChunkPos> 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<B>(&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<B>(mut buf: B) -> Result<Self, std::io::Error> | ||
where | ||
B: rimecraft_edcode::bytes::Buf, | ||
{ | ||
Ok(buf.get_u64().into()) | ||
} | ||
} | ||
} |
Oops, something went wrong.