Cleaned up collision code in breakout

This commit is contained in:
reidbhuntley 2020-08-11 23:43:27 -04:00
parent 391b08a779
commit 11748456b6

View File

@ -30,8 +30,10 @@ struct Scoreboard {
score: usize, score: usize,
} }
struct Brick; enum Collider {
struct Wall; Solid,
Scorable,
}
fn setup( fn setup(
mut commands: Commands, mut commands: Commands,
@ -46,17 +48,18 @@ fn setup(
// paddle // paddle
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()), material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
translation: Translation(Vec3::new(0.0, -250.0, 0.0)), translation: Translation(Vec3::new(0.0, -215.0, 0.0)),
sprite: Sprite { sprite: Sprite {
size: Vec2::new(120.0, 30.0), size: Vec2::new(120.0, 30.0),
}, },
..Default::default() ..Default::default()
}) })
.with(Paddle { speed: 500.0 }) .with(Paddle { speed: 500.0 })
.with(Collider::Solid)
// ball // ball
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: materials.add(Color::rgb(0.8, 0.2, 0.2).into()), material: materials.add(Color::rgb(0.8, 0.2, 0.2).into()),
translation: Translation(Vec3::new(0.0, -100.0, 1.0)), translation: Translation(Vec3::new(0.0, -50.0, 1.0)),
sprite: Sprite { sprite: Sprite {
size: Vec2::new(30.0, 30.0), size: Vec2::new(30.0, 30.0),
}, },
@ -102,7 +105,7 @@ fn setup(
}, },
..Default::default() ..Default::default()
}) })
.with(Wall) .with(Collider::Solid)
// right // right
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
@ -112,7 +115,7 @@ fn setup(
}, },
..Default::default() ..Default::default()
}) })
.with(Wall) .with(Collider::Solid)
// bottom // bottom
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
@ -122,7 +125,7 @@ fn setup(
}, },
..Default::default() ..Default::default()
}) })
.with(Wall) .with(Collider::Solid)
// top // top
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
@ -132,7 +135,7 @@ fn setup(
}, },
..Default::default() ..Default::default()
}) })
.with(Wall); .with(Collider::Solid);
// Add bricks // Add bricks
let brick_rows = 4; let brick_rows = 4;
@ -159,7 +162,7 @@ fn setup(
translation: Translation(brick_position), translation: Translation(brick_position),
..Default::default() ..Default::default()
}) })
.with(Brick); .with(Collider::Scorable);
} }
} }
} }
@ -180,6 +183,9 @@ fn paddle_movement_system(
} }
*translation.0.x_mut() += time.delta_seconds * direction * paddle.speed; *translation.0.x_mut() += time.delta_seconds * direction * paddle.speed;
// bound the paddle within the walls
*translation.0.x_mut() = f32::max(-380.0, f32::min(380.0, translation.0.x()));
} }
} }
@ -199,67 +205,46 @@ fn ball_collision_system(
mut commands: Commands, mut commands: Commands,
mut scoreboard: ResMut<Scoreboard>, mut scoreboard: ResMut<Scoreboard>,
mut ball_query: Query<(&mut Ball, &Translation, &Sprite)>, mut ball_query: Query<(&mut Ball, &Translation, &Sprite)>,
mut paddle_query: Query<(&Paddle, &Translation, &Sprite)>, mut collider_query: Query<(Entity, &Collider, &Translation, &Sprite)>,
mut brick_query: Query<(Entity, &Brick, &Translation, &Sprite)>,
mut wall_query: Query<(&Wall, &Translation, &Sprite)>,
) { ) {
for (mut ball, ball_translation, sprite) in &mut ball_query.iter() { for (mut ball, ball_translation, sprite) in &mut ball_query.iter() {
let ball_size = sprite.size; let ball_size = sprite.size;
let velocity = &mut ball.velocity; let velocity = &mut ball.velocity;
let mut collision = None;
// check collision with walls // check collision with walls
for (_wall, translation, sprite) in &mut wall_query.iter() { for (collider_entity, _collider, translation, sprite) in &mut collider_query.iter() {
if collision.is_some() { let collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
if let Some(collision) = collision {
// scorable colliders should be despawned and increment the scoreboard on collision
if let &Collider::Scorable = _collider {
scoreboard.score += 1;
commands.despawn(collider_entity);
}
// reflect the ball when it collides
let mut reflect_x = false;
let mut reflect_y = false;
// only reflect if the ball's velocity is going in the opposite direction of the collision
match collision {
Collision::Left => reflect_x = velocity.x() > 0.0,
Collision::Right => reflect_x = velocity.x() < 0.0,
Collision::Top => reflect_y = velocity.y() < 0.0,
Collision::Bottom => reflect_y = velocity.y() > 0.0,
}
// reflect velocity on the x-axis if we hit something on the x-axis
if reflect_x {
*velocity.x_mut() = -velocity.x();
}
// reflect velocity on the y-axis if we hit something on the y-axis
if reflect_y {
*velocity.y_mut() = -velocity.y();
}
break; break;
} }
collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
}
// check collision with paddle(s)
for (_paddle, translation, sprite) in &mut paddle_query.iter() {
if collision.is_some() {
break;
}
collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
}
// check collision with bricks
for (brick_entity, _brick, translation, sprite) in &mut brick_query.iter() {
if collision.is_some() {
break;
}
collision = collide(ball_translation.0, ball_size, translation.0, sprite.size);
if collision.is_some() {
scoreboard.score += 1;
commands.despawn(brick_entity);
}
}
// reflect the ball when it collides
let mut reflect_x = false;
let mut reflect_y = false;
// only reflect if the ball's velocity is going in the opposite direction of the collision
match collision {
Some(Collision::Left) => reflect_x = velocity.x() > 0.0,
Some(Collision::Right) => reflect_x = velocity.x() < 0.0,
Some(Collision::Top) => reflect_y = velocity.y() < 0.0,
Some(Collision::Bottom) => reflect_y = velocity.y() > 0.0,
None => {}
}
// reflect velocity on the x-axis if we hit something on the x-axis
if reflect_x {
*velocity.x_mut() = -velocity.x();
}
// reflect velocity on the y-axis if we hit something on the y-axis
if reflect_y {
*velocity.y_mut() = -velocity.y();
} }
} }
} }