summaryrefslogtreecommitdiff
path: root/src/systems/useitem.rs
blob: 4317162245e036ba5296414896e8c9f8c9035363 (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
64


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

use crate::{
	components::{
		Controller,
		Position,
		Inventory,
		Attacked
	},
	resources::{NewEntities},
	components::item::ItemAction::{None, Build, Eat},
	controls::Control,
	attack::Attack
};


pub struct Use;
impl <'a> System<'a> for Use {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Controller>,
		WriteStorage<'a, Position>,
		WriteStorage<'a, Inventory>,
		Write<'a, NewEntities>,
		WriteStorage<'a, Attacked>
	);
	
	fn run(&mut self, (entities, controllers, positions, mut inventories, mut new, mut attacked): Self::SystemData) {
		for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).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) => {
								attacked
									.entry(ent)
									.unwrap()
									.or_insert_with(Attacked::default)
									.attacks
									.push(Attack::new(-*health_diff));
								inventory.items.remove(*rank);
							}
							None => {}
						}
					}
				}
				_ => {}
			}
		}
	}
}