-
Notifications
You must be signed in to change notification settings - Fork 11
/
hierarchy.rs
169 lines (150 loc) · 5.23 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
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
extern crate legion;
extern crate legion_transform;
use legion::*;
use legion_transform::prelude::*;
#[allow(unused)]
fn tldr_sample() {
// Create a normal Legion World
let mut world = World::default();
let mut resources = Resources::default();
// Create a system bundle (vec of systems) for LegionTransform
let transform_system_bundle = transform_system_bundle::build();
let parent_entity = world.push((
// Always needed for an Entity that has any space transform
LocalToWorld::identity(),
// The only mutable space transform a parent has is a translation.
Translation::new(100.0, 0.0, 0.0),
));
world.extend(vec![
(
// Again, always need a `LocalToWorld` component for the Entity to have a custom
// space transform.
LocalToWorld::identity(),
// Here we define a Translation, Rotation and uniform Scale.
Translation::new(1.0, 2.0, 3.0),
Rotation::from_euler_angles(3.14, 0.0, 0.0),
Scale(2.0),
// Add a Parent and LocalToParent component to attach a child to a parent.
Parent(parent_entity),
LocalToParent::identity(),
);
4
]);
}
fn main() {
// Create a normal Legion World
let mut resources = Resources::default();
let mut world = World::default();
// Create a system bundle (vec of systems) for LegionTransform
let mut transform_system_bundle = transform_system_bundle::build();
// See `./types_of_transforms.rs` for an explanation of space-transform types.
let parent_entity = world.push((LocalToWorld::identity(), Translation::new(100.0, 0.0, 0.0)));
let four_children: Vec<_> = world
.extend(vec![
(
LocalToWorld::identity(),
Translation::new(1.0, 2.0, 3.0),
Rotation::from_euler_angles(3.14, 0.0, 0.0),
Scale(2.0),
// Add a Parent and LocalToParent component to attach a child to a parent.
Parent(parent_entity),
LocalToParent::identity(),
);
4
])
.iter()
.cloned()
.collect();
// At this point the parent does NOT have a `Children` component attached to it. The `Children`
// component is updated by the transform system bundle and thus can be out of date (or
// non-existent for newly added members). By this logic, the `Parent` components should be
// considered the always-correct 'source of truth' for any hierarchy.
for system in transform_system_bundle.iter_mut() {
system.prepare(&world);
system.run(&mut world, &mut resources);
system
.command_buffer_mut(world.id())
.unwrap()
.flush(&mut world);
}
// At this point all parents with children have a correct `Children` component.
let parents_children = world
.entry_ref(parent_entity)
.unwrap()
.get_component::<Children>()
.unwrap()
.0
.clone();
println!("Parent {:?}", parent_entity);
for child in parents_children.iter() {
println!(" -> Has child: {:?}", child);
}
// Each child will also have a `LocalToParent` component attached to it, which is a
// space-transform from its local space to that of its parent.
for child in four_children.iter() {
println!("The child {:?}", child);
println!(
" -> Has a LocalToParent matrix: {}",
world
.entry_ref(*child)
.unwrap()
.get_component::<LocalToParent>()
.unwrap()
);
println!(
" -> Has a LocalToWorld matrix: {}",
world
.entry_ref(*child)
.unwrap()
.get_component::<LocalToWorld>()
.unwrap()
);
}
// Re-parent the second child to be a grandchild of the first.
world
.entry(four_children[1])
.unwrap()
.add_component(Parent(four_children[0]));
// Re-running the system will cleanup and fix all `Children` components.
for system in transform_system_bundle.iter_mut() {
system.prepare(&world);
system.run(&mut world, &mut resources);
system
.command_buffer_mut(world.id())
.unwrap()
.flush(&mut world);
}
println!("After the second child was re-parented as a grandchild of the first child...");
for child in world
.entry_ref(parent_entity)
.unwrap()
.get_component::<Children>()
.unwrap()
.0
.iter()
{
println!("Parent {:?} has child: {:?}", parent_entity, child);
}
for grandchild in world
.entry_ref(four_children[0])
.unwrap()
.get_component::<Children>()
.unwrap()
.0
.iter()
{
println!(
"Child {:?} has grandchild: {:?}",
four_children[0], grandchild
);
}
println!("Grandchild: {:?}", four_children[1]);
println!(
" -> Has a LocalToWorld matrix: {}",
world
.entry_ref(four_children[1])
.unwrap()
.get_component::<LocalToWorld>()
.unwrap()
);
}