From 804186ea2f9653e8629bc66d25448ddb03b9b24b Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Sun, 10 Aug 2025 21:29:25 -0500 Subject: [PATCH] Handle player ship destruction... some of it... The player will be respawned, their lives decreased, and the board cleared. The UI doesn't update, and the sudden snap to a freshly reset board is quite jarring. The state transition to GameOver stops the game, but there isn't anything else running in that state so it just looks frozen. Basically, there's a ton left to do, but technically I have handled player ship destruction! --- src/lib.rs | 1 + src/ship.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b10a434..2ff9c00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,7 @@ impl Plugin for AsteroidPlugin { asteroids::spawn_asteroid.after(asteroids::tick_asteroid_manager), asteroids::split_asteroids, ship::bullet_impact_listener, + ship::ship_impact_listener, collision_listener, // TODO: Remove debug printing debug_collision_event_printer, diff --git a/src/ship.rs b/src/ship.rs index b76920b..0675d1f 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,6 +1,7 @@ use crate::{ - AngularVelocity, GameAssets, - event::BulletDestroy, + AngularVelocity, GameAssets, GameState, Lives, + asteroids::Asteroid, + event::{BulletDestroy, ShipDestroy}, physics::{Velocity, Wrapping}, }; @@ -44,3 +45,38 @@ pub fn bullet_impact_listener(mut commands: Commands, mut events: EventReader, + mut commands: Commands, + mut lives: ResMut, + rocks: Query>, + mut player: Single<(&mut Transform, &mut Velocity), With>, + mut next_state: ResMut>, +) { + for _ in events.read() { + // STEP 1: Decrement lives (and maybe go to game over) + if lives.0 == 0 { + // If already at 0, game is over. + next_state.set(GameState::GameOver); + } else { + // Decrease life count. + lives.0 -= 1; + } + + // STEP 2: Clear asteroids + for rock in rocks { + commands.entity(rock).despawn(); + } + + // STEP 3: Respawn player (teleport them to the origin) + player.0.translation = Vec3::ZERO; + player.1.0 = Vec2::ZERO; + } +}