From 6bcb498c5d14f18359ff5ed784f13b8d8f161624 Mon Sep 17 00:00:00 2001 From: Alula Date: Thu, 10 Sep 2020 12:27:33 +0200 Subject: [PATCH] add inventory structure --- src/inventory.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 2 +- src/player.rs | 3 +++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/inventory.rs b/src/inventory.rs index b1789d8..a57001b 100644 --- a/src/inventory.rs +++ b/src/inventory.rs @@ -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, + weapons: Vec, +} + +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) + } } diff --git a/src/main.rs b/src/main.rs index ecb447d..5a3ee3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,7 +67,7 @@ mod sound; mod text_script; mod texture_set; mod ui; -mod weapon; +mod inventory; struct Game { scene: Option>, diff --git a/src/player.rs b/src/player.rs index a33de12..1357f4c 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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, pub hit_bounds: Rect, @@ -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,