examples/entities/
entity_hierarchy.rs

1#![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    // Print hierarchical name of entity & the entity type
23    println!("{} [{:?}]", entity.path().unwrap(), entity.archetype());
24
25    // Map is the same as get, but allows you to return a value
26    let pos_actual = entity.map::<&Position, _>(|pos| {
27        // Calculate actual position
28        Position {
29            x: pos.x + position_parent.x,
30            y: pos.y + position_parent.y,
31        }
32    });
33
34    // Print the position
35    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    // Create a simple hierarchy.
46    // Hierarchies use ECS relationships and the builtin flecs::ChildOf relationship to
47    // create entities as children of other entities.
48
49    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); // Shortcut for add(flecs::ChildOf, sun)
56
57    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    // Is the Moon a child of the Earth?
76    println!(
77        "Is the Moon a child of the Earth? {} / {}",
78        moon.has_id((flecs::ChildOf::ID, earth)), //or you can do
79        moon.has_first::<flecs::ChildOf>(earth)
80    );
81
82    println!();
83
84    // Do a depth-first traversal of the tree
85    iterate_tree(sun, &Position { x: 0.0, y: 0.0 });
86
87    // Output:
88    //  Is the Moon a child of the Earth? true / true
89    //  ::Sun [Position, (Identifier,Name)]
90    //  Position { x: 1.0, y: 1.0 }
91    //  ::Sun::Mercury [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
92    //  Position { x: 2.0, y: 2.0 }
93    //  ::Sun::Venus [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
94    //  Position { x: 3.0, y: 3.0 }
95    //  ::Sun::Earth [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
96    //  Position { x: 4.0, y: 4.0 }
97    //  ::Sun::Earth::Moon [Component, Position, Sun.Earth.Moon, (Identifier,Name), (Identifier,Symbol), (ChildOf,Sun.Earth), (OnDelete,Panic)]
98    //  Position { x: 4.1, y: 4.1 }
99}
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}