From 6681b25728001a73e18b7bdb34acedff7ffc6584 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Mon, 28 Jul 2025 15:35:50 -0500 Subject: [PATCH] Functional prototype of WIP asteroid spawning It needs a whole lot more work, but hey, look: A rock! ... well a circle, anyway. --- src/lib.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2b3fa1b..f980211 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,9 +2,11 @@ pub mod config; mod preparation_widget; mod title_screen; +use std::time::Duration; + use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE}; -use bevy::prelude::*; +use bevy::{color::palettes::css::GRAY, prelude::*}; use bevy_inspector_egui::prelude::ReflectInspectorOptions; use bevy_inspector_egui::InspectorOptions; @@ -26,11 +28,19 @@ impl Plugin for AsteroidPlugin { .insert_resource(Lives(3)) .register_type::() .insert_resource(Score(0)) + .insert_resource(AsteroidSpawner { + timer: Timer::new(Duration::from_secs(3), TimerMode::Repeating), + }) .add_systems(Startup, spawn_camera) .add_systems(OnEnter(GameState::Playing), (spawn_player, spawn_ui)) .add_systems( FixedUpdate, - (input_ship_thruster, input_ship_rotation, wrap_entities) + ( + input_ship_thruster, + input_ship_rotation, + wrap_entities, + tick_asteroid_manager, + ) .run_if(in_state(GameState::Playing)), ) .add_systems( @@ -62,6 +72,24 @@ struct Rotation(f32); #[derive(Component)] struct Ship; +#[derive(Component, Deref, DerefMut)] +struct Asteroid(AsteroidSize); + +enum AsteroidSize { + SMALL, + MEDIUM, + LARGE, +} + +#[derive(Resource)] +struct AsteroidSpawner { + timer: Timer, + // TODO: Configurables? + // - interval + // - density + // - size distribution +} + /// Marker for any entity that should wrap on screen edges #[derive(Component)] struct Wrapping; @@ -69,6 +97,7 @@ struct Wrapping; // Data component to store color properties attached to an entity // This was easier (and imo better) than holding global consts with // UUID assets. +// TODO: Convert to Resource. I don't need per-entity thruster colors for this. #[derive(Component)] struct ThrusterColors(Handle, Handle); @@ -141,6 +170,36 @@ fn spawn_player( )); } +/// Update the asteroid spawn timer and spawn any asteroids +/// that are due this frame. +fn tick_asteroid_manager( + mut commands: Commands, + mut spawner: ResMut, + // TODO: move the mesh & material loading somewhere else + mut meshes: ResMut>, + mut materials: ResMut>, + time: Res