Skip to content

Commit

Permalink
Fix warning on despawning (#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rahmatnazali authored Jul 19, 2024
1 parent 4e48ad7 commit 9b1b45e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 7 additions & 2 deletions src/asteroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ fn handle_asteroid_collisions(
query: Query<(Entity, &Collider), With<Asteroid>>,
) {
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;
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/despawn.rs
Original file line number Diff line number Diff line change
@@ -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<Asteroid>, With<SpaceshipMisslie>)>>,
) {
for (entity, global_transform) in query.iter() {
let distance = global_transform.translation().distance(Vec3::ZERO);
if DESPAWN_DISTANCE <= distance {
Expand Down
2 changes: 1 addition & 1 deletion src/spaceship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn spawn_spaceship(mut commands: Commands, scene_assets: Res<SceneAssets>) {
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),
Expand Down

0 comments on commit 9b1b45e

Please sign in to comment.