summaryrefslogtreecommitdiff
path: root/ecs/examples/with_sole.rs
blob: 47aa0b3a3fdc41369b998dbee310b75bcfaf150f (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
use ecs::phase::{Phase, UPDATE as UPDATE_PHASE};
use ecs::relationship::{ChildOf, Relationship};
use ecs::sole::Single;
use ecs::{static_entity, Component, Query, Sole, World};

#[derive(Component)]
struct Ammo
{
    ammo_left: u32,
}

#[derive(Sole, Default)]
struct AmmoCounter
{
    counter: u32,
}

fn count_ammo(query: Query<(Ammo,)>, mut ammo_counter: Single<AmmoCounter>)
{
    for (ammo,) in &query {
        println!("Found {} ammo", ammo.ammo_left);

        ammo_counter.counter += ammo.ammo_left;
    }
}

fn print_total_ammo_count(ammo_counter: Single<AmmoCounter>)
{
    println!("Total ammo count: {}", ammo_counter.counter);

    assert_eq!(ammo_counter.counter, 19);
}

static_entity!(
    PRINT_AMMO_COUNT_PHASE,
    (Phase, <Relationship<ChildOf, Phase>>::new(*UPDATE_PHASE))
);

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

    world.register_system(*UPDATE_PHASE, count_ammo);
    world.register_system(*PRINT_AMMO_COUNT_PHASE, print_total_ammo_count);

    world.create_entity((Ammo { ammo_left: 4 },));
    world.create_entity((Ammo { ammo_left: 7 },));
    world.create_entity((Ammo { ammo_left: 8 },));

    world.add_sole(AmmoCounter::default()).unwrap();

    world.step();
}