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:
@@ -2,7 +2,7 @@ use std::time::Duration;
|
||||
|
||||
/// This is the module containing all the rock-related things
|
||||
/// not... not the whole game.
|
||||
use bevy::prelude::*;
|
||||
use bevy::{math::VectorSpace, prelude::*};
|
||||
|
||||
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
|
||||
/// that are due this frame.
|
||||
pub fn tick_asteroid_manager(
|
||||
mut commands: Commands,
|
||||
mut events: EventWriter<SpawnAsteroid>,
|
||||
mut spawner: ResMut<AsteroidSpawner>,
|
||||
game_assets: Res<GameAssets>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
spawner.timer.tick(time.delta());
|
||||
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((
|
||||
Asteroid(AsteroidSize::SMALL),
|
||||
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!();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user