summaryrefslogtreecommitdiff
path: root/src/systems/interact.rs
blob: 4c0245d4ce88583d9a22493a80215fcfa75e253e (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
58
59
60
61
62
63

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

use crate::components::{
	Controller,
	Position,
	ControlCooldown,
	Interactable,
	Dead
};

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



pub struct Interact;
impl <'a> System<'a> for Interact {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Controller>,
		ReadStorage<'a, Position>,
		Read<'a, Ground>,
		WriteStorage<'a, ControlCooldown>,
		ReadStorage<'a, Interactable>,
		WriteStorage<'a, Dead>
	);
	
	fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads): Self::SystemData) {
		for (entity, controller, position) in (&entities, &controllers, &positions).join(){
			let mut target = None;
			match &controller.control {
				Control::Interact(directions) => {
					'targets: for direction in directions {
						for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) {
							if let Some(interactable) = interactables.get(*ent) {
								target = Some((*ent, interactable));
								break 'targets;
							}
						}
					}
				}
				_ => {}
			}
			if let Some((ent, interactable)) = target {
				match interactable {
					Interactable::Harvest => {
						deads.insert(ent, Dead).unwrap();
					}
				}
				cooldowns.insert(entity, ControlCooldown{amount: 2}).unwrap();
			}
		}
	}
}