examples/entities/
entity_hierarchy.rs1#![allow(dead_code)]
2use crate::z_ignore_test_common::*;
3
4use flecs_ecs::prelude::*;
5
6#[derive(Debug, Component)]
7pub struct Position {
8 pub x: f32,
9 pub y: f32,
10}
11
12#[derive(Debug, Component)]
13struct Star;
14
15#[derive(Debug, Component)]
16struct Planet;
17
18#[derive(Debug, Component)]
19struct Moon;
20
21fn iterate_tree(entity: EntityView, position_parent: &Position) {
22 println!("{} [{:?}]", entity.path().unwrap(), entity.archetype());
24
25 let pos_actual = entity.map::<&Position, _>(|pos| {
27 Position {
29 x: pos.x + position_parent.x,
30 y: pos.y + position_parent.y,
31 }
32 });
33
34 println!("{:?}", pos_actual);
36
37 entity.each_child(|child| {
38 iterate_tree(child, &pos_actual);
39 });
40}
41
42fn main() {
43 let world = World::new();
44
45 let sun = world.entity_named("Sun").set(Position { x: 1.0, y: 1.0 });
50
51 world
52 .entity_named("Mercury")
53 .set(Position { x: 1.0, y: 1.0 })
54 .add::<Planet>()
55 .child_of_id(sun); world
58 .entity_named("Venus")
59 .set(Position { x: 2.0, y: 2.0 })
60 .add::<Planet>()
61 .child_of_id(sun);
62
63 let earth = world
64 .entity_named("Earth")
65 .set(Position { x: 3.0, y: 3.0 })
66 .add::<Planet>()
67 .child_of_id(sun);
68
69 let moon = world
70 .entity_named("Moon")
71 .set(Position { x: 0.1, y: 0.1 })
72 .add::<Moon>()
73 .child_of_id(earth);
74
75 println!(
77 "Is the Moon a child of the Earth? {} / {}",
78 moon.has_id((flecs::ChildOf::ID, earth)), moon.has_first::<flecs::ChildOf>(earth)
80 );
81
82 println!();
83
84 iterate_tree(sun, &Position { x: 0.0, y: 0.0 });
86
87 }
100
101#[cfg(feature = "flecs_nightly_tests")]
102#[test]
103fn test() {
104 let output_capture = OutputCapture::capture().unwrap();
105 main();
106 output_capture.test("entity_hierarchy".to_string());
107}