From d80d0fb01d20d862b5a15fd23b06c709c5f0594d Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 14 Oct 2021 00:22:23 -0600 Subject: [PATCH] add some functions to Inventory --- src/ship/items/inventory.rs | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index 50d161d..314eeab 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -87,6 +87,18 @@ pub enum InventoryItemAddToError { pub enum InventoryAddError { } +#[derive(Debug, Clone)] +pub enum YesThereIsSpace { + NewStack, + ExistingStack, +} + +#[derive(Debug, Clone)] +pub enum SpaceForStack { + Yes(YesThereIsSpace), + No, +} + impl InventoryItem { pub fn entity_ids(&self) -> Vec { match self { @@ -427,6 +439,45 @@ impl CharacterInventory { self.items.len() } + pub fn space_for_individual_item(&self) -> bool { + self.count() < INVENTORY_CAPACITY + } + + pub fn space_for_stacked_item(&self, tool: &Tool, amount: usize) -> SpaceForStack { + let existing_item = self.items.iter() + .filter_map(|item| { + match item { + InventoryItem::Stacked(s_item) => { + Some(s_item) + }, + _ => None + } + }) + .find(|s_item| { + s_item.tool == *tool + }); + + match existing_item { + Some(item) => { + if item.count() + amount <= tool.tool.max_stack() { + SpaceForStack::Yes(YesThereIsSpace::ExistingStack) + } + else { + SpaceForStack::No + } + } + None => { + if self.count() < INVENTORY_CAPACITY { + SpaceForStack::Yes(YesThereIsSpace::NewStack) + } + else { + SpaceForStack::No + } + } + } + } + + pub fn get_item_handle_by_id(&mut self, item_id: ClientItemId) -> Option { let (slot, _) = self.items.iter() .enumerate() @@ -526,6 +577,16 @@ impl CharacterInventory { Ok(()) } + pub fn add_individual_floor_item(&mut self, floor_item: &IndividualFloorItem) -> &InventoryItem { + self.items.push(InventoryItem::Individual(IndividualInventoryItem { + entity_id: floor_item.entity_id, + item_id: floor_item.item_id, + item: floor_item.item.clone(), + })); + + self.items.last().unwrap() + } + // TODO: should these pick up functions take floor_item as mut and remove the ids? pub fn pick_up_individual_floor_item(&mut self, floor_item: &IndividualFloorItem) -> Option<(&IndividualInventoryItem, InventorySlot)> { if self.count() >= 30 { @@ -546,6 +607,33 @@ impl CharacterInventory { } } + pub fn add_stacked_floor_item(&mut self, floor_item: &StackedFloorItem) { + let existing_item = self.items.iter_mut() + .filter_map(|item| { + match item { + InventoryItem::Stacked(s_item) => Some(s_item), + _ => None, + } + + }) + .find(|item| { + item.tool == floor_item.tool + }); + + match existing_item { + Some(item) => { + item.entity_ids.append(&mut floor_item.entity_ids.clone()) + }, + None => { + self.items.push(InventoryItem::Stacked(StackedInventoryItem { + entity_ids: floor_item.entity_ids.clone(), + item_id: floor_item.item_id, + tool: floor_item.tool, + })); + } + } + } + // TODO: can be simplified using find instead of position pub fn pick_up_stacked_floor_item(&mut self, floor_item: &StackedFloorItem) -> Option<(&StackedInventoryItem, InventorySlot)> { let existing_stack_position = self.items.iter()