From 0cf0022bf9b76ef53420e05a100c04aae67ab1f5 Mon Sep 17 00:00:00 2001 From: jake Date: Fri, 3 Apr 2020 22:56:14 -0700 Subject: [PATCH] StackedItem -> ItemInstance, add Meseta --- src/entity/item/mod.rs | 11 +++++++++++ src/ship/items.rs | 44 ++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/entity/item/mod.rs b/src/entity/item/mod.rs index e3cbd09..303edf4 100644 --- a/src/entity/item/mod.rs +++ b/src/entity/item/mod.rs @@ -40,6 +40,17 @@ pub enum ItemLocation { */ } +#[derive(Debug, PartialEq)] +pub struct Meseta(pub u32); + +impl Meseta { + pub fn as_bytes(&self) -> [u8; 16] { + let mut result = [0; 16]; + result[12..16].copy_from_slice(&u32::to_le_bytes(self.0)); + result + } +} + #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum ItemType { diff --git a/src/ship/items.rs b/src/ship/items.rs index 9d6be6e..9dcd068 100644 --- a/src/ship/items.rs +++ b/src/ship/items.rs @@ -11,12 +11,14 @@ use crate::entity::item::shield::Shield; use crate::entity::item::unit::Unit; use crate::entity::item::tool::Tool; use crate::entity::item::mag::Mag; +use crate::entity::item::Meseta; #[derive(Debug, PartialEq)] -pub enum StackedItem { +pub enum ItemInstance { Individual(ItemEntity), Stacked(Vec), + Meseta(Meseta), } #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -26,13 +28,13 @@ pub struct ActiveItemId(u32); #[derive(Debug)] pub struct ActiveItem { id: ActiveItemId, - item: StackedItem, + item: ItemInstance, } impl ActiveItem { pub fn as_client_bytes(&self) -> [u8; 16] { match &self.item { - StackedItem::Individual(i) => { + ItemInstance::Individual(i) => { match &i.item { ItemDetail::Weapon(w) => w.as_bytes(), ItemDetail::Armor(a) => a.as_bytes(), @@ -43,12 +45,15 @@ impl ActiveItem { ItemDetail::Mag(m) => m.as_bytes(), } }, - StackedItem::Stacked(i) => { + ItemInstance::Stacked(i) => { let len = i.len(); match &i[0].item { ItemDetail::Tool(t) => t.as_stacked_bytes(len), _ => panic!(), } + }, + ItemInstance::Meseta(m) => { + m.as_bytes() } } } @@ -68,12 +73,12 @@ impl ActiveInventory { // does this do anything? inventory[index].equipped = match item.item { - StackedItem::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 1, + ItemInstance::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 1, _ => 0, }; // because this actually equips the item inventory[index].flags |= match item.item { - StackedItem::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 8, + ItemInstance::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 8, _ => 0, }; inventory @@ -85,24 +90,25 @@ impl ActiveInventory { } } -fn inventory_item_index(item: &StackedItem) -> usize { +fn inventory_item_index(item: &ItemInstance) -> usize { match item { - StackedItem::Individual(i) => { + ItemInstance::Individual(i) => { match i.location { ItemLocation::Inventory{index: index, ..} => index, _ => panic!() } }, - StackedItem::Stacked(i) => { + ItemInstance::Stacked(i) => { match i[0].location { ItemLocation::Inventory{index: index, ..} => index, _ => panic!() } - } + }, + _ => panic!(), } } -fn stack_items(items: Vec) -> Vec { +fn stack_items(items: Vec) -> Vec { let mut stacks = HashMap::new(); for item in items { @@ -113,11 +119,11 @@ fn stack_items(items: Vec) -> Vec { .map(|(itype, items)| { match items[0].item.is_stackable() { true => { - vec![StackedItem::Stacked(items)] + vec![ItemInstance::Stacked(items)] }, false => { items.into_iter().map(|i| { - StackedItem::Individual(i) + ItemInstance::Individual(i) }).collect() } } @@ -141,7 +147,7 @@ impl ActiveItemDatabase { } } - fn activate_item(&mut self, item: StackedItem) -> ActiveItem { + fn activate_item(&mut self, item: ItemInstance) -> ActiveItem { self.id += 1; ActiveItem { id: ActiveItemId(self.id), @@ -294,23 +300,23 @@ mod test { assert!(stacked.len() == 5); assert!(stacked.iter().filter(|k| { - **k == StackedItem::Individual(item6.clone()) + **k == ItemInstance::Individual(item6.clone()) }).count() == 1); assert!(stacked.iter().filter(|k| { - **k == StackedItem::Individual(item3.clone()) + **k == ItemInstance::Individual(item3.clone()) }).count() == 1); assert!(stacked.iter().filter(|k| { - **k == StackedItem::Individual(item1.clone()) + **k == ItemInstance::Individual(item1.clone()) }).count() == 1); assert!(stacked.iter().filter(|k| { - **k == StackedItem::Stacked(vec![item2.clone(), item4.clone(), item5.clone()]) + **k == ItemInstance::Stacked(vec![item2.clone(), item4.clone(), item5.clone()]) }).count() == 1); assert!(stacked.iter().filter(|k| { - **k == StackedItem::Stacked(vec![item7.clone(), item8.clone(), item9.clone()]) + **k == ItemInstance::Stacked(vec![item7.clone(), item8.clone(), item9.clone()]) }).count() == 1); } }