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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::cmp;
use specs::{prelude::*, System};
use crate::{engine::components::UnitComponent, unit::UnitType};
pub struct ShooterSystem {
pub name: String,
}
impl ShooterSystem {
pub fn new(name: String) -> ShooterSystem {
ShooterSystem { name }
}
}
impl<'a> System<'a> for ShooterSystem {
type SystemData = WriteStorage<'a, UnitComponent>;
fn run(&mut self, mut units: Self::SystemData) {
let mut warrior_comp = None;
let mut shooter_comps = Vec::new();
let mut enemy_comps = Vec::new();
for unit_comp in (&mut units).join() {
match unit_comp.unit.unit_type {
UnitType::Warrior => {
warrior_comp = Some(unit_comp);
}
UnitType::Archer | UnitType::Wizard => {
shooter_comps.push(unit_comp);
}
_ => {
enemy_comps.push(unit_comp);
}
}
}
let warrior_comp = warrior_comp.unwrap();
for shooter_comp in &shooter_comps {
let (wx, _) = warrior_comp.unit.position;
let (sx, _) = shooter_comp.unit.position;
let (hp, _) = shooter_comp.unit.hp;
let in_range = (sx - wx).abs() < 4;
let mut obstructions: Vec<&&mut UnitComponent> = enemy_comps
.iter()
.filter(|comp| {
let (x, _) = comp.unit.position;
(wx < x && x < sx) || (wx > x && x > sx)
})
.collect();
obstructions.extend(shooter_comps.iter().filter(|comp| {
let (x, _) = comp.unit.position;
(wx < x && x < sx) || (wx > x && x > sx)
}));
if hp > 0 && in_range && obstructions.is_empty() {
println!(
"{shooter:?} attacks {warrior}",
shooter = shooter_comp.unit.unit_type,
warrior = &self.name
);
let (current, max) = warrior_comp.unit.hp;
let remaining = cmp::max(current - shooter_comp.unit.atk, 0);
println!(
"{warrior} takes {atk} damage, {remaining} HP left",
warrior = &self.name,
atk = shooter_comp.unit.atk,
remaining = remaining
);
warrior_comp.unit.hp = (remaining, max);
}
}
}
}