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
use std::cmp;
use specs::{prelude::*, System};
use crate::{engine::components::UnitComponent, unit::UnitType};
pub struct ArcherSystem;
impl<'a> System<'a> for ArcherSystem {
type SystemData = WriteStorage<'a, UnitComponent>;
fn run(&mut self, mut units: Self::SystemData) {
let mut units = (&mut units).join();
let warrior_comp = units
.by_ref()
.find(|comp| match comp.unit.unit_type {
UnitType::Warrior(_) => true,
_ => false,
})
.unwrap();
let enemy_comps: Vec<&mut UnitComponent> = units
.by_ref()
.filter(|comp| match comp.unit.unit_type {
UnitType::Warrior(_) => false,
_ => true,
})
.collect();
for archer_comp in enemy_comps
.iter()
.filter(|comp| comp.unit.unit_type == UnitType::Archer)
{
let (wx, _) = warrior_comp.unit.position;
let (sx, _) = archer_comp.unit.position;
let (hp, _) = archer_comp.unit.hp;
let obstructions: Vec<&&mut UnitComponent> = enemy_comps
.iter()
.filter(|comp| {
let (x, _) = comp.unit.position;
wx < x && x < sx
})
.collect();
if hp > 0 && obstructions.is_empty() {
println!(
"{archer:?} attacks {warrior:?}",
archer = archer_comp.unit.unit_type,
warrior = warrior_comp.unit.unit_type
);
let (current, max) = warrior_comp.unit.hp;
let remaining = cmp::max(current - archer_comp.unit.atk, 0);
println!(
"{warrior:?} takes {atk} damage, {remaining} HP left",
warrior = warrior_comp.unit.unit_type,
atk = archer_comp.unit.atk,
remaining = remaining
);
warrior_comp.unit.hp = (remaining, max);
}
}
}
}