Skip to content

Commit

Permalink
Rewrite enum utils and refine game option menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Oct 4, 2024
1 parent d0a5802 commit a353cc7
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 171 deletions.
40 changes: 36 additions & 4 deletions src/game/das_counter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use bevy::prelude::*;
use num_traits::FromPrimitive;

Expand All @@ -10,12 +12,32 @@ pub enum DASCounter {
}

impl DASCounter {
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_has_prev(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 - 1).is_some()
}

pub fn enum_has_next(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 + 1).is_some()
}

pub fn enum_prev(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 - 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
pub fn enum_next(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 + 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn get_counter_visibility(&self) -> Visibility {
Expand All @@ -26,3 +48,13 @@ impl DASCounter {
}
}
}

impl Display for DASCounter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DASCounter::Off => f.write_str("OFF"),
DASCounter::Default => f.write_str("DEFAULT"),
DASCounter::Full => f.write_str("FULL"),
}
}
}
39 changes: 35 additions & 4 deletions src/game/gravity.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use num_traits::FromPrimitive;

#[derive(Default, Clone, Copy, PartialEq, Eq, FromPrimitive)]
Expand All @@ -8,11 +10,40 @@ pub enum Gravity {
}

impl Gravity {
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_has_prev(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 - 1).is_some()
}

pub fn enum_has_next(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 + 1).is_some()
}

pub fn enum_prev(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 - 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn enum_next(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 + 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
impl Display for Gravity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Gravity::Level => f.write_str("LEVEL"),
Gravity::Locked => f.write_str("LOCKED"),
}
}
}
43 changes: 37 additions & 6 deletions src/game/linecap.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
use std::fmt::Display;

use num_traits::FromPrimitive;

#[derive(Default, Clone, Copy, PartialEq, Eq, FromPrimitive)]
pub enum Linecap {
#[default]
None,
KillScreenX2,
Off,
SuperKillScreen,
}

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_has_prev(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 - 1).is_some()
}

pub fn enum_has_next(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 + 1).is_some()
}

pub fn enum_prev(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 - 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn enum_next(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 + 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
impl Display for Linecap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Linecap::Off => f.write_str("OFF"),
Linecap::SuperKillScreen => f.write_str("SUPER KILL SCREEN"),
}
}
}
40 changes: 36 additions & 4 deletions src/game/next_piece_hint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use bevy::prelude::*;
use num_traits::FromPrimitive;

Expand All @@ -10,12 +12,32 @@ pub enum NextPieceHint {
}

impl NextPieceHint {
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_has_prev(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 - 1).is_some()
}

pub fn enum_has_next(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 + 1).is_some()
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
pub fn enum_prev(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 - 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn enum_next(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 + 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn get_visibility(&self, index: usize) -> Visibility {
Expand All @@ -31,3 +53,13 @@ impl NextPieceHint {
}
}
}

impl Display for NextPieceHint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NextPieceHint::Off => f.write_str("OFF"),
NextPieceHint::Classic => f.write_str("CLASSIC"),
NextPieceHint::Modern => f.write_str("MODERN"),
}
}
}
8 changes: 4 additions & 4 deletions src/game/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ impl FallTimer {
19..29 => tv_system.ticks_to_duration(2),
29..39 => tv_system.ticks_to_duration(1),
_ => match linecap {
Linecap::None => tv_system.ticks_to_duration(1),
Linecap::KillScreenX2 => tv_system.subticks_to_duration(500),
Linecap::Off => tv_system.ticks_to_duration(1),
Linecap::SuperKillScreen => tv_system.subticks_to_duration(500),
},
},
TVSystem::PAL => match level {
Expand All @@ -94,8 +94,8 @@ impl FallTimer {
16..19 => tv_system.ticks_to_duration(2),
19..29 => tv_system.ticks_to_duration(1),
_ => match linecap {
Linecap::None => tv_system.ticks_to_duration(1),
Linecap::KillScreenX2 => tv_system.subticks_to_duration(500),
Linecap::Off => tv_system.ticks_to_duration(1),
Linecap::SuperKillScreen => tv_system.subticks_to_duration(500),
},
},
}
Expand Down
41 changes: 37 additions & 4 deletions src/game/transition.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use num_traits::FromPrimitive;

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, FromPrimitive)]
Expand All @@ -10,12 +12,32 @@ pub enum Transition {
}

impl Transition {
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_has_prev(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 - 1).is_some()
}

pub fn enum_has_next(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 + 1).is_some()
}

pub fn enum_prev(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 - 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
pub fn enum_next(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 + 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn get_level(&self, start_level: usize, lines: usize) -> usize {
Expand Down Expand Up @@ -56,6 +78,17 @@ impl Transition {
}
}

impl Display for Transition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Transition::Classic => f.write_str("CLASSIC"),
Transition::Fixed => f.write_str("FIXED"),
Transition::Every10Lines => f.write_str("EVERY 10 LINES"),
Transition::Every4Lines => f.write_str("EVERY 4 LINES"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
39 changes: 34 additions & 5 deletions src/game/tv_system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{fmt::Display, time::Duration};

use num_traits::FromPrimitive;

Expand All @@ -10,12 +10,32 @@ pub enum TVSystem {
}

impl TVSystem {
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_has_prev(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 - 1).is_some()
}

pub fn enum_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
pub fn enum_has_next(&self) -> bool {
<Self as FromPrimitive>::from_i64(*self as i64 + 1).is_some()
}

pub fn enum_prev(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 - 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub fn enum_next(&mut self) -> bool {
match FromPrimitive::from_i64(*self as i64 + 1) {
Some(n) => {
*self = n;
true
}
None => false,
}
}

pub const fn ticks_to_duration(&self, ticks: u64) -> Duration {
Expand All @@ -37,3 +57,12 @@ impl TVSystem {
}
}
}

impl Display for TVSystem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TVSystem::NTSC => f.write_str("NTSC"),
TVSystem::PAL => f.write_str("PAL"),
}
}
}
Loading

0 comments on commit a353cc7

Please sign in to comment.