From 0369b37d1063f2a85787fc90fd834d3a64112a0f Mon Sep 17 00:00:00 2001 From: dawnDus <96957561+dawndus@users.noreply.github.com> Date: Tue, 8 Feb 2022 19:04:36 -0500 Subject: [PATCH] Save, load, and display item counts --- src/components/inventory.rs | 17 +++++++++++++++++ src/inventory.rs | 8 ++++++++ src/profile.rs | 8 +++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/components/inventory.rs b/src/components/inventory.rs index 538af72..bd5349c 100644 --- a/src/components/inventory.rs +++ b/src/components/inventory.rs @@ -362,6 +362,23 @@ impl GameEntity<(&mut Context, &mut Player, &mut Inventory)> for InventoryUI { batch.draw(ctx)?; + for (idx, (item_id, amount)) in self.item_data.iter().enumerate() { + if *item_id == 0 || *amount == 0 { + break; + } + + if *amount > 1 { + draw_number( + x + 12.0 + (idx % count_x) as f32 * 32.0 + 32.0, + y + 68.0 + (idx / count_x) as f32 * 16.0, + *amount as usize, + Alignment::Right, + state, + ctx, + )?; + } + } + for (idx, weapon) in self.weapon_data.iter().enumerate() { if weapon.wtype == WeaponType::None { break; diff --git a/src/inventory.rs b/src/inventory.rs index 4b48480..6ba390d 100644 --- a/src/inventory.rs +++ b/src/inventory.rs @@ -43,6 +43,14 @@ impl Inventory { } } + pub fn add_item_amount(&mut self, item_id: u16, amount: u16) { + if !self.has_item(item_id) { + self.items.push(Item(item_id, amount)); + } else if let Some(item) = self.get_item(item_id) { + item.1 += amount; + } + } + pub fn consume_item(&mut self, item_id: u16) { if let Some(item) = self.get_item(item_id) { if item.1 > 1 { diff --git a/src/profile.rs b/src/profile.rs index d3c72aa..4588572 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -78,11 +78,13 @@ impl GameProfile { } for item in self.items.iter().copied() { - if item == 0 { + let item_id = item as u16; + let amount = (item >> 16) as u16; + if item_id == 0 { break; } - game_scene.inventory_player1.add_item(item as u16); + game_scene.inventory_player1.add_item_amount(item_id, amount + 1); } for slot in &self.teleporter_slots { @@ -190,7 +192,7 @@ impl GameProfile { for (idx, item) in items.iter_mut().enumerate() { if let Some(sitem) = game_scene.inventory_player1.get_item_idx(idx) { - *item = sitem.0 as u32; + *item = sitem.0 as u32 + (((sitem.1 - 1) as u32) << 16); } }