Add despawn_recursive to EntityMut (#2855)

This commit is contained in:
TheRawMeatball 2021-09-28 20:35:11 +00:00
parent 10cbe05175
commit c207950172

View file

@ -2,7 +2,7 @@ use crate::components::{Children, Parent};
use bevy_ecs::{ use bevy_ecs::{
entity::Entity, entity::Entity,
system::{Command, EntityCommands}, system::{Command, EntityCommands},
world::World, world::{EntityMut, World},
}; };
use bevy_utils::tracing::debug; use bevy_utils::tracing::debug;
@ -44,17 +44,29 @@ impl Command for DespawnRecursive {
pub trait DespawnRecursiveExt { pub trait DespawnRecursiveExt {
/// Despawns the provided entity and its children. /// Despawns the provided entity and its children.
fn despawn_recursive(&mut self); fn despawn_recursive(self);
} }
impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> {
/// Despawns the provided entity and its children. /// Despawns the provided entity and its children.
fn despawn_recursive(&mut self) { fn despawn_recursive(mut self) {
let entity = self.id(); let entity = self.id();
self.commands().add(DespawnRecursive { entity }); self.commands().add(DespawnRecursive { entity });
} }
} }
impl<'w> DespawnRecursiveExt for EntityMut<'w> {
/// Despawns the provided entity and its children.
fn despawn_recursive(mut self) {
let entity = self.id();
// SAFE: EntityMut is consumed so even though the location is no longer
// valid, it cannot be accessed again with the invalid location.
unsafe {
despawn_with_children_recursive(self.world_mut(), entity);
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bevy_ecs::{ use bevy_ecs::{