sort/
sort.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
extern crate specs;
extern crate specs_hierarchy;

use specs::prelude::*;
use specs_hierarchy::{Hierarchy, HierarchySystem};

struct Parent {
    entity: Entity,
}

impl Component for Parent {
    type Storage = FlaggedStorage<Self, DenseVecStorage<Self>>;
}

impl specs_hierarchy::Parent for Parent {
    fn parent_entity(&self) -> Entity {
        self.entity
    }
}

fn main() {
    let mut world = World::new();
    let mut dispatcher = DispatcherBuilder::new()
        .with(
            HierarchySystem::<Parent>::new(&mut world),
            "hierarchy_system",
            &[],
        )
        .build();
    dispatcher.setup(&mut world);

    let _e0 = world.create_entity().build();
    let e1 = world.create_entity().build();
    let e2 = world.create_entity().build();
    let e3 = world.create_entity().build();
    let e4 = world.create_entity().build();
    let e5 = world.create_entity().build();
    let _e6 = world.create_entity().build();
    let _e7 = world.create_entity().build();
    let _e8 = world.create_entity().build();
    let _e9 = world.create_entity().build();

    {
        let mut parents = world.write_storage::<Parent>();
        parents.insert(e1, Parent { entity: e5 }).unwrap();
        parents.insert(e3, Parent { entity: e1 }).unwrap();
        parents.insert(e4, Parent { entity: e5 }).unwrap();
        parents.insert(e5, Parent { entity: e2 }).unwrap();
    }

    dispatcher.dispatch(&mut world);

    {
        let parents = world.read_storage::<Parent>();
        for entity in world.read_resource::<Hierarchy<Parent>>().all() {
            let formatted = parents
                .get(*entity)
                .map(|parent| format!("{:?}", parent.entity))
                .unwrap_or(format!("None"));
            println!("{:?}: {}", entity, formatted);
        }
    }
}