From 95438d85614aaecfe36bb5a36b64a123708d87e1 Mon Sep 17 00:00:00 2001 From: Alula Date: Fri, 11 Sep 2020 02:55:07 +0200 Subject: [PATCH] weapon hud --- src/common.rs | 3 +- src/inventory.rs | 40 ++++++++++++++++++++++---- src/scene/game_scene.rs | 62 +++++++++++++++++++++++++++++++++++------ 3 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/common.rs b/src/common.rs index f6e2bad..2102720 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,4 +1,4 @@ -use num_traits::{Num, AsPrimitive}; +use num_traits::{AsPrimitive, Num}; use crate::bitfield; @@ -96,7 +96,6 @@ pub enum FadeDirection { Center, } - impl FadeDirection { pub fn from_int(val: usize) -> Option { match val { diff --git a/src/inventory.rs b/src/inventory.rs index 8ba1af4..244d45c 100644 --- a/src/inventory.rs +++ b/src/inventory.rs @@ -1,10 +1,10 @@ #[derive(Clone)] pub struct Weapon { - id: u16, - level: u16, - experience: u16, - ammo: u16, - max_ammo: u16, + pub id: u16, + pub level: u16, + pub experience: u16, + pub ammo: u16, + pub max_ammo: u16, } #[derive(Clone, Copy)] @@ -59,7 +59,11 @@ impl Inventory { self.weapons.retain(|weapon| weapon.id != weapon_id); } - pub fn get_current_weapon(&mut self) -> Option<&mut Weapon> { + pub fn get_weapon(&self, idx: usize) -> Option<&Weapon> { + self.weapons.get(idx) + } + + pub fn get_current_weapon_mut(&mut self) -> Option<&mut Weapon> { self.weapons.get_mut(self.current_weapon as usize) } @@ -76,6 +80,30 @@ impl Inventory { } } + 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) -> u16 { + if let Some(weapon) = self.weapons.get(self.current_weapon as usize) { + weapon.level + } else { + 0 + } + } + + 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, weapon_id: u16) -> bool { self.weapons.iter().any(|weapon| weapon.id == weapon_id) } diff --git a/src/scene/game_scene.rs b/src/scene/game_scene.rs index 77e70ee..f5a7fc5 100644 --- a/src/scene/game_scene.rs +++ b/src/scene/game_scene.rs @@ -28,6 +28,7 @@ pub struct GameScene { life_bar: u16, life_bar_counter: u16, map_name_counter: u16, + weapon_x_pos: isize, } #[derive(Debug, EnumIter, PartialEq, Eq, Hash, Copy, Clone)] @@ -69,6 +70,7 @@ impl GameScene { life_bar: 0, life_bar_counter: 0, map_name_counter: 0, + weapon_x_pos: 16, }) } @@ -91,14 +93,17 @@ impl GameScene { } fn draw_hud(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult { - let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "TextBox")?; - // todo: max ammo display - // none - batch.add_rect(16.0 + 48.0, 16.0, - &Rect::::new_size(80, 48, 16, 8)); - batch.add_rect(16.0 + 48.0, 24.0, - &Rect::::new_size(80, 48, 16, 8)); + let weap_x = self.weapon_x_pos as f32; + let (ammo, max_ammo) = self.player.inventory.get_current_ammo(); + let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "TextBox")?; + + if max_ammo == 0 { + batch.add_rect(weap_x + 48.0, 16.0, + &Rect::::new_size(80, 48, 16, 8)); + batch.add_rect(weap_x + 48.0, 24.0, + &Rect::::new_size(80, 48, 16, 8)); + } // per batch.add_rect(16.0 + 32.0, 24.0, @@ -126,8 +131,43 @@ impl GameScene { } batch.draw(ctx)?; + let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "ArmsImage")?; - self.draw_number(40.0, 32.0, 0, Alignment::Right, state, ctx)?; + let weapon_count = self.player.inventory.get_weapon_count(); + if weapon_count != 0 { + let current_weapon = self.player.inventory.get_current_weapon_idx() as usize; + let mut rect = Rect::new(0, 0, 0, 16); + + for a in 0..weapon_count { + let mut pos_x = ((a - current_weapon) as f32 * 16.0) + weap_x; + + if pos_x < 8.0 { + pos_x += 48.0 + weapon_count as f32 * 16.0; + } else if pos_x >= 24.0 { + pos_x += 48.0; + } + + if pos_x >= 72.0 + ((weapon_count - 1) as f32 * 16.0) { + pos_x -= 48.0 + weapon_count as f32 * 16.0; + } else if pos_x < 72.0 && pos_x >= 24.0 { + pos_x -= 48.0; + } + + if let Some(weapon) = self.player.inventory.get_weapon(a) { + rect.left = weapon.id as usize * 16; + rect.right = rect.left + 16; + batch.add_rect(pos_x, 16.0, &rect); + } + } + } + + batch.draw(ctx)?; + + if max_ammo != 0 { + self.draw_number(weap_x + 64.0, 16.0, ammo as usize, Alignment::Right, state, ctx)?; + self.draw_number(weap_x + 64.0, 24.0, max_ammo as usize, Alignment::Right, state, ctx)?; + } + self.draw_number(40.0, 32.0, self.player.inventory.get_current_level() as usize, Alignment::Right, state, ctx)?; self.draw_number(40.0, 40.0, self.life_bar as usize, Alignment::Right, state, ctx)?; Ok(()) @@ -570,6 +610,12 @@ impl Scene for GameScene { self.map_name_counter -= 1; } + if self.weapon_x_pos > 16 { + self.weapon_x_pos -= 2; + } else if self.weapon_x_pos < 16 { + self.weapon_x_pos += 2; + } + match state.fade_state { FadeState::FadeOut(tick, direction) if tick < 15 => { state.fade_state = FadeState::FadeOut(tick + 1, direction);