mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Add inventory counter
This commit is contained in:
parent
fbb24e7c8a
commit
1c145e8d3a
@ -14,6 +14,7 @@ pub const MAX_PICKUP_RANGE_SQR: f32 = 64.0;
|
|||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub struct Inventory {
|
pub struct Inventory {
|
||||||
pub slots: Vec<Option<Item>>,
|
pub slots: Vec<Option<Item>>,
|
||||||
|
pub amount: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Errors which the methods on `Inventory` produce
|
/// Errors which the methods on `Inventory` produce
|
||||||
@ -29,10 +30,26 @@ impl Inventory {
|
|||||||
|
|
||||||
pub fn len(&self) -> usize { self.slots.len() }
|
pub fn len(&self) -> usize { self.slots.len() }
|
||||||
|
|
||||||
|
pub fn recount_items(&mut self) {
|
||||||
|
self.amount = 0;
|
||||||
|
for item in self.slots.iter() {
|
||||||
|
if let Some(item) = item {
|
||||||
|
match item.kind {
|
||||||
|
ItemKind::Tool(_) | ItemKind::Armor { .. } => self.amount += 1,
|
||||||
|
ItemKind::Utility { amount, .. }
|
||||||
|
| ItemKind::Ingredient { amount, .. }
|
||||||
|
| ItemKind::Consumable { amount, .. } => {
|
||||||
|
self.amount += amount;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Adds a new item to the first fitting group of the inventory or starts a
|
/// Adds a new item to the first fitting group of the inventory or starts a
|
||||||
/// new group. Returns the item again if no space was found.
|
/// new group. Returns the item again if no space was found.
|
||||||
pub fn push(&mut self, item: Item) -> Option<Item> {
|
pub fn push(&mut self, item: Item) -> Option<Item> {
|
||||||
match item.kind {
|
let item = match item.kind {
|
||||||
ItemKind::Tool(_) | ItemKind::Armor { .. } => self.add_to_first_empty(item),
|
ItemKind::Tool(_) | ItemKind::Armor { .. } => self.add_to_first_empty(item),
|
||||||
ItemKind::Utility {
|
ItemKind::Utility {
|
||||||
kind: item_kind,
|
kind: item_kind,
|
||||||
@ -55,6 +72,7 @@ impl Inventory {
|
|||||||
{
|
{
|
||||||
if item_kind == *kind {
|
if item_kind == *kind {
|
||||||
*amount += new_amount;
|
*amount += new_amount;
|
||||||
|
self.recount_items();
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,6 +103,7 @@ impl Inventory {
|
|||||||
{
|
{
|
||||||
if item_kind == *kind {
|
if item_kind == *kind {
|
||||||
*amount += new_amount;
|
*amount += new_amount;
|
||||||
|
self.recount_items();
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,6 +133,7 @@ impl Inventory {
|
|||||||
{
|
{
|
||||||
if item_kind == *kind {
|
if item_kind == *kind {
|
||||||
*amount += new_amount;
|
*amount += new_amount;
|
||||||
|
self.recount_items();
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,19 +142,23 @@ impl Inventory {
|
|||||||
// It didn't work
|
// It didn't work
|
||||||
self.add_to_first_empty(item)
|
self.add_to_first_empty(item)
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
|
self.recount_items();
|
||||||
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new item to the first empty slot of the inventory. Returns the
|
/// Adds a new item to the first empty slot of the inventory. Returns the
|
||||||
/// item again if no free slot was found.
|
/// item again if no free slot was found.
|
||||||
fn add_to_first_empty(&mut self, item: Item) -> Option<Item> {
|
fn add_to_first_empty(&mut self, item: Item) -> Option<Item> {
|
||||||
match self.slots.iter_mut().find(|slot| slot.is_none()) {
|
let item = match self.slots.iter_mut().find(|slot| slot.is_none()) {
|
||||||
Some(slot) => {
|
Some(slot) => {
|
||||||
*slot = Some(item);
|
*slot = Some(item);
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
None => Some(item),
|
None => Some(item),
|
||||||
}
|
};
|
||||||
|
self.recount_items();
|
||||||
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a series of items to inventory, returning any which do not fit as an
|
/// Add a series of items to inventory, returning any which do not fit as an
|
||||||
@ -150,6 +174,7 @@ impl Inventory {
|
|||||||
leftovers.push(item);
|
leftovers.push(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.recount_items();
|
||||||
if leftovers.len() > 0 {
|
if leftovers.len() > 0 {
|
||||||
Err(Error::Full(leftovers))
|
Err(Error::Full(leftovers))
|
||||||
} else {
|
} else {
|
||||||
@ -187,6 +212,7 @@ impl Inventory {
|
|||||||
Some(slot) => {
|
Some(slot) => {
|
||||||
let old = slot.take();
|
let old = slot.take();
|
||||||
*slot = Some(item);
|
*slot = Some(item);
|
||||||
|
self.recount_items();
|
||||||
Ok(old)
|
Ok(old)
|
||||||
},
|
},
|
||||||
None => Err(item),
|
None => Err(item),
|
||||||
@ -217,7 +243,9 @@ impl Inventory {
|
|||||||
|
|
||||||
/// Remove an item from the slot
|
/// Remove an item from the slot
|
||||||
pub fn remove(&mut self, cell: usize) -> Option<Item> {
|
pub fn remove(&mut self, cell: usize) -> Option<Item> {
|
||||||
self.slots.get_mut(cell).and_then(|item| item.take())
|
let item = self.slots.get_mut(cell).and_then(|item| item.take());
|
||||||
|
self.recount_items();
|
||||||
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove just one item from the slot
|
/// Remove just one item from the slot
|
||||||
@ -235,6 +263,7 @@ impl Inventory {
|
|||||||
kind: *kind,
|
kind: *kind,
|
||||||
amount: 1,
|
amount: 1,
|
||||||
};
|
};
|
||||||
|
self.recount_items();
|
||||||
Some(return_item)
|
Some(return_item)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -252,6 +281,7 @@ impl Inventory {
|
|||||||
effect: *effect,
|
effect: *effect,
|
||||||
amount: 1,
|
amount: 1,
|
||||||
};
|
};
|
||||||
|
self.recount_items();
|
||||||
Some(return_item)
|
Some(return_item)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -264,6 +294,7 @@ impl Inventory {
|
|||||||
kind: *kind,
|
kind: *kind,
|
||||||
amount: 1,
|
amount: 1,
|
||||||
};
|
};
|
||||||
|
self.recount_items();
|
||||||
Some(return_item)
|
Some(return_item)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -278,6 +309,7 @@ impl Default for Inventory {
|
|||||||
fn default() -> Inventory {
|
fn default() -> Inventory {
|
||||||
let mut inventory = Inventory {
|
let mut inventory = Inventory {
|
||||||
slots: vec![None; 18],
|
slots: vec![None; 18],
|
||||||
|
amount: 0,
|
||||||
};
|
};
|
||||||
inventory.push(assets::load_expect_cloned("common.items.cheese"));
|
inventory.push(assets::load_expect_cloned("common.items.cheese"));
|
||||||
inventory.push(assets::load_expect_cloned("common.items.apple"));
|
inventory.push(assets::load_expect_cloned("common.items.apple"));
|
||||||
|
@ -15,6 +15,7 @@ fn create_default_count() { assert_eq!(Inventory::default().count(), 2) }
|
|||||||
fn push_full() {
|
fn push_full() {
|
||||||
let mut inv = Inventory {
|
let mut inv = Inventory {
|
||||||
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
|
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
|
||||||
|
amount: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
inv.push(TEST_ITEMS[0].clone()).unwrap(),
|
inv.push(TEST_ITEMS[0].clone()).unwrap(),
|
||||||
@ -27,6 +28,7 @@ fn push_full() {
|
|||||||
fn push_all_full() {
|
fn push_all_full() {
|
||||||
let mut inv = Inventory {
|
let mut inv = Inventory {
|
||||||
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
|
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
|
||||||
|
amount: 0,
|
||||||
};
|
};
|
||||||
let Error::Full(leftovers) = inv
|
let Error::Full(leftovers) = inv
|
||||||
.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
|
.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
|
||||||
@ -40,6 +42,7 @@ fn push_all_full() {
|
|||||||
fn push_unique_all_full() {
|
fn push_unique_all_full() {
|
||||||
let mut inv = Inventory {
|
let mut inv = Inventory {
|
||||||
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
|
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
|
||||||
|
amount: 0,
|
||||||
};
|
};
|
||||||
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
|
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
|
||||||
.expect("Pushing unique items into an inventory that already contains them didn't work!");
|
.expect("Pushing unique items into an inventory that already contains them didn't work!");
|
||||||
@ -51,6 +54,7 @@ fn push_unique_all_full() {
|
|||||||
fn push_all_empty() {
|
fn push_all_empty() {
|
||||||
let mut inv = Inventory {
|
let mut inv = Inventory {
|
||||||
slots: vec![None, None],
|
slots: vec![None, None],
|
||||||
|
amount: 0,
|
||||||
};
|
};
|
||||||
inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
|
inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
|
||||||
.expect("Pushing items into an empty inventory didn't work!");
|
.expect("Pushing items into an empty inventory didn't work!");
|
||||||
@ -62,6 +66,7 @@ fn push_all_empty() {
|
|||||||
fn push_all_unique_empty() {
|
fn push_all_unique_empty() {
|
||||||
let mut inv = Inventory {
|
let mut inv = Inventory {
|
||||||
slots: vec![None, None],
|
slots: vec![None, None],
|
||||||
|
amount: 0,
|
||||||
};
|
};
|
||||||
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
|
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
|
||||||
.expect(
|
.expect(
|
||||||
|
@ -184,7 +184,7 @@ impl<'a> Widget for Bag<'a> {
|
|||||||
self.stats.exp.maximum(),
|
self.stats.exp.maximum(),
|
||||||
&self.localized_strings.get("hud.bag.exp")
|
&self.localized_strings.get("hud.bag.exp")
|
||||||
);
|
);
|
||||||
let space_used = 0; // TODO: Add functionality
|
let space_used = inventory.amount;
|
||||||
let space_max = 0;
|
let space_max = 0;
|
||||||
let bag_space = format!("{}/{}", space_used, space_max);
|
let bag_space = format!("{}/{}", space_used, space_max);
|
||||||
let level = (self.stats.level.level()).to_string();
|
let level = (self.stats.level.level()).to_string();
|
||||||
|
Loading…
Reference in New Issue
Block a user