process_macro/
process_macro.rs

1#[macro_use] extern crate tinyecs;
2
3use tinyecs::*;
4
5struct Position {
6    x : i32
7}
8impl Component for Position {}
9
10struct Velocity {
11    x : i32
12}
13impl Component for Velocity {}
14
15struct Player;
16impl Component for Player {}
17
18struct Bot;
19impl Component for Bot {}
20
21struct SomeTarget;
22impl Component for SomeTarget {}
23
24register_system!((MoveSystem): |_pos: Position, _vel: Velocity| => {
25    _pos.x += _vel.x;
26    println!("Moving! position: {}, velocity: {}", _pos.x, _vel.x);
27});
28
29register_system!((AiSystem): |_bot: Bot, _pos: Position, _vel: Velocity|
30                 with (_players: aspect_all!(Player, Position),
31                       _targets: aspect_all!(SomeTarget, Position)) => {
32    _pos.x += _vel.x;
33    for target in _targets {
34        let Position {x, ..} = *target.get_component::<Position>();
35        if _pos.x >= x {
36            println!("Maybe attack this target?");
37        }
38    }
39    println!("Moving! position: {}, velocity: {}", _pos.x, _vel.x);
40});
41
42
43fn main() {
44    let mut world = World::new();
45
46    {
47        let mut entity_manager = world.entity_manager();
48        let entity = entity_manager.create_entity();
49
50        entity.add_component(Position {x : 0});
51        entity.add_component(Velocity {x : 1});
52        entity.refresh();
53    }
54    world.set_system(MoveSystem);
55    world.update();
56
57    world.update();
58    world.update();
59}