-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransform.rs
76 lines (69 loc) · 2.39 KB
/
transform.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
use flax::*;
use glam::{vec3, Vec3};
use tracing_subscriber::{prelude::*, EnvFilter};
use tracing_tree::HierarchicalLayer;
fn main() {
tracing_subscriber::registry()
.with(EnvFilter::from_default_env())
.with(HierarchicalLayer::default().with_indent_lines(true))
.init();
let mut world = World::new();
// ANCHOR: builder
component! {
world_position: Vec3,
position: Vec3,
}
let root = Entity::builder()
.set(name(), "root".into())
.set_default(world_position())
.set(position(), vec3(0.0, 1.0, 0.0))
.attach(
child_of,
Entity::builder()
.set(name(), "root.child.1".into())
.set_default(world_position())
.set(position(), vec3(1.0, 0.0, 0.0))
.attach(
child_of,
Entity::builder()
.set(name(), "root.child.1.1".into())
.set_default(world_position()),
),
)
.attach(
child_of,
Entity::builder()
.set(name(), "root.child.2".into())
.set_default(world_position())
.set(position(), vec3(0.0, 0.5, 0.0)),
)
.attach(
child_of,
Entity::builder()
.set(name(), "root.child.3".into())
.set_default(world_position())
.set(position(), vec3(0.0, -1.0, 0.0)),
)
.spawn(&mut world);
let update_world_position = System::builder()
.with(
Query::new((world_position().as_mut(), position().opt_or_default()))
.with_strategy(Dfs::new(child_of)),
)
.build(
|mut query: DfsBorrow<(Mutable<Vec3>, Component<Vec3>), All, ()>| {
query.traverse(&Vec3::ZERO, |(world_pos, &pos), _, &parent_pos| {
*world_pos = pos + parent_pos;
*world_pos
});
},
);
// let mut buf = String::new();
// let print_hierarchy = System::builder()
// .with(Query::new((name(), world_position())))
// .build(|mut query: DfsBorrow<_, _, _>| {
// query.traverse(0usize, |(name, world_pos), _, depth| {
// write!(buf, "{:indent$}{name}: {world_pos}", "", indent = depth * 4)
// });
// });
}