Browse Source

probably trying too hard

pbs
jake 4 years ago
parent
commit
755ff575cc
  1. 52
      src/ship/items/inventory.rs
  2. 2
      src/ship/items/manager.rs

52
src/ship/items/inventory.rs

@ -163,11 +163,35 @@ pub enum InventoryItemConsumeError {
InvalidAmount, InvalidAmount,
} }
pub struct ConsumedItem {
pub struct IndividualConsumedItem {
pub entity_ids: ItemEntityId,
pub item: ItemDetail,
}
pub struct StackedConsumedItem {
pub entity_ids: Vec<ItemEntityId>, 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> { pub struct InventoryItemHandle<'a> {
inventory: &'a mut CharacterInventory, inventory: &'a mut CharacterInventory,
slot: usize, slot: usize,
@ -185,27 +209,27 @@ impl<'a> InventoryItemHandle<'a> {
pub fn consume(self, amount: usize) -> Result<ConsumedItem, InventoryItemConsumeError> { pub fn consume(self, amount: usize) -> Result<ConsumedItem, InventoryItemConsumeError> {
enum RemoveMethod { enum RemoveMethod {
EntireThing(ConsumedItem), EntireThing(ConsumedItem),
Partial(ItemDetail),
Partial(Tool),
} }
let inventory_item = self.inventory.0.get(self.slot).ok_or(InventoryItemConsumeError::InconsistentState)?; let inventory_item = self.inventory.0.get(self.slot).ok_or(InventoryItemConsumeError::InconsistentState)?;
let remove_method = match inventory_item { let remove_method = match inventory_item {
InventoryItem::Individual(individual_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() item: individual_inventory_item.item.clone()
})
}))
}, },
InventoryItem::Stacked(stacked_inventory_item) => { InventoryItem::Stacked(stacked_inventory_item) => {
match stacked_inventory_item.count().cmp(&amount) { match stacked_inventory_item.count().cmp(&amount) {
Ordering::Equal => { Ordering::Equal => {
RemoveMethod::EntireThing(ConsumedItem {
RemoveMethod::EntireThing(ConsumedItem::Stacked(StackedConsumedItem {
entity_ids: stacked_inventory_item.entity_ids.clone(), entity_ids: stacked_inventory_item.entity_ids.clone(),
item: ItemDetail::Tool(stacked_inventory_item.tool),
})
tool: stacked_inventory_item.tool,
}))
}, },
Ordering::Greater => { Ordering::Greater => {
RemoveMethod::Partial(ItemDetail::Tool(stacked_inventory_item.tool))
RemoveMethod::Partial(stacked_inventory_item.tool)
}, },
Ordering::Less => { Ordering::Less => {
return Err(InventoryItemConsumeError::InvalidAmount) return Err(InventoryItemConsumeError::InvalidAmount)
@ -219,7 +243,7 @@ impl<'a> InventoryItemHandle<'a> {
self.inventory.0.remove(self.slot); self.inventory.0.remove(self.slot);
Ok(consumed_item) Ok(consumed_item)
}, },
RemoveMethod::Partial(item_detail) => {
RemoveMethod::Partial(tool) => {
let entity_ids = self.inventory.0.get_mut(self.slot) let entity_ids = self.inventory.0.get_mut(self.slot)
.and_then(|item| { .and_then(|item| {
if let InventoryItem::Stacked(stacked_inventory_item) = item { if let InventoryItem::Stacked(stacked_inventory_item) = item {
@ -230,10 +254,10 @@ impl<'a> InventoryItemHandle<'a> {
} }
}) })
.ok_or(InventoryItemConsumeError::InvalidAmount)?; .ok_or(InventoryItemConsumeError::InvalidAmount)?;
Ok(ConsumedItem {
Ok(ConsumedItem::Stacked(StackedConsumedItem {
entity_ids: entity_ids, entity_ids: entity_ids,
item: item_detail,
})
tool: tool,
}))
} }
} }
} }

2
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 used_item = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
let consumed_item = used_item.consume(amount)?; 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, entity_gateway.change_item_location(&entity_id,
ItemLocation::Consumed).await; ItemLocation::Consumed).await;
} }

Loading…
Cancel
Save