summaryrefslogtreecommitdiff
path: root/src/systems/controlinput.rs
blob: 1a582e407f39a5be428c121b02d9579525068477 (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

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

use crate::{
	components::{Controller, Player, ControlCooldown, Autofight},
	resources::{Input},
	controls::Control
};


pub struct ControlInput;
impl <'a> System<'a> for ControlInput {
	type SystemData = (
		Entities<'a>,
		Write<'a, Input>,
		WriteStorage<'a, Controller>,
		ReadStorage<'a, Player>,
		ReadStorage<'a, ControlCooldown>,
		WriteStorage<'a, Autofight>
	);
	fn run(&mut self, (entities, mut input, mut controllers, players, cooldowns, mut autofighters): Self::SystemData) {
	
		for (player, entity, ()) in (&players, &entities, !&cooldowns).join() {
			if let Some(control) = input.actions.remove(&player.id){
				controllers.insert(entity, Controller{control: control}).unwrap();
				if let Some(autofighter) = autofighters.get_mut(entity) {
					autofighter.target = None;
				}
			} else if let Some(autofighter) = autofighters.get_mut(entity) {
				if let Some(target) = autofighter.target {
					if !entities.is_alive(target) {
						autofighter.target = None;
					} else {
						controllers.insert(entity, Controller{control: Control::AttackTarget(target)}).unwrap();
					}
				}
			}
		}
	}
}