summaryrefslogtreecommitdiff
path: root/src/systems/attacking.rs
blob: 1f518cb7a8d3397d5bfe40f8ea88ee9d3f459e90 (plain)
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

use specs::{
	WriteStorage,
	System,
	Entities,
	Join
};

use crate::{
	components::{Health, Attacked, Dying, Removed},
	util
};


pub struct Attacking;
impl <'a> System<'a> for Attacking {
	type SystemData = (
		Entities<'a>,
		WriteStorage<'a, Attacked>,
		WriteStorage<'a, Health>,
		WriteStorage<'a, Dying>,
		WriteStorage<'a, Removed>
	);
	fn run(&mut self, (entities, mut victims, mut healths, mut deads, mut removals): Self::SystemData) {
		for (ent, health, attacked) in (&entities, &mut healths, &mut victims).join() {
			for attack in attacked.attacks.drain(..) {
				health.health -= attack.damage;
			}
			health.health = util::clamp(health.health, 0, health.maxhealth);
			if health.health == 0 {
				deads.insert(ent, Dying).unwrap();
				removals.insert(ent, Removed).unwrap();
			}
		}
		victims.clear();
	}
}