Skip to content

Commit

Permalink
transform: impl deref/derefmut for components
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jul 18, 2020
1 parent fe1adb6 commit f0fc380
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 12 deletions.
14 changes: 14 additions & 0 deletions crates/bevy_transform/src/components/children.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy_ecs::Entity;
use bevy_property::Properties;
use smallvec::SmallVec;
use std::ops::{DerefMut, Deref};

#[derive(Default, Clone, Properties, Debug)]
pub struct Children(pub SmallVec<[Entity; 8]>);
Expand All @@ -10,3 +11,16 @@ impl Children {
Self(SmallVec::from_slice(entity))
}
}

impl Deref for Children {
type Target = SmallVec<[Entity; 8]>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Children {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
15 changes: 14 additions & 1 deletion crates/bevy_transform/src/components/local_transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy_math::Mat4;
use bevy_property::Properties;
use std::fmt;
use std::{ops::{DerefMut, Deref}, fmt};

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
pub struct LocalTransform(pub Mat4);
Expand All @@ -22,3 +22,16 @@ impl fmt::Display for LocalTransform {
write!(f, "{}", self.0)
}
}

impl Deref for LocalTransform {
type Target = Mat4;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for LocalTransform {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
15 changes: 14 additions & 1 deletion crates/bevy_transform/src/components/non_uniform_scale.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy_math::Vec3;
use bevy_property::Properties;
use std::fmt;
use std::{ops::{DerefMut, Deref}, fmt};

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
pub struct NonUniformScale(pub Vec3);
Expand Down Expand Up @@ -41,3 +41,16 @@ impl fmt::Display for NonUniformScale {
write!(f, "NonUniformScale({}, {}, {})", x, y, z)
}
}

impl Deref for NonUniformScale {
type Target = Vec3;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for NonUniformScale {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
14 changes: 14 additions & 0 deletions crates/bevy_transform/src/components/parent.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
use bevy_ecs::Entity;
use bevy_property::Properties;
use std::ops::{DerefMut, Deref};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Properties)]
pub struct Parent(pub Entity);

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PreviousParent(pub Option<Entity>);

impl Deref for Parent {
type Target = Entity;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Parent {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
14 changes: 14 additions & 0 deletions crates/bevy_transform/src/components/rotation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy_math::Quat;
use bevy_property::Properties;
use std::ops::{Deref, DerefMut};

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
pub struct Rotation(pub Quat);
Expand All @@ -21,3 +22,16 @@ impl From<Quat> for Rotation {
Self(rotation)
}
}

impl Deref for Rotation {
type Target = Quat;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Rotation {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
15 changes: 14 additions & 1 deletion crates/bevy_transform/src/components/scale.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_property::Properties;
use std::fmt;
use std::{ops::{DerefMut, Deref}, fmt};

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
pub struct Scale(pub f32);
Expand Down Expand Up @@ -29,3 +29,16 @@ impl fmt::Display for Scale {
write!(f, "Scale({})", self.0)
}
}

impl Deref for Scale {
type Target = f32;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Scale {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
16 changes: 15 additions & 1 deletion crates/bevy_transform/src/components/translation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bevy_math::Vec3;
use bevy_property::Properties;
use std::ops::{DerefMut, Deref};

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
#[derive(Debug, PartialEq, Copy, Clone, Properties)]
pub struct Translation(pub Vec3);

impl Translation {
Expand All @@ -27,3 +28,16 @@ impl From<Vec3> for Translation {
Self(Vec3::from(translation))
}
}

impl Deref for Translation {
type Target = Vec3;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Translation {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ mod test {
world
.get::<Children>(parent)
.unwrap()
.0
.iter()
.cloned()
.collect::<Vec<_>>(),
Expand All @@ -177,7 +176,6 @@ mod test {
world
.get::<Children>(children[1])
.unwrap()
.0
.iter()
.cloned()
.collect::<Vec<_>>(),
Expand All @@ -192,7 +190,6 @@ mod test {
world
.get::<Children>(parent)
.unwrap()
.0
.iter()
.cloned()
.collect::<Vec<_>>(),
Expand Down
9 changes: 4 additions & 5 deletions examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ fn ball_collision_system(
mut brick_query: Query<(Entity, &Brick, &Translation, &Sprite)>,
mut wall_query: Query<(&Wall, &Translation, &Sprite)>,
) {
for (mut ball, translation, sprite) in &mut ball_query.iter() {
let ball_position = translation.0;
for (mut ball, ball_translation, sprite) in &mut ball_query.iter() {
let ball_size = sprite.size;
let velocity = &mut ball.velocity;
let mut collision = None;
Expand All @@ -205,7 +204,7 @@ fn ball_collision_system(
break;
}

collision = collide(ball_position, ball_size, translation.0, sprite.size);
collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
}

// check collision with paddle(s)
Expand All @@ -214,7 +213,7 @@ fn ball_collision_system(
break;
}

collision = collide(ball_position, ball_size, translation.0, sprite.size);
collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
}

// check collision with bricks
Expand All @@ -223,7 +222,7 @@ fn ball_collision_system(
break;
}

collision = collide(ball_position, ball_size, translation.0, sprite.size);
collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
if collision.is_some() {
scoreboard.score += 1;
commands.despawn(brick_entity);
Expand Down

0 comments on commit f0fc380

Please sign in to comment.