From 1ffb34b93b3f70598522c4cea2e86800a5dba900 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Thu, 29 Feb 2024 19:41:25 -0800 Subject: [PATCH] Well I got endgame working, then I kept going Probably broke something, but I'm working on escape handling in different contexts. Mostly going to the "expected" menu. --- src/game.rs | 102 ++++++++++++++++++++++------ src/menu.rs | 192 ++++++++++------------------------------------------ 2 files changed, 117 insertions(+), 177 deletions(-) diff --git a/src/game.rs b/src/game.rs index afbe5c5..80b8982 100644 --- a/src/game.rs +++ b/src/game.rs @@ -34,7 +34,7 @@ impl Plugin for GamePlugin { reset_game.run_if(just_pressed(KeyCode::R)), ), ) - .add_systems(OnEnter(GameState::Endgame), set_endgame) + .add_systems(OnEnter(GameState::Endgame), set_endgame.after(manage_score)) .add_systems(OnExit(GameState::Endgame), clear_endgame) .add_systems( PreUpdate, @@ -246,8 +246,8 @@ impl Board { inner: vec![ vec![ Some(Queen), - Some(Queen), - Some(Drone), + None, // Some(Queen), + None, // Some(Drone), None, None, None, @@ -255,24 +255,20 @@ impl Board { None, ], vec![ - Some(Queen), - Some(Drone), - Some(Pawn), - None, - None, - Some(Pawn), - Some(Pawn), - Some(Drone), + None, // Some(Queen), + None, // Some(Drone), + None, // Some(Pawn), + None, None, None, // Some(Pawn), + None, // Some(Pawn), + None, // Some(Drone), ], vec![ - Some(Drone), - Some(Pawn), - Some(Pawn), - None, - None, - Some(Pawn), - Some(Drone), - Some(Queen), + None, // Some(Drone), + None, // Some(Pawn), + None, // Some(Pawn), + None, None, None, // Some(Pawn), + None, // Some(Drone), + None, // Some(Queen), ], vec![ None, @@ -280,8 +276,8 @@ impl Board { None, None, None, - Some(Drone), - Some(Queen), + None, // Some(Drone), + None, // Some(Queen), Some(Queen), ], ], @@ -640,6 +636,70 @@ fn set_endgame(score: Res, mut commands: Commands) { ..default() }, )); + + parent + .spawn(( + Endgame, + NodeBundle { + style: Style { + flex_direction: FlexDirection::Row, + ..default() + }, + ..default() + }, + )) + .with_children(|parent| { + parent + .spawn(( + menu::ButtonAction(GameState::Restart), + menu::ButtonAction(menu::MenuState::None), + ButtonBundle { + style: Style { + padding: UiRect::all(Val::Px(5.0)), + margin: UiRect::all(Val::Px(5.0)), + ..default() + }, + background_color: Color::ORANGE.with_a(0.5).into(), + ..default() + }, + )) + .with_children(|parent| { + parent.spawn((TextBundle::from_section( + "New Game", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); + }); + + // Quit button + parent + .spawn(( + menu::ButtonAction(menu::MenuState::None), + menu::ButtonAction(GameState::Quit), + ButtonBundle { + style: Style { + padding: UiRect::all(Val::Px(5.0)), + margin: UiRect::all(Val::Px(5.0)), + ..default() + }, + background_color: Color::ORANGE.with_a(0.5).into(), + ..default() + }, + )) + .with_children(|parent| { + parent.spawn((TextBundle::from_section( + "Quit", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); + }); + }); }); } diff --git a/src/menu.rs b/src/menu.rs index 8d581a8..09327ec 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -9,8 +9,9 @@ impl Plugin for MenuPlugin { app.add_state::() // Initialize menus .add_systems( + // TODO: move this to game.rs or display3d.rs OnExit(GameState::Loading), - (init_play_menu, init_tutorial_menu, init_endgame_menu), + init_play_menu, ) // Manage visible/hidden states .add_systems( @@ -26,7 +27,7 @@ impl Plugin for MenuPlugin { ) .run_if(any_component_changed::()), ) - .add_systems(Update, set_menu_state.run_if(just_pressed(KeyCode::Escape))); + .add_systems(Update, handle_escape.run_if(just_pressed(KeyCode::Escape))); } } @@ -180,165 +181,44 @@ fn init_play_menu(mut commands: Commands) { }); } -fn init_tutorial_menu(mut commands: Commands) { - commands - .spawn(( - MenuState::Tutorial, - NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - flex_direction: FlexDirection::Column, - position_type: PositionType::Absolute, - ..default() - }, - background_color: Color::NONE.into(), - visibility: Visibility::Hidden, - ..default() - }, - )) - .with_children(|parent| { - // Continue button - parent - .spawn(( - ButtonAction(GameState::Play), - ButtonAction(MenuState::None), - ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - }, - )) - .with_children(|parent| { - parent.spawn((TextBundle::from_section( - "Continue Game", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ),)); - }); - - // Quit button - parent - .spawn(( - ButtonAction(GameState::Restart), - ButtonAction(MenuState::None), - ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - }, - )) - .with_children(|parent| { - parent.spawn((TextBundle::from_section( - "Restart", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ),)); - }); - }); -} - -fn init_endgame_menu(mut commands: Commands) { - commands - .spawn(( - MenuState::Endgame, - NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - flex_direction: FlexDirection::Column, - position_type: PositionType::Absolute, - ..default() - }, - background_color: Color::NONE.into(), - visibility: Visibility::Hidden, - ..default() - }, - )) - .with_children(|parent| { - // New Game button - parent - .spawn(( - ButtonAction(GameState::Restart), - ButtonAction(MenuState::None), - ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - }, - )) - .with_children(|parent| { - parent.spawn((TextBundle::from_section( - "New Game", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ),)); - }); - - // Quit button - parent - .spawn(( - ButtonAction(MenuState::None), - ButtonAction(GameState::Quit), - ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - }, - )) - .with_children(|parent| { - parent.spawn((TextBundle::from_section( - "Quit", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ),)); - }); - }); -} - -fn set_menu_state( +fn handle_escape( game_state: Res>, + tutorial_state: Res>, + mut next_game_state: ResMut>, mut next_menu_state: ResMut>, + mut next_tutorial_state: ResMut>, ) { match game_state.get() { GameState::Loading => error!("No menu while loading!"), - GameState::Play => next_menu_state.set(MenuState::Play), - GameState::Endgame => next_menu_state.set(MenuState::Endgame), - GameState::Intro => error!("Should skip to GameState::Play"), - GameState::Credits => error!("Should pop back to GameState::Play"), - GameState::Restart | GameState::Quit => panic!("This shouldn't be possible!"), + // State(Play): ... + GameState::Play => { + match tutorial_state.get() { + // State(Tutorial::None): Escape -> Play Menu + TutorialState::None => { + next_menu_state.set(MenuState::Play); + } + // State(Tutorial::*): Escape -> Quit Tutorial + _ => { + next_tutorial_state.set(TutorialState::None); + } + } + } + // State(Endgame): Escape -> Quit game + GameState::Endgame => { + next_game_state.set(GameState::Quit); + } + // State(Credits): Escape -> Play + Menu + GameState::Credits => { + next_game_state.set(GameState::Play); + next_menu_state.set(MenuState::Play); + } + // State(Intro): Escape -> Play + GameState::Intro => { + next_game_state.set(GameState::Play); + } + GameState::Restart | GameState::Quit => { + panic!("This shouldn't be possible!"); + } } }