From 4b5c90f1ba833711ab123774843b4e93becdccfa Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 28 Feb 2024 20:57:34 -0800 Subject: [PATCH] saving my place working on menus Also refactored my custom any_component_X methods to act more like other conditionals --- src/audio.rs | 2 +- src/display3d.rs | 28 ++++---- src/game.rs | 6 +- src/intro.rs | 3 +- src/main.rs | 21 +++--- src/menu.rs | 180 +++++++++++++++++++++++++---------------------- src/tutorial.rs | 4 +- src/ui.rs | 6 +- 8 files changed, 132 insertions(+), 118 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index 3c105f6..20cf814 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -26,7 +26,7 @@ impl Plugin for AudioPlugin { app.add_systems( Update, ( - play_audio.run_if(any_component_added::), + play_audio.run_if(any_component_added::()), audio_trigger.run_if(on_event::()), control_volume.run_if(resource_changed::()), toggle_volume.run_if(just_pressed(KeyCode::M)), diff --git a/src/display3d.rs b/src/display3d.rs index 641175a..84f5817 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -52,22 +52,22 @@ impl Plugin for Display3dPlugin { load_assets .run_if(in_state(GameState::Loading)) .run_if(on_event::>()), - hydrate_camera.run_if(any_component_added::), - set_piece_model.run_if(any_component_added::), - set_piece_model.run_if(any_component_changed::), - set_board_model.run_if(any_component_added::), - set_board_model.run_if(any_component_added::), - set_valid_move_model.run_if(any_component_added::), - set_tile_hitbox.run_if(any_component_added::), - set_piece_position.run_if(any_component_changed::), + hydrate_camera.run_if(any_component_added::()), + set_piece_model.run_if(any_component_added::()), + set_piece_model.run_if(any_component_changed::()), + set_board_model.run_if(any_component_added::()), + set_board_model.run_if(any_component_added::()), + set_valid_move_model.run_if(any_component_added::()), + set_tile_hitbox.run_if(any_component_added::()), + set_piece_position.run_if(any_component_changed::()), set_piece_texture - .run_if(any_component_changed::) + .run_if(any_component_changed::()) .run_if(resource_exists::()), select .run_if(in_state(GameState::Play)) .run_if(in_state(DisplayState::Display3d)) .run_if(on_event::()), - pick_up.run_if(any_component_added::), + pick_up.run_if(any_component_added::()), put_down.run_if(any_component_removed::()), switch_sides .run_if(in_state(GameState::Play)) @@ -76,12 +76,12 @@ impl Plugin for Display3dPlugin { .run_if(on_event::>()) .run_if(resource_exists::()), scale_lighting.run_if( - any_component_added:: - .or_else(any_component_added::) - .or_else(any_component_added::) + any_component_added::() + .or_else(any_component_added::()) + .or_else(any_component_added::()) .or_else(on_event::>()), ), - setup_capture_piece.run_if(any_component_changed::>), + setup_capture_piece.run_if(any_component_changed::>()), capture_piece.run_if(any_with_component::()), skip_animation .run_if(just_pressed(KeyCode::Return)) diff --git a/src/game.rs b/src/game.rs index 5c577a8..2a6654b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -23,12 +23,12 @@ impl Plugin for GamePlugin { update_board .run_if(on_event::()) .after(handle_selection), - set_side.run_if(any_component_changed::), + set_side.run_if(any_component_changed::()), cancel_place.run_if(just_pressed(MouseButton::Right)), handle_selection.run_if(on_event::()), - show_valid_moves.run_if(any_component_added::), + show_valid_moves.run_if(any_component_added::()), hide_valid_moves.run_if(any_component_removed::()), - manage_score.run_if(any_component_added::), + manage_score.run_if(any_component_added::()), check_endgame.run_if(resource_changed::()), reset_game.run_if(just_pressed(KeyCode::R)), ), diff --git a/src/intro.rs b/src/intro.rs index 7f7249d..be3b58e 100644 --- a/src/intro.rs +++ b/src/intro.rs @@ -23,7 +23,8 @@ impl Plugin for IntroPlugin { // Started when the TextScrollAnimation component is added to the parent entity // Updated for as long as there is scrolling text manage_scroll_text_animation.run_if( - any_component_added::.or_else(just_pressed(KeyCode::Return)), + any_component_added::() + .or_else(just_pressed(KeyCode::Return)), ), // Play intro manages playing the intro of each individual paragraph // Runs every time the TextScroll component (managed by manage_scroll_text_animation) is updated diff --git a/src/main.rs b/src/main.rs index 6f2107e..6b7c051 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,12 +123,14 @@ where } } -pub(crate) fn any_component_changed(q: Query>) -> bool { - !q.is_empty() +pub(crate) fn any_component_changed( +) -> impl Fn(Query>) -> bool + Clone { + |q: Query>| -> bool { !q.is_empty() } } -pub(crate) fn any_component_added(q: Query>) -> bool { - !q.is_empty() +pub(crate) fn any_component_added() -> impl Fn(Query>) -> bool + Clone +{ + |q: Query>| -> bool { !q.is_empty() } } pub(crate) fn _any_component_added_or_changed( @@ -163,12 +165,9 @@ fn set_window_icon( } } - -pub(crate) fn just_pressed(button: T) -> impl FnMut(Res>) -> bool + Clone +pub(crate) fn just_pressed(button: T) -> impl FnMut(Res>) -> bool + Clone where - T: Copy + Eq + Hash + Send + Sync + 'static + T: Copy + Eq + Hash + Send + Sync + 'static, { - Box::new(move |buttons: Res>| -> bool { - buttons.just_pressed(button) - }) -} \ No newline at end of file + Box::new(move |buttons: Res>| -> bool { buttons.just_pressed(button) }) +} diff --git a/src/menu.rs b/src/menu.rs index 31b333f..0076ab5 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -1,3 +1,5 @@ +use bevy::app::AppExit; + use crate::prelude::*; use self::tutorial::TutorialState; @@ -19,8 +21,9 @@ impl Plugin for MenuPlugin { ) .add_systems( Update, - set_menu_state.run_if(just_pressed(KeyCode::Escape)) - ); + handle_button_press.run_if(any_component_changed::()), + ) + .add_systems(Update, set_menu_state.run_if(just_pressed(KeyCode::Escape))); } } @@ -65,16 +68,14 @@ fn init_play_menu(mut commands: Commands) { )) .with_children(|parent| { // Title - parent.spawn(( - TextBundle::from_section( - "M A R T I A N C H E S S", - TextStyle { - font_size: 48.0, - color: Color::ORANGE_RED, - ..default() - }, - ), - )); + parent.spawn((TextBundle::from_section( + "M A R T I A N C H E S S", + TextStyle { + font_size: 48.0, + color: Color::ORANGE_RED, + ..default() + }, + ),)); // Continue button parent @@ -91,16 +92,14 @@ fn init_play_menu(mut commands: Commands) { }, )) .with_children(|parent| { - parent.spawn(( - TextBundle::from_section( - "Continue", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ), - )); + parent.spawn((TextBundle::from_section( + "Continue", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); }); // Tutorial button @@ -118,16 +117,14 @@ fn init_play_menu(mut commands: Commands) { }, )) .with_children(|parent| { - parent.spawn(( - TextBundle::from_section( - "Tutorial", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ), - )); + parent.spawn((TextBundle::from_section( + "Tutorial", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); }); // Credits button @@ -145,16 +142,14 @@ fn init_play_menu(mut commands: Commands) { }, )) .with_children(|parent| { - parent.spawn(( - TextBundle::from_section( - "Credits", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ), - )); + parent.spawn((TextBundle::from_section( + "Credits", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); }); // Quit button @@ -162,14 +157,15 @@ fn init_play_menu(mut commands: Commands) { .spawn(( ButtonAction::Quit, ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), + 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() }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - },)) + )) .with_children(|parent| { parent.spawn((TextBundle::from_section( "Quit", @@ -218,16 +214,14 @@ fn init_tutorial_menu(mut commands: Commands) { }, )) .with_children(|parent| { - parent.spawn(( - TextBundle::from_section( - "Continue Game", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ), - )); + parent.spawn((TextBundle::from_section( + "Continue Game", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); }); // Quit button @@ -235,14 +229,15 @@ fn init_tutorial_menu(mut commands: Commands) { .spawn(( ButtonAction::Restart, ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), + 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() }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - },)) + )) .with_children(|parent| { parent.spawn((TextBundle::from_section( "Restart", @@ -291,16 +286,14 @@ fn init_endgame_menu(mut commands: Commands) { }, )) .with_children(|parent| { - parent.spawn(( - TextBundle::from_section( - "New Game", - TextStyle { - color: Color::BLACK, - font_size: 32.0, - ..default() - }, - ), - )); + parent.spawn((TextBundle::from_section( + "New Game", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ),)); }); // Quit button @@ -308,14 +301,15 @@ fn init_endgame_menu(mut commands: Commands) { .spawn(( ButtonAction::Quit, ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), + 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() }, - background_color: Color::ORANGE.with_a(0.5).into(), - ..default() - },)) + )) .with_children(|parent| { parent.spawn((TextBundle::from_section( "Quit", @@ -329,7 +323,6 @@ fn init_endgame_menu(mut commands: Commands) { }); } - fn set_menu_state( game_state: Res>, mut next_menu_state: ResMut>, @@ -339,6 +332,27 @@ fn set_menu_state( 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::Credits => error!("Should pop back to GameState::Play"), } -} \ No newline at end of file +} + +fn handle_button_press( + events: Query<(&Interaction, &ButtonAction), Changed>, + mut next_game_state: ResMut>, + mut next_tutorial_state: ResMut>, + mut next_menu_state: ResMut>, + mut app_exit_events: EventWriter, +) { + events + .iter() + .filter_map(|(interaction, button_action)| { + (*interaction == Interaction::Pressed).then_some(button_action) + }) + .for_each(|button_action| match button_action { + ButtonAction::GameState(gs) => next_game_state.set(gs.clone()), + ButtonAction::MenuState(ms) => next_menu_state.set(ms.clone()), + ButtonAction::TutorialState(ts) => next_tutorial_state.set(ts.clone()), + ButtonAction::Quit => app_exit_events.send(AppExit), + ButtonAction::Restart => panic!("TODO: Restart game!"), + }); +} diff --git a/src/tutorial.rs b/src/tutorial.rs index fc89f75..7369089 100644 --- a/src/tutorial.rs +++ b/src/tutorial.rs @@ -21,9 +21,9 @@ impl Plugin for TutorialPlugin { step.run_if( state_exists::().and_then( // A piece changes sides - any_component_changed:: + any_component_changed::() // When a piece is selected, we - .or_else(any_component_added::) + .or_else(any_component_added::()) // A piece is de-selected .or_else(any_component_removed::()) // TEMP: The user hits 'enter' diff --git a/src/ui.rs b/src/ui.rs index 0217983..74dab46 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -10,12 +10,12 @@ impl Plugin for UiPlugin { Update, ( manage_cursor.run_if( - any_component_changed::.or_else(state_changed::()), + any_component_changed::().or_else(state_changed::()), ), - interactive_button.run_if(any_component_changed::), + interactive_button.run_if(any_component_changed::()), scale_ui.run_if( on_event::>() - .or_else(any_component_changed::) + .or_else(any_component_changed::()) .and_then(resource_exists::()), ), ),