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
|
use specs::{
ReadStorage,
WriteStorage,
Write,
System,
Entities,
Join
};
use crate::{
components::{Health, AttackInbox, Dead, Position},
resources::NewEntities,
Template,
util
};
pub struct Attacking;
impl <'a> System<'a> for Attacking {
type SystemData = (
Entities<'a>,
WriteStorage<'a, AttackInbox>,
WriteStorage<'a, Health>,
WriteStorage<'a, Dead>,
ReadStorage<'a, Position>,
Write<'a, NewEntities>
);
fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new): Self::SystemData) {
for (ent, health, attacked) in (&entities, &mut healths, &mut attackeds).join() {
let mut wounded = false;
for attack in attacked.messages.drain(..) {
health.health -= attack.damage;
if attack.damage > 0 {
wounded = true;
}
}
health.health = util::clamp(health.health, 0, health.maxhealth);
if health.health == 0 {
deads.insert(ent, Dead).unwrap();
}
if let Some(position) = positions.get(ent){
if wounded {
new.create(position.pos, Template::empty("wound")).unwrap();
}
}
}
attackeds.clear();
}
}
|