From 622f59db7b24df64d0b109a72c06b460a5019cf4 Mon Sep 17 00:00:00 2001 From: Aevyrie Roessler Date: Wed, 29 Mar 2023 01:04:23 -0700 Subject: [PATCH] Update docs and organization --- src/grid_cell.rs | 107 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 118 ++++++----------------------------------------- 2 files changed, 121 insertions(+), 104 deletions(-) create mode 100644 src/grid_cell.rs diff --git a/src/grid_cell.rs b/src/grid_cell.rs new file mode 100644 index 0000000..32d777c --- /dev/null +++ b/src/grid_cell.rs @@ -0,0 +1,107 @@ +//! Contains the grid cell implementation + +use bevy::prelude::*; + +use crate::precision::GridPrecision; + +/// Defines the grid cell this entity's `Transform` is relative to. +/// +/// This component is generic over a few integer types to allow you to select the grid size you +/// need. These correspond to a total usable volume of a cube with the following edge lengths: +/// +/// **Assuming you are using a grid cell edge length of 10,000 meters, and `1.0` == 1 meter** +/// +/// - i8: 2,560 km = 74% of the diameter of the Moon +/// - i16: 655,350 km = 85% of the diameter of the Moon's orbit around Earth +/// - i32: 0.0045 light years = ~4 times the width of the solar system +/// - i64: 19.5 million light years = ~100 times the width of the milky way galaxy +/// - i128: 3.6e+26 light years = ~3.9e+15 times the width of the observable universe +/// +/// where +/// +/// `usable_edge_length = 2^(integer_bits) * grid_cell_edge_length` +/// +/// # Note +/// +/// Be sure you are using the same grid index precision everywhere. It might be a good idea to +/// define a type alias! +/// +/// ``` +/// # use big_space::GridCell; +/// type GalacticGrid = GridCell; +/// ``` +/// +#[derive(Component, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Reflect)] +#[reflect(Component, Default, PartialEq)] +pub struct GridCell { + /// The x-index of the cell. + pub x: P, + /// The y-index of the cell. + pub y: P, + /// The z-index of the cell. + pub z: P, +} + +impl GridCell

{ + /// Construct a new [`GridCell`]. + pub fn new(x: P, y: P, z: P) -> Self { + Self { x, y, z } + } + + /// The origin [`GridCell`]. + pub const ZERO: Self = GridCell { + x: P::ZERO, + y: P::ZERO, + z: P::ZERO, + }; + + /// A unit value [`GridCell`]. Useful for offsets. + pub const ONE: Self = GridCell { + x: P::ONE, + y: P::ONE, + z: P::ONE, + }; +} +impl std::ops::Add for GridCell

{ + type Output = GridCell

; + + fn add(self, rhs: Self) -> Self::Output { + GridCell { + x: self.x.wrapping_add(rhs.x), + y: self.y.wrapping_add(rhs.y), + z: self.z.wrapping_add(rhs.z), + } + } +} +impl std::ops::Sub for GridCell

{ + type Output = GridCell

; + + fn sub(self, rhs: Self) -> Self::Output { + GridCell { + x: self.x.wrapping_sub(rhs.x), + y: self.y.wrapping_sub(rhs.y), + z: self.z.wrapping_sub(rhs.z), + } + } +} +impl std::ops::Add for &GridCell

{ + type Output = GridCell

; + + fn add(self, rhs: Self) -> Self::Output { + (*self).add(*rhs) + } +} +impl std::ops::Sub for &GridCell

{ + type Output = GridCell

; + + fn sub(self, rhs: Self) -> Self::Output { + (*self).sub(*rhs) + } +} + +impl std::ops::AddAssign for GridCell

{ + fn add_assign(&mut self, rhs: Self) { + use std::ops::Add; + *self = self.add(rhs); + } +} diff --git a/src/lib.rs b/src/lib.rs index 7ee2a4a..ebdac08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,8 +41,7 @@ //! 3. Add the [`GridCell`] component to all spatial entities //! 4. Add the [`FloatingOrigin`] component to the active camera //! -//! Take a look at [`FloatingOriginSettings`] resource for configuration options, as well as some -//! useful helper methods. +//! Take a look at [`FloatingOriginSettings`] resource for some useful helper methods. //! //! # Moving Entities //! @@ -92,9 +91,12 @@ use propagation::propagate_transforms; use std::marker::PhantomData; pub mod camera; +pub mod grid_cell; pub mod precision; pub mod propagation; +pub use grid_cell::GridCell; + #[cfg(feature = "debug")] pub mod debug; @@ -188,6 +190,16 @@ impl FloatingOriginSettings { } } + /// Get the plugin's `grid_edge_length`. + pub fn grid_edge_length(&self) -> f32 { + self.grid_edge_length + } + + /// Get the plugin's `maximum_distance_from_origin`. + pub fn maximum_distance_from_origin(&self) -> f32 { + self.maximum_distance_from_origin + } + /// Compute the double precision position of an entity's [`Transform`] with respect to the given /// [`GridCell`]. pub fn grid_position_double( @@ -272,108 +284,6 @@ pub struct FloatingSpatialBundle { pub grid_position: GridCell

, } -/// Defines the grid cell this entity's `Transform` is relative to. -/// -/// This component is generic over a few integer types to allow you to select the grid size you -/// need. These correspond to a total usable volume of a cube with the following edge lengths: -/// -/// **Assuming you are using a grid cell edge length of 10,000 meters, and `1.0` == 1 meter** -/// -/// - i8: 2,560 km = 74% of the diameter of the Moon -/// - i16: 655,350 km = 85% of the diameter of the Moon's orbit around Earth -/// - i32: 0.0045 light years = ~4 times the width of the solar system -/// - i64: 19.5 million light years = ~100 times the width of the milky way galaxy -/// - i128: 3.6e+26 light years = ~3.9e+15 times the width of the observable universe -/// -/// where -/// -/// `usable_edge_length = 2^(integer_bits) * grid_cell_edge_length` -/// -/// # Note -/// -/// Be sure you are using the same grid index precision everywhere. It might be a good idea to -/// define a type alias! -/// -/// ``` -/// # use big_space::GridCell; -/// type GalacticGrid = GridCell; -/// ``` -/// -#[derive(Component, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Reflect)] -#[reflect(Component, Default, PartialEq)] -pub struct GridCell { - /// The x-index of the cell. - pub x: P, - /// The y-index of the cell. - pub y: P, - /// The z-index of the cell. - pub z: P, -} - -impl GridCell

{ - /// Construct a new [`GridCell`]. - pub fn new(x: P, y: P, z: P) -> Self { - Self { x, y, z } - } - - /// The origin [`GridCell`]. - pub const ZERO: Self = GridCell { - x: P::ZERO, - y: P::ZERO, - z: P::ZERO, - }; - - /// A unit value [`GridCell`]. Useful for offsets. - pub const ONE: Self = GridCell { - x: P::ONE, - y: P::ONE, - z: P::ONE, - }; -} -impl std::ops::Add for GridCell

{ - type Output = GridCell

; - - fn add(self, rhs: Self) -> Self::Output { - GridCell { - x: self.x.wrapping_add(rhs.x), - y: self.y.wrapping_add(rhs.y), - z: self.z.wrapping_add(rhs.z), - } - } -} -impl std::ops::Sub for GridCell

{ - type Output = GridCell

; - - fn sub(self, rhs: Self) -> Self::Output { - GridCell { - x: self.x.wrapping_sub(rhs.x), - y: self.y.wrapping_sub(rhs.y), - z: self.z.wrapping_sub(rhs.z), - } - } -} -impl std::ops::Add for &GridCell

{ - type Output = GridCell

; - - fn add(self, rhs: Self) -> Self::Output { - (*self).add(*rhs) - } -} -impl std::ops::Sub for &GridCell

{ - type Output = GridCell

; - - fn sub(self, rhs: Self) -> Self::Output { - (*self).sub(*rhs) - } -} - -impl std::ops::AddAssign for GridCell

{ - fn add_assign(&mut self, rhs: Self) { - use std::ops::Add; - *self = self.add(rhs); - } -} - /// Marks the entity to use as the floating origin. All other entities will be positioned relative /// to this entity's [`GridCell`]. #[derive(Component, Reflect)]