1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-16 10:52:44 +00:00

weapon hud

This commit is contained in:
Alula 2020-09-11 02:55:07 +02:00
parent f4dc0def53
commit 95438d8561
No known key found for this signature in database
GPG key ID: 3E00485503A1D8BA
3 changed files with 89 additions and 16 deletions

View file

@ -1,4 +1,4 @@
use num_traits::{Num, AsPrimitive}; use num_traits::{AsPrimitive, Num};
use crate::bitfield; use crate::bitfield;
@ -96,7 +96,6 @@ pub enum FadeDirection {
Center, Center,
} }
impl FadeDirection { impl FadeDirection {
pub fn from_int(val: usize) -> Option<FadeDirection> { pub fn from_int(val: usize) -> Option<FadeDirection> {
match val { match val {

View file

@ -1,10 +1,10 @@
#[derive(Clone)] #[derive(Clone)]
pub struct Weapon { pub struct Weapon {
id: u16, pub id: u16,
level: u16, pub level: u16,
experience: u16, pub experience: u16,
ammo: u16, pub ammo: u16,
max_ammo: u16, pub max_ammo: u16,
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -59,7 +59,11 @@ impl Inventory {
self.weapons.retain(|weapon| weapon.id != weapon_id); 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) 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 { pub fn has_weapon(&self, weapon_id: u16) -> bool {
self.weapons.iter().any(|weapon| weapon.id == weapon_id) self.weapons.iter().any(|weapon| weapon.id == weapon_id)
} }

View file

@ -28,6 +28,7 @@ pub struct GameScene {
life_bar: u16, life_bar: u16,
life_bar_counter: u16, life_bar_counter: u16,
map_name_counter: u16, map_name_counter: u16,
weapon_x_pos: isize,
} }
#[derive(Debug, EnumIter, PartialEq, Eq, Hash, Copy, Clone)] #[derive(Debug, EnumIter, PartialEq, Eq, Hash, Copy, Clone)]
@ -69,6 +70,7 @@ impl GameScene {
life_bar: 0, life_bar: 0,
life_bar_counter: 0, life_bar_counter: 0,
map_name_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 { 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 // none
batch.add_rect(16.0 + 48.0, 16.0, let weap_x = self.weapon_x_pos as f32;
&Rect::<usize>::new_size(80, 48, 16, 8)); let (ammo, max_ammo) = self.player.inventory.get_current_ammo();
batch.add_rect(16.0 + 48.0, 24.0, let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "TextBox")?;
&Rect::<usize>::new_size(80, 48, 16, 8));
if max_ammo == 0 {
batch.add_rect(weap_x + 48.0, 16.0,
&Rect::<usize>::new_size(80, 48, 16, 8));
batch.add_rect(weap_x + 48.0, 24.0,
&Rect::<usize>::new_size(80, 48, 16, 8));
}
// per // per
batch.add_rect(16.0 + 32.0, 24.0, batch.add_rect(16.0 + 32.0, 24.0,
@ -126,8 +131,43 @@ impl GameScene {
} }
batch.draw(ctx)?; 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)?; self.draw_number(40.0, 40.0, self.life_bar as usize, Alignment::Right, state, ctx)?;
Ok(()) Ok(())
@ -570,6 +610,12 @@ impl Scene for GameScene {
self.map_name_counter -= 1; 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 { match state.fade_state {
FadeState::FadeOut(tick, direction) if tick < 15 => { FadeState::FadeOut(tick, direction) if tick < 15 => {
state.fade_state = FadeState::FadeOut(tick + 1, direction); state.fade_state = FadeState::FadeOut(tick + 1, direction);