2020-09-12 00:42:44 +00:00
|
|
|
use crate::weapon::{Weapon, WeaponLevel, WeaponType};
|
2020-09-10 10:27:33 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Item(u16);
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Inventory {
|
|
|
|
current_item: u16,
|
|
|
|
current_weapon: u16,
|
|
|
|
items: Vec<Item>,
|
|
|
|
weapons: Vec<Weapon>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inventory {
|
|
|
|
#[allow(clippy::new_without_default)]
|
|
|
|
pub fn new() -> Inventory {
|
|
|
|
Inventory {
|
|
|
|
current_item: 0,
|
|
|
|
current_weapon: 0,
|
|
|
|
items: Vec::with_capacity(16),
|
|
|
|
weapons: Vec::with_capacity(16),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_item(&mut self, item_id: u16) {
|
|
|
|
if !self.has_item(item_id) {
|
|
|
|
self.items.push(Item(item_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_item(&mut self, item_id: u16) {
|
|
|
|
self.items.retain(|item| item.0 != item_id);
|
|
|
|
}
|
2020-08-20 18:31:47 +00:00
|
|
|
|
2020-09-10 10:27:33 +00:00
|
|
|
pub fn has_item(&self, item_id: u16) -> bool {
|
|
|
|
self.items.iter().any(|item| item.0 == item_id)
|
|
|
|
}
|
2020-09-10 13:24:04 +00:00
|
|
|
|
2020-09-12 00:42:44 +00:00
|
|
|
pub fn add_weapon(&mut self, weapon_id: WeaponType, max_ammo: u16) {
|
2020-09-10 13:24:04 +00:00
|
|
|
if !self.has_weapon(weapon_id) {
|
2020-09-12 00:42:44 +00:00
|
|
|
self.weapons.push(Weapon::new(
|
|
|
|
weapon_id,
|
|
|
|
WeaponLevel::Level1,
|
|
|
|
0,
|
|
|
|
max_ammo,
|
2020-09-10 13:24:04 +00:00
|
|
|
max_ammo,
|
2020-09-12 00:42:44 +00:00
|
|
|
));
|
2020-09-10 13:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:42:44 +00:00
|
|
|
pub fn remove_weapon(&mut self, wtype: WeaponType) {
|
|
|
|
self.weapons.retain(|weapon| weapon.wtype != wtype);
|
2020-09-10 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 00:55:07 +00:00
|
|
|
pub fn get_weapon(&self, idx: usize) -> Option<&Weapon> {
|
|
|
|
self.weapons.get(idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_current_weapon_mut(&mut self) -> Option<&mut Weapon> {
|
2020-09-10 13:24:04 +00:00
|
|
|
self.weapons.get_mut(self.current_weapon as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn refill_all_ammo(&mut self) {
|
|
|
|
for weapon in self.weapons.iter_mut() {
|
|
|
|
weapon.ammo = weapon.max_ammo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset_all_weapon_xp(&mut self) {
|
|
|
|
for weapon in self.weapons.iter_mut() {
|
2020-09-12 00:42:44 +00:00
|
|
|
weapon.level = WeaponLevel::Level1;
|
2020-09-10 13:24:04 +00:00
|
|
|
weapon.experience = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:55:07 +00:00
|
|
|
pub fn get_current_ammo(&self) -> (u16, u16) {
|
|
|
|
if let Some(weapon) = self.weapons.get(self.current_weapon as usize) {
|
|
|
|
(weapon.ammo, weapon.max_ammo)
|
|
|
|
} else {
|
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:42:44 +00:00
|
|
|
pub fn get_current_level(&self) -> WeaponLevel {
|
2020-09-11 00:55:07 +00:00
|
|
|
if let Some(weapon) = self.weapons.get(self.current_weapon as usize) {
|
|
|
|
weapon.level
|
|
|
|
} else {
|
2020-09-12 00:42:44 +00:00
|
|
|
WeaponLevel::None
|
2020-09-11 00:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_current_weapon_idx(&self) -> u16 {
|
|
|
|
self.current_weapon
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_weapon_count(&self) -> usize {
|
|
|
|
self.weapons.len()
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:42:44 +00:00
|
|
|
pub fn has_weapon(&self, wtype: WeaponType) -> bool {
|
|
|
|
self.weapons.iter().any(|weapon| weapon.wtype == wtype)
|
2020-09-10 13:24:04 +00:00
|
|
|
}
|
2020-08-20 18:31:47 +00:00
|
|
|
}
|