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
use std::marker::PhantomData;
use hecs::{Component, DynamicBundleClone, Entity, EntityBuilderClone, World};
use hecs_schedule::{CommandBuffer, GenericWorld};
use once_cell::sync::OnceCell;
use crate::HierarchyMut;
pub struct TreeBuilderClone<T> {
pub(crate) children: Vec<TreeBuilderClone<T>>,
pub(crate) builder: EntityBuilderClone,
pub(crate) marker: PhantomData<T>,
pub(crate) reserved: OnceCell<Entity>,
}
impl<T: Component> TreeBuilderClone<T> {
pub fn new() -> Self {
Self {
children: Vec::new(),
builder: EntityBuilderClone::new(),
marker: PhantomData,
reserved: OnceCell::new(),
}
}
pub fn reserve(&self, world: &impl GenericWorld) -> Entity {
*self.reserved.get_or_init(|| world.reserve())
}
pub fn spawn(self, world: &mut World) -> Entity {
let parent = self.reserve(world);
let builder = self.builder.build();
world.insert(parent, &builder).unwrap();
for child in self.children {
let child = child.spawn(world);
world.attach::<T>(child, parent).unwrap();
}
parent
}
pub fn spawn_deferred(self, world: &impl GenericWorld, cmd: &mut CommandBuffer) -> Entity {
let parent = self.reserve(world);
let builder = self.builder.build();
cmd.insert(parent, &builder);
for child in self.children {
let child = child.spawn_deferred(world, cmd);
cmd.write(move |w: &mut World| {
w.attach::<T>(child, parent).unwrap();
});
}
parent
}
pub fn add(&mut self, component: impl Component + Clone) -> &mut Self {
self.builder.add(component);
self
}
pub fn add_bundle(&mut self, bundle: impl DynamicBundleClone) -> &mut Self {
self.builder.add_bundle(bundle);
self
}
pub fn attach_tree(&mut self, child: Self) -> &mut Self {
self.children.push(child);
self
}
pub fn attach(&mut self, child: impl Into<Self>) -> &mut Self {
self.children.push(child.into());
self
}
pub fn attach_move(mut self, child: impl Into<Self>) -> Self {
self.children.push(child.into());
self
}
pub fn attach_tree_move(mut self, child: impl Into<Self>) -> Self {
self.children.push(child.into());
self
}
pub fn children(&self) -> &[Self] {
self.children.as_ref()
}
pub fn root(&self) -> &EntityBuilderClone {
&self.builder
}
pub fn root_mut(&mut self) -> &mut EntityBuilderClone {
&mut self.builder
}
}
impl<T> Clone for TreeBuilderClone<T> {
fn clone(&self) -> Self {
Self {
children: self.children.clone(),
builder: self.builder.clone(),
marker: PhantomData,
reserved: OnceCell::new(),
}
}
}
impl<B: DynamicBundleClone, T: Component> From<B> for TreeBuilderClone<T> {
fn from(bundle: B) -> Self {
let mut builder = EntityBuilderClone::new();
builder.add_bundle(bundle);
Self {
children: Vec::new(),
builder,
marker: PhantomData,
reserved: OnceCell::new(),
}
}
}