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
//! contains system which prints out the state of the world

use specs::{prelude::*, System};

use crate::{
    engine::components::UnitComponent,
    floor::Floor,
    unit::{Unit, UnitType},
};

/// This system simply calls the `draw` method of
/// [`Floor`](crate::floor::Floor) after each turn is executed.
pub struct UiSystem {
    pub floor: Floor,
}

impl UiSystem {
    pub fn new(floor: Floor) -> UiSystem {
        UiSystem { floor }
    }
}

impl<'a> System<'a> for UiSystem {
    type SystemData = ReadStorage<'a, UnitComponent>;

    fn run(&mut self, units: Self::SystemData) {
        let mut unit_comps = (&units).join();
        let mut units = Vec::new();
        let warrior_comp = unit_comps
            .find(|comp| comp.unit.unit_type == UnitType::Warrior)
            .unwrap();
        units.push(Unit::warrior(warrior_comp.unit.position));

        for sludge_comp in unit_comps.filter(|comp| comp.unit.unit_type == UnitType::Sludge) {
            units.push(Unit::sludge(sludge_comp.unit.position));
        }

        self.floor.units = units;
        self.floor.draw();
    }
}