mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
add breakout example game
This commit is contained in:
parent
1e614e41f1
commit
7441ac1a01
5 changed files with 299 additions and 1 deletions
|
@ -139,6 +139,10 @@ path = "examples/ecs/startup_system.rs"
|
|||
name = "ecs_guide"
|
||||
path = "examples/ecs/ecs_guide.rs"
|
||||
|
||||
[[example]]
|
||||
name = "breakout"
|
||||
path = "examples/game/breakout/main.rs"
|
||||
|
||||
[[example]]
|
||||
name = "mouse_input"
|
||||
path = "examples/input/mouse_input.rs"
|
||||
|
|
62
crates/bevy_sprite/src/collide_aabb.rs
Normal file
62
crates/bevy_sprite/src/collide_aabb.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use glam::{Vec3, Vec2};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Collision {
|
||||
Left,
|
||||
Right,
|
||||
Top,
|
||||
Bottom,
|
||||
}
|
||||
|
||||
// TODO: ideally we can remove this once bevy gets a physics system
|
||||
/// Axis-aligned bounding box collision with "side" detection
|
||||
pub fn collide(a_pos: Vec3, a_size: Vec2, b_pos: Vec3, b_size: Vec2) -> Option<Collision> {
|
||||
let a_min = a_pos.truncate() - a_size / 2.0;
|
||||
let a_max = a_pos.truncate() + a_size / 2.0;
|
||||
|
||||
let b_min = b_pos.truncate() - b_size / 2.0;
|
||||
let b_max = b_pos.truncate() + b_size / 2.0;
|
||||
|
||||
// check to see if the two rectangles are intersecting
|
||||
if a_min.x() < b_max.x()
|
||||
&& a_max.x() > b_min.x()
|
||||
&& a_min.y() < b_max.y()
|
||||
&& a_max.y() > b_min.y()
|
||||
{
|
||||
// check to see if we hit on the left or right side
|
||||
let (x_collision, x_depth) =
|
||||
if a_min.x() < b_min.x() && a_max.x() > b_min.x() && a_max.x() < b_max.x() {
|
||||
(Some(Collision::Left), b_min.x() - a_max.x())
|
||||
} else if a_min.x() > b_min.x() && a_min.x() < b_max.x() && a_max.x() > b_max.x() {
|
||||
(Some(Collision::Right), a_min.x() - b_max.x())
|
||||
} else {
|
||||
(None, 0.0)
|
||||
};
|
||||
|
||||
// check to see if we hit on the top or bottom side
|
||||
let (y_collision, y_depth) =
|
||||
if a_min.y() < b_min.y() && a_max.y() > b_min.y() && a_max.y() < b_max.y() {
|
||||
(Some(Collision::Bottom), b_min.y() - a_max.y())
|
||||
} else if a_min.y() > b_min.y() && a_min.y() < b_max.y() && a_max.y() > b_max.y() {
|
||||
(Some(Collision::Top), a_min.y() - b_max.y())
|
||||
} else {
|
||||
(None, 0.0)
|
||||
};
|
||||
|
||||
// if we had an "x" and a "y" collision, pick the "primary" side using penetration depth
|
||||
match (x_collision, y_collision) {
|
||||
(Some(x_collision), Some(y_collision)) => {
|
||||
if y_depth < x_depth {
|
||||
Some(y_collision)
|
||||
} else {
|
||||
Some(x_collision)
|
||||
}
|
||||
}
|
||||
(Some(x_collision), None) => Some(x_collision),
|
||||
(None, Some(y_collision)) => Some(y_collision),
|
||||
(None, None) => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
mod color_material;
|
||||
mod dynamic_texture_atlas_builder;
|
||||
pub mod entity;
|
||||
pub mod collide_aabb;
|
||||
mod rect;
|
||||
mod render;
|
||||
mod sprite;
|
||||
|
|
|
@ -12,7 +12,7 @@ use glam::Vec2;
|
|||
#[derive(Default, RenderResources, RenderResource)]
|
||||
#[render_resources(from_self)]
|
||||
pub struct Sprite {
|
||||
size: Vec2,
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
// SAFE: sprite is repr(C) and only consists of byteables
|
||||
|
|
231
examples/game/breakout/main.rs
Normal file
231
examples/game/breakout/main.rs
Normal file
|
@ -0,0 +1,231 @@
|
|||
use bevy::{
|
||||
prelude::*,
|
||||
sprite::collide_aabb::{collide, Collision},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
.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())
|
||||
.run();
|
||||
}
|
||||
|
||||
struct Paddle {
|
||||
speed: f32,
|
||||
}
|
||||
|
||||
struct Ball {
|
||||
velocity: Vec3,
|
||||
}
|
||||
|
||||
struct Brick;
|
||||
struct Wall;
|
||||
|
||||
fn setup(command_buffer: &mut CommandBuffer, mut materials: ResMut<Assets<ColorMaterial>>) {
|
||||
// Add the game's entities to our world
|
||||
let mut builder = command_buffer.build();
|
||||
builder
|
||||
// camera
|
||||
.entity_with(OrthographicCameraComponents::default())
|
||||
// paddle
|
||||
.entity_with(SpriteComponents {
|
||||
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
|
||||
.entity_with(SpriteComponents {
|
||||
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(),
|
||||
});
|
||||
|
||||
// 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);
|
||||
|
||||
builder
|
||||
// left
|
||||
.entity_with(SpriteComponents {
|
||||
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
|
||||
.entity_with(SpriteComponents {
|
||||
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
|
||||
.entity_with(SpriteComponents {
|
||||
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
|
||||
.entity_with(SpriteComponents {
|
||||
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;
|
||||
builder
|
||||
// brick
|
||||
.entity_with(SpriteComponents {
|
||||
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(
|
||||
world: &mut SubWorld,
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
query: &mut Query<(Read<Paddle>, Write<Translation>)>,
|
||||
) {
|
||||
for (paddle, mut translation) in query.iter_mut(world) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
fn ball_movement_system(
|
||||
world: &mut SubWorld,
|
||||
time: Res<Time>,
|
||||
ball_query: &mut Query<(Read<Ball>, Write<Translation>)>,
|
||||
) {
|
||||
for (ball, mut translation) in ball_query.iter_mut(world) {
|
||||
translation.0 += ball.velocity * time.delta_seconds;
|
||||
}
|
||||
}
|
||||
|
||||
fn ball_collision_system(
|
||||
command_buffer: &mut CommandBuffer,
|
||||
world: &mut SubWorld,
|
||||
ball_query: &mut Query<(Write<Ball>, Read<Translation>, Read<Sprite>)>,
|
||||
paddle_query: &mut Query<(Read<Paddle>, Read<Translation>, Read<Sprite>)>,
|
||||
brick_query: &mut Query<(Read<Brick>, Read<Translation>, Read<Sprite>)>,
|
||||
wall_query: &mut Query<(Read<Wall>, Read<Translation>, Read<Sprite>)>,
|
||||
) {
|
||||
for (mut ball, translation, sprite) in ball_query.iter_mut(world) {
|
||||
let ball_position = translation.0;
|
||||
let ball_size = sprite.size;
|
||||
let velocity = &mut ball.velocity;
|
||||
let mut collision = None;
|
||||
|
||||
// check collision with walls
|
||||
for (_wall, translation, sprite) in wall_query.iter(world) {
|
||||
if collision.is_some() {
|
||||
break;
|
||||
}
|
||||
|
||||
collision = collide(ball_position, ball_size, translation.0, sprite.size);
|
||||
}
|
||||
|
||||
// check collision with paddle(s)
|
||||
for (_paddle, translation, sprite) in paddle_query.iter(world) {
|
||||
if collision.is_some() {
|
||||
break;
|
||||
}
|
||||
|
||||
collision = collide(ball_position, ball_size, translation.0, sprite.size);
|
||||
}
|
||||
|
||||
// check collision with bricks
|
||||
for (brick_entity, (_brick, translation, sprite)) in brick_query.iter_entities(world) {
|
||||
if collision.is_some() {
|
||||
break;
|
||||
}
|
||||
|
||||
collision = collide(ball_position, ball_size, translation.0, sprite.size);
|
||||
if collision.is_some() {
|
||||
command_buffer.delete(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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue