fix despawning of scenes (fixes framerate drop after traveling far)

This commit is contained in:
yuni 2024-04-02 17:17:31 +02:00
parent 9802a09478
commit 910d1b4407

View file

@ -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<DespawnEvent>,
mut db: ResMut<ActiveAsteroids>,
asset_server: Res<AssetServer>,
mut q_asteroid: Query<(Entity, &mut Visibility), With<Asteroid>>,
mut q_asteroid: Query<(Entity, &SceneInstance), With<Asteroid>>,
mut last_player_cell: Local<I64Vec3>,
) {
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<DespawnEvent>,
mut db: ResMut<ActiveAsteroids>,
mut scene_spawner: ResMut<SceneSpawner>,
) {
for despawn in er_despawn.read() {
commands.entity(despawn.entity).despawn();
scene_spawner.despawn_instance(despawn.sceneinstance);
db.0.remove(&despawn.origin);
}
}