summaryrefslogtreecommitdiff
path: root/ecs/examples/relationship.rs
blob: 33b7091e929b91dd8b38ee6cfb9760e80d9bb63d (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
use ecs::event::start::Start as StartEvent;
use ecs::relationship::Relationship;
use ecs::{Component, Query, World};

#[derive(Component)]
struct Sword
{
    attack_strength: u32,
}

#[derive(Component)]
struct Player;

#[derive(Component)]
struct Health
{
    health: u32,
}

struct Holding;

fn print_player_stats(player_query: Query<(Player, Health, Relationship<Holding, Sword>)>)
{
    for (_, health, sword_relationship) in &player_query {
        println!("Player health: {}", health.health);

        if let Some(sword) = sword_relationship.get() {
            println!("Player sword attack strength: {}", sword.attack_strength);
        }
    }
}

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

    world.register_system(StartEvent, print_player_stats);

    let sword_uid = world.create_entity((Sword { attack_strength: 17 },));

    world.create_entity((
        Player,
        Health { health: 180 },
        Relationship::<Holding, Sword>::new(sword_uid),
    ));

    world.emit(StartEvent);
}