summaryrefslogtreecommitdiff
path: root/src/systems
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-22 23:41:51 +0100
committertroido <troido@protonmail.com>2020-02-22 23:41:51 +0100
commit522aad7889cd62e96af7c420789507ccbf5b7aaa (patch)
treebfb3c21d627552bcda6f2743854cee920722fd3b /src/systems
parentf4331041e5d906f95063f317852f32f19e6cdf9c (diff)
it is now possible to use items
Diffstat (limited to 'src/systems')
-rw-r--r--src/systems/mod.rs1
-rw-r--r--src/systems/useitem.rs57
2 files changed, 58 insertions, 0 deletions
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 => {}
+ }
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+}