summaryrefslogtreecommitdiff
path: root/ecs/examples/simple.rs
blob: 9983c4a02d95d3462b844b372d93f285f6a0c75e (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::event::{Event, Id as EventId};
use ecs::{Component, Query, World};

#[derive(Component)]
struct SomeData
{
    num: u64,
}

#[derive(Component)]
struct Greeting
{
    greeting: String,
}

fn say_hello(query: Query<(SomeData, Greeting)>)
{
    for (data, greeting) in &query {
        println!("{}: {}", greeting.greeting, data.num);
    }
}

#[derive(Debug)]
struct Start;

impl Event for Start
{
    fn id(&self) -> EventId
    {
        EventId::of::<Self>()
    }
}

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

    world.register_system(&Start, say_hello);

    world.create_entity((
        SomeData { num: 987_654 },
        Greeting {
            greeting: "Good afternoon".to_string(),
        },
    ));

    world.create_entity((
        SomeData { num: 345 },
        Greeting { greeting: "Good evening".to_string() },
    ));

    world.emit(&Start);
}