From 522aad7889cd62e96af7c420789507ccbf5b7aaa Mon Sep 17 00:00:00 2001 From: troido Date: Sat, 22 Feb 2020 23:41:51 +0100 Subject: it is now possible to use items --- src/systems/mod.rs | 1 + src/systems/useitem.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/systems/useitem.rs (limited to 'src/systems') diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 68de813..546669b 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -7,3 +7,4 @@ pub mod remove; pub mod create; pub mod take; pub mod migrate; +pub mod useitem; diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs new file mode 100644 index 0000000..d77239c --- /dev/null +++ b/src/systems/useitem.rs @@ -0,0 +1,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 => {} + } + } + } + _ => {} + } + } + } +} -- cgit