From 4c316e36a664c4e661098dff4a8af51ebeb71ac0 Mon Sep 17 00:00:00 2001 From: hut Date: Wed, 24 Apr 2024 04:36:49 +0200 Subject: [PATCH] despawn asteroids only when they're 1km+ away --- src/world.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/world.rs b/src/world.rs index 469fe3a..3ab8f01 100644 --- a/src/world.rs +++ b/src/world.rs @@ -172,7 +172,7 @@ fn spawn_despawn_asteroids( q_player: Query<&Position, With>, mut ew_despawn: EventWriter, mut db: ResMut, - mut q_asteroid: Query<&SceneInstance, With>, + q_asteroid: Query<(&Position, &SceneInstance), With>, mut last_player_cell: Local, id2pos: Res, asset_server: Res, @@ -216,12 +216,14 @@ fn spawn_despawn_asteroids( || origin.y < y_min || origin.y > y_max || origin.z < z_min || origin.z > z_max { - if let Ok(sceneinstance) = q_asteroid.get(asteroid.entity) { - ew_despawn.send(DespawnEvent { - entity: asteroid.entity, - sceneinstance: **sceneinstance, - origin: origin.clone(), - }); + if let Ok((pos, sceneinstance)) = q_asteroid.get(asteroid.entity) { + if pos.0.distance(player.0) > 1000.0 { + ew_despawn.send(DespawnEvent { + entity: asteroid.entity, + sceneinstance: **sceneinstance, + origin: origin.clone(), + }); + } } else { error!("Couldn't despawn asteroid:"); dbg!(origin);