bevy/examples/game/breakout.rs

257 lines
8.1 KiB
Rust
Raw Normal View History

2020-06-27 04:40:09 +00:00
use bevy::{
prelude::*,
render::pass::ClearColor,
2020-06-27 04:40:09 +00:00
sprite::collide_aabb::{collide, Collision},
};
fn main() {
App::build()
.add_default_plugins()
2020-06-27 09:10:07 +00:00
.add_resource(Scoreboard { score: 0 })
2020-06-27 04:40:09 +00:00
.add_resource(ClearColor(Color::rgb(0.7, 0.7, 0.7)))
.add_startup_system(setup.system())
.add_system(paddle_movement_system.system())
.add_system(ball_collision_system.system())
.add_system(ball_movement_system.system())
2020-06-27 09:10:07 +00:00
.add_system(scoreboard_system.system())
2020-06-27 04:40:09 +00:00
.run();
}
struct Paddle {
speed: f32,
}
struct Ball {
velocity: Vec3,
}
2020-06-27 09:10:07 +00:00
struct Scoreboard {
score: usize,
}
2020-07-10 04:18:35 +00:00
struct Brick;
struct Wall;
2020-06-27 17:18:27 +00:00
fn setup(
2020-07-10 04:18:35 +00:00
mut commands: Commands,
2020-06-27 17:18:27 +00:00
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
2020-06-27 04:40:09 +00:00
// Add the game's entities to our world
2020-07-10 04:18:35 +00:00
commands
2020-06-27 04:40:09 +00:00
// camera
2020-07-10 04:18:35 +00:00
.spawn(OrthographicCameraComponents::default())
2020-06-27 04:40:09 +00:00
// paddle
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
translation: Translation(Vec3::new(0.0, -250.0, 0.0)),
sprite: Sprite {
size: Vec2::new(120.0, 30.0),
},
..Default::default()
})
.with(Paddle { speed: 500.0 })
// ball
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: materials.add(Color::rgb(0.8, 0.2, 0.2).into()),
translation: Translation(Vec3::new(0.0, -100.0, 1.0)),
sprite: Sprite {
size: Vec2::new(30.0, 30.0),
},
..Default::default()
})
.with(Ball {
velocity: 400.0 * Vec3::new(0.5, -0.5, 0.0).normalize(),
2020-06-27 09:10:07 +00:00
})
// scoreboard
2020-07-10 04:18:35 +00:00
.spawn(LabelComponents {
2020-06-27 09:10:07 +00:00
label: Label {
font: asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap(),
text: "Score:".to_string(),
style: TextStyle {
color: Color::rgb(0.2, 0.2, 0.8).into(),
font_size: 40.0,
2020-06-27 17:18:27 +00:00
},
2020-06-27 09:10:07 +00:00
},
2020-06-27 17:18:27 +00:00
node: Node::new(Anchors::TOP_LEFT, Margins::new(10.0, 50.0, 10.0, 50.0)),
2020-06-27 09:10:07 +00:00
..Default::default()
2020-06-27 17:18:27 +00:00
});
2020-06-27 04:40:09 +00:00
// Add walls
let wall_material = materials.add(Color::rgb(0.5, 0.5, 0.5).into());
let wall_thickness = 10.0;
let bounds = Vec2::new(900.0, 600.0);
2020-07-10 04:18:35 +00:00
commands
2020-06-27 04:40:09 +00:00
// left
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: wall_material,
translation: Translation(Vec3::new(-bounds.x() / 2.0, 0.0, 0.0)),
sprite: Sprite {
size: Vec2::new(wall_thickness, bounds.y() + wall_thickness),
},
..Default::default()
})
.with(Wall)
// right
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: wall_material,
translation: Translation(Vec3::new(bounds.x() / 2.0, 0.0, 0.0)),
sprite: Sprite {
size: Vec2::new(wall_thickness, bounds.y() + wall_thickness),
},
..Default::default()
})
.with(Wall)
// bottom
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: wall_material,
translation: Translation(Vec3::new(0.0, -bounds.y() / 2.0, 0.0)),
sprite: Sprite {
size: Vec2::new(bounds.x() + wall_thickness, wall_thickness),
},
..Default::default()
})
.with(Wall)
// top
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: wall_material,
translation: Translation(Vec3::new(0.0, bounds.y() / 2.0, 0.0)),
sprite: Sprite {
size: Vec2::new(bounds.x() + wall_thickness, wall_thickness),
},
..Default::default()
})
.with(Wall);
// Add bricks
let brick_rows = 4;
let brick_columns = 5;
let brick_spacing = 20.0;
let brick_size = Vec2::new(150.0, 30.0);
let bricks_width = brick_columns as f32 * (brick_size.x() + brick_spacing) - brick_spacing;
// center the bricks and move them up a bit
let bricks_offset = Vec3::new(-(bricks_width - brick_size.x()) / 2.0, 100.0, 0.0);
for row in 0..brick_rows {
let y_position = row as f32 * (brick_size.y() + brick_spacing);
for column in 0..brick_columns {
let brick_position = Vec3::new(
column as f32 * (brick_size.x() + brick_spacing),
y_position,
0.0,
) + bricks_offset;
2020-07-10 04:18:35 +00:00
commands
2020-06-27 04:40:09 +00:00
// brick
2020-07-10 04:18:35 +00:00
.spawn(SpriteComponents {
2020-06-27 04:40:09 +00:00
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
sprite: Sprite { size: brick_size },
translation: Translation(brick_position),
..Default::default()
})
.with(Brick);
}
}
}
fn paddle_movement_system(
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
2020-07-11 19:52:25 +00:00
mut query: Query<(&Paddle, &mut Translation)>,
2020-06-27 04:40:09 +00:00
) {
2020-07-10 04:18:35 +00:00
for (paddle, translation) in &mut query.iter() {
2020-06-27 04:40:09 +00:00
let mut direction = 0.0;
if keyboard_input.pressed(KeyCode::Left) {
direction -= 1.0;
}
if keyboard_input.pressed(KeyCode::Right) {
direction += 1.0;
}
*translation.0.x_mut() += time.delta_seconds * direction * paddle.speed;
}
}
2020-07-10 08:37:06 +00:00
fn ball_movement_system(time: Res<Time>, mut ball_query: Query<(&Ball, &mut Translation)>) {
2020-07-10 04:18:35 +00:00
for (ball, translation) in &mut ball_query.iter() {
2020-06-27 04:40:09 +00:00
translation.0 += ball.velocity * time.delta_seconds;
}
}
2020-07-10 08:37:06 +00:00
fn scoreboard_system(scoreboard: Res<Scoreboard>, mut query: Query<&mut Label>) {
2020-07-10 04:18:35 +00:00
for mut label in &mut query.iter() {
2020-06-27 09:10:07 +00:00
label.text = format!("Score: {}", scoreboard.score);
}
}
2020-06-27 04:40:09 +00:00
fn ball_collision_system(
2020-07-10 04:18:35 +00:00
mut commands: Commands,
2020-06-27 17:18:27 +00:00
mut scoreboard: ResMut<Scoreboard>,
2020-07-10 04:18:35 +00:00
mut ball_query: Query<(&mut Ball, &Translation, &Sprite)>,
mut paddle_query: Query<(&Paddle, &Translation, &Sprite)>,
mut brick_query: Query<(Entity, &Brick, &Translation, &Sprite)>,
mut wall_query: Query<(&Wall, &Translation, &Sprite)>,
2020-06-27 04:40:09 +00:00
) {
2020-07-10 04:18:35 +00:00
for (ball, translation, sprite) in &mut ball_query.iter() {
2020-06-27 04:40:09 +00:00
let ball_position = translation.0;
let ball_size = sprite.size;
let velocity = &mut ball.velocity;
let mut collision = None;
// check collision with walls
2020-07-10 04:18:35 +00:00
for (_wall, translation, sprite) in &mut wall_query.iter() {
2020-06-27 04:40:09 +00:00
if collision.is_some() {
break;
}
collision = collide(ball_position, ball_size, translation.0, sprite.size);
}
// check collision with paddle(s)
2020-07-10 04:18:35 +00:00
for (_paddle, translation, sprite) in &mut paddle_query.iter() {
2020-06-27 04:40:09 +00:00
if collision.is_some() {
break;
}
collision = collide(ball_position, ball_size, translation.0, sprite.size);
}
// check collision with bricks
2020-07-10 04:18:35 +00:00
for (brick_entity, _brick, translation, sprite) in &mut brick_query.iter() {
2020-06-27 04:40:09 +00:00
if collision.is_some() {
break;
}
collision = collide(ball_position, ball_size, translation.0, sprite.size);
if collision.is_some() {
2020-06-27 09:10:07 +00:00
scoreboard.score += 1;
2020-07-10 04:18:35 +00:00
commands.despawn(brick_entity);
2020-06-27 04:40:09 +00:00
}
}
// 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();
}
}
}