From f87ccaf694f5fcdc8a3a600c89cbbd6567d40c91 Mon Sep 17 00:00:00 2001 From: Alula Date: Mon, 21 Sep 2020 01:55:52 +0200 Subject: [PATCH] weapon switching --- src/inventory.rs | 16 ++++++++++++++++ src/scene/game_scene.rs | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/inventory.rs b/src/inventory.rs index 0cab938..7607676 100644 --- a/src/inventory.rs +++ b/src/inventory.rs @@ -73,6 +73,22 @@ impl Inventory { self.weapons.get_mut(self.current_weapon as usize) } + 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; diff --git a/src/scene/game_scene.rs b/src/scene/game_scene.rs index bd68a89..2e88dfe 100644 --- a/src/scene/game_scene.rs +++ b/src/scene/game_scene.rs @@ -151,11 +151,11 @@ impl GameScene { let weapon_count = self.inventory.get_weapon_count(); if weapon_count != 0 { - let current_weapon = self.inventory.get_current_weapon_idx() as usize; + let current_weapon = self.inventory.get_current_weapon_idx() as isize; 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; + let mut pos_x = ((a as isize - current_weapon) as f32 * 16.0) + weap_x; if pos_x < 8.0 { pos_x += 48.0 + weapon_count as f32 * 16.0; @@ -753,6 +753,16 @@ impl Scene for GameScene { weapon.shoot_bullet(&self.player, &mut self.bullet_manager, state); } + if state.key_trigger.weapon_next() { + self.inventory.next_weapon(); + self.weapon_x_pos = 32; + } + + if state.key_trigger.weapon_prev() { + self.inventory.prev_weapon(); + self.weapon_x_pos = 0; + } + // update health bar if self.life_bar < self.player.life as u16 { self.life_bar = self.player.life as u16;