From 6997f1b6c8630aa0a613f72b6053d2928d488a15 Mon Sep 17 00:00:00 2001 From: Joda Date: Sat, 18 Oct 2025 15:51:19 -0500 Subject: [PATCH 1/5] refactor out stars --- src/main.rs | 6 ++- src/screens/game.rs | 61 ++---------------------- src/screens/splash.rs | 97 +------------------------------------- src/stars/mod.rs | 106 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 152 deletions(-) create mode 100644 src/stars/mod.rs diff --git a/src/main.rs b/src/main.rs index a746f17..a2b8a47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ use crate::input::InputPlugin; mod audio; mod input; mod screens; +mod stars; mod sundry; const RES_WIDTH: u32 = 853; @@ -59,7 +60,8 @@ impl Plugin for AppPlugin { primary_window: Some(Window { title: "Star Journey".to_string(), fit_canvas_to_parent: true, - mode: WindowMode::BorderlessFullscreen(MonitorSelection::Current), + // mode: WindowMode::BorderlessFullscreen(MonitorSelection::Current), + mode: WindowMode::Windowed, resolution: WindowResolution::new(1920, 1080), resizable: false, position: WindowPosition::Centered(MonitorSelection::Current), @@ -76,6 +78,8 @@ impl Plugin for AppPlugin { app.add_plugins(screens::plugin); + app.add_plugins(stars::plugin); + // #[cfg(feature = "dev")] // app.add_plugins((RemotePlugin::default(), RemoteHttpPlugin::default())); diff --git a/src/screens/game.rs b/src/screens/game.rs index 91ca916..137b7a2 100644 --- a/src/screens/game.rs +++ b/src/screens/game.rs @@ -54,7 +54,6 @@ pub(super) fn plugin(app: &mut App) { spawn_asteroids, update_asteroids, collide_with_asteroid_check, - update_stars, ) .in_set(GameSystems::Environment) .run_if(in_state(Screen::Game)), @@ -90,47 +89,9 @@ fn spawn_game_screen( PIXEL_PERFECT_LAYERS, )); - spawn_default_stars(&mut commands, &asset_server); - time.unpause(); } -fn spawn_default_stars(commands: &mut Commands, asset_server: &Res) { - let star_count = 100; - for _ in 0..star_count { - let x = random_range(-400.0..400.0); - let y = random_range(-220.0..220.0); - let variant = random_range(1..=2); - - let brightness: f32 = random_range(0.6..1.0); - let brightness = brightness * brightness; - - let color = if random_range(0.0..1.0) < 0.5 { - Color::linear_rgb(brightness, brightness * 0.9, brightness * 0.7) // warmer star - } else { - Color::linear_rgb(brightness * 0.8, brightness * 0.9, brightness) // cooler star - }; - - commands.spawn(( - Name::new("Star"), - Star { - speed: random_range(0.5..=1.0), - }, - Sprite { - image: asset_server.load(format!("sprites/stars/star{variant}.png")), - color, - ..default() - }, - Transform { - translation: Vec3::new(x, y, -10.0), - ..default() - }, - DespawnOnExit(Screen::Game), - PIXEL_PERFECT_LAYERS, - )); - } -} - fn despawn_game_screen( mut commands: Commands, player: Query>, @@ -165,11 +126,6 @@ struct Player { #[derive(Component)] struct Projectile; -#[derive(Component)] -pub struct Star { - pub speed: f32, -} - #[derive(Component)] struct Asteroid { pub speed: f32, @@ -187,7 +143,11 @@ fn player_input( asset_server: Res, ) { let action_state = input_query.single().unwrap(); - let (mut player_transform, mut player) = player_query.single_mut().unwrap(); + let player = player_query.single_mut(); + if player.is_err() { + return; + } + let (mut player_transform, mut player) = player.unwrap(); player.timer.tick(time.delta()); if player.timer.is_finished() { @@ -319,17 +279,6 @@ fn update_asteroids( } } -fn update_stars(mut stars: Query<(&mut Transform, &mut Star)>) { - for (mut transform, mut star) in stars.iter_mut() { - transform.translation -= Vec3::Y * star.speed; - if transform.translation.y < -240.0 { - transform.translation.y = 240.0 + random_range(0.0..20.0); - transform.translation.x = random_range(-400.0..400.0); - star.speed = random_range(0.5..=1.0); - } - } -} - fn spawn_asteroid( commands: &mut Commands, asset_server: &Res, diff --git a/src/screens/splash.rs b/src/screens/splash.rs index cdc4cf2..13e977c 100644 --- a/src/screens/splash.rs +++ b/src/screens/splash.rs @@ -3,15 +3,10 @@ use rand::random_range; use crate::{ PIXEL_PERFECT_LAYERS, - screens::{Screen, game::Star}, + screens::Screen, + stars::{Star, StarPool}, }; -#[derive(Resource)] -struct StarPool { - active_count: usize, - inactive_stars: Vec, -} - #[derive(Component)] struct Title; @@ -22,15 +17,7 @@ struct Developer; struct FadeOut; pub(super) fn plugin(app: &mut App) { - app.insert_resource::(StarPool { - active_count: 0, - inactive_stars: Vec::new(), - }); app.add_systems(OnEnter(Screen::Splash), setup_splash); - app.add_systems( - FixedUpdate, - (spawn_stars, update_stars).run_if(in_state(Screen::Splash).or(in_state(Screen::Title))), - ); app.add_systems( FixedUpdate, (splash_update, update_title, update_developer, fade_out).run_if(in_state(Screen::Splash)), @@ -111,86 +98,6 @@ fn splash_update(mut state: ResMut>, time: Res