mirror of
https://github.com/eliasstepanik/big_space_with_trim.git
synced 2026-01-16 16:08:29 +00:00
Update docs and organization
This commit is contained in:
parent
023170b718
commit
622f59db7b
107
src/grid_cell.rs
Normal file
107
src/grid_cell.rs
Normal file
@ -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<i64>;
|
||||
/// ```
|
||||
///
|
||||
#[derive(Component, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Reflect)]
|
||||
#[reflect(Component, Default, PartialEq)]
|
||||
pub struct GridCell<P: GridPrecision> {
|
||||
/// 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<P: GridPrecision> GridCell<P> {
|
||||
/// 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<P: GridPrecision> std::ops::Add for GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
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<P: GridPrecision> std::ops::Sub for GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
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<P: GridPrecision> std::ops::Add for &GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
(*self).add(*rhs)
|
||||
}
|
||||
}
|
||||
impl<P: GridPrecision> std::ops::Sub for &GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
(*self).sub(*rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: GridPrecision> std::ops::AddAssign for GridCell<P> {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
use std::ops::Add;
|
||||
*self = self.add(rhs);
|
||||
}
|
||||
}
|
||||
118
src/lib.rs
118
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<P: GridPrecision>(
|
||||
@ -272,108 +284,6 @@ pub struct FloatingSpatialBundle<P: GridPrecision> {
|
||||
pub grid_position: GridCell<P>,
|
||||
}
|
||||
|
||||
/// 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<i64>;
|
||||
/// ```
|
||||
///
|
||||
#[derive(Component, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Reflect)]
|
||||
#[reflect(Component, Default, PartialEq)]
|
||||
pub struct GridCell<P: GridPrecision> {
|
||||
/// 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<P: GridPrecision> GridCell<P> {
|
||||
/// 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<P: GridPrecision> std::ops::Add for GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
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<P: GridPrecision> std::ops::Sub for GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
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<P: GridPrecision> std::ops::Add for &GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
(*self).add(*rhs)
|
||||
}
|
||||
}
|
||||
impl<P: GridPrecision> std::ops::Sub for &GridCell<P> {
|
||||
type Output = GridCell<P>;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
(*self).sub(*rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: GridPrecision> std::ops::AddAssign for GridCell<P> {
|
||||
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)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user