From 0fe5e8f6c92af3c3737bc29467765530a85dc659 Mon Sep 17 00:00:00 2001 From: moo Date: Tue, 17 Mar 2026 14:56:49 -0700 Subject: [PATCH] working --- src/entities.rs | 4 +- src/main.rs | 296 ++---------------------------------------------- src/systems.rs | 156 +++++++++++++------------ 3 files changed, 94 insertions(+), 362 deletions(-) diff --git a/src/entities.rs b/src/entities.rs index 13687f1..1bc88db 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; //=================== Resources =================== -#[derive(Resource, Default)] +#[derive(Resource)] pub struct Sprites { pub ship_sprite: Handle, pub asteroid_sprite: Handle, @@ -36,4 +36,4 @@ pub struct Velocity { pub linear: Vec2, ///degs per sec, positive is clockwise pub angular: f32, -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index bba2927..4729732 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,63 +1,9 @@ -use bevy::{ - color::palettes::css::{GREEN, RED}, - gizmos::circles, - prelude::*, - sprite, transform, - window::WindowResolution, -}; -use rand::Rng; -use std::f32::consts::PI; +mod entities; +mod systems; -const PLAYER_SIZE: f32 = 30.0; -const PLAYER_ROTATION_SPEED: f32 = 3.0 * PI / 180.0; -const PLAYER_ACCELERATION: f32 = 0.5; -const PLAYER_FRICTION: f32 = 0.99; -const PLAYER_MAX_SPEED: f32 = 5.0; -const PLAYER_Z: f32 = 3.0; - -const BULLET_SIZE: f32 = 5.0; -const BULLET_SPEED: f32 = 10.0; -const BULLET_LIFETIME: f32 = 1.0; // seconds -const BULLET_Z: f32 = 2.0; - -const ASTEROID_SIZE: f32 = 150.0; -const ASTEROID_SPEED: f32 = 2.0; -const INITIAL_ASTEROID_COUNT: i32 = 5; -const ASTEROID_Z: f32 = 1.0; - -#[derive(Resource)] -struct Sprites { - ship_sprite: Handle, - asteroid_sprite: Handle, -} - -#[derive(Component)] -struct Player { - lives: i8, -} - -#[derive(Component)] -struct Asteroid; - -#[derive(Component)] -struct Collider { - radius: f32, -} - -#[derive(Component)] -struct Bullet { - lifetime: Timer, -} - -#[derive(Component)] -struct Velocity { - linear: Vec2, - ///degs per sec, positive is clockwise - angular: f32, -} - -#[derive(Resource, Deref, DerefMut)] -struct WindowSize(Vec2); +use bevy::{prelude::*, window::WindowResolution}; +use entities::*; +use systems::*; fn main() { App::new() @@ -75,7 +21,7 @@ fn main() { asteroid_sprite: Handle::default(), }) .add_systems(Startup, (load_sprites, setup).chain()) - .add_systems(Update, (resolution)) + .add_systems(Update, resolution) .add_systems( Update, ( @@ -85,237 +31,9 @@ fn main() { bullet_lifetime_system, screen_wrap_system, collision_system, + restart_system, ) .chain(), ) .run(); } - -fn load_sprites( - mut commands: Commands, - asset_server: Res, - mut sprites: ResMut, -) { - sprites.ship_sprite = asset_server.load("ship.png"); - sprites.asteroid_sprite = asset_server.load("asteroid.png"); -} - -fn setup(mut commands: Commands, window_size: Res, sprites: Res) { - commands.spawn(Camera2d::default()); - commands.spawn(( - Sprite::from_image(sprites.ship_sprite.clone()), - Transform { - scale: Vec3::splat(0.2), - translation: Vec3::new(0.0, 0.0, PLAYER_Z), - ..default() - }, - Player { lives: 3 }, - Velocity { - linear: Vec2::ZERO, - angular: 0.0, - }, - Collider { - radius: PLAYER_SIZE / 2.0, - }, - )); - - let mut rng = rand::rng(); - for _ in 0..INITIAL_ASTEROID_COUNT { - let x = rng.random_range(-(window_size.0.x) / 2.0..window_size.0.x as f32 / 2.0); - let y = rng.random_range(-(window_size.0.y as f32) / 2.0..window_size.0.y as f32 / 2.0); - let angle = rng.random_range(0.0..2.0 * PI); - - commands.spawn(( - Transform { - translation: Vec3::new(x, y, ASTEROID_Z), - scale: Vec3::splat(0.5), - rotation: Quat::from_rotation_z(rng.random_range(0.0..2.0 * PI)), - ..default() - }, - Sprite::from_image(sprites.asteroid_sprite.clone()), - Asteroid, - Velocity { - linear: Vec2::new(angle.cos(), angle.sin()) * ASTEROID_SPEED, - angular: rng.random_range(-0.5..0.5), - }, - Collider { - radius: ASTEROID_SIZE / 2.0, - }, - )); - } -} - -fn player_movement_system( - keyboard_input: Res>, - mut player: Query<(&mut Transform, &mut Velocity), With>, - mut commands: Commands, -) { - if keyboard_input.pressed(KeyCode::KeyQ) { - commands.write_message(AppExit::Success); - } - - if let Ok((mut transform, mut velocity)) = player.single_mut() { - if keyboard_input.pressed(KeyCode::ArrowLeft) { - transform.rotate_z(PLAYER_ROTATION_SPEED); - // velocity.angular += PLAYER_ROTATION_SPEED; - } - if keyboard_input.pressed(KeyCode::ArrowRight) { - transform.rotate_z(-PLAYER_ROTATION_SPEED); - // velocity.angular -= PLAYER_ROTATION_SPEED; - } - - if keyboard_input.pressed(KeyCode::ArrowUp) { - let direction = transform.rotation * Vec3::Y; - velocity.linear += direction.truncate() * PLAYER_ACCELERATION; - - // max spped - // if velocity.linear.length() > PLAYER_MAX_SPEED { - // velocity.linear = velocity.linear.normalize() * PLAYER_MAX_SPEED; - // } - } - - velocity.linear *= PLAYER_FRICTION; - } -} - -fn player_shooting_system( - mut commands: Commands, - keyboard_input: Res>, - query: Query<&Transform, With>, -) { - if keyboard_input.just_pressed(KeyCode::Space) { - if let Ok(player_transform) = query.single() { - let direction = player_transform.rotation * Vec3::Y; - let bullet_velocity = direction.truncate() * BULLET_SPEED; - - commands.spawn(( - Sprite::from_color(Color::WHITE, Vec2::splat(BULLET_SIZE)), - Transform { - translation: Vec3 { - x: player_transform.translation.x, - y: player_transform.translation.y, - z: BULLET_Z, - }, - ..default() - }, - Bullet { - lifetime: Timer::from_seconds(BULLET_LIFETIME, TimerMode::Once), - }, - Velocity { - linear: bullet_velocity, - angular: 0.0, - }, - Collider { - radius: BULLET_SIZE / 2.0, - }, - )); - } - } -} - -fn move_system(time: Res