Skip to content

Commit

Permalink
Add Linecap enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 17, 2024
1 parent 8880238 commit d128ab7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/game/drop_speed.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
18 changes: 18 additions & 0 deletions src/game/linecap.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
FromPrimitive::from_i8(*self as i8 - 1).map(|n| std::mem::replace(self, n))
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
}
}
1 change: 1 addition & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod asset;
mod board;
pub mod drop_speed;
pub mod linecap;
pub mod palette;
pub mod piece;
pub mod player;
Expand Down
9 changes: 5 additions & 4 deletions src/game/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,17 +24,17 @@ pub enum PlayerState {
pub struct PlayerConfig {
pub start_level: usize,
pub transition: Transition,
pub lv39_linecap: bool,
pub linecap: Linecap,
pub drop_speed: DropSpeed,
}

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(),
}
}
}
Expand Down Expand Up @@ -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(),
Expand Down
25 changes: 11 additions & 14 deletions src/game/tick.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,24 +20,24 @@ 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,
}
}

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 => (),
}
}
Expand All @@ -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),
Expand All @@ -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),
},
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/game/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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),
}
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 18 additions & 16 deletions src/game_option_menu/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit d128ab7

Please sign in to comment.