Browse Source

StackedItem -> ItemInstance, add Meseta

pbs
jake 5 years ago
parent
commit
0cf0022bf9
  1. 11
      src/entity/item/mod.rs
  2. 44
      src/ship/items.rs

11
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)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ItemType { pub enum ItemType {

44
src/ship/items.rs

@ -11,12 +11,14 @@ use crate::entity::item::shield::Shield;
use crate::entity::item::unit::Unit; use crate::entity::item::unit::Unit;
use crate::entity::item::tool::Tool; use crate::entity::item::tool::Tool;
use crate::entity::item::mag::Mag; use crate::entity::item::mag::Mag;
use crate::entity::item::Meseta;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum StackedItem {
pub enum ItemInstance {
Individual(ItemEntity), Individual(ItemEntity),
Stacked(Vec<ItemEntity>), Stacked(Vec<ItemEntity>),
Meseta(Meseta),
} }
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
@ -26,13 +28,13 @@ pub struct ActiveItemId(u32);
#[derive(Debug)] #[derive(Debug)]
pub struct ActiveItem { pub struct ActiveItem {
id: ActiveItemId, id: ActiveItemId,
item: StackedItem,
item: ItemInstance,
} }
impl ActiveItem { impl ActiveItem {
pub fn as_client_bytes(&self) -> [u8; 16] { pub fn as_client_bytes(&self) -> [u8; 16] {
match &self.item { match &self.item {
StackedItem::Individual(i) => {
ItemInstance::Individual(i) => {
match &i.item { match &i.item {
ItemDetail::Weapon(w) => w.as_bytes(), ItemDetail::Weapon(w) => w.as_bytes(),
ItemDetail::Armor(a) => a.as_bytes(), ItemDetail::Armor(a) => a.as_bytes(),
@ -43,12 +45,15 @@ impl ActiveItem {
ItemDetail::Mag(m) => m.as_bytes(), ItemDetail::Mag(m) => m.as_bytes(),
} }
}, },
StackedItem::Stacked(i) => {
ItemInstance::Stacked(i) => {
let len = i.len(); let len = i.len();
match &i[0].item { match &i[0].item {
ItemDetail::Tool(t) => t.as_stacked_bytes(len), ItemDetail::Tool(t) => t.as_stacked_bytes(len),
_ => panic!(), _ => panic!(),
} }
},
ItemInstance::Meseta(m) => {
m.as_bytes()
} }
} }
} }
@ -68,12 +73,12 @@ impl ActiveInventory {
// does this do anything? // does this do anything?
inventory[index].equipped = match item.item { 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, _ => 0,
}; };
// because this actually equips the item // because this actually equips the item
inventory[index].flags |= match item.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, _ => 0,
}; };
inventory inventory
@ -85,24 +90,25 @@ impl ActiveInventory {
} }
} }
fn inventory_item_index(item: &StackedItem) -> usize {
fn inventory_item_index(item: &ItemInstance) -> usize {
match item { match item {
StackedItem::Individual(i) => {
ItemInstance::Individual(i) => {
match i.location { match i.location {
ItemLocation::Inventory{index: index, ..} => index, ItemLocation::Inventory{index: index, ..} => index,
_ => panic!() _ => panic!()
} }
}, },
StackedItem::Stacked(i) => {
ItemInstance::Stacked(i) => {
match i[0].location { match i[0].location {
ItemLocation::Inventory{index: index, ..} => index, ItemLocation::Inventory{index: index, ..} => index,
_ => panic!() _ => panic!()
} }
}
},
_ => panic!(),
} }
} }
fn stack_items(items: Vec<ItemEntity>) -> Vec<StackedItem> {
fn stack_items(items: Vec<ItemEntity>) -> Vec<ItemInstance> {
let mut stacks = HashMap::new(); let mut stacks = HashMap::new();
for item in items { for item in items {
@ -113,11 +119,11 @@ fn stack_items(items: Vec<ItemEntity>) -> Vec<StackedItem> {
.map(|(itype, items)| { .map(|(itype, items)| {
match items[0].item.is_stackable() { match items[0].item.is_stackable() {
true => { true => {
vec![StackedItem::Stacked(items)]
vec![ItemInstance::Stacked(items)]
}, },
false => { false => {
items.into_iter().map(|i| { items.into_iter().map(|i| {
StackedItem::Individual(i)
ItemInstance::Individual(i)
}).collect() }).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; self.id += 1;
ActiveItem { ActiveItem {
id: ActiveItemId(self.id), id: ActiveItemId(self.id),
@ -294,23 +300,23 @@ mod test {
assert!(stacked.len() == 5); assert!(stacked.len() == 5);
assert!(stacked.iter().filter(|k| { assert!(stacked.iter().filter(|k| {
**k == StackedItem::Individual(item6.clone())
**k == ItemInstance::Individual(item6.clone())
}).count() == 1); }).count() == 1);
assert!(stacked.iter().filter(|k| { assert!(stacked.iter().filter(|k| {
**k == StackedItem::Individual(item3.clone())
**k == ItemInstance::Individual(item3.clone())
}).count() == 1); }).count() == 1);
assert!(stacked.iter().filter(|k| { assert!(stacked.iter().filter(|k| {
**k == StackedItem::Individual(item1.clone())
**k == ItemInstance::Individual(item1.clone())
}).count() == 1); }).count() == 1);
assert!(stacked.iter().filter(|k| { 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); }).count() == 1);
assert!(stacked.iter().filter(|k| { 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); }).count() == 1);
} }
} }
Loading…
Cancel
Save