-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a convenience helper for reading GridCell and Transform from a qu…
…ery at the same time (#12) Co-authored-by: Oliver Scherer <[email protected]> Co-authored-by: Aevyrie <[email protected]>
- Loading branch information
1 parent
946719c
commit 67f3c14
Showing
5 changed files
with
158 additions
and
33 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,109 @@ | ||
//! A helper query argument that ensures you don't forget to handle | ||
//! the [`GridCell`] when you work with a [`Transform`]. | ||
use bevy::ecs::query::QueryData; | ||
use bevy::math::DVec3; | ||
use bevy::prelude::*; | ||
|
||
use crate::precision::GridPrecision; | ||
use crate::{FloatingOriginSettings, GridCell}; | ||
|
||
#[derive(QueryData)] | ||
#[query_data(mutable)] | ||
/// A convenience query argument that groups a [`Transform`] with its [`GridCell`]. | ||
/// If you only want to read from the position, use [`GridTransformReadOnly`] instead, | ||
/// as this will allow the bevy ECS to run multiple queries using [`GridTransformReadOnly`] | ||
/// at the same time (just like multiple queries with `&Transform` are fine). | ||
pub struct GridTransform<P: GridPrecision> { | ||
/// Grid local transform | ||
pub transform: &'static mut Transform, | ||
/// The grid to which `transform` is relative to. | ||
pub cell: &'static mut GridCell<P>, | ||
} | ||
|
||
impl<'w, P: GridPrecision> GridTransformItem<'w, P> { | ||
/// Compute the global position with double precision. | ||
pub fn position_double(&self, settings: &FloatingOriginSettings) -> DVec3 { | ||
settings.grid_position_double(&self.cell, &self.transform) | ||
} | ||
|
||
/// Compute the global position. | ||
pub fn position(&self, settings: &FloatingOriginSettings) -> Vec3 { | ||
settings.grid_position(&self.cell, &self.transform) | ||
} | ||
|
||
/// Get a copy of the fields to work with. | ||
pub fn to_owned(&self) -> GridTransformOwned<P> { | ||
GridTransformOwned { | ||
transform: *self.transform, | ||
cell: *self.cell, | ||
} | ||
} | ||
} | ||
|
||
impl<'w, P: GridPrecision> GridTransformReadOnlyItem<'w, P> { | ||
/// Compute the global position with double precision. | ||
pub fn position_double(&self, settings: &FloatingOriginSettings) -> DVec3 { | ||
settings.grid_position_double(self.cell, self.transform) | ||
} | ||
|
||
/// Compute the global position. | ||
pub fn position(&self, settings: &FloatingOriginSettings) -> Vec3 { | ||
settings.grid_position(self.cell, self.transform) | ||
} | ||
|
||
/// Get a copy of the fields to work with. | ||
pub fn to_owned(&self) -> GridTransformOwned<P> { | ||
GridTransformOwned { | ||
transform: *self.transform, | ||
cell: *self.cell, | ||
} | ||
} | ||
} | ||
|
||
/// A convenience wrapper that allows working with grid and transform easily | ||
#[derive(Copy, Clone)] | ||
pub struct GridTransformOwned<P: GridPrecision> { | ||
/// Grid local transform | ||
pub transform: Transform, | ||
/// The grid to which `transform` is relative to. | ||
pub cell: GridCell<P>, | ||
} | ||
|
||
impl<P: GridPrecision> std::ops::Sub for GridTransformOwned<P> { | ||
type Output = Self; | ||
|
||
/// Compute a new transform that maps from `source` to `self`. | ||
fn sub(mut self, source: Self) -> Self { | ||
self.cell -= source.cell; | ||
self.transform.translation -= source.transform.translation; | ||
self.transform.scale /= source.transform.scale; | ||
self.transform.rotation *= source.transform.rotation.inverse(); | ||
self | ||
} | ||
} | ||
|
||
impl<P: GridPrecision> std::ops::Add for GridTransformOwned<P> { | ||
type Output = Self; | ||
|
||
/// Compute a new transform that shifts, scales and rotates `self` by `diff`. | ||
fn add(mut self, diff: Self) -> Self { | ||
self.cell += diff.cell; | ||
self.transform.translation += diff.transform.translation; | ||
self.transform.scale *= diff.transform.scale; | ||
self.transform.rotation *= diff.transform.rotation; | ||
self | ||
} | ||
} | ||
|
||
impl<P: GridPrecision> GridTransformOwned<P> { | ||
/// Compute the global position with double precision. | ||
pub fn position_double(&self, space: &FloatingOriginSettings) -> DVec3 { | ||
space.grid_position_double(&self.cell, &self.transform) | ||
} | ||
|
||
/// Compute the global position. | ||
pub fn position(&self, space: &FloatingOriginSettings) -> Vec3 { | ||
space.grid_position(&self.cell, &self.transform) | ||
} | ||
} |