Format all cargo source files.

This commit is contained in:
2024-07-05 16:19:32 -05:00
parent 2349538199
commit 491014be0a
3 changed files with 31 additions and 29 deletions

View File

@@ -1,5 +1,9 @@
use bevy::{
use bevy::{input::keyboard::Key, math::bounding::{Aabb2d, BoundingCircle, BoundingVolume, IntersectsVolume}, prelude::*, sprite::MaterialMesh2dBundle}; input::keyboard::Key,
math::bounding::{Aabb2d, BoundingCircle, BoundingVolume, IntersectsVolume},
prelude::*,
sprite::MaterialMesh2dBundle,
};
// Using the default 2D camera they correspond 1:1 with screen pixels. // Using the default 2D camera they correspond 1:1 with screen pixels.
const PADDLE_SIZE: Vec2 = Vec2::new(120.0, 20.0); const PADDLE_SIZE: Vec2 = Vec2::new(120.0, 20.0);
@@ -41,7 +45,6 @@ const WALL_COLOR: Color = Color::srgb(0.8, 0.8, 0.8);
const TEXT_COLOR: Color = Color::srgb(0.5, 0.5, 1.0); const TEXT_COLOR: Color = Color::srgb(0.5, 0.5, 1.0);
const SCORE_COLOR: Color = Color::srgb(1.0, 0.5, 0.5); const SCORE_COLOR: Color = Color::srgb(1.0, 0.5, 0.5);
#[derive(Component)] #[derive(Component)]
struct Paddle; struct Paddle;
@@ -67,7 +70,7 @@ struct Score(usize);
struct ScoreboardUi; struct ScoreboardUi;
#[derive(Bundle)] #[derive(Bundle)]
struct WallBundle{ struct WallBundle {
sprite_bundle: SpriteBundle, sprite_bundle: SpriteBundle,
collider: Collider, collider: Collider,
} }
@@ -91,7 +94,12 @@ impl WallBundle {
} }
} }
} }
enum WallLocation { Right, Top, Left, Bottom } enum WallLocation {
Right,
Top,
Left,
Bottom,
}
impl WallLocation { impl WallLocation {
fn position(&self) -> Vec2 { fn position(&self) -> Vec2 {
@@ -111,7 +119,7 @@ impl WallLocation {
match self { match self {
WallLocation::Left | WallLocation::Right => { WallLocation::Left | WallLocation::Right => {
Vec2::new(WALL_THICKNESS, arena_height + WALL_THICKNESS) Vec2::new(WALL_THICKNESS, arena_height + WALL_THICKNESS)
}, }
WallLocation::Top | WallLocation::Bottom => { WallLocation::Top | WallLocation::Bottom => {
Vec2::new(WALL_THICKNESS + arena_width, WALL_THICKNESS) Vec2::new(WALL_THICKNESS + arena_width, WALL_THICKNESS)
} }
@@ -119,7 +127,12 @@ impl WallLocation {
} }
} }
enum Collision { Right, Top, Left, Bottom } enum Collision {
Right,
Top,
Left,
Bottom,
}
fn ball_collision(ball: BoundingCircle, bounding_box: Aabb2d) -> Option<Collision> { fn ball_collision(ball: BoundingCircle, bounding_box: Aabb2d) -> Option<Collision> {
if !ball.intersects(&bounding_box) { if !ball.intersects(&bounding_box) {
@@ -150,14 +163,8 @@ impl Plugin for BreakoutPlugin {
.insert_resource(ClearColor(BACKGROUND_COLOR)) .insert_resource(ClearColor(BACKGROUND_COLOR))
.add_event::<CollisionEvent>() .add_event::<CollisionEvent>()
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(FixedUpdate, ( .add_systems(FixedUpdate, (apply_velocity, move_paddle).chain())
apply_velocity, .add_systems(FixedUpdate, (check_for_collisions, update_scoreboard));
move_paddle,
).chain())
.add_systems(FixedUpdate, (
check_for_collisions,
update_scoreboard
));
} }
} }
@@ -167,7 +174,7 @@ fn setup(
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
// paddle // paddle
let paddle_y = BOTTOM_WALL + GAP_BETWEEN_PADDLE_AND_FLOOR; let paddle_y = BOTTOM_WALL + GAP_BETWEEN_PADDLE_AND_FLOOR;
commands.spawn(( commands.spawn((
@@ -223,7 +230,7 @@ fn setup(
top: SCOREBOARD_TEXT_PADDING, top: SCOREBOARD_TEXT_PADDING,
left: SCOREBOARD_TEXT_PADDING, left: SCOREBOARD_TEXT_PADDING,
..default() ..default()
}) }),
)); ));
// walls // walls
@@ -257,7 +264,7 @@ fn setup(
for row in 0..n_rows { for row in 0..n_rows {
for column in 0..n_columns { for column in 0..n_columns {
let brick_position = Vec2::new ( let brick_position = Vec2::new(
offset_x + column as f32 * (BRICK_SIZE.x + GAP_BETWEEN_BRICKS), offset_x + column as f32 * (BRICK_SIZE.x + GAP_BETWEEN_BRICKS),
offset_y + row as f32 * (BRICK_SIZE.y + GAP_BETWEEN_BRICKS), offset_y + row as f32 * (BRICK_SIZE.y + GAP_BETWEEN_BRICKS),
); );
@@ -306,7 +313,8 @@ fn move_paddle(
direction += 1.0; direction += 1.0;
} }
let new_paddle_position = paddle_transform.translation.x + direction * PADDLE_SPEED * time.delta_seconds(); let new_paddle_position =
paddle_transform.translation.x + direction * PADDLE_SPEED * time.delta_seconds();
let left_bound = LEFT_WALL + WALL_THICKNESS / 2.0 + PADDLE_SIZE.x / 2.0 + PADDLE_PADDING; let left_bound = LEFT_WALL + WALL_THICKNESS / 2.0 + PADDLE_SIZE.x / 2.0 + PADDLE_PADDING;
let right_bound = RIGHT_WALL - WALL_THICKNESS / 2.0 + PADDLE_SIZE.x / 2.0 - PADDLE_PADDING; let right_bound = RIGHT_WALL - WALL_THICKNESS / 2.0 + PADDLE_SIZE.x / 2.0 - PADDLE_PADDING;

View File

@@ -1,4 +1,3 @@
use bevy::prelude::*; use bevy::prelude::*;
#[derive(Component)] #[derive(Component)]
@@ -16,11 +15,7 @@ fn add_people(mut commands: Commands) {
#[derive(Resource)] #[derive(Resource)]
struct GreetTimer(Timer); struct GreetTimer(Timer);
fn greet_people( fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
time: Res<Time>,
mut timer: ResMut<GreetTimer>,
query: Query<&Name, With<Person>>
) {
if timer.0.tick(time.delta()).just_finished() { if timer.0.tick(time.delta()).just_finished() {
for name in &query { for name in &query {
println!("Hello {}!", name.0); println!("Hello {}!", name.0);
@@ -40,7 +35,7 @@ fn update_people(mut query: Query<&mut Name, With<Person>>) {
pub struct HelloPlugin; pub struct HelloPlugin;
impl Plugin for HelloPlugin { impl Plugin for HelloPlugin {
fn build(&self, app: &mut App){ fn build(&self, app: &mut App) {
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating))) app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
.add_systems(Startup, add_people) .add_systems(Startup, add_people)
.add_systems(Update, (update_people, greet_people).chain()); .add_systems(Update, (update_people, greet_people).chain());

View File

@@ -1,11 +1,10 @@
use bevy::prelude::*; use bevy::prelude::*;
mod hello_world_plugin;
mod breakout_plugin; mod breakout_plugin;
mod hello_world_plugin;
use hello_world_plugin::HelloPlugin;
use breakout_plugin::BreakoutPlugin; use breakout_plugin::BreakoutPlugin;
use hello_world_plugin::HelloPlugin;
fn main() { fn main() {
App::new() App::new()