use crate::prelude::*;
use bevy_ecs::query::QueryData;
use bevy_math::{prelude::*, DVec3};
use bevy_transform::prelude::*;
#[derive(QueryData)]
#[query_data(mutable)]
pub struct CellTransform {
pub cell: &'static mut CellCoord,
pub transform: &'static mut Transform,
}
impl CellTransformItem<'_, '_> {
pub fn position_double(&self, grid: &Grid) -> DVec3 {
grid.grid_position_double(&self.cell, &self.transform)
}
pub fn position(&self, grid: &Grid) -> Vec3 {
grid.grid_position(&self.cell, &self.transform)
}
pub fn to_owned(&self) -> CellTransformOwned {
CellTransformOwned {
transform: *self.transform,
cell: *self.cell,
}
}
}
impl CellTransformReadOnlyItem<'_, '_> {
pub fn position_double(&self, grid: &Grid) -> DVec3 {
grid.grid_position_double(self.cell, self.transform)
}
pub fn position(&self, grid: &Grid) -> Vec3 {
grid.grid_position(self.cell, self.transform)
}
pub fn to_owned(&self) -> CellTransformOwned {
CellTransformOwned {
transform: *self.transform,
cell: *self.cell,
}
}
}
#[derive(Copy, Clone)]
pub struct CellTransformOwned {
pub transform: Transform,
pub cell: CellCoord,
}
impl core::ops::Sub for CellTransformOwned {
type Output = 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 core::ops::Add for CellTransformOwned {
type Output = Self;
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 CellTransformOwned {
pub fn position_double(&self, grid: &Grid) -> DVec3 {
grid.grid_position_double(&self.cell, &self.transform)
}
pub fn position(&self, grid: &Grid) -> Vec3 {
grid.grid_position(&self.cell, &self.transform)
}
}