1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-19 04:02:47 +00:00
doukutsu-rs/src/inventory.rs

306 lines
9.2 KiB
Rust
Raw Normal View History

2020-09-25 17:14:52 +00:00
use std::cmp::Ordering;
use crate::engine_constants::EngineConstants;
use crate::shared_game_state::SharedGameState;
use crate::weapon::{Weapon, WeaponLevel, WeaponType};
2021-03-23 12:51:55 +00:00
use crate::player::{Player, TargetPlayer};
use crate::weapon::bullet::BulletManager;
2020-09-10 10:27:33 +00:00
#[derive(Clone, Copy)]
/// (id, amount)
2020-10-30 01:29:53 +00:00
pub struct Item(pub u16, pub u16);
2020-09-10 10:27:33 +00:00
#[derive(Clone)]
pub struct Inventory {
2020-09-21 23:53:46 +00:00
pub current_item: u16,
pub current_weapon: u16,
2020-09-10 10:27:33 +00:00
items: Vec<Item>,
weapons: Vec<Weapon>,
}
#[derive(Clone, Copy)]
pub enum TakeExperienceResult {
None,
LevelDown,
}
2020-09-10 10:27:33 +00:00
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, 1));
2022-01-17 22:29:30 +00:00
} else if let Some(item) = self.get_item(item_id) {
item.1 += 1;
2020-09-10 10:27:33 +00:00
}
}
2022-02-09 00:04:36 +00:00
pub fn add_item_amount(&mut self, item_id: u16, amount: u16) {
if !self.has_item(item_id) {
self.items.push(Item(item_id, amount));
} else if let Some(item) = self.get_item(item_id) {
item.1 += amount;
}
}
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);
}
2020-09-10 10:27:33 +00:00
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
pub fn get_item(&mut self, item_id: u16) -> Option<&mut Item> {
self.items.iter_mut().by_ref().find(|item| item.0 == item_id)
}
2020-10-30 01:29:53 +00:00
pub fn get_item_idx(&self, idx: usize) -> Option<&Item> {
self.items.get(idx)
}
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-25 17:14:52 +00:00
pub fn has_item_amount(&self, item_id: u16, operator: Ordering, amount: u16) -> bool {
let result = self.items.iter().any(|item| item.0 == item_id && item.1.cmp(&amount) == operator);
if (operator == Ordering::Equal && amount == 0 && !result) || (operator == Ordering::Less && !self.has_item(item_id)) {
return true;
}
result
}
pub fn add_weapon_data(&mut self, weapon_id: WeaponType, ammo: u16, max_ammo: u16, experience: u16, level: WeaponLevel) {
let weapon = Weapon::new(
weapon_id,
level,
experience,
ammo,
max_ammo,
);
if !self.has_weapon(weapon_id) {
self.weapons.push(weapon);
} else {
let w = self.get_weapon_by_type_mut(weapon_id);
if let Some(w) = w {
*w = weapon
}
}
}
pub fn add_weapon(&mut self, weapon_id: WeaponType, ammo: u16) {
let w = self.get_weapon_by_type_mut(weapon_id);
if let Some(w) = w {
w.ammo += ammo;
w.max_ammo += ammo;
} else {
self.weapons.push(Weapon::new(
weapon_id,
WeaponLevel::Level1,
0,
ammo,
ammo,
));
}
}
2021-01-16 13:51:52 +00:00
pub fn trade_weapon(&mut self, old: Option<WeaponType>, new: WeaponType, max_ammo: u16) {
if let Some(wtype) = old {
if let Some(weapon) = self.get_weapon_by_type_mut(wtype) {
let ammo = if max_ammo == 0 { weapon.max_ammo } else { max_ammo };
*weapon = Weapon::new(new, WeaponLevel::Level1, 0, ammo, ammo);
2021-01-16 13:51:52 +00:00
} else {
self.add_weapon(new, max_ammo);
}
} else {
self.add_weapon(new, max_ammo);
}
}
pub fn remove_weapon(&mut self, wtype: WeaponType) {
self.weapons.retain(|weapon| weapon.wtype != wtype);
}
2020-09-11 00:55:07 +00:00
pub fn get_weapon(&self, idx: usize) -> Option<&Weapon> {
self.weapons.get(idx)
}
pub fn get_weapon_by_type_mut(&mut self, wtype: WeaponType) -> Option<&mut Weapon> {
2020-12-19 16:29:59 +00:00
self.weapons.iter_mut().find(|weapon| weapon.wtype == wtype)
}
pub fn get_current_weapon(&self) -> Option<&Weapon> {
self.weapons.get(self.current_weapon as usize)
}
2020-09-11 00:55:07 +00:00
pub fn get_current_weapon_mut(&mut self) -> Option<&mut Weapon> {
self.weapons.get_mut(self.current_weapon as usize)
}
2020-09-20 23:55:52 +00:00
pub fn next_weapon(&mut self) {
if (1 + self.current_weapon as usize) < self.weapons.len() {
self.current_weapon += 1;
} else {
self.current_weapon = 0;
}
}
pub fn prev_weapon(&mut self) {
if self.current_weapon as usize > 0 {
self.current_weapon -= 1;
} else {
self.current_weapon = self.weapons.len().saturating_sub(1) as u16;
}
}
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() {
weapon.level = WeaponLevel::Level1;
weapon.experience = 0;
}
}
2021-01-16 13:51:52 +00:00
pub fn reset_current_weapon_xp(&mut self) {
if let Some(weapon) = self.get_current_weapon_mut() {
2021-03-23 12:51:55 +00:00
weapon.reset_xp();
2021-01-16 13:51:52 +00:00
}
}
pub fn take_xp(&mut self, exp: u16, state: &mut SharedGameState) -> TakeExperienceResult {
let mut result = TakeExperienceResult::None;
if let Some(weapon) = self.get_current_weapon_mut() {
let lvl_table = state.constants.weapon.level_table[weapon.wtype as usize];
let mut tmp_exp = weapon.experience as isize - exp as isize;
2020-09-23 13:10:42 +00:00
if tmp_exp >= 0 {
weapon.experience = tmp_exp as u16;
} else {
while tmp_exp < 0 {
if weapon.level == WeaponLevel::Level1 {
weapon.experience = 0;
tmp_exp = 0;
} else if weapon.experience < exp {
weapon.level = weapon.level.prev();
2020-09-23 13:10:42 +00:00
let max_level = lvl_table[weapon.level as usize - 1] as isize;
weapon.experience = (max_level + tmp_exp.max(-max_level)) as u16;
tmp_exp += max_level;
if weapon.wtype != WeaponType::Spur {
result = TakeExperienceResult::LevelDown;
}
}
}
}
}
result
}
2021-01-16 13:51:52 +00:00
pub fn add_xp(&mut self, exp: u16, player: &mut Player, state: &mut SharedGameState) {
if let Some(weapon) = self.get_current_weapon_mut() {
2021-03-23 12:51:55 +00:00
weapon.add_xp(exp, player, state);
}
}
2020-09-13 00:29:11 +00:00
/// Get current experience state. Returns a (exp, max exp, max level/exp) tuple.
pub fn get_current_max_exp(&self, constants: &EngineConstants) -> (u16, u16, bool) {
if let Some(weapon) = self.weapons.get(self.current_weapon as usize) {
2021-03-23 12:51:55 +00:00
weapon.get_max_exp(constants)
} else {
2020-09-13 00:29:11 +00:00
(0, 0, false)
}
}
2020-09-13 00:29:11 +00:00
/// Get current ammunition state. Returns a (ammo, max ammo) tuple.
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)
}
}
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 {
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()
}
pub fn has_weapon(&self, wtype: WeaponType) -> bool {
self.weapons.iter().any(|weapon| weapon.wtype == wtype)
}
2021-03-23 12:51:55 +00:00
pub fn tick_weapons(&mut self, state: &mut SharedGameState, player: &mut Player, player_id: TargetPlayer, bullet_manager: &mut BulletManager) {
if let Some(weapon) = self.get_current_weapon_mut() {
weapon.tick(state, player, player_id, bullet_manager);
}
}
2020-08-20 18:31:47 +00:00
}
2020-09-25 17:14:52 +00:00
#[test]
fn inventory_test() {
let mut inventory = Inventory::new();
inventory.add_item(3);
2022-01-17 22:29:30 +00:00
assert!(!inventory.has_item(2));
assert!(inventory.has_item(3));
2020-09-25 17:14:52 +00:00
2022-01-17 22:29:30 +00:00
assert!(inventory.has_item_amount(3, Ordering::Equal, 1));
assert!(inventory.has_item_amount(3, Ordering::Less, 2));
2020-09-25 17:14:52 +00:00
inventory.consume_item(3);
2022-01-17 22:29:30 +00:00
assert!(inventory.has_item_amount(3, Ordering::Equal, 0));
assert!(inventory.has_item_amount(3, Ordering::Less, 2));
2020-09-25 17:14:52 +00:00
inventory.add_item(2);
2022-01-17 22:29:30 +00:00
assert!(inventory.has_item(2));
assert!(inventory.has_item_amount(2, Ordering::Equal, 1));
assert!(!inventory.has_item_amount(2, Ordering::Less, 1));
2020-09-25 17:14:52 +00:00
inventory.add_item(4);
inventory.add_item(4);
inventory.add_item(4);
inventory.add_item(4);
2022-01-17 22:29:30 +00:00
assert!(inventory.has_item(4));
assert!(inventory.has_item_amount(4, Ordering::Greater, 3));
assert!(inventory.has_item_amount(4, Ordering::Equal, 4));
assert!(!inventory.has_item_amount(4, Ordering::Less, 2));
2020-09-25 17:14:52 +00:00
}