summaryrefslogtreecommitdiff
path: root/src/systems/take.rs
blob: f0e3990b1412051d64ae3c727af7f03d7d07b743 (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
65

use std::collections::HashSet;

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

use super::super::pos::Pos;

use super::super::components::{
	Controller,
	Position,
	Removed,
	Inventory,
	Item
};

use super::super::controls::{Control};
use super::super::resources::{Ground, NewEntities};



pub struct Take;
impl <'a> System<'a> for Take {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Controller>,
		WriteStorage<'a, Position>,
		Write<'a, Ground>,
		WriteStorage<'a, Removed>,
		ReadStorage<'a, Item>,
		WriteStorage<'a, Inventory>,
		Write<'a, NewEntities>
	);
	
	fn run(&mut self, (entities, controllers, positions, ground, mut removed, items, mut inventories, mut new): Self::SystemData) {
		for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){
			match &controller.0 {
				Control::Take(_rank) if inventory.items.len() < inventory.capacity => {
					let mut ents = ground.cells.get(&position.pos).unwrap_or(&HashSet::new()).clone();
					ents.remove(&ent);
					for ent in ents {
						if let Some(item) = items.get(ent) {
							inventory.items.push(item.clone());
							if let Err(msg) = removed.insert(ent, Removed) {
								println!("{:?}", msg);
							}
						}
					}
				}
				Control::Drop(_rank) => {
					if let Some(item) = inventory.items.pop() {
						new.templates.push((position.pos, item.ent));
					}
				}
				_ => {}
			}
		}
	}
}