Remove child from parent when it is despawned (#386)

Remove child from parent when it is despawned
This commit is contained in:
Nathan Stocks 2020-09-01 17:39:22 -06:00 committed by GitHub
parent b91fd8a43a
commit 3d6d2431fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
use crate::components::Children; use crate::components::{Children, Parent};
use bevy_ecs::{Commands, Entity, Query, World, WorldWriter}; use bevy_ecs::{Commands, Entity, Query, World, WorldWriter};
pub fn run_on_hierarchy<T, S>( pub fn run_on_hierarchy<T, S>(
@ -44,6 +44,18 @@ pub struct DespawnRecursive {
} }
fn despawn_with_children_recursive(world: &mut World, entity: Entity) { fn despawn_with_children_recursive(world: &mut World, entity: Entity) {
// first, make the entity's own parent forget about it
if let Ok(parent) = world.get::<Parent>(entity) {
if let Ok(mut children) = world.get_mut::<Children>(parent.0) {
children.retain(|c| *c != entity);
}
}
// 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) {
if let Some(children) = world if let Some(children) = world
.get::<Children>(entity) .get::<Children>(entity)
.ok() .ok()
@ -78,32 +90,41 @@ impl DespawnRecursiveExt for Commands {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::DespawnRecursiveExt; use super::DespawnRecursiveExt;
use crate::hierarchy::BuildChildren; use crate::{components::Children, hierarchy::BuildChildren};
use bevy_ecs::{Commands, Entity, Resources, World}; use bevy_ecs::{Commands, Resources, World};
#[test] #[test]
fn despawn_recursive() { fn despawn_recursive() {
let mut world = World::default(); let mut world = World::default();
let mut resources = Resources::default(); let mut resources = Resources::default();
let mut command_buffer = Commands::default(); let mut command_buffer = Commands::default();
let parent_entity = Entity::new();
command_buffer.spawn((0u32, 0u64)).with_children(|parent| { command_buffer.spawn((0u32, 0u64)).with_children(|parent| {
parent.spawn((0u32, 0u64)); parent.spawn((0u32, 0u64));
}); });
command_buffer // Create a grandparent entity which will _not_ be deleted
.spawn_as_entity(parent_entity, (1u32, 2u64)) command_buffer.spawn((1u32, 1u64));
.with_children(|parent| { let grandparent_entity = command_buffer.current_entity().unwrap();
parent.spawn((1u32, 2u64)).with_children(|parent| {
parent.spawn((1u32, 2u64)); command_buffer.with_children(|parent| {
// Add a child to the grandparent (the "parent"), which will get deleted
parent.spawn((2u32, 2u64));
// All descendents of the "parent" should also be deleted.
parent.with_children(|parent| {
parent.spawn((3u32, 3u64)).with_children(|parent| {
// child
parent.spawn((4u32, 4u64));
});
parent.spawn((5u32, 5u64));
}); });
parent.spawn((1u32, 2u64));
}); });
command_buffer.spawn((0u32, 0u64)); command_buffer.spawn((0u32, 0u64));
command_buffer.apply(&mut world, &mut resources); command_buffer.apply(&mut world, &mut resources);
let parent_entity = world.get::<Children>(grandparent_entity).unwrap()[0];
command_buffer.despawn_recursive(parent_entity); command_buffer.despawn_recursive(parent_entity);
command_buffer.apply(&mut world, &mut resources); command_buffer.apply(&mut world, &mut resources);
@ -113,8 +134,20 @@ mod tests {
.map(|(a, b)| (*a, *b)) .map(|(a, b)| (*a, *b))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
{
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"
);
}
// parent_entity and its children should be deleted, // parent_entity and its children should be deleted,
// the (0, 0) tuples remaining. // the grandparent tuple (1, 1) and (0, 0) tuples remaining.
assert_eq!(results, vec![(0u32, 0u64), (0u32, 0u64), (0u32, 0u64)]); assert_eq!(
results,
vec![(0u32, 0u64), (0u32, 0u64), (0u32, 0u64), (1u32, 1u64)]
);
} }
} }