From 9b1b45e5fc0a47884018500d3f6bf00adb972e0b Mon Sep 17 00:00:00 2001 From: Rahmat Nazali Salimi Date: Fri, 19 Jul 2024 13:40:47 +0700 Subject: [PATCH] Fix warning on despawning (#5) * disable bevy internal log * fix multiple despawn issuance on asteroid collision * fix entity doesn't exist warning when despawning far away entities the former systems query for `all` entities, including `SceneBundle`, for example. The despawn_recursive() is then issued to both the parent Asteroid and the inner `SceneBundle`, hence the warning * add Spaceship Collider radius * disable clippy type complexity warning --- Cargo.lock | 1 + Cargo.toml | 1 + src/asteroid.rs | 9 +++++++-- src/despawn.rs | 13 ++++++++++--- src/spaceship.rs | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 98b3e82..93d5496 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -300,6 +300,7 @@ name = "bevy-spaceship" version = "0.1.0" dependencies = [ "bevy", + "log", "rand", ] diff --git a/Cargo.toml b/Cargo.toml index 18f808c..7ea60cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] bevy = "0.13.2" rand = "0.8.5" +log = { version = "*", features = ["max_level_debug", "release_max_level_warn"] } # Enable a small amount of optimization in the dev profile. [profile.dev] diff --git a/src/asteroid.rs b/src/asteroid.rs index faa6696..ea9a8b1 100644 --- a/src/asteroid.rs +++ b/src/asteroid.rs @@ -67,13 +67,18 @@ fn handle_asteroid_collisions( query: Query<(Entity, &Collider), With>, ) { for (entity, collider) in query.iter() { + let mut despawn_issued: bool = false; for &collided_entity in collider.colliding_entities.iter() { // Asteroid collided with another asteroid. if query.get(collided_entity).is_ok() { continue; } - // Despawn this asteroid - commands.entity(entity).despawn_recursive(); + + if !despawn_issued { + // Despawn this asteroid + commands.entity(entity).despawn_recursive(); + despawn_issued = true; + } } } } diff --git a/src/despawn.rs b/src/despawn.rs index 163248d..384b3a8 100644 --- a/src/despawn.rs +++ b/src/despawn.rs @@ -1,17 +1,24 @@ use bevy::app::App; use bevy::prelude::{ - Commands, DespawnRecursiveExt, Entity, GlobalTransform, Plugin, Query, Update, Vec3, + Commands, DespawnRecursiveExt, Entity, GlobalTransform, Or, Plugin, Query, Update, Vec3, With, }; +use crate::asteroid::Asteroid; +use crate::spaceship::SpaceshipMisslie; + pub struct DespawnPlugin; impl Plugin for DespawnPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, despawn_far_away_entities); + app.add_systems(Update, despawn_far_away_objects); } } -fn despawn_far_away_entities(mut commands: Commands, query: Query<(Entity, &GlobalTransform)>) { +#[allow(clippy::type_complexity)] +fn despawn_far_away_objects( + mut commands: Commands, + query: Query<(Entity, &GlobalTransform), Or<(With, With)>>, +) { for (entity, global_transform) in query.iter() { let distance = global_transform.translation().distance(Vec3::ZERO); if DESPAWN_DISTANCE <= distance { diff --git a/src/spaceship.rs b/src/spaceship.rs index 5485122..e467446 100644 --- a/src/spaceship.rs +++ b/src/spaceship.rs @@ -24,7 +24,7 @@ fn spawn_spaceship(mut commands: Commands, scene_assets: Res) { MovingObjectBundle { velocity: Velocity::new(Vec3::ZERO), acceleration: Acceleration::new(Vec3::ZERO), - collider: Collider::new(1.0), + collider: Collider::new(10.0), model: SceneBundle { scene: scene_assets.spaceship.clone(), transform: Transform::from_translation(STARTING_TRANSLATION),