-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
hierarchy.rs
120 lines (103 loc) · 3.51 KB
/
hierarchy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::components::Children;
use bevy_ecs::{Commands, Entity, Query, World, WorldWriter};
pub fn run_on_hierarchy<T, S>(
children_query: &Query<&Children>,
state: &mut S,
entity: Entity,
parent_result: Option<T>,
mut previous_result: Option<T>,
run: &mut dyn FnMut(&mut S, Entity, Option<T>, Option<T>) -> Option<T>,
) -> Option<T>
where
T: Clone,
{
// TODO: not a huge fan of this pattern. are there ways to do recursive updates in legion without allocations?
// TODO: the problem above might be resolvable with world splitting
let children = children_query
.get::<Children>(entity)
.ok()
.map(|children| children.0.iter().cloned().collect::<Vec<Entity>>());
let parent_result = run(state, entity, parent_result, previous_result);
previous_result = None;
if let Some(children) = children {
for child in children {
previous_result = run_on_hierarchy(
children_query,
state,
child,
parent_result.clone(),
previous_result,
run,
);
}
} else {
previous_result = parent_result;
}
previous_result
}
pub struct DespawnRecursive {
entity: Entity,
}
fn despawn_with_children_recursive(world: &mut World, entity: Entity) {
if let Some(children) = world
.get::<Children>(entity)
.ok()
.map(|children| children.0.iter().cloned().collect::<Vec<Entity>>())
{
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;
use crate::hierarchy::BuildChildren;
use bevy_ecs::{Commands, Entity, Resources, World};
#[test]
fn despawn_recursive() {
let mut world = World::default();
let mut resources = Resources::default();
let mut command_buffer = Commands::default();
let parent_entity = Entity::new();
command_buffer.spawn((0u32, 0u64)).with_children(|parent| {
parent.spawn((0u32, 0u64));
});
command_buffer
.spawn_as_entity(parent_entity, (1u32, 2u64))
.with_children(|parent| {
parent.spawn((1u32, 2u64)).with_children(|parent| {
parent.spawn((1u32, 2u64));
});
parent.spawn((1u32, 2u64));
});
command_buffer.spawn((0u32, 0u64));
command_buffer.apply(&mut world, &mut resources);
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<_>>();
// parent_entity and its children should be deleted,
// the (0, 0) tuples remaining.
assert_eq!(results, vec![(0u32, 0u64), (0u32, 0u64), (0u32, 0u64)]);
}
}