bitsy_script/
inventory.rs1use crate::*;
2use hashbrown::HashMap;
3
4#[derive(Default, Clone, Debug)]
5pub struct Inventory {
6 items: HashMap<ID, u16>,
7}
8
9impl Inventory {
10 pub fn new() -> Self {
11 Self::default()
12 }
13
14 pub fn put(&mut self, id: ID) -> u16 {
15 *self
16 .items
17 .entry(id)
18 .and_modify(|q| *q = q.saturating_add(1))
19 .or_insert(1)
20 }
21
22 pub fn pop(&mut self, id: ID) -> u16 {
23 *self
24 .items
25 .entry(id)
26 .and_modify(|q| *q = q.saturating_sub(1))
27 .or_insert(1)
28 }
29
30 pub fn get(&self, id: &ID) -> u16 {
31 self.items.get(id).copied().unwrap_or_default()
32 }
33}