|
|
@ -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<ItemEntityId>,
|
|
|
|
pub item: ItemDetail
|
|
|
|
pub tool: Tool
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ConsumedItem {
|
|
|
|
Individual(IndividualConsumedItem),
|
|
|
|
Stacked(StackedConsumedItem),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConsumedItem {
|
|
|
|
pub fn entity_ids(&self) -> Vec<ItemEntityId> {
|
|
|
|
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<ConsumedItem, InventoryItemConsumeError> {
|
|
|
|
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,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|