diff --git a/src/engine_constants/mod.rs b/src/engine_constants/mod.rs index cbb64ff..1b98172 100644 --- a/src/engine_constants/mod.rs +++ b/src/engine_constants/mod.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::io::{BufRead, BufReader, Cursor, Read}; -use byteorder::{LE, ReadBytesExt}; +use byteorder::{ReadBytesExt, LE}; use case_insensitive_hashmap::CaseInsensitiveHashMap; use xmltree::Element; @@ -303,6 +303,7 @@ pub struct EngineConstants { pub credit_illustration_paths: Vec, pub animated_face_table: Vec, pub string_table: HashMap, + pub missile_flags: Vec, } impl Clone for EngineConstants { @@ -332,6 +333,7 @@ impl Clone for EngineConstants { credit_illustration_paths: self.credit_illustration_paths.clone(), animated_face_table: self.animated_face_table.clone(), string_table: self.string_table.clone(), + missile_flags: self.missile_flags.clone(), } } } @@ -1606,6 +1608,7 @@ impl EngineConstants { ], animated_face_table: vec![AnimatedFace { face_id: 0, anim_id: 0, anim_frames: vec![(0, 0)] }], string_table: HashMap::new(), + missile_flags: vec![200, 201, 202, 218, 550, 766, 880, 920, 1551], } } diff --git a/src/menu/mod.rs b/src/menu/mod.rs index 01c7f62..cce7c54 100644 --- a/src/menu/mod.rs +++ b/src/menu/mod.rs @@ -437,18 +437,13 @@ impl Menu { // Difficulty if state.constants.is_cs_plus && !state.settings.original_textures { - let difficulty = GameDifficulty::from_save_value(save.difficulty); + let difficulty = GameDifficulty::from_primitive(save.difficulty); let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "MyChar")?; batch.add_rect( self.x as f32 + 20.0, y + 10.0, - &Rect::new_size( - 0, - GameDifficulty::get_skinsheet_offset(difficulty).saturating_mul(4 * 16), - 16, - 16, - ), + &Rect::new_size(0, (difficulty as u16).saturating_mul(2 * 16), 16, 16), ); batch.draw(ctx)?; } else { diff --git a/src/menu/save_select_menu.rs b/src/menu/save_select_menu.rs index 5f2ac54..2db038e 100644 --- a/src/menu/save_select_menu.rs +++ b/src/menu/save_select_menu.rs @@ -149,8 +149,18 @@ impl SaveSelectMenu { MenuSelectionResult::Selected(4, _) | MenuSelectionResult::Canceled => { self.current_menu = CurrentMenu::SaveMenu; } - MenuSelectionResult::Selected(item, _) => { - state.difficulty = GameDifficulty::from_index(item - 1); + MenuSelectionResult::Selected(1, _) => { + state.difficulty = GameDifficulty::Easy; + state.reload_resources(ctx)?; + state.load_or_start_game(ctx)?; + } + MenuSelectionResult::Selected(2, _) => { + state.difficulty = GameDifficulty::Normal; + state.reload_resources(ctx)?; + state.load_or_start_game(ctx)?; + } + MenuSelectionResult::Selected(3, _) => { + state.difficulty = GameDifficulty::Hard; state.reload_resources(ctx)?; state.load_or_start_game(ctx)?; } diff --git a/src/npc/ai/misc.rs b/src/npc/ai/misc.rs index c632633..ea479a1 100644 --- a/src/npc/ai/misc.rs +++ b/src/npc/ai/misc.rs @@ -137,7 +137,7 @@ impl NPC { } pub(crate) fn tick_n015_chest_closed(&mut self, state: &mut SharedGameState, npc_list: &NPCList) -> GameResult { - if state.difficulty == GameDifficulty::Hard && self.chest_has_missile_flag() { + if state.difficulty == GameDifficulty::Hard && state.constants.missile_flags.contains(&self.flag_num) { self.cond.set_alive(false); return Ok(()); } @@ -337,7 +337,7 @@ impl NPC { } pub(crate) fn tick_n021_chest_open(&mut self, state: &mut SharedGameState) -> GameResult { - if state.difficulty == GameDifficulty::Hard && self.chest_has_missile_flag() { + if state.difficulty == GameDifficulty::Hard && state.constants.missile_flags.contains(&self.flag_num) { self.cond.set_alive(false); return Ok(()); } @@ -2624,9 +2624,4 @@ impl NPC { Ok(()) } - - fn chest_has_missile_flag(&self) -> bool { - let missile_flags: [u16; 9] = [200, 201, 202, 218, 550, 766, 880, 920, 1551]; - missile_flags.contains(&self.flag_num) - } } diff --git a/src/player/skin/basic.rs b/src/player/skin/basic.rs index 84bab9c..a007694 100644 --- a/src/player/skin/basic.rs +++ b/src/player/skin/basic.rs @@ -119,7 +119,7 @@ impl BasicPlayerSkin { fn get_y_offset_by(&self, y: u16) -> u16 { return self .skinsheet_offset - .saturating_mul(self.metadata.frame_size_height.saturating_mul(4)) + .saturating_mul(self.metadata.frame_size_height.saturating_mul(2)) .saturating_add(y); } } diff --git a/src/profile.rs b/src/profile.rs index 5f51661..a4ea575 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -147,7 +147,7 @@ impl GameProfile { game_scene.player1.cond.0 = 0x80; - state.difficulty = GameDifficulty::from_save_value(self.difficulty); + state.difficulty = GameDifficulty::from_primitive(self.difficulty); game_scene.player1.skin.apply_gamestate(state); game_scene.player2.skin.apply_gamestate(state); @@ -236,7 +236,7 @@ impl GameProfile { } let timestamp = get_timestamp(); - let difficulty = state.difficulty.to_save_value(); + let difficulty = state.difficulty as u8; GameProfile { current_map, diff --git a/src/shared_game_state.rs b/src/shared_game_state.rs index ff55f9e..60ddf7f 100644 --- a/src/shared_game_state.rs +++ b/src/shared_game_state.rs @@ -67,46 +67,16 @@ impl TimingMode { } } -#[derive(PartialEq, Eq, Copy, Clone)] +#[derive(PartialEq, Eq, Copy, Clone, num_derive::FromPrimitive)] pub enum GameDifficulty { - Easy, - Normal, - Hard, + Normal = 0, + Easy = 2, + Hard = 4, } impl GameDifficulty { - pub fn from_index(index: usize) -> GameDifficulty { - match index { - 0 => GameDifficulty::Easy, - 1 => GameDifficulty::Normal, - 2 => GameDifficulty::Hard, - _ => unreachable!(), - } - } - - pub fn from_save_value(val: u8) -> GameDifficulty { - match val { - 0 => GameDifficulty::Normal, - 2 => GameDifficulty::Easy, - 4 => GameDifficulty::Hard, - _ => unreachable!(), - } - } - - pub fn to_save_value(self) -> u8 { - match self { - GameDifficulty::Normal => 0, - GameDifficulty::Easy => 2, - GameDifficulty::Hard => 4, - } - } - - pub fn get_skinsheet_offset(difficulty: GameDifficulty) -> u16 { - match difficulty { - GameDifficulty::Easy => 1, // Yellow Quote - GameDifficulty::Normal => 0, // Good Quote - GameDifficulty::Hard => 2, // Human Quote - } + pub fn from_primitive(val: u8) -> GameDifficulty { + return num_traits::FromPrimitive::from_u8(val).unwrap_or(GameDifficulty::Normal); } } @@ -654,6 +624,6 @@ impl SharedGameState { } } - GameDifficulty::get_skinsheet_offset(self.difficulty) + return self.difficulty as u16; } }