summaryrefslogtreecommitdiff
path: root/src/systems/take.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-09 23:38:22 +0100
committertroido <troido@protonmail.com>2020-02-09 23:38:22 +0100
commitb9cfb78c20fd309929aae98f24acc8ba4a9a7481 (patch)
tree898684ce84a06651f78a0973fa1a6d4d2d192cb3 /src/systems/take.rs
parent105c5ab0c0e969f3fda2cd43ae5195cbdb4da016 (diff)
can now pick up and drop items
Diffstat (limited to 'src/systems/take.rs')
-rw-r--r--src/systems/take.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/systems/take.rs b/src/systems/take.rs
new file mode 100644
index 0000000..95bec1b
--- /dev/null
+++ b/src/systems/take.rs
@@ -0,0 +1,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) => {
+ 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));
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+}