2 Commits

Author SHA1 Message Date
2dd3b2ff61 Fix case on AsteroidSize enum variants
All checks were successful
Basic checks / Basic build-and-test supertask (push) Successful in 6m28s
Closes #9: "Fix case on AsteroidSize enum variants"
2025-07-29 13:15:54 -05:00
2c43bc699e 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.
2025-07-29 13:12:22 -05:00
2 changed files with 34 additions and 15 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};
@@ -10,9 +10,9 @@ use crate::{GameAssets, Position, Rotation, Velocity};
pub struct Asteroid(AsteroidSize); pub struct Asteroid(AsteroidSize);
pub enum AsteroidSize { pub enum AsteroidSize {
SMALL, Small,
MEDIUM, Medium,
LARGE, Large,
} }
#[derive(Resource)] #[derive(Resource)]
@@ -32,18 +32,42 @@ 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)),
Velocity(Vec2::new(10.0, 0.0)), Velocity(Vec2::new(10.0, 0.0)),
Rotation(0.0), Rotation(0.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);
} }
} }