summaryrefslogtreecommitdiff
path: root/src/systems/useitem.rs
blob: d77239cad9c3bbee097fa6d67050024e42e38e0c (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 specs::{
	ReadStorage,
	WriteStorage,
	System,
	Join,
	Write
};

use crate::components::{
	Controller,
	Position,
	Inventory,
	Health
};

use crate::resources::{NewEntities};
use crate::components::item::ItemAction::{None, Build, Eat};
use crate::controls::Control;


pub struct Use;
impl <'a> System<'a> for Use {
	type SystemData = (
		ReadStorage<'a, Controller>,
		WriteStorage<'a, Position>,
		WriteStorage<'a, Inventory>,
		Write<'a, NewEntities>,
		WriteStorage<'a, Health>
	);
	
	fn run(&mut self, (controllers, positions, mut inventories, mut new, mut healths): Self::SystemData) {
		for (controller, position, inventory, maybe_health) in (&controllers, &positions, &mut inventories, (&mut healths).maybe()).join(){
			match &controller.0 {
				Control::Use(rank) => {
					if let Some(item) = inventory.items.get(*rank) {
						match &item.action {
							Build(template) => {
								let _ = new.create(position.pos, template.clone());
								inventory.items.remove(*rank);
							}
							Eat(health_diff) => {
								if let Some(health) = maybe_health {
									health.heal(*health_diff);
								}
								inventory.items.remove(*rank);
							}
							None => {}
						}
					}
				}
				_ => {}
			}
		}
	}
}