2020-09-01 23:39:22 +00:00
|
|
|
use crate::components::{Children, Parent};
|
2020-07-27 22:10:32 +00:00
|
|
|
use bevy_ecs::{Commands, Entity, Query, World, WorldWriter};
|
2020-01-15 06:23:00 +00:00
|
|
|
|
2020-07-10 04:18:35 +00:00
|
|
|
pub fn run_on_hierarchy<T, S>(
|
|
|
|
children_query: &Query<&Children>,
|
|
|
|
state: &mut S,
|
2020-01-17 09:29:01 +00:00
|
|
|
entity: Entity,
|
2020-07-25 06:04:45 +00:00
|
|
|
parent_result: Option<T>,
|
2020-06-25 17:13:00 +00:00
|
|
|
mut previous_result: Option<T>,
|
2020-07-25 06:04:45 +00:00
|
|
|
run: &mut dyn FnMut(&mut S, Entity, Option<T>, Option<T>) -> Option<T>,
|
2020-07-10 04:18:35 +00:00
|
|
|
) -> Option<T>
|
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
{
|
2020-05-06 20:49:07 +00:00
|
|
|
// TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocations?
|
2020-06-25 17:13:00 +00:00
|
|
|
// TODO: the problem above might be resolvable with world splitting
|
2020-08-16 07:30:04 +00:00
|
|
|
let children = children_query
|
|
|
|
.get::<Children>(entity)
|
|
|
|
.ok()
|
|
|
|
.map(|children| children.0.iter().cloned().collect::<Vec<Entity>>());
|
2020-01-13 06:18:17 +00:00
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
let parent_result = run(state, entity, parent_result, previous_result);
|
2020-06-25 17:13:00 +00:00
|
|
|
previous_result = None;
|
|
|
|
if let Some(children) = children {
|
|
|
|
for child in children {
|
2020-07-10 04:18:35 +00:00
|
|
|
previous_result = run_on_hierarchy(
|
|
|
|
children_query,
|
|
|
|
state,
|
2020-06-25 17:13:00 +00:00
|
|
|
child,
|
2020-07-25 06:04:45 +00:00
|
|
|
parent_result.clone(),
|
2020-06-25 17:13:00 +00:00
|
|
|
previous_result,
|
|
|
|
run,
|
|
|
|
);
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
2020-05-06 20:49:07 +00:00
|
|
|
} else {
|
2020-06-25 17:13:00 +00:00
|
|
|
previous_result = parent_result;
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
2020-06-25 17:13:00 +00:00
|
|
|
|
|
|
|
previous_result
|
2020-02-08 07:17:51 +00:00
|
|
|
}
|
2020-07-27 22:10:32 +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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-01 23:39:22 +00:00
|
|
|
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) {
|
2020-08-16 07:30:04 +00:00
|
|
|
if let Some(children) = world
|
|
|
|
.get::<Children>(entity)
|
|
|
|
.ok()
|
|
|
|
.map(|children| children.0.iter().cloned().collect::<Vec<Entity>>())
|
|
|
|
{
|
2020-07-27 22:10:32 +00:00
|
|
|
for e in children {
|
|
|
|
despawn_with_children_recursive(world, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
world.despawn(entity).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorldWriter for DespawnRecursive {
|
|
|
|
fn write(self: Box<Self>, world: &mut World) {
|
|
|
|
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 {
|
|
|
|
self.write_world(DespawnRecursive { entity })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
|
|
|
command_buffer.spawn((0u32, 0u64)).with_children(|parent| {
|
|
|
|
parent.spawn((0u32, 0u64));
|
|
|
|
});
|
|
|
|
|
2020-09-01 23:39:22 +00:00
|
|
|
// Create a grandparent entity which will _not_ be deleted
|
|
|
|
command_buffer.spawn((1u32, 1u64));
|
|
|
|
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
|
|
|
|
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));
|
2020-07-27 22:23:59 +00:00
|
|
|
});
|
2020-09-01 23:39:22 +00:00
|
|
|
parent.spawn((5u32, 5u64));
|
2020-07-27 22:10:32 +00:00
|
|
|
});
|
2020-09-01 23:39:22 +00:00
|
|
|
});
|
2020-07-27 22:10:32 +00:00
|
|
|
|
|
|
|
command_buffer.spawn((0u32, 0u64));
|
|
|
|
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);
|
|
|
|
command_buffer.apply(&mut world, &mut resources);
|
|
|
|
|
|
|
|
let results = world
|
|
|
|
.query::<(&u32, &u64)>()
|
|
|
|
.iter()
|
|
|
|
.map(|(a, b)| (*a, *b))
|
|
|
|
.collect::<Vec<_>>();
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-27 22:10:32 +00:00
|
|
|
// parent_entity and its children should be deleted,
|
2020-09-01 23:39:22 +00:00
|
|
|
// the grandparent tuple (1, 1) and (0, 0) tuples remaining.
|
|
|
|
assert_eq!(
|
|
|
|
results,
|
|
|
|
vec![(0u32, 0u64), (0u32, 0u64), (0u32, 0u64), (1u32, 1u64)]
|
|
|
|
);
|
2020-07-27 22:10:32 +00:00
|
|
|
}
|
|
|
|
}
|