From a520382eb19e4234ed52fd1eb9fb965b5967d522 Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 5 Mar 2020 14:47:48 +0100 Subject: equipment is now a part of the inventory --- src/components/inventory.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/components/inventory.rs (limited to 'src/components/inventory.rs') diff --git a/src/components/inventory.rs b/src/components/inventory.rs new file mode 100644 index 0000000..81064d8 --- /dev/null +++ b/src/components/inventory.rs @@ -0,0 +1,44 @@ + +use std::collections::HashMap; +use specs::{Component, FlaggedStorage, HashMapStorage}; +use super::{ + item::{Item, ItemAction}, + equipment::{Stat, Equippable}, +}; + +#[derive(Debug, Clone, Default)] +pub struct Inventory { + pub items: Vec<(Item, bool)>, + pub capacity: usize +} +impl Component for Inventory { + type Storage = FlaggedStorage>; +} + +impl Inventory { + + fn equipped(&self) -> Vec { + let mut equippables = Vec::new(); + for (item, is_equipped) in self.items.iter() { + if *is_equipped { + if let ItemAction::Equip(equippable) = &item.action { + equippables.push(equippable.clone()); + } else { + panic!("unequippable item equipped!"); + } + } + } + equippables + } + + pub fn equipment_bonuses(&self) -> HashMap { + let mut bonuses: HashMap = HashMap::new(); + for equippable in self.equipped() { + for (stat, s) in equippable.stats.iter(){ + let current: i64 = *bonuses.entry(*stat).or_insert(0); + bonuses.insert(*stat, current + s); + } + } + bonuses + } +} -- cgit