add inventory structure

This commit is contained in:
Alula 2020-09-10 12:27:33 +02:00
parent 41f72f3c7a
commit 6bcb498c5d
No known key found for this signature in database
GPG Key ID: 3E00485503A1D8BA
3 changed files with 47 additions and 2 deletions

View File

@ -1,3 +1,45 @@
#[derive(Clone)]
pub struct Weapon {
id: u16,
level: u16,
experience: u16,
ammo: u16,
max_ammo: u16,
}
#[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);
}
pub fn has_item(&self, item_id: u16) -> bool {
self.items.iter().any(|item| item.0 == item_id)
}
}

View File

@ -67,7 +67,7 @@ mod sound;
mod text_script;
mod texture_set;
mod ui;
mod weapon;
mod inventory;
struct Game {
scene: Option<Box<dyn Scene>>,

View File

@ -10,6 +10,7 @@ use crate::frame::Frame;
use crate::ggez::{Context, GameResult};
use crate::SharedGameState;
use crate::str;
use crate::inventory::Inventory;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
@ -31,6 +32,7 @@ pub struct Player {
pub cond: Condition,
pub flags: Flag,
pub equip: Equipment,
pub inventory: Inventory,
pub direction: Direction,
pub display_bounds: Rect<usize>,
pub hit_bounds: Rect<usize>,
@ -70,6 +72,7 @@ impl Player {
cond: Condition(0x80),
flags: Flag(0),
equip: Equipment(0),
inventory: Inventory::new(),
direction: Direction::Right,
display_bounds: constants.my_char.display_bounds,
hit_bounds: constants.my_char.hit_bounds,