multiple_fields/
multiple_fields.rs

1extern crate tinyecs;
2
3use tinyecs::*;
4
5
6struct Health {
7    hp : i32
8}
9impl Component for Health {}
10
11struct Position {
12    x : i32
13}
14impl Component for Position {}
15
16struct Alive;
17impl Component for Alive {}
18
19pub struct BleedZoneSystem;
20impl System for BleedZoneSystem {
21    fn aspect(&self) -> Aspect {
22        Aspect::all3::<Position, Health, Alive>()
23    }
24
25    fn process_one(&mut self, entity : &mut Entity) {
26        let pos = entity.get_component::<Position>();
27        let mut health = entity.get_component::<Health>();
28
29        if pos.x == 5 {
30            health.hp -= 10;
31            println!("You are in bleeding zone, hp: {}", health.hp);
32        }
33        if health.hp <= 0 {
34            entity.remove_component::<Alive>();
35            entity.refresh();
36        }
37    }
38}
39
40fn main() {
41    let mut world = World::new();
42
43    {
44        let mut manager = world.entity_manager();
45        let entity = manager.create_entity();
46        entity.add_component(Health {hp : 100});
47        entity.add_component(Position {x : 5});
48        entity.add_component(Alive);
49        entity.refresh();
50    }
51    world.set_system(BleedZoneSystem);
52
53    for _ in 0..100 {
54        world.update();
55    }
56}