From 5bfde260c84fb74ebbd063a0e5924f9c12afe035 Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 30 Apr 2022 11:40:21 -0600 Subject: [PATCH] add/move stuff around for itemstate --- src/ship/items/actions.rs | 2 +- src/ship/items/state.rs | 85 +++++++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/src/ship/items/actions.rs b/src/ship/items/actions.rs index 8675b9b..09e6f1a 100644 --- a/src/ship/items/actions.rs +++ b/src/ship/items/actions.rs @@ -58,7 +58,7 @@ fn add_floor_item_to_inventory(character: &CharacterEntity) }}).await?; let add_result = inventory.add_floor_item(floor_item)?; - transaction.gateway().set_character_inventory(&character.id, &inventory.inventory.as_inventory_entity(&character.id)).await?; + transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?; item_state.set_inventory(inventory); Ok(((item_state, transaction), diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index 7bdc42e..40131a2 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -3,6 +3,7 @@ use crate::ship::items::ClientItemId; use crate::entity::item::{Meseta, ItemEntityId, ItemDetail, ItemEntity, InventoryEntity, InventoryItemEntity}; use std::future::Future; +use crate::ship::map::MapArea; use crate::ship::location::RoomId; use crate::entity::character::CharacterEntityId; use crate::entity::gateway::GatewayError; @@ -253,6 +254,10 @@ impl FloorItemDetail { pub struct FloorItem { item_id: ClientItemId, item: FloorItemDetail, + map_area: MapArea, + x: f32, + y: f32, + z: f32, } impl FloorItem { @@ -294,35 +299,6 @@ impl FloorItem { #[derive(Clone, Debug)] pub struct Inventory(Vec); -impl Inventory { - pub fn as_inventory_entity(&self, _character_id: &CharacterEntityId) -> InventoryEntity { - InventoryEntity { - items: self.0.iter() - .map(|item| { - match &item.item { - InventoryItemDetail::Individual(item) => { - InventoryItemEntity::Individual(ItemEntity { - id: item.entity_id, - item: item.item.clone(), - }) - }, - InventoryItemDetail::Stacked(items) => { - InventoryItemEntity::Stacked(items.entity_ids.iter() - .map(|id| { - ItemEntity { - id: *id, - item: ItemDetail::Tool(items.tool) - } - }) - .collect()) - }, - } - }) - .collect() - } - } -} - #[derive(thiserror::Error, Debug)] pub enum InventoryError { @@ -349,7 +325,7 @@ pub struct RoomFloorItems(Vec); pub struct InventoryState { character_id: CharacterEntityId, - pub inventory: Inventory, + inventory: Inventory, meseta: Meseta, } @@ -411,6 +387,39 @@ impl InventoryState { }, } } + + pub fn take_item(&mut self, item_id: &ClientItemId) -> Option { + self.inventory.0 + .drain_filter(|i| i.item_id == *item_id) + .next() + } + + pub fn as_inventory_entity(&self, _character_id: &CharacterEntityId) -> InventoryEntity { + InventoryEntity { + items: self.inventory.0.iter() + .map(|item| { + match &item.item { + InventoryItemDetail::Individual(item) => { + InventoryItemEntity::Individual(ItemEntity { + id: item.entity_id, + item: item.item.clone(), + }) + }, + InventoryItemDetail::Stacked(items) => { + InventoryItemEntity::Stacked(items.entity_ids.iter() + .map(|id| { + ItemEntity { + id: *id, + item: ItemDetail::Tool(items.tool) + } + }) + .collect()) + }, + } + }) + .collect() + } + } } pub struct FloorState { @@ -434,6 +443,22 @@ impl FloorState { .next() }) } + + pub fn add_inventory_item(&mut self, inventory_item: InventoryItem, map_area: MapArea, position: (f32, f32, f32)) { + let floor_item = FloorItem { + item_id: inventory_item.item_id, + item: match inventory_item.item { + InventoryItemDetail::Individual(individual_item) => FloorItemDetail::Individual(individual_item), + InventoryItemDetail::Stacked(stacked_item) => FloorItemDetail::Stacked(stacked_item), + }, + map_area: map_area, + x: position.0, + y: position.1, + z: position.2, + }; + + self.shared.0.push(floor_item); + } }