From d128ab7aab012c00a99929870ec8bba164545cef Mon Sep 17 00:00:00 2001 From: Ramirisu Date: Wed, 18 Sep 2024 04:20:19 +0800 Subject: [PATCH] Add `Linecap` enum. --- src/game/drop_speed.rs | 3 ++- src/game/linecap.rs | 18 ++++++++++++++++++ src/game/mod.rs | 1 + src/game/player.rs | 9 +++++---- src/game/tick.rs | 25 +++++++++++-------------- src/game/transition.rs | 8 ++++---- src/game_option_menu/plugin.rs | 34 ++++++++++++++++++---------------- 7 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 src/game/linecap.rs diff --git a/src/game/drop_speed.rs b/src/game/drop_speed.rs index 8a992f9..ce7c653 100644 --- a/src/game/drop_speed.rs +++ b/src/game/drop_speed.rs @@ -1,7 +1,8 @@ use num_traits::FromPrimitive; -#[derive(Clone, Copy, PartialEq, Eq, FromPrimitive)] +#[derive(Default, Clone, Copy, PartialEq, Eq, FromPrimitive)] pub enum DropSpeed { + #[default] Level = 0, Locked, } diff --git a/src/game/linecap.rs b/src/game/linecap.rs new file mode 100644 index 0000000..33b0398 --- /dev/null +++ b/src/game/linecap.rs @@ -0,0 +1,18 @@ +use num_traits::FromPrimitive; + +#[derive(Default, Clone, Copy, PartialEq, Eq, FromPrimitive)] +pub enum Linecap { + #[default] + None = 0, + KillScreenX2, +} + +impl Linecap { + pub fn enum_prev(&mut self) -> Option { + FromPrimitive::from_i8(*self as i8 - 1).map(|n| std::mem::replace(self, n)) + } + + pub fn enum_next(&mut self) -> Option { + FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n)) + } +} diff --git a/src/game/mod.rs b/src/game/mod.rs index 4d2bae7..c1e650a 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1,6 +1,7 @@ mod asset; mod board; pub mod drop_speed; +pub mod linecap; pub mod palette; pub mod piece; pub mod player; diff --git a/src/game/player.rs b/src/game/player.rs index 2b25e30..546b0de 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -3,6 +3,7 @@ use bevy::{prelude::*, time::Stopwatch}; use super::{ board::Board, drop_speed::DropSpeed, + linecap::Linecap, tick::{EntryDelayTick, FallTick, LineClearTick}, timer::{DelayAutoShiftTimer, GameTimer, PressDownTimer}, transition::Transition, @@ -23,7 +24,7 @@ pub enum PlayerState { pub struct PlayerConfig { pub start_level: usize, pub transition: Transition, - pub lv39_linecap: bool, + pub linecap: Linecap, pub drop_speed: DropSpeed, } @@ -31,9 +32,9 @@ impl Default for PlayerConfig { fn default() -> Self { Self { start_level: 0, - lv39_linecap: false, + linecap: Linecap::default(), transition: Transition::default(), - drop_speed: DropSpeed::Level, + drop_speed: DropSpeed::default(), } } } @@ -64,7 +65,7 @@ impl PlayerData { can_press_down: false, press_down_timer: PressDownTimer::default(), das_timer: DelayAutoShiftTimer::default(), - fall_tick: FallTick::new(config.start_level, config.lv39_linecap, config.drop_speed), + fall_tick: FallTick::new(config.start_level, config.linecap, config.drop_speed), line_clear_tick: LineClearTick::default(), line_clear_rows: default(), line_clear_phase: LineClearPhase::default(), diff --git a/src/game/tick.rs b/src/game/tick.rs index 67eee82..4376fd9 100644 --- a/src/game/tick.rs +++ b/src/game/tick.rs @@ -1,6 +1,6 @@ use std::time::Duration; -use super::drop_speed::DropSpeed; +use super::{drop_speed::DropSpeed, linecap::Linecap}; const TICKS_PER_MICROSECOND: u64 = 60_000_000; @@ -20,16 +20,16 @@ pub fn duration_to_ticks(duration: Duration) -> u64 { pub struct FallTick { threshold: Duration, initial_entry_delay: bool, - lv39_linecap: bool, + linecap: Linecap, drop_speed: DropSpeed, } impl FallTick { - pub fn new(level: usize, lv39_linecap: bool, drop_speed: DropSpeed) -> Self { + pub fn new(level: usize, linecap: Linecap, drop_speed: DropSpeed) -> Self { Self { - threshold: Self::get_trigger_tick(level, lv39_linecap), + threshold: Self::get_trigger_tick(level, linecap), initial_entry_delay: true, - lv39_linecap, + linecap, drop_speed, } } @@ -37,7 +37,7 @@ impl FallTick { pub fn set_level(&mut self, level: usize) { self.initial_entry_delay = false; match self.drop_speed { - DropSpeed::Level => self.threshold = Self::get_trigger_tick(level, self.lv39_linecap), + DropSpeed::Level => self.threshold = Self::get_trigger_tick(level, self.linecap), DropSpeed::Locked => (), } } @@ -50,7 +50,7 @@ impl FallTick { } } - fn get_trigger_tick(level: usize, lv39_linecap: bool) -> Duration { + fn get_trigger_tick(level: usize, linecap: Linecap) -> Duration { match level { 0 => ticks_to_duration(48), 1 => ticks_to_duration(43), @@ -67,13 +67,10 @@ impl FallTick { 16..19 => ticks_to_duration(3), 19..29 => ticks_to_duration(2), 29..39 => ticks_to_duration(1), - _ => { - if lv39_linecap { - sub_ticks_to_duration(500) - } else { - ticks_to_duration(1) - } - } + _ => match linecap { + Linecap::None => ticks_to_duration(1), + Linecap::KillScreenX2 => sub_ticks_to_duration(500), + }, } } diff --git a/src/game/transition.rs b/src/game/transition.rs index 7918f11..4cec56a 100644 --- a/src/game/transition.rs +++ b/src/game/transition.rs @@ -5,7 +5,7 @@ use num_traits::FromPrimitive; #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, FromPrimitive)] pub enum Transition { #[default] - Default = 0, + Classic = 0, Every10Lines, Every4Lines, } @@ -21,7 +21,7 @@ impl Transition { pub fn get_level(&self, start_level: usize, lines: usize) -> usize { match self { - Transition::Default => Self::get_level_classic(start_level, lines), + Transition::Classic => Self::get_level_classic(start_level, lines), Transition::Every10Lines => Self::get_level_every_n_lines(start_level, lines, 10), Transition::Every4Lines => Self::get_level_every_n_lines(start_level, lines, 4), } @@ -63,8 +63,8 @@ mod tests { use super::*; #[test] - fn test_default() { - let transition = Transition::Default; + fn test_classic() { + let transition = Transition::Classic; assert_eq!(transition.get_level(0, 0), 0); assert_eq!(transition.get_level(0, 10), 1); diff --git a/src/game_option_menu/plugin.rs b/src/game_option_menu/plugin.rs index e2031db..c7fd86b 100644 --- a/src/game_option_menu/plugin.rs +++ b/src/game_option_menu/plugin.rs @@ -4,7 +4,7 @@ use crate::{ app_state::AppState, audio::plugin::PlaySoundEvent, controller::Controller, - game::{drop_speed::DropSpeed, transition::Transition}, + game::{drop_speed::DropSpeed, linecap::Linecap, transition::Transition}, inputs::{ControllerMapping, PlayerInputs}, level_menu::plugin::LevelMenuData, logo::{load_logo_images, TETRIS_BITMAP}, @@ -88,7 +88,7 @@ impl GameOptionMenuSelection { struct GameOptionMenuData { selection: GameOptionMenuSelection, transition: Transition, - lv39_linecap: bool, + linecap: Linecap, drop_speed: DropSpeed, #[cfg(not(target_arch = "wasm32"))] window_mode: WindowMode, @@ -98,9 +98,9 @@ impl GameOptionMenuData { pub fn new() -> Self { Self { selection: GameOptionMenuSelection::default(), - transition: Transition::Default, - lv39_linecap: false, - drop_speed: DropSpeed::Level, + transition: Transition::default(), + linecap: Linecap::default(), + drop_speed: DropSpeed::default(), #[cfg(not(target_arch = "wasm32"))] window_mode: WindowMode::Windowed, } @@ -252,7 +252,7 @@ fn update_ui_system( GameOptionMenuSelection::Transition => { text.sections[0].value = fname("TRANSITION", NameKind::Option); match game_option_menu_data.transition { - Transition::Default => text.sections[1].value = fopt("DEFAULT", false, true), + Transition::Classic => text.sections[1].value = fopt("CLASSIC", false, true), Transition::Every10Lines => { text.sections[1].value = fopt("10 LINES", true, true) } @@ -262,11 +262,10 @@ fn update_ui_system( }; } GameOptionMenuSelection::Linecap => { - text.sections[0].value = fname("LV39 LINECAP", NameKind::Option); - if game_option_menu_data.lv39_linecap { - text.sections[1].value = fopt("ON", true, false); - } else { - text.sections[1].value = fopt("OFF", false, true); + text.sections[0].value = fname("LINECAP", NameKind::Option); + match game_option_menu_data.linecap { + Linecap::None => text.sections[1].value = fopt("OFF", false, true), + Linecap::KillScreenX2 => text.sections[1].value = fopt("ON", true, false), } } GameOptionMenuSelection::DropSpeed => { @@ -381,7 +380,7 @@ fn handle_input_system( } if player_inputs.start { level_menu_data.config.transition = game_option_menu_data.transition; - level_menu_data.config.lv39_linecap = game_option_menu_data.lv39_linecap; + level_menu_data.config.linecap = game_option_menu_data.linecap; level_menu_data.config.drop_speed = game_option_menu_data.drop_speed; e_play_sound.send(PlaySoundEvent::StartGame); app_state.set(AppState::LevelMenu); @@ -417,12 +416,15 @@ fn handle_input_system( game_option_menu_data.selection = GameOptionMenuSelection::DropSpeed; selection_changed = true; } + if player_inputs.right.0 { - game_option_menu_data.lv39_linecap = true; - option_changed = true; + if let Some(_) = game_option_menu_data.linecap.enum_next() { + scale_changed = true; + } } else if player_inputs.left.0 { - game_option_menu_data.lv39_linecap = false; - option_changed = true; + if let Some(_) = game_option_menu_data.linecap.enum_prev() { + scale_changed = true; + } } } GameOptionMenuSelection::DropSpeed => {