systems/
systems.rs

1#[macro_use] extern crate tinyecs;
2
3use tinyecs::*;
4
5pub struct Dead;
6impl Component for Dead {}
7
8pub struct Position {
9    pub pos : [f32; 3]
10}
11impl Component for Position {}
12
13
14pub struct DrawerSystem;
15impl System for DrawerSystem {
16    fn aspect(&self) -> Aspect {
17        Aspect::all::<Position>()
18    }
19
20    fn on_added(&mut self, entity : &mut Entity) {
21        println!("drawer added {}", entity.id);
22    }
23
24    fn process_one(&mut self, entity : &mut Entity) {
25        let pos = entity.get_component::<Position>();
26        println!("{}", pos.pos[0]);
27    }
28}
29
30pub struct DeadDrawerSystem;
31impl System for DeadDrawerSystem {
32    fn aspect(&self) -> Aspect {
33        Aspect::all::<Position>().except::<Dead>()
34    }
35    fn process_one(&mut self, entity : &mut Entity) {
36        let pos = entity.get_component::<Position>();
37        println!("is dead {}", pos.pos[0]);
38    }
39}
40
41pub struct MoverSystem;
42impl System for MoverSystem {
43    fn aspect(&self) -> Aspect {
44        Aspect::all2::<Position, Dead>()
45    }
46
47    fn process_one(&mut self, entity : &mut Entity) {
48        let mut pos = entity.get_component::<Position>();
49        pos.pos[0] += 0.1;
50        println!("Moved! {}", pos.pos[0]);
51    }
52}
53
54
55fn main() {
56    let mut world = World::new();
57
58    {
59        let mut entity_manager = world.entity_manager();
60        let entity = entity_manager.create_entity();
61        entity.add_component(Position {pos : [0.0, 0.0, 0.0]});
62        entity.refresh();
63    }
64
65    // if you have position, you will be drawn
66    world.set_system(DrawerSystem);
67    // except you are dead
68    world.set_system(MoverSystem);
69    // but only if you are dead your corpse will be draw, too
70    world.set_system(DeadDrawerSystem);
71
72    world.update();
73    world.update();
74}