From 910d1b44070f9206d7fabfbbc2aff2390227b794 Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 2 Apr 2024 17:17:31 +0200 Subject: [PATCH] fix despawning of scenes (fixes framerate drop after traveling far) --- src/world.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/world.rs b/src/world.rs index 805cc66..f572beb 100644 --- a/src/world.rs +++ b/src/world.rs @@ -2,6 +2,7 @@ use crate::{actor, nature, settings}; use bevy::prelude::*; use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::math::{DVec3, I64Vec3}; +use bevy::scene::{InstanceId, SceneInstance}; use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::plugins::sync::SyncConfig; use std::collections::HashMap; @@ -78,6 +79,7 @@ struct AsteroidData { #[derive(Event)] pub struct DespawnEvent { entity: Entity, + sceneinstance: InstanceId, origin: I64Vec3, } @@ -182,7 +184,7 @@ fn spawn_despawn_asteroids( mut ew_despawn: EventWriter, mut db: ResMut, asset_server: Res, - mut q_asteroid: Query<(Entity, &mut Visibility), With>, + mut q_asteroid: Query<(Entity, &SceneInstance), With>, mut last_player_cell: Local, ) { if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { @@ -218,11 +220,11 @@ fn spawn_despawn_asteroids( || origin.z < z_min || origin.z > z_max { let mut despawning_worked = false; - for (ent, mut vis) in &mut q_asteroid { + for (ent, sceneinstance) in &mut q_asteroid { if ent == asteroid.entity { - *vis = Visibility::Hidden; ew_despawn.send(DespawnEvent { entity: asteroid.entity, + sceneinstance: **sceneinstance, origin: origin.clone(), }); despawning_worked = true; @@ -336,9 +338,11 @@ fn handle_despawn( mut commands: Commands, mut er_despawn: EventReader, mut db: ResMut, + mut scene_spawner: ResMut, ) { for despawn in er_despawn.read() { commands.entity(despawn.entity).despawn(); + scene_spawner.despawn_instance(despawn.sceneinstance); db.0.remove(&despawn.origin); } }