Begin work on event-based asteroid spawning

The events are being emitted by the spawn manager, and consumed by the
spawn_asteroid system. Now to wire in the spawn properties, and then
make a good spawning manager.
This commit is contained in:
2025-07-29 13:12:22 -05:00
parent 911b6c5fe7
commit 2c43bc699e
2 changed files with 30 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ use std::time::Duration;
/// This is the module containing all the rock-related things /// This is the module containing all the rock-related things
/// not... not the whole game. /// not... not the whole game.
use bevy::prelude::*; use bevy::{math::VectorSpace, prelude::*};
use crate::{GameAssets, Position, Rotation, Velocity}; use crate::{GameAssets, Position, Rotation, Velocity};
@@ -32,16 +32,40 @@ impl AsteroidSpawner {
} }
} }
#[derive(Event)]
pub struct SpawnAsteroid {
pos: Vec2,
vel: Vec2,
size: AsteroidSize,
}
/// Update the asteroid spawn timer and spawn any asteroids /// Update the asteroid spawn timer and spawn any asteroids
/// that are due this frame. /// that are due this frame.
pub fn tick_asteroid_manager( pub fn tick_asteroid_manager(
mut commands: Commands, mut events: EventWriter<SpawnAsteroid>,
mut spawner: ResMut<AsteroidSpawner>, mut spawner: ResMut<AsteroidSpawner>,
game_assets: Res<GameAssets>,
time: Res<Time>, time: Res<Time>,
) { ) {
spawner.timer.tick(time.delta()); spawner.timer.tick(time.delta());
if spawner.timer.just_finished() { if spawner.timer.just_finished() {
events.write(SpawnAsteroid {
pos: Vec2::ZERO,
vel: Vec2::ZERO,
size: AsteroidSize::SMALL,
});
}
}
/// Utility function to spawn a single asteroid of a given type
/// TODO: convert to an event listener monitoring for "spawn asteroid" events
/// from the `fn tick_asteroid_manager(...)` system.
pub fn spawn_asteroid(
mut events: EventReader<SpawnAsteroid>,
mut commands: Commands,
game_assets: Res<GameAssets>,
) {
for spawn in events.read() {
// TODO: Use the asteroid properties defined in the event to spawn the entity
commands.spawn(( commands.spawn((
Asteroid(AsteroidSize::SMALL), Asteroid(AsteroidSize::SMALL),
Position(Vec2::new(40.0, 40.0)), Position(Vec2::new(40.0, 40.0)),
@@ -52,10 +76,3 @@ pub fn tick_asteroid_manager(
)); ));
} }
} }
/// Utility function to spawn a single asteroid of a given type
/// TODO: convert to an event listener monitoring for "spawn asteroid" events
/// from the `fn tick_asteroid_manager(...)` system.
fn spawn_asteroid(mut commands: Commands) {
todo!();
}

View File

@@ -39,6 +39,7 @@ impl Plugin for AsteroidPlugin {
input_ship_rotation, input_ship_rotation,
wrap_entities, wrap_entities,
asteroids::tick_asteroid_manager, asteroids::tick_asteroid_manager,
asteroids::spawn_asteroid.after(asteroids::tick_asteroid_manager),
) )
.run_if(in_state(GameState::Playing)), .run_if(in_state(GameState::Playing)),
) )
@@ -46,7 +47,8 @@ impl Plugin for AsteroidPlugin {
FixedPostUpdate, FixedPostUpdate,
(integrate_velocity, update_positions, apply_rotation_to_mesh) (integrate_velocity, update_positions, apply_rotation_to_mesh)
.run_if(in_state(GameState::Playing)), .run_if(in_state(GameState::Playing)),
); )
.add_event::<asteroids::SpawnAsteroid>();
app.insert_state(GameState::TitleScreen); app.insert_state(GameState::TitleScreen);
} }
} }