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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
mod iter;
mod debug;
mod components;
use shipyard::prelude::*;
pub use self::iter::*;
pub use self::debug::*;
pub use self::components::*;
pub trait HierarchyMut {
fn attach(&mut self, id: EntityId, parent: EntityId);
fn attach_new(&mut self, parent: EntityId) -> EntityId;
fn remove_single(&mut self, id: EntityId);
fn remove(&mut self, id: EntityId);
fn sort_children_by<F>(&mut self, id: EntityId, compare: F)
where F: FnMut(&EntityId, &EntityId) -> std::cmp::Ordering;
}
pub type HierarchyStoragesMut<'a, 'b> = (&'b mut EntitiesViewMut<'a>, &'b mut ViewMut<'a, Parent>, &'b mut ViewMut<'a, Child>);
pub(crate) fn detach (hierarchy: &mut HierarchyStoragesMut, id: EntityId) {
let (_, parent_storage, child_storage) = hierarchy;
if let Some(child) = child_storage.remove(id) {
let parent = &mut parent_storage[child.parent];
parent.num_children -= 1;
if parent.num_children == 0 {
parent_storage.remove(child.parent);
} else {
if parent.first_child == id {
parent.first_child = child.next;
}
child_storage[child.prev].next = child.next;
child_storage[child.next].prev = child.prev;
}
}
}
impl HierarchyMut for HierarchyStoragesMut<'_, '_> {
fn attach(&mut self, id: EntityId, parent: EntityId) {
detach(self, id);
let (entities, parent_storage, child_storage) = self;
if let Ok(p) = parent_storage.get(parent) {
p.num_children += 1;
let prev = child_storage[p.first_child].prev;
let next = p.first_child;
child_storage[prev].next = id;
child_storage[next].prev = id;
entities.add_component(&mut **child_storage, Child { parent, prev, next }, id);
} else {
entities.add_component(
&mut **child_storage,
Child {
parent,
prev: id,
next: id,
},
id,
);
entities.add_component(
&mut **parent_storage,
Parent {
num_children: 1,
first_child: id,
},
parent,
);
}
}
fn attach_new(&mut self, parent: EntityId) -> EntityId {
let entities = &mut self.0;
let id = entities.add_entity((), ());
self.attach(id, parent);
id
}
fn remove_single(&mut self, id: EntityId) {
detach(self, id);
let (_, parent_storage, child_storage) = self;
let children = (&**parent_storage, &**child_storage).children(id).collect::<Vec<_>>();
for child_id in children {
detach(self, child_id);
}
let parent_storage = &mut self.1;
parent_storage.remove(id);
}
fn remove(&mut self, id: EntityId) {
let (_, parent_storage, child_storage) = self;
for child_id in (&**parent_storage, &**child_storage).children(id).collect::<Vec<_>>() {
self.remove(child_id);
}
self.remove_single(id);
}
fn sort_children_by<F>(&mut self, id: EntityId, mut compare: F)
where
F: FnMut(&EntityId, &EntityId) -> std::cmp::Ordering,
{
let (_, parent_storage, child_storage) = self;
let mut children = (&**parent_storage, &**child_storage).children(id).collect::<Vec<EntityId>>();
if children.len() > 1 {
children.sort_by(|a, b| compare(a, b));
parent_storage[id].first_child = children[0];
for i in 0..children.len() - 1 {
child_storage[children[i]].next = children[i + 1];
child_storage[children[i + 1]].prev = children[i];
}
child_storage[children[0]].prev = *children.last().unwrap();
child_storage[*children.last().unwrap()].next = children[0];
}
}
}
#[test]
fn test_detach() {
let world = World::new();
let mut storages = world.borrow::<(EntitiesMut, &mut Parent, &mut Child)>();
let root1 = storages.0.add_entity((), ());
let mut hierarchy = (&mut storages.0, &mut storages.1, &mut storages.2);
let e1 = hierarchy.attach_new(root1);
let e2 = hierarchy.attach_new(e1);
detach(&mut hierarchy, e1);
{
let storages = (&*hierarchy.1, &*hierarchy.2);
assert!(storages.descendants_depth_first(root1).eq(None));
assert!(storages.ancestors(e1).eq(None));
assert!(storages.children(e1).eq([e2].iter().cloned()));
}
}