Skip to content

Commit

Permalink
Add option to enable/disable next piece hint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 21, 2024
1 parent 6fd42cf commit e8706c4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/game/game.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::{drop_speed::DropSpeed, linecap::Linecap, transition::Transition};
use super::{
drop_speed::DropSpeed, linecap::Linecap, next_piece_hint::NextPieceHint, transition::Transition,
};

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct GameConfig {
pub start_level: usize,
pub transition: Transition,
pub linecap: Linecap,
pub drop_speed: DropSpeed,
pub next_piece_hint: NextPieceHint,
}

impl Default for GameConfig {
Expand All @@ -15,6 +18,7 @@ impl Default for GameConfig {
linecap: Linecap::default(),
transition: Transition::default(),
drop_speed: DropSpeed::default(),
next_piece_hint: NextPieceHint::default(),
}
}
}
1 change: 1 addition & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod drop_speed;
pub mod game;
mod level;
pub mod linecap;
pub mod next_piece_hint;
pub mod palette;
pub mod piece;
pub mod player;
Expand Down
28 changes: 28 additions & 0 deletions src/game/next_piece_hint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bevy::prelude::*;
use num_traits::FromPrimitive;

#[derive(Default, Clone, Copy, PartialEq, Eq, FromPrimitive)]
pub enum NextPieceHint {
Off,
#[default]
One,
}

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_next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
}
}

impl Into<Visibility> for NextPieceHint {
fn into(self) -> Visibility {
match self {
NextPieceHint::Off => Visibility::Hidden,
NextPieceHint::One => Visibility::Inherited,
}
}
}
3 changes: 3 additions & 0 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,
game::GameConfig,
next_piece_hint::NextPieceHint,
tick::{EntryDelayTick, FallTick, LineClearTick},
timer::{DelayAutoShiftTimer, GameTimer, PressDownTimer},
};
Expand All @@ -19,6 +20,7 @@ pub enum PlayerState {

#[derive(Resource)]
pub struct PlayerData {
pub next_piece_hint: NextPieceHint,
pub board: Board,
pub game_stopwatch: Stopwatch,
pub game_timer: GameTimer,
Expand All @@ -36,6 +38,7 @@ pub struct PlayerData {
impl PlayerData {
pub fn new(config: GameConfig) -> Self {
Self {
next_piece_hint: config.next_piece_hint,
board: Board::new(config.start_level, config.transition),
game_stopwatch: Stopwatch::new(),
game_timer: GameTimer::default(),
Expand Down
1 change: 1 addition & 0 deletions src/game/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ fn setup_screen(
SquareImageSize::Normal,
player_data.board.get_next_piece().shape(),
),
visibility: player_data.next_piece_hint.into(),
..default()
},
GameEntityMarker,
Expand Down
65 changes: 52 additions & 13 deletions src/game_option_menu/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
app_state::AppState,
audio::plugin::PlaySoundEvent,
controller::Controller,
game::{drop_speed::DropSpeed, game::GameConfig, linecap::Linecap, transition::Transition},
game::{
drop_speed::DropSpeed, game::GameConfig, linecap::Linecap, next_piece_hint::NextPieceHint,
transition::Transition,
},
inputs::{ControllerMapping, PlayerInputs},
level_menu::plugin::LevelMenuData,
logo::{load_logo_images, TETRIS_BITMAP},
Expand Down Expand Up @@ -57,6 +60,7 @@ enum GameOptionMenuSelection {
Transition,
Linecap,
DropSpeed,
NextPieceHint,
ControllerMapping,
BlankLine2,
VideoCategory,
Expand All @@ -71,9 +75,9 @@ enum GameOptionMenuSelection {
impl GameOptionMenuSelection {
pub fn iter() -> std::slice::Iter<'static, GameOptionMenuSelection> {
#[cfg(not(target_arch = "wasm32"))]
type ArrayType = [GameOptionMenuSelection; 14];
type ArrayType = [GameOptionMenuSelection; 15];
#[cfg(target_arch = "wasm32")]
type ArrayType = [GameOptionMenuSelection; 12];
type ArrayType = [GameOptionMenuSelection; 13];
const STATES: ArrayType = [
GameOptionMenuSelection::Tetris,
GameOptionMenuSelection::BlankLine0,
Expand All @@ -82,6 +86,7 @@ impl GameOptionMenuSelection {
GameOptionMenuSelection::Transition,
GameOptionMenuSelection::Linecap,
GameOptionMenuSelection::DropSpeed,
GameOptionMenuSelection::NextPieceHint,
GameOptionMenuSelection::ControllerMapping,
GameOptionMenuSelection::BlankLine2,
GameOptionMenuSelection::VideoCategory,
Expand Down Expand Up @@ -296,6 +301,13 @@ fn update_ui_system(
DropSpeed::Locked => text.sections[1].value = fopt("LOCKED", true, false),
};
}
GameOptionMenuSelection::NextPieceHint => {
text.sections[0].value = fname("NEXT PIECE HINT", NameKind::Option);
match game_option_menu_data.game_config.next_piece_hint {
NextPieceHint::Off => text.sections[1].value = fopt("OFF", false, true),
NextPieceHint::One => text.sections[1].value = fopt("CLASSIC", true, false),
}
}
GameOptionMenuSelection::ControllerMapping => {
text.sections[0].value = fname("CONTROLLER MAPPING", NameKind::Option);
match *controller_mapping {
Expand Down Expand Up @@ -440,11 +452,11 @@ fn handle_input_system(

if player_inputs.right.0 {
if let Some(_) = game_option_menu_data.game_config.transition.enum_next() {
scale_changed = true;
option_changed = true;
}
} else if player_inputs.left.0 {
if let Some(_) = game_option_menu_data.game_config.transition.enum_prev() {
scale_changed = true;
option_changed = true;
}
}
}
Expand All @@ -459,11 +471,11 @@ fn handle_input_system(

if player_inputs.right.0 {
if let Some(_) = game_option_menu_data.game_config.linecap.enum_next() {
scale_changed = true;
option_changed = true;
}
} else if player_inputs.left.0 {
if let Some(_) = game_option_menu_data.game_config.linecap.enum_prev() {
scale_changed = true;
option_changed = true;
}
}
}
Expand All @@ -472,35 +484,62 @@ fn handle_input_system(
game_option_menu_data.selection = GameOptionMenuSelection::Linecap;
selection_changed = true;
} else if player_inputs.down.0 {
game_option_menu_data.selection = GameOptionMenuSelection::ControllerMapping;
game_option_menu_data.selection = GameOptionMenuSelection::NextPieceHint;
selection_changed = true;
}

if player_inputs.right.0 {
if let Some(_) = game_option_menu_data.game_config.drop_speed.enum_next() {
scale_changed = true;
option_changed = true;
}
} else if player_inputs.left.0 {
if let Some(_) = game_option_menu_data.game_config.drop_speed.enum_prev() {
scale_changed = true;
option_changed = true;
}
}
}
GameOptionMenuSelection::ControllerMapping => {
GameOptionMenuSelection::NextPieceHint => {
if player_inputs.up.0 {
game_option_menu_data.selection = GameOptionMenuSelection::DropSpeed;
selection_changed = true;
} else if player_inputs.down.0 {
game_option_menu_data.selection = GameOptionMenuSelection::ControllerMapping;
selection_changed = true;
}

if player_inputs.right.0 {
if let Some(_) = game_option_menu_data
.game_config
.next_piece_hint
.enum_next()
{
option_changed = true;
}
} else if player_inputs.left.0 {
if let Some(_) = game_option_menu_data
.game_config
.next_piece_hint
.enum_prev()
{
option_changed = true;
}
}
}
GameOptionMenuSelection::ControllerMapping => {
if player_inputs.up.0 {
game_option_menu_data.selection = GameOptionMenuSelection::NextPieceHint;
selection_changed = true;
} else if player_inputs.down.0 {
game_option_menu_data.selection = GameOptionMenuSelection::ScaleFactor;
selection_changed = true;
}
if player_inputs.right.0 {
if let Some(_) = controller_mapping.enum_next() {
scale_changed = true;
option_changed = true;
}
} else if player_inputs.left.0 {
if let Some(_) = controller_mapping.enum_prev() {
scale_changed = true;
option_changed = true;
}
}
}
Expand Down

0 comments on commit e8706c4

Please sign in to comment.