Create Lifetime component and system
Closes #13 The lifetime component, and system to operate it, are ready! Now I can delete things that have lived for too long.
This commit is contained in:
13
src/lib.rs
13
src/lib.rs
@@ -58,6 +58,7 @@ impl Plugin for AsteroidPlugin {
|
||||
collision_listener,
|
||||
// TODO: Remove debug printing
|
||||
debug_collision_event_printer,
|
||||
tick_lifetimes,
|
||||
)
|
||||
.run_if(in_state(GameState::Playing)),
|
||||
)
|
||||
@@ -163,6 +164,9 @@ impl From<Score> for String {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Lifetime(Timer);
|
||||
|
||||
#[derive(InspectorOptions, Reflect, Resource, Debug, Deref, Clone, Copy)]
|
||||
#[reflect(Resource, InspectorOptions)]
|
||||
struct Lives(i32);
|
||||
@@ -346,3 +350,12 @@ fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user