diff --git a/snake-game/.gitignore b/snake-game/.gitignore index e9e2199..143b8a6 100644 --- a/snake-game/.gitignore +++ b/snake-game/.gitignore @@ -1,2 +1,3 @@ /target/ /Cargo.lock +/*.log diff --git a/snake-game/Cargo.toml b/snake-game/Cargo.toml index 03c9a77..848db05 100644 --- a/snake-game/Cargo.toml +++ b/snake-game/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] bevy = "0.18" tracing = "0.1" +tracing-subscriber = "0.3" [profile.release] opt-level = "z" diff --git a/snake-game/examples/paddle_demo.rs b/snake-game/examples/paddle_demo.rs new file mode 100644 index 0000000..9345eba --- /dev/null +++ b/snake-game/examples/paddle_demo.rs @@ -0,0 +1,223 @@ + +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>, + mut materials: ResMut>, +) { + 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>, + time: Res