summaryrefslogtreecommitdiff
path: root/ecs/examples/with_local.rs
blob: 0bd8f66f0769ef8642a7b694afdb3dd865ba2b7b (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use ecs::component::Local;
use ecs::system::{Input as SystemInput, Into, System};
use ecs::{Query, World};

struct SomeData
{
    num: u64,
}

struct Name
{
    name: String,
}

struct SayHelloState
{
    cnt: usize,
}

impl SystemInput for SayHelloState {}

fn say_hello(mut query: Query<(SomeData, String)>, mut state: Local<SayHelloState>)
{
    for (data, text) in query.iter_mut() {
        println!("Hello there. Count {}: {}: {}", state.cnt, text, data.num);

        state.cnt += 1;
    }
}

fn say_whats_up(mut query: Query<(SomeData, Name)>, mut state: Local<SayHelloState>)
{
    for (data, name) in query.iter_mut() {
        println!(
            "Whats up, {}. Number is {}. Count {}",
            name.name, data.num, state.cnt
        );

        state.cnt += 1;
    }
}

#[derive(Debug, PartialEq, Eq, Hash)]
enum Event
{
    Update,
}

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

    world.register_system(
        Event::Update,
        say_hello
            .into_system()
            .initialize((SayHelloState { cnt: 0 },)),
    );

    world.register_system(
        Event::Update,
        say_whats_up
            .into_system()
            .initialize((SayHelloState { cnt: 0 },)),
    );

    world.create_entity((
        SomeData { num: 987_654 },
        "Yoo".to_string(),
        Name { name: "Bob".to_string() },
    ));

    world.create_entity((SomeData { num: 345 }, "Haha".to_string()));

    world.emit(&Event::Update);

    println!("Haha");

    world.emit(&Event::Update);
}