diff options
| author | troido <troido@protonmail.com> | 2020-02-22 23:41:51 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-22 23:41:51 +0100 |
| commit | 522aad7889cd62e96af7c420789507ccbf5b7aaa (patch) | |
| tree | bfb3c21d627552bcda6f2743854cee920722fd3b /src/systems/useitem.rs | |
| parent | f4331041e5d906f95063f317852f32f19e6cdf9c (diff) | |
it is now possible to use items
Diffstat (limited to 'src/systems/useitem.rs')
| -rw-r--r-- | src/systems/useitem.rs | 57 |
1 files changed, 57 insertions, 0 deletions
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 => {} + } + } + } + _ => {} + } + } + } +} |
