summaryrefslogtreecommitdiff
path: root/ecs/examples/component_relationship.rs
blob: 4453e3a699df66c386618e4a5f1d4126f81f55f6 (plain)
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
use ecs::pair::Pair;
use ecs::phase::START as START_PHASE;
use ecs::{Component, Query, World};

#[derive(Component)]
struct Person
{
    name: String,
}

fn print_dog_likers(query: Query<(&Person, Pair<Likes, &Dogs>)>)
{
    for (person, liked_dogs) in &query {
        println!(
            "{} likes {} dogs!",
            person.name,
            if liked_dogs.large { "large" } else { "small" },
        );
    }
}

#[derive(Component)]
struct Likes;

#[derive(Component)]
struct Cats;

#[derive(Component)]
struct Dogs
{
    large: bool,
}

fn main()
{
    let mut world = World::new();

    world.register_system(*START_PHASE, print_dog_likers);

    world.create_entity((
        Person { name: "Irving".to_string() },
        Pair::new_with_comp_target::<Likes>(Dogs { large: true }),
    ));

    world.create_entity((
        Person { name: "Mark".to_string() },
        Pair::new_with_comp_target::<Likes>(Cats),
    ));

    world.create_entity((
        Person { name: "Helena".to_string() },
        Pair::new_with_comp_target::<Likes>(Dogs { large: false }),
    ));

    world.step();
}