summaryrefslogtreecommitdiff
path: root/src/systems/fight.rs
blob: 2a663e3bbcd48e7f0fdb4b04c5b1d6071d85ebb6 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

use std::collections::HashSet;
use specs::{
	Entities,
	ReadStorage,
	WriteStorage,
	System,
	Join,
	Read
};

use crate::components::{
	Controller,
	Position,
	AttackInbox,
	Fighter,
	Health,
	ControlCooldown
};

use crate::controls::{Control};
use crate::resources::{Ground};



pub struct Fight;
impl <'a> System<'a> for Fight {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Controller>,
		WriteStorage<'a, Position>,
		Read<'a, Ground>,
		WriteStorage<'a, AttackInbox>,
		ReadStorage<'a, Fighter>,
		ReadStorage<'a, Health>,
		WriteStorage<'a, ControlCooldown>
	);
	
	fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths, mut cooldowns): Self::SystemData) {
		for (entity, controller, position, fighter) in (&entities, &controllers, &positions, &fighters).join(){
			match &controller.control {
				Control::Attack(directions) => {
					'targets: for direction in directions {
						for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) {
							if healths.contains(*ent) && *ent != entity {
								AttackInbox::add_message(&mut attacked, *ent, fighter.attack.clone());
								cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap();
								break 'targets;
							}
						}
					}
				}
				_ => {}
			}
		}
	}
}