From cfbd23f485f39981b603e5b1dc0093be210c9365 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 1 Apr 2024 23:54:56 +0200 Subject: [PATCH] fix despawning of scene-based asteroids, turn them back from spheres to scenes --- src/world.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/world.rs b/src/world.rs index 869ab80..2d57c01 100644 --- a/src/world.rs +++ b/src/world.rs @@ -11,7 +11,7 @@ const ASTEROID_SIZE: f32 = 100.0; const STARS_MAX_MAGNITUDE: f32 = 5.5; const CENTER_WORLD_ON_PLAYER: bool = true; -const ASTEROIDS_ARE_SPHERES: bool = true; +const ASTEROIDS_ARE_SPHERES: bool = false; const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0"; const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0"; @@ -36,6 +36,7 @@ impl Plugin for WorldPlugin { app.add_systems(Startup, setup); app.add_systems(Startup, generate_asteroids); app.add_systems(Update, handle_cheats); + app.add_systems(PostUpdate, handle_despawn); app.add_systems(Update, spawn_despawn_asteroids); app.add_plugins(PhysicsPlugins::default()); app.add_plugins(MaterialPlugin::::default()); @@ -44,6 +45,7 @@ impl Plugin for WorldPlugin { app.insert_resource(AsteroidUpdateTimer( Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating))); app.insert_resource(AsteroidDatabase(Vec::new())); + app.add_event::(); if CENTER_WORLD_ON_PLAYER { // Disable bevy_xpbd's position->transform sync function @@ -71,6 +73,9 @@ struct AsteroidData { pos: DVec3, } +#[derive(Event)] +pub struct DespawnEvent(Entity); + #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] pub struct RingMaterial { alpha_mode: AlphaMode, @@ -207,6 +212,7 @@ fn spawn_despawn_asteroids( mut materials: ResMut>, asset_server: Res, mut log: ResMut, + mut ew_despawn: EventWriter, ) { if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { return; @@ -230,7 +236,7 @@ fn spawn_despawn_asteroids( asteroid.entity = None; asteroid.is_spawned = false; despawned += 1; - commands.entity(entity).despawn(); + ew_despawn.send(DespawnEvent(entity)); break; } } @@ -291,6 +297,15 @@ fn spawn_despawn_asteroids( } } +fn handle_despawn( + mut commands: Commands, + mut er_despawn: EventReader, +) { + for despawn in er_despawn.read() { + commands.entity(despawn.0).despawn(); + } +} + fn handle_cheats( mut commands: Commands, key_input: Res>,