Move Lifetime component to machinery module

This commit is contained in:
2025-08-11 23:33:56 -05:00
parent 34ee2fcc7d
commit 9a262fcffc
3 changed files with 20 additions and 14 deletions

View File

@@ -22,6 +22,7 @@ use bevy_rapier2d::{
prelude::*, prelude::*,
render::RapierDebugRenderPlugin, render::RapierDebugRenderPlugin,
}; };
use machinery::Lifetime;
use resources::{GameAssets, Lives, Score, WorldSize}; use resources::{GameAssets, Lives, Score, WorldSize};
pub struct AsteroidPlugin; pub struct AsteroidPlugin;
@@ -64,7 +65,7 @@ impl Plugin for AsteroidPlugin {
physics::collision_listener, physics::collision_listener,
// TODO: Remove debug printing // TODO: Remove debug printing
debug_collision_event_printer, debug_collision_event_printer,
tick_lifetimes, machinery::tick_lifetimes,
) )
.run_if(in_state(GameState::Playing)), .run_if(in_state(GameState::Playing)),
) )
@@ -98,9 +99,6 @@ pub enum GameState {
GameOver, // Game has ended. Present game over dialogue and await user restart GameOver, // Game has ended. Present game over dialogue and await user restart
} }
#[derive(Component)]
struct Lifetime(Timer);
fn spawn_camera(mut commands: Commands) { fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera2d); commands.spawn(Camera2d);
} }
@@ -196,12 +194,3 @@ fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
TextFont::from_font_size(25.0), TextFont::from_font_size(25.0),
)); ));
} }
fn tick_lifetimes(mut commands: Commands, time: Res<Time>, query: Query<(Entity, &mut Lifetime)>) {
for (e, mut life) in query {
life.0.tick(time.delta());
if life.0.just_finished() {
commands.entity(e).despawn();
}
}
}

View File

@@ -76,3 +76,19 @@ pub fn tick_asteroid_manager(
events.write(SpawnAsteroid { pos, vel, size }); events.write(SpawnAsteroid { pos, vel, size });
} }
} }
#[derive(Component)]
pub struct Lifetime(pub Timer);
pub fn tick_lifetimes(
mut commands: Commands,
time: Res<Time>,
query: Query<(Entity, &mut Lifetime)>,
) {
for (e, mut life) in query {
life.0.tick(time.delta());
if life.0.just_finished() {
commands.entity(e).despawn();
}
}
}

View File

@@ -21,9 +21,10 @@ use bevy::{
use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Sensor}; use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Sensor};
use crate::{ use crate::{
AngularVelocity, GameAssets, GameState, Lifetime, Lives, AngularVelocity, GameAssets, GameState, Lives,
config::ASTEROID_LIFETIME, config::ASTEROID_LIFETIME,
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid}, events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
machinery::Lifetime,
physics::{Velocity, Wrapping}, physics::{Velocity, Wrapping},
}; };