saving my place working on menus

Also refactored my custom any_component_X methods to act more like other
conditionals
main
Elijah C. Voigt 2 years ago
parent 5fc0706c10
commit 4b5c90f1ba

@ -26,7 +26,7 @@ impl Plugin for AudioPlugin {
app.add_systems( app.add_systems(
Update, Update,
( (
play_audio.run_if(any_component_added::<AudioSource>), play_audio.run_if(any_component_added::<AudioSource>()),
audio_trigger.run_if(on_event::<AudioEvent>()), audio_trigger.run_if(on_event::<AudioEvent>()),
control_volume.run_if(resource_changed::<AudioVolume>()), control_volume.run_if(resource_changed::<AudioVolume>()),
toggle_volume.run_if(just_pressed(KeyCode::M)), toggle_volume.run_if(just_pressed(KeyCode::M)),

@ -52,22 +52,22 @@ impl Plugin for Display3dPlugin {
load_assets load_assets
.run_if(in_state(GameState::Loading)) .run_if(in_state(GameState::Loading))
.run_if(on_event::<AssetEvent<Tweaks>>()), .run_if(on_event::<AssetEvent<Tweaks>>()),
hydrate_camera.run_if(any_component_added::<Camera3d>), hydrate_camera.run_if(any_component_added::<Camera3d>()),
set_piece_model.run_if(any_component_added::<Piece>), set_piece_model.run_if(any_component_added::<Piece>()),
set_piece_model.run_if(any_component_changed::<Piece>), set_piece_model.run_if(any_component_changed::<Piece>()),
set_board_model.run_if(any_component_added::<game::BoardComponent>), set_board_model.run_if(any_component_added::<game::BoardComponent>()),
set_board_model.run_if(any_component_added::<TilesComponent>), set_board_model.run_if(any_component_added::<TilesComponent>()),
set_valid_move_model.run_if(any_component_added::<game::ValidMove>), set_valid_move_model.run_if(any_component_added::<game::ValidMove>()),
set_tile_hitbox.run_if(any_component_added::<game::Tile>), set_tile_hitbox.run_if(any_component_added::<game::Tile>()),
set_piece_position.run_if(any_component_changed::<BoardIndex>), set_piece_position.run_if(any_component_changed::<BoardIndex>()),
set_piece_texture set_piece_texture
.run_if(any_component_changed::<Side>) .run_if(any_component_changed::<Side>())
.run_if(resource_exists::<tweak::GameTweaks>()), .run_if(resource_exists::<tweak::GameTweaks>()),
select select
.run_if(in_state(GameState::Play)) .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d)) .run_if(in_state(DisplayState::Display3d))
.run_if(on_event::<MouseButtonInput>()), .run_if(on_event::<MouseButtonInput>()),
pick_up.run_if(any_component_added::<game::Selected>), pick_up.run_if(any_component_added::<game::Selected>()),
put_down.run_if(any_component_removed::<game::Selected>()), put_down.run_if(any_component_removed::<game::Selected>()),
switch_sides switch_sides
.run_if(in_state(GameState::Play)) .run_if(in_state(GameState::Play))
@ -76,12 +76,12 @@ impl Plugin for Display3dPlugin {
.run_if(on_event::<AssetEvent<Tweaks>>()) .run_if(on_event::<AssetEvent<Tweaks>>())
.run_if(resource_exists::<tweak::GameTweaks>()), .run_if(resource_exists::<tweak::GameTweaks>()),
scale_lighting.run_if( scale_lighting.run_if(
any_component_added::<DirectionalLight> any_component_added::<DirectionalLight>()
.or_else(any_component_added::<SpotLight>) .or_else(any_component_added::<SpotLight>())
.or_else(any_component_added::<PointLight>) .or_else(any_component_added::<PointLight>())
.or_else(on_event::<AssetEvent<Tweaks>>()), .or_else(on_event::<AssetEvent<Tweaks>>()),
), ),
setup_capture_piece.run_if(any_component_changed::<Handle<StandardMaterial>>), setup_capture_piece.run_if(any_component_changed::<Handle<StandardMaterial>>()),
capture_piece.run_if(any_with_component::<game::Captured>()), capture_piece.run_if(any_with_component::<game::Captured>()),
skip_animation skip_animation
.run_if(just_pressed(KeyCode::Return)) .run_if(just_pressed(KeyCode::Return))

@ -23,12 +23,12 @@ impl Plugin for GamePlugin {
update_board update_board
.run_if(on_event::<Move>()) .run_if(on_event::<Move>())
.after(handle_selection), .after(handle_selection),
set_side.run_if(any_component_changed::<BoardIndex>), set_side.run_if(any_component_changed::<BoardIndex>()),
cancel_place.run_if(just_pressed(MouseButton::Right)), cancel_place.run_if(just_pressed(MouseButton::Right)),
handle_selection.run_if(on_event::<Selection>()), handle_selection.run_if(on_event::<Selection>()),
show_valid_moves.run_if(any_component_added::<Selected>), show_valid_moves.run_if(any_component_added::<Selected>()),
hide_valid_moves.run_if(any_component_removed::<Selected>()), hide_valid_moves.run_if(any_component_removed::<Selected>()),
manage_score.run_if(any_component_added::<Captured>), manage_score.run_if(any_component_added::<Captured>()),
check_endgame.run_if(resource_changed::<Board>()), check_endgame.run_if(resource_changed::<Board>()),
reset_game.run_if(just_pressed(KeyCode::R)), reset_game.run_if(just_pressed(KeyCode::R)),
), ),

@ -23,7 +23,8 @@ impl Plugin for IntroPlugin {
// Started when the TextScrollAnimation component is added to the parent entity // Started when the TextScrollAnimation component is added to the parent entity
// Updated for as long as there is scrolling text // Updated for as long as there is scrolling text
manage_scroll_text_animation.run_if( manage_scroll_text_animation.run_if(
any_component_added::<ui::TextScrollAnimation>.or_else(just_pressed(KeyCode::Return)), any_component_added::<ui::TextScrollAnimation>()
.or_else(just_pressed(KeyCode::Return)),
), ),
// Play intro manages playing the intro of each individual paragraph // Play intro manages playing the intro of each individual paragraph
// Runs every time the TextScroll component (managed by manage_scroll_text_animation) is updated // Runs every time the TextScroll component (managed by manage_scroll_text_animation) is updated

@ -123,12 +123,14 @@ where
} }
} }
pub(crate) fn any_component_changed<C: Component>(q: Query<Entity, Changed<C>>) -> bool { pub(crate) fn any_component_changed<C: Component>(
!q.is_empty() ) -> impl Fn(Query<Entity, Changed<C>>) -> bool + Clone {
|q: Query<Entity, Changed<C>>| -> bool { !q.is_empty() }
} }
pub(crate) fn any_component_added<C: Component>(q: Query<Entity, Added<C>>) -> bool { pub(crate) fn any_component_added<C: Component>() -> impl Fn(Query<Entity, Added<C>>) -> bool + Clone
!q.is_empty() {
|q: Query<Entity, Added<C>>| -> bool { !q.is_empty() }
} }
pub(crate) fn _any_component_added_or_changed<C: Component>( pub(crate) fn _any_component_added_or_changed<C: Component>(
@ -163,12 +165,9 @@ fn set_window_icon(
} }
} }
pub(crate) fn just_pressed<T>(button: T) -> impl FnMut(Res<Input<T>>) -> bool + Clone pub(crate) fn just_pressed<T>(button: T) -> impl FnMut(Res<Input<T>>) -> bool + Clone
where where
T: Copy + Eq + Hash + Send + Sync + 'static T: Copy + Eq + Hash + Send + Sync + 'static,
{ {
Box::new(move |buttons: Res<Input<T>>| -> bool { Box::new(move |buttons: Res<Input<T>>| -> bool { buttons.just_pressed(button) })
buttons.just_pressed(button)
})
} }

@ -1,3 +1,5 @@
use bevy::app::AppExit;
use crate::prelude::*; use crate::prelude::*;
use self::tutorial::TutorialState; use self::tutorial::TutorialState;
@ -19,8 +21,9 @@ impl Plugin for MenuPlugin {
) )
.add_systems( .add_systems(
Update, Update,
set_menu_state.run_if(just_pressed(KeyCode::Escape)) handle_button_press.run_if(any_component_changed::<Interaction>()),
); )
.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| { .with_children(|parent| {
// Title // Title
parent.spawn(( parent.spawn((TextBundle::from_section(
TextBundle::from_section(
"M A R T I A N C H E S S", "M A R T I A N C H E S S",
TextStyle { TextStyle {
font_size: 48.0, font_size: 48.0,
color: Color::ORANGE_RED, color: Color::ORANGE_RED,
..default() ..default()
}, },
), ),));
));
// Continue button // Continue button
parent parent
@ -91,16 +92,14 @@ fn init_play_menu(mut commands: Commands) {
}, },
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((TextBundle::from_section(
TextBundle::from_section(
"Continue", "Continue",
TextStyle { TextStyle {
color: Color::BLACK, color: Color::BLACK,
font_size: 32.0, font_size: 32.0,
..default() ..default()
}, },
), ),));
));
}); });
// Tutorial button // Tutorial button
@ -118,16 +117,14 @@ fn init_play_menu(mut commands: Commands) {
}, },
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((TextBundle::from_section(
TextBundle::from_section(
"Tutorial", "Tutorial",
TextStyle { TextStyle {
color: Color::BLACK, color: Color::BLACK,
font_size: 32.0, font_size: 32.0,
..default() ..default()
}, },
), ),));
));
}); });
// Credits button // Credits button
@ -145,16 +142,14 @@ fn init_play_menu(mut commands: Commands) {
}, },
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((TextBundle::from_section(
TextBundle::from_section(
"Credits", "Credits",
TextStyle { TextStyle {
color: Color::BLACK, color: Color::BLACK,
font_size: 32.0, font_size: 32.0,
..default() ..default()
}, },
), ),));
));
}); });
// Quit button // Quit button
@ -169,7 +164,8 @@ fn init_play_menu(mut commands: Commands) {
}, },
background_color: Color::ORANGE.with_a(0.5).into(), background_color: Color::ORANGE.with_a(0.5).into(),
..default() ..default()
},)) },
))
.with_children(|parent| { .with_children(|parent| {
parent.spawn((TextBundle::from_section( parent.spawn((TextBundle::from_section(
"Quit", "Quit",
@ -218,16 +214,14 @@ fn init_tutorial_menu(mut commands: Commands) {
}, },
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((TextBundle::from_section(
TextBundle::from_section(
"Continue Game", "Continue Game",
TextStyle { TextStyle {
color: Color::BLACK, color: Color::BLACK,
font_size: 32.0, font_size: 32.0,
..default() ..default()
}, },
), ),));
));
}); });
// Quit button // Quit button
@ -242,7 +236,8 @@ fn init_tutorial_menu(mut commands: Commands) {
}, },
background_color: Color::ORANGE.with_a(0.5).into(), background_color: Color::ORANGE.with_a(0.5).into(),
..default() ..default()
},)) },
))
.with_children(|parent| { .with_children(|parent| {
parent.spawn((TextBundle::from_section( parent.spawn((TextBundle::from_section(
"Restart", "Restart",
@ -291,16 +286,14 @@ fn init_endgame_menu(mut commands: Commands) {
}, },
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((TextBundle::from_section(
TextBundle::from_section(
"New Game", "New Game",
TextStyle { TextStyle {
color: Color::BLACK, color: Color::BLACK,
font_size: 32.0, font_size: 32.0,
..default() ..default()
}, },
), ),));
));
}); });
// Quit button // Quit button
@ -315,7 +308,8 @@ fn init_endgame_menu(mut commands: Commands) {
}, },
background_color: Color::ORANGE.with_a(0.5).into(), background_color: Color::ORANGE.with_a(0.5).into(),
..default() ..default()
},)) },
))
.with_children(|parent| { .with_children(|parent| {
parent.spawn((TextBundle::from_section( parent.spawn((TextBundle::from_section(
"Quit", "Quit",
@ -329,7 +323,6 @@ fn init_endgame_menu(mut commands: Commands) {
}); });
} }
fn set_menu_state( fn set_menu_state(
game_state: Res<State<GameState>>, game_state: Res<State<GameState>>,
mut next_menu_state: ResMut<NextState<MenuState>>, mut next_menu_state: ResMut<NextState<MenuState>>,
@ -339,6 +332,27 @@ fn set_menu_state(
GameState::Play => next_menu_state.set(MenuState::Play), GameState::Play => next_menu_state.set(MenuState::Play),
GameState::Endgame => next_menu_state.set(MenuState::Endgame), GameState::Endgame => next_menu_state.set(MenuState::Endgame),
GameState::Intro => error!("Should skip to GameState::Play"), 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"),
}
} }
fn handle_button_press(
events: Query<(&Interaction, &ButtonAction), Changed<Interaction>>,
mut next_game_state: ResMut<NextState<GameState>>,
mut next_tutorial_state: ResMut<NextState<TutorialState>>,
mut next_menu_state: ResMut<NextState<MenuState>>,
mut app_exit_events: EventWriter<AppExit>,
) {
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!"),
});
} }

@ -21,9 +21,9 @@ impl Plugin for TutorialPlugin {
step.run_if( step.run_if(
state_exists::<TutorialState>().and_then( state_exists::<TutorialState>().and_then(
// A piece changes sides // A piece changes sides
any_component_changed::<game::Side> any_component_changed::<game::Side>()
// When a piece is selected, we // When a piece is selected, we
.or_else(any_component_added::<game::Selected>) .or_else(any_component_added::<game::Selected>())
// A piece is de-selected // A piece is de-selected
.or_else(any_component_removed::<game::Selected>()) .or_else(any_component_removed::<game::Selected>())
// TEMP: The user hits 'enter' // TEMP: The user hits 'enter'

@ -10,12 +10,12 @@ impl Plugin for UiPlugin {
Update, Update,
( (
manage_cursor.run_if( manage_cursor.run_if(
any_component_changed::<Interaction>.or_else(state_changed::<GameState>()), any_component_changed::<Interaction>().or_else(state_changed::<GameState>()),
), ),
interactive_button.run_if(any_component_changed::<Interaction>), interactive_button.run_if(any_component_changed::<Interaction>()),
scale_ui.run_if( scale_ui.run_if(
on_event::<AssetEvent<tweak::Tweaks>>() on_event::<AssetEvent<tweak::Tweaks>>()
.or_else(any_component_changed::<Window>) .or_else(any_component_changed::<Window>())
.and_then(resource_exists::<tweak::GameTweaks>()), .and_then(resource_exists::<tweak::GameTweaks>()),
), ),
), ),

Loading…
Cancel
Save