summaryrefslogtreecommitdiff
path: root/ecs/examples/component_removed_event.rs
blob: ffa37f4907034ce784cd6013a244410ca39d4858 (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
use ecs::actions::Actions;
use ecs::component::Component;
use ecs::event::component::Removed;
use ecs::pair::Pair;
use ecs::phase::UPDATE;
use ecs::system::observer::Observe;
use ecs::{Component, Query, World};

#[derive(Debug, Component)]
struct Cheese;

fn eat_cheese(query: Query<(&Cheese,)>, mut actions: Actions)
{
    for (cheese_ent_id, (_,)) in query.iter_with_euids() {
        println!("Eating cheese!");

        actions.remove_components(cheese_ent_id, [Cheese::id()]);
    }
}

fn on_cheese_removed(observe: Observe<Pair<Removed, Cheese>>)
{
    for cheese_ent in &observe {
        println!("Cheese entity {} was eaten", cheese_ent.uid());
    }
}

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

    world.register_system(*UPDATE, eat_cheese);
    world.register_observer(on_cheese_removed);

    world.create_entity((Cheese,));

    world.step();
    world.step();
}