Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - bevy_derive: Add derives for Deref and DerefMut #4328

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Deref/DerefMut derives to breakout example
  • Loading branch information
MrGVSV committed Mar 25, 2022
commit 1b6c3979cc351cf144974cc1f78a805dbcd183dd
18 changes: 9 additions & 9 deletions examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct Paddle;
#[derive(Component)]
struct Ball;

#[derive(Component)]
#[derive(Component, Deref, DerefMut)]
struct Velocity(Vec2);

#[derive(Component)]
Expand Down Expand Up @@ -334,8 +334,8 @@ fn move_paddle(

fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>) {
for (mut transform, velocity) in query.iter_mut() {
transform.translation.x += velocity.0.x * TIME_STEP;
transform.translation.y += velocity.0.y * TIME_STEP;
transform.translation.x += velocity.x * TIME_STEP;
transform.translation.y += velocity.y * TIME_STEP;
}
}

Expand Down Expand Up @@ -375,21 +375,21 @@ fn check_for_collisions(
// only reflect if the ball's velocity is going in the opposite direction of the
// collision
match collision {
Collision::Left => reflect_x = ball_velocity.0.x > 0.0,
Collision::Right => reflect_x = ball_velocity.0.x < 0.0,
Collision::Top => reflect_y = ball_velocity.0.y < 0.0,
Collision::Bottom => reflect_y = ball_velocity.0.y > 0.0,
Collision::Left => reflect_x = ball_velocity.x > 0.0,
Collision::Right => reflect_x = ball_velocity.x < 0.0,
Collision::Top => reflect_y = ball_velocity.y < 0.0,
Collision::Bottom => reflect_y = ball_velocity.y > 0.0,
Collision::Inside => { /* do nothing */ }
}

// reflect velocity on the x-axis if we hit something on the x-axis
if reflect_x {
ball_velocity.0.x = -ball_velocity.0.x;
ball_velocity.x = -ball_velocity.x;
}

// reflect velocity on the y-axis if we hit something on the y-axis
if reflect_y {
ball_velocity.0.y = -ball_velocity.0.y;
ball_velocity.y = -ball_velocity.y;
}
}
}
Expand Down