diff --git a/examples/game/breakout.rs b/examples/game/breakout.rs index 1e1b0e36c9..f62a97ec92 100644 --- a/examples/game/breakout.rs +++ b/examples/game/breakout.rs @@ -30,8 +30,10 @@ struct Scoreboard { score: usize, } -struct Brick; -struct Wall; +enum Collider { + Solid, + Scorable, +} fn setup( mut commands: Commands, @@ -46,17 +48,18 @@ fn setup( // paddle .spawn(SpriteComponents { 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 { size: Vec2::new(120.0, 30.0), }, ..Default::default() }) .with(Paddle { speed: 500.0 }) + .with(Collider::Solid) // ball .spawn(SpriteComponents { 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 { size: Vec2::new(30.0, 30.0), }, @@ -102,7 +105,7 @@ fn setup( }, ..Default::default() }) - .with(Wall) + .with(Collider::Solid) // right .spawn(SpriteComponents { material: wall_material, @@ -112,7 +115,7 @@ fn setup( }, ..Default::default() }) - .with(Wall) + .with(Collider::Solid) // bottom .spawn(SpriteComponents { material: wall_material, @@ -122,7 +125,7 @@ fn setup( }, ..Default::default() }) - .with(Wall) + .with(Collider::Solid) // top .spawn(SpriteComponents { material: wall_material, @@ -132,7 +135,7 @@ fn setup( }, ..Default::default() }) - .with(Wall); + .with(Collider::Solid); // Add bricks let brick_rows = 4; @@ -159,7 +162,7 @@ fn setup( translation: Translation(brick_position), ..Default::default() }) - .with(Brick); + .with(Collider::Scorable); } } } @@ -180,12 +183,18 @@ fn paddle_movement_system( } *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())); } } fn ball_movement_system(time: Res