diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index c831dcf..71d9306 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -163,11 +163,35 @@ pub enum InventoryItemConsumeError { InvalidAmount, } -pub struct ConsumedItem { +pub struct IndividualConsumedItem { + pub entity_ids: ItemEntityId, + pub item: ItemDetail, +} + +pub struct StackedConsumedItem { pub entity_ids: Vec, - pub item: ItemDetail + pub tool: Tool } +pub enum ConsumedItem { + Individual(IndividualConsumedItem), + Stacked(StackedConsumedItem), +} + +impl ConsumedItem { + pub fn entity_ids(&self) -> Vec { + match self { + ConsumedItem::Individual(individual_consumed_item) => { + vec![individual_consumed_item.entity_id] + }, + ConsumedItem::Stacked(stacked_consumed_item) => { + stacked_consumed_item.entity_ids + } + } + } +} + + pub struct InventoryItemHandle<'a> { inventory: &'a mut CharacterInventory, slot: usize, @@ -185,27 +209,27 @@ impl<'a> InventoryItemHandle<'a> { pub fn consume(self, amount: usize) -> Result { enum RemoveMethod { EntireThing(ConsumedItem), - Partial(ItemDetail), + Partial(Tool), } let inventory_item = self.inventory.0.get(self.slot).ok_or(InventoryItemConsumeError::InconsistentState)?; let remove_method = match inventory_item { InventoryItem::Individual(individual_inventory_item) => { - RemoveMethod::EntireThing(ConsumedItem { - entity_ids: vec![individual_inventory_item.entity_id], + RemoveMethod::EntireThing(ConsumedItem::Individual(IndividualConsumedItem { + entity_ids: individual_inventory_item.entity_id, item: individual_inventory_item.item.clone() - }) + })) }, InventoryItem::Stacked(stacked_inventory_item) => { match stacked_inventory_item.count().cmp(&amount) { Ordering::Equal => { - RemoveMethod::EntireThing(ConsumedItem { + RemoveMethod::EntireThing(ConsumedItem::Stacked(StackedConsumedItem { entity_ids: stacked_inventory_item.entity_ids.clone(), - item: ItemDetail::Tool(stacked_inventory_item.tool), - }) + tool: stacked_inventory_item.tool, + })) }, Ordering::Greater => { - RemoveMethod::Partial(ItemDetail::Tool(stacked_inventory_item.tool)) + RemoveMethod::Partial(stacked_inventory_item.tool) }, Ordering::Less => { return Err(InventoryItemConsumeError::InvalidAmount) @@ -219,7 +243,7 @@ impl<'a> InventoryItemHandle<'a> { self.inventory.0.remove(self.slot); Ok(consumed_item) }, - RemoveMethod::Partial(item_detail) => { + RemoveMethod::Partial(tool) => { let entity_ids = self.inventory.0.get_mut(self.slot) .and_then(|item| { if let InventoryItem::Stacked(stacked_inventory_item) = item { @@ -230,10 +254,10 @@ impl<'a> InventoryItemHandle<'a> { } }) .ok_or(InventoryItemConsumeError::InvalidAmount)?; - Ok(ConsumedItem { + Ok(ConsumedItem::Stacked(StackedConsumedItem { entity_ids: entity_ids, - item: item_detail, - }) + tool: tool, + })) } } } diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index e586e69..e2b6183 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -501,7 +501,7 @@ impl ItemManager { let used_item = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; let consumed_item = used_item.consume(amount)?; - for entity_id in consumed_item.entity_ids { + for entity_id in consumed_item.entity_ids() { entity_gateway.change_item_location(&entity_id, ItemLocation::Consumed).await; }