Files
rust-otus/snake-game/examples/paddle_demo.rs
T

224 lines
6.0 KiB
Rust

use bevy::math::bounding::{Aabb2d, BoundingCircle, BoundingVolume, IntersectsVolume};
use bevy::prelude::*;
fn main() {
App::new()
.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),
)
.run();
}
const PADDLE_GAP_Y: f32 = 50.0;
const PADDLE_COLOR: Color = Color::srgb(0.5, 0.5, 1.0);
const PADDLE_SIZE: Vec2 = Vec2::new(120.0, 20.0);
const PADDLE_SPEED: f32 = 500.0;
const PADDLE_PADDING: f32 = 10.0;
const LEFT_WALL: f32 = -600.0;
const RIGHT_WALL: f32 = 600.0;
const BOTTOM_WALL: f32 = -300.0;
const TOP_WALL: f32 = 300.0;
const WALL_WIDTH: f32 = 20.0;
const BALL_START_POS: Vec2 = Vec2::new(0.0, -100.0);
const BALL_COLOR: Color = Color::srgb(0.2, 0.5, 0.2);
const BALL_DIAMETER: f32 = 20.0;
const INIT_BALL_SPEED: Vec2 = Vec2::new(100.0, 100.0);
#[derive(Component)]
struct Paddle;
#[derive(Component)]
struct Ball;
#[derive(Component)]
struct Collider;
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;
commands.spawn((
Sprite::from_color(PADDLE_COLOR, Vec2::ONE),
Transform {
translation: Vec3::new(0.0, paddle_y, 0.0),
scale: PADDLE_SIZE.extend(1.0),
..Default::default()
},
Paddle,
Collider,
));
commands.spawn((
Mesh2d(meshes.add(Circle::default())),
MeshMaterial2d(materials.add(BALL_COLOR)),
Transform {
translation: BALL_START_POS.extend(0.0),
scale: Vec3::splat(BALL_DIAMETER),
..Default::default()
},
Velocity(INIT_BALL_SPEED),
Ball,
));
commands.spawn(Wall::new(WallLocation::Left));
commands.spawn(Wall::new(WallLocation::Right));
commands.spawn(Wall::new(WallLocation::Top));
commands.spawn(Wall::new(WallLocation::Bottom));
}
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) {
1.0
} else {
0.0
};
let new_pos = paddle_transform.translation.x + direction * PADDLE_SPEED * time.delta_secs();
const LEFT_BOUND: f32 = LEFT_WALL + WALL_WIDTH / 2.0 + PADDLE_SIZE.x / 2.0 + PADDLE_PADDING;
const RIGHT_BOUND: f32 = RIGHT_WALL - WALL_WIDTH / 2.0 - PADDLE_SIZE.x / 2.0 - PADDLE_PADDING;
paddle_transform.translation.x = new_pos.clamp(LEFT_BOUND, RIGHT_BOUND);
}
#[derive(Component, Deref, DerefMut)]
struct Velocity(Vec2);
fn apply_velocity(time: Res<Time>, mut query: Query<(&mut Transform, &Velocity)>) {
for (mut transform, velocity) in &mut query {
transform.translation += velocity.extend(0.0) * time.delta_secs();
}
}
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,
),
);
if let Some(collision) = collision {
let mut reflect_x = false;
let mut reflect_y = false;
match collision {
Collision::Left => reflect_x = ball_velocity.x > 0.0,
Collision::Right => reflect_x = ball_velocity.x < 0.0,
Collision::Top => reflect_y = ball_velocity.y < 0.0,
Collision::Bottom => reflect_y = ball_velocity.y > 0.0,
};
if reflect_x {
ball_velocity.x *= -1.0;
}
if reflect_y {
ball_velocity.y *= -1.0;
}
}
}
}
enum Collision {
Left,
Right,
Top,
Bottom,
}
fn ball_collision(circle: BoundingCircle, rect: Aabb2d) -> Option<Collision> {
if !circle.intersects(&rect) {
return None;
}
let closest = rect.closest_point(circle.center());
let offset = circle.center() - closest;
let side = if offset.x.abs() > offset.y.abs() {
if offset.x < 0.0 {
Collision::Left
} else {
Collision::Right
}
} else if offset.y > 0.0 {
Collision::Top
} else {
Collision::Bottom
};
Some(side)
}
#[derive(Component)]
#[require(Sprite, Transform)]
struct Wall;
impl Wall {
pub fn new(location: WallLocation) -> (Wall, Sprite, Transform, Collider) {
(
Wall,
Sprite::from_color(Color::srgb(1.0, 0.4, 0.4), Vec2::ONE),
Transform {
translation: location.position().extend(0.0),
scale: location.size().extend(1.0),
..Default::default()
},
Collider,
)
}
}
enum WallLocation {
Left,
Right,
Top,
Bottom,
}
impl WallLocation {
fn position(&self) -> Vec2 {
match self {
WallLocation::Left => Vec2::new(LEFT_WALL, 0.0),
WallLocation::Right => Vec2::new(RIGHT_WALL, 0.0),
WallLocation::Top => Vec2::new(0.0, TOP_WALL),
WallLocation::Bottom => Vec2::new(0.0, BOTTOM_WALL),
}
}
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)
}
}
}
}