summaryrefslogtreecommitdiff
path: root/src/systems/fight.rs
blob: d0342b53db315421a3cb43e46ecf5edd6da67c04 (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

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

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

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, Attacked>,
		ReadStorage<'a, Fighter>,
		ReadStorage<'a, Health>
	);
	
	fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths): Self::SystemData) {
		for (entity, controller, position, fighter) in (&entities, &controllers, &positions, &fighters).join(){
			match &controller.0 {
				Control::Attack(directions) => {
					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 {
								Attacked::add_attack(&mut attacked, *ent, fighter.attack.clone());
								break;
							}
						}
					}
				}
				_ => {}
			}
		}
	}
}