1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-22 13:42:47 +00:00

refactor stuff to make code cleaner

This commit is contained in:
Sallai József 2022-02-27 17:27:34 +02:00 committed by alula
parent 0c33795356
commit 657b73aefc
7 changed files with 30 additions and 57 deletions

View file

@ -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<String>,
pub animated_face_table: Vec<AnimatedFace>,
pub string_table: HashMap<String, String>,
pub missile_flags: Vec<u16>,
}
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],
}
}

View file

@ -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 {

View file

@ -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)?;
}

View file

@ -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)
}
}

View file

@ -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);
}
}

View file

@ -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,

View file

@ -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;
}
}