Проектная работа - WIP

This commit is contained in:
13 changed files with 149 additions and 225 deletions
+8 -34
View File
@@ -1,4 +1,3 @@
use bevy::math::bounding::{Aabb2d, BoundingCircle, BoundingVolume, IntersectsVolume};
use bevy::prelude::*;
@@ -7,10 +6,7 @@ fn main() {
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::srgb(0.8, 0.8, 1.0)))
.add_systems(Startup, setup)
.add_systems(
FixedUpdate,
(move_paddle, apply_velocity, check_for_collisions),
)
.add_systems(FixedUpdate, (move_paddle, apply_velocity, check_for_collisions))
.run();
}
@@ -40,11 +36,7 @@ struct Ball;
#[derive(Component)]
struct Collider;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<ColorMaterial>>) {
commands.spawn(Camera2d);
let paddle_y = BOTTOM_WALL + PADDLE_GAP_Y;
@@ -77,11 +69,7 @@ fn setup(
commands.spawn(Wall::new(WallLocation::Bottom));
}
fn move_paddle(
input: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
mut paddle_transform: Single<&mut Transform, With<Paddle>>,
) {
fn move_paddle(input: Res<ButtonInput<KeyCode>>, time: Res<Time>, mut paddle_transform: Single<&mut Transform, With<Paddle>>) {
let direction = if input.pressed(KeyCode::ArrowLeft) {
-1.0
} else if input.pressed(KeyCode::ArrowRight) {
@@ -107,19 +95,13 @@ fn apply_velocity(time: Res<Time>, mut query: Query<(&mut Transform, &Velocity)>
}
}
fn check_for_collisions(
ball_query: Single<(&mut Velocity, &Transform), With<Ball>>,
collider_query: Query<&Transform, With<Collider>>,
) {
fn check_for_collisions(ball_query: Single<(&mut Velocity, &Transform), With<Ball>>, collider_query: Query<&Transform, With<Collider>>) {
let (mut ball_velocity, ball_transform) = ball_query.into_inner();
for collider_transform in &collider_query {
let collision = ball_collision(
BoundingCircle::new(ball_transform.translation.truncate(), BALL_DIAMETER / 2.0),
Aabb2d::new(
collider_transform.translation.truncate(),
collider_transform.scale.truncate() / 2.0,
),
Aabb2d::new(collider_transform.translation.truncate(), collider_transform.scale.truncate() / 2.0),
);
if let Some(collision) = collision {
@@ -160,11 +142,7 @@ fn ball_collision(circle: BoundingCircle, rect: Aabb2d) -> Option<Collision> {
let offset = circle.center() - closest;
let side = if offset.x.abs() > offset.y.abs() {
if offset.x < 0.0 {
Collision::Left
} else {
Collision::Right
}
if offset.x < 0.0 { Collision::Left } else { Collision::Right }
} else if offset.y > 0.0 {
Collision::Top
} else {
@@ -212,12 +190,8 @@ impl WallLocation {
fn size(&self) -> Vec2 {
match self {
WallLocation::Left | WallLocation::Right => {
Vec2::new(WALL_WIDTH, TOP_WALL - BOTTOM_WALL)
}
WallLocation::Top | WallLocation::Bottom => {
Vec2::new(RIGHT_WALL - LEFT_WALL, WALL_WIDTH)
}
WallLocation::Left | WallLocation::Right => Vec2::new(WALL_WIDTH, TOP_WALL - BOTTOM_WALL),
WallLocation::Top | WallLocation::Bottom => Vec2::new(RIGHT_WALL - LEFT_WALL, WALL_WIDTH),
}
}
}