1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
use std::collections::HashMap;
use specs::{Component, FlaggedStorage, HashMapStorage};
use crate::{
ItemId,
item::{Item, ItemAction},
components::equipment::{Stat, Equippable},
Encyclopedia
};
#[derive(Debug, Clone)]
pub struct InventoryEntry {
pub itemid: ItemId,
pub item: Item,
pub is_equipped: bool
}
#[derive(Debug, Clone, Default)]
pub struct Inventory {
pub items: Vec<InventoryEntry>,
pub capacity: usize
}
impl Component for Inventory {
type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
}
impl Inventory {
pub fn add_item(&mut self, itemid: ItemId, enc: &Encyclopedia) {
self.items.insert(0, InventoryEntry{
itemid: itemid.clone(),
item: enc.get_item(&itemid).unwrap(),
is_equipped: false
});
}
fn equipped(&self) -> Vec<Equippable> {
let mut equippables = Vec::new();
for entry in self.items.iter() {
if entry.is_equipped {
if let ItemAction::Equip(equippable) = &entry.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
}
}
|