diff options
| author | troido <troido@protonmail.com> | 2020-03-05 14:47:48 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-05 14:47:48 +0100 |
| commit | a520382eb19e4234ed52fd1eb9fb965b5967d522 (patch) | |
| tree | 5b4a8831cd544f57c6119eac5785973dc9e9bd50 /src/components/inventory.rs | |
| parent | 7846b871f5c9d57a19dbf09b7acbf0d6b38a69ca (diff) | |
equipment is now a part of the inventory
Diffstat (limited to 'src/components/inventory.rs')
| -rw-r--r-- | src/components/inventory.rs | 44 |
1 files changed, 44 insertions, 0 deletions
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<Self, HashMapStorage<Self>>; +} + +impl Inventory { + + fn equipped(&self) -> Vec<Equippable> { + 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<Stat, i64> { + let mut bonuses: HashMap<Stat, i64> = 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 + } +} |
