2020-09-01 23:39:22 +00:00
|
|
|
use crate::components::{Children, Parent};
|
2020-11-26 00:55:55 +00:00
|
|
|
use bevy_ecs::{Command, Commands, Entity, Resources, World};
|
2020-11-13 01:23:57 +00:00
|
|
|
use bevy_utils::tracing::debug;
|
2020-01-15 06:23:00 +00:00
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug)]
|
2020-07-27 22:10:32 +00:00
|
|
|
pub struct DespawnRecursive {
|
|
|
|
entity: Entity,
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:13:11 +00:00
|
|
|
pub fn despawn_with_children_recursive(world: &mut World, entity: Entity) {
|
2020-09-01 23:39:22 +00:00
|
|
|
// first, make the entity's own parent forget about it
|
2020-09-18 00:16:38 +00:00
|
|
|
if let Ok(parent) = world.get::<Parent>(entity).map(|parent| parent.0) {
|
|
|
|
if let Ok(mut children) = world.get_mut::<Children>(parent) {
|
2020-11-22 20:05:58 +00:00
|
|
|
children.0.retain(|c| *c != entity);
|
2020-09-01 23:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 06:39:55 +00:00
|
|
|
|
2020-09-01 23:39:22 +00:00
|
|
|
// then despawn the entity and all of its children
|
|
|
|
despawn_with_children_recursive_inner(world, entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should only be called by `despawn_with_children_recursive`!
|
|
|
|
fn despawn_with_children_recursive_inner(world: &mut World, entity: Entity) {
|
2020-11-26 01:13:11 +00:00
|
|
|
if let Ok(mut children) = world.get_mut::<Children>(entity) {
|
|
|
|
for e in std::mem::take(&mut children.0) {
|
2020-07-27 22:10:32 +00:00
|
|
|
despawn_with_children_recursive(world, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 01:43:41 +00:00
|
|
|
if let Err(e) = world.despawn(entity) {
|
2020-11-13 01:23:57 +00:00
|
|
|
debug!("Failed to despawn entity {:?}: {}", entity, e);
|
2020-10-09 01:43:41 +00:00
|
|
|
}
|
2020-07-27 22:10:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
impl Command for DespawnRecursive {
|
|
|
|
fn write(self: Box<Self>, world: &mut World, _resources: &mut Resources) {
|
2020-07-27 22:10:32 +00:00
|
|
|
despawn_with_children_recursive(world, self.entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DespawnRecursiveExt {
|
|
|
|
/// Despawns the provided entity and its children.
|
|
|
|
fn despawn_recursive(&mut self, entity: Entity) -> &mut Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DespawnRecursiveExt for Commands {
|
|
|
|
/// Despawns the provided entity and its children.
|
|
|
|
fn despawn_recursive(&mut self, entity: Entity) -> &mut Self {
|
2020-10-18 20:48:15 +00:00
|
|
|
self.add_command(DespawnRecursive { entity })
|
2020-07-27 22:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::DespawnRecursiveExt;
|
2020-09-01 23:39:22 +00:00
|
|
|
use crate::{components::Children, hierarchy::BuildChildren};
|
|
|
|
use bevy_ecs::{Commands, Resources, World};
|
2020-07-27 22:10:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn despawn_recursive() {
|
|
|
|
let mut world = World::default();
|
|
|
|
let mut resources = Resources::default();
|
|
|
|
let mut command_buffer = Commands::default();
|
2020-09-18 00:16:38 +00:00
|
|
|
command_buffer.set_entity_reserver(world.get_entity_reserver());
|
2020-07-27 22:10:32 +00:00
|
|
|
|
2020-11-26 01:13:11 +00:00
|
|
|
command_buffer
|
|
|
|
.spawn(("Another parent".to_owned(), 0u32))
|
|
|
|
.with_children(|parent| {
|
|
|
|
parent.spawn(("Another child".to_owned(), 1u32));
|
|
|
|
});
|
2020-07-27 22:10:32 +00:00
|
|
|
|
2020-09-01 23:39:22 +00:00
|
|
|
// Create a grandparent entity which will _not_ be deleted
|
2020-11-26 01:13:11 +00:00
|
|
|
command_buffer.spawn(("Grandparent".to_owned(), 2u32));
|
2020-09-01 23:39:22 +00:00
|
|
|
let grandparent_entity = command_buffer.current_entity().unwrap();
|
|
|
|
|
|
|
|
command_buffer.with_children(|parent| {
|
|
|
|
// Add a child to the grandparent (the "parent"), which will get deleted
|
2020-11-26 01:13:11 +00:00
|
|
|
parent.spawn(("Parent, to be deleted".to_owned(), 3u32));
|
2020-09-01 23:39:22 +00:00
|
|
|
// All descendents of the "parent" should also be deleted.
|
|
|
|
parent.with_children(|parent| {
|
2020-11-26 01:13:11 +00:00
|
|
|
parent
|
|
|
|
.spawn(("First Child, to be deleted".to_owned(), 4u32))
|
|
|
|
.with_children(|parent| {
|
|
|
|
// child
|
|
|
|
parent.spawn(("First grand child, to be deleted".to_owned(), 5u32));
|
|
|
|
});
|
|
|
|
parent.spawn(("Second child, to be deleted".to_owned(), 6u32));
|
2020-07-27 22:10:32 +00:00
|
|
|
});
|
2020-09-01 23:39:22 +00:00
|
|
|
});
|
2020-07-27 22:10:32 +00:00
|
|
|
|
2020-11-26 01:13:11 +00:00
|
|
|
command_buffer.spawn(("An innocent bystander".to_owned(), 7u32));
|
2020-07-27 22:10:32 +00:00
|
|
|
command_buffer.apply(&mut world, &mut resources);
|
|
|
|
|
2020-09-01 23:39:22 +00:00
|
|
|
let parent_entity = world.get::<Children>(grandparent_entity).unwrap()[0];
|
|
|
|
|
2020-07-27 22:10:32 +00:00
|
|
|
command_buffer.despawn_recursive(parent_entity);
|
2020-10-09 01:43:41 +00:00
|
|
|
command_buffer.despawn_recursive(parent_entity); // despawning the same entity twice should not panic
|
2020-07-27 22:10:32 +00:00
|
|
|
command_buffer.apply(&mut world, &mut resources);
|
|
|
|
|
2020-11-26 01:13:11 +00:00
|
|
|
let mut results = world
|
|
|
|
.query::<(&String, &u32)>()
|
|
|
|
.map(|(a, b)| (a.clone(), *b))
|
2020-07-27 22:10:32 +00:00
|
|
|
.collect::<Vec<_>>();
|
2020-11-26 01:13:11 +00:00
|
|
|
results.sort_unstable_by_key(|(_, index)| *index);
|
2020-07-27 22:23:59 +00:00
|
|
|
|
2020-09-01 23:39:22 +00:00
|
|
|
{
|
|
|
|
let children = world.get::<Children>(grandparent_entity).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
children.iter().any(|&i| i == parent_entity),
|
|
|
|
false,
|
|
|
|
"grandparent should no longer know about its child which has been removed"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
results,
|
2020-11-26 01:13:11 +00:00
|
|
|
vec![
|
|
|
|
("Another parent".to_owned(), 0u32),
|
|
|
|
("Another child".to_owned(), 1u32),
|
|
|
|
("Grandparent".to_owned(), 2u32),
|
|
|
|
("An innocent bystander".to_owned(), 7u32)
|
|
|
|
]
|
2020-09-01 23:39:22 +00:00
|
|
|
);
|
2020-07-27 22:10:32 +00:00
|
|
|
}
|
|
|
|
}
|