1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-16 10:52:44 +00:00

add item count value, which according to people might be used by switch version?

This commit is contained in:
Alula 2020-09-22 00:07:13 +02:00
parent fc7d47a262
commit 2bcc0292de
No known key found for this signature in database
GPG key ID: 3E00485503A1D8BA

View file

@ -3,7 +3,8 @@ use crate::shared_game_state::SharedGameState;
use crate::weapon::{Weapon, WeaponLevel, WeaponType};
#[derive(Clone, Copy)]
pub struct Item(u16);
/// (id, amount)
pub struct Item(u16, u16);
#[derive(Clone)]
pub struct Inventory {
@ -33,14 +34,29 @@ impl Inventory {
pub fn add_item(&mut self, item_id: u16) {
if !self.has_item(item_id) {
self.items.push(Item(item_id));
self.items.push(Item(item_id, 1));
}
}
pub fn consume_item(&mut self, item_id: u16) {
if let Some(item) = self.get_item(item_id) {
if item.1 > 1 {
item.1 -= 1;
return;
}
}
self.remove_item(item_id);
}
pub fn remove_item(&mut self, item_id: u16) {
self.items.retain(|item| item.0 != item_id);
}
pub fn get_item(&mut self, item_id: u16) -> Option<&mut Item> {
self.items.iter_mut().by_ref().find(|item| item.0 == item_id)
}
pub fn has_item(&self, item_id: u16) -> bool {
self.items.iter().any(|item| item.0 == item_id)
}