use bevy::{ prelude::*, render::pass::ClearColor, sprite::collide_aabb::{collide, Collision}, }; /// An implementation of the classic game "Breakout" fn main() { App::build() .add_plugins(bevy_webgl2::DefaultPlugins) .insert_resource(Scoreboard { score: 0 }) .insert_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9))) .add_startup_system(setup.system()) .add_system(paddle_movement_system.system()) .add_system(ball_collision_system.system()) .add_system(ball_movement_system.system()) .add_system(scoreboard_system.system()) .run(); } struct Paddle { speed: f32, } struct Ball { velocity: Vec3, } struct Scoreboard { score: usize, } enum Collider { Solid, Scorable, Paddle, } fn setup( mut commands: Commands, mut materials: ResMut>, asset_server: Res, ) { // Add the game's entities to our world commands // cameras .spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(UiCameraBundle::default()); // paddle commands .spawn_bundle(SpriteBundle { material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()), transform: Transform::from_xyz(0.0, -215.0, 0.0), sprite: Sprite::new(Vec2::new(120.0, 30.0)), ..Default::default() }) .insert(Paddle { speed: 500.0 }) .insert(Collider::Paddle); // ball commands .spawn_bundle(SpriteBundle { material: materials.add(Color::rgb(1.0, 0.5, 0.5).into()), transform: Transform::from_xyz(0.0, -50.0, 1.0), sprite: Sprite::new(Vec2::new(30.0, 30.0)), ..Default::default() }) .insert(Ball { velocity: 400.0 * Vec3::new(0.5, -0.5, 0.0).normalize(), }); // scoreboard commands.spawn_bundle(TextBundle { text: Text { sections: vec![ TextSection { value: "Score: ".to_string(), style: TextStyle { font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.5, 0.5, 1.0), }, }, TextSection { value: "".to_string(), style: TextStyle { font: asset_server.load("fonts/FiraMono-Medium.ttf"), font_size: 40.0, color: Color::rgb(1.0, 0.5, 0.5), }, }, ], ..Default::default() }, style: Style { position_type: PositionType::Absolute, position: Rect { top: Val::Px(5.0), left: Val::Px(5.0), ..Default::default() }, ..Default::default() }, ..Default::default() }); // Add walls let wall_material = materials.add(Color::rgb(0.8, 0.8, 0.8).into()); let wall_thickness = 10.0; let bounds = Vec2::new(900.0, 600.0); commands // left .spawn_bundle(SpriteBundle { material: wall_material.clone(), transform: Transform::from_xyz(-bounds.x / 2.0, 0.0, 0.0), sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y + wall_thickness)), ..Default::default() }) .insert(Collider::Solid); // right commands .spawn_bundle(SpriteBundle { material: wall_material.clone(), transform: Transform::from_xyz(bounds.x / 2.0, 0.0, 0.0), sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y + wall_thickness)), ..Default::default() }) .insert(Collider::Solid); // bottom commands .spawn_bundle(SpriteBundle { material: wall_material.clone(), transform: Transform::from_xyz(0.0, -bounds.y / 2.0, 0.0), sprite: Sprite::new(Vec2::new(bounds.x + wall_thickness, wall_thickness)), ..Default::default() }) .insert(Collider::Solid); // top commands .spawn_bundle(SpriteBundle { material: wall_material, transform: Transform::from_xyz(0.0, bounds.y / 2.0, 0.0), sprite: Sprite::new(Vec2::new(bounds.x + wall_thickness, wall_thickness)), ..Default::default() }) .insert(Collider::Solid); // 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); let brick_material = materials.add(Color::rgb(0.5, 0.5, 1.0).into()); 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; commands // brick .spawn_bundle(SpriteBundle { material: brick_material.clone(), sprite: Sprite::new(brick_size), transform: Transform::from_translation(brick_position), ..Default::default() }) .insert(Collider::Scorable); } } } fn paddle_movement_system( time: Res