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(
Update,
(
play_audio.run_if(any_component_added::<AudioSource>),
play_audio.run_if(any_component_added::<AudioSource>()),
audio_trigger.run_if(on_event::<AudioEvent>()),
control_volume.run_if(resource_changed::<AudioVolume>()),
toggle_volume.run_if(just_pressed(KeyCode::M)),

@ -52,22 +52,22 @@ impl Plugin for Display3dPlugin {
load_assets
.run_if(in_state(GameState::Loading))
.run_if(on_event::<AssetEvent<Tweaks>>()),
hydrate_camera.run_if(any_component_added::<Camera3d>),
set_piece_model.run_if(any_component_added::<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::<TilesComponent>),
set_valid_move_model.run_if(any_component_added::<game::ValidMove>),
set_tile_hitbox.run_if(any_component_added::<game::Tile>),
set_piece_position.run_if(any_component_changed::<BoardIndex>),
hydrate_camera.run_if(any_component_added::<Camera3d>()),
set_piece_model.run_if(any_component_added::<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::<TilesComponent>()),
set_valid_move_model.run_if(any_component_added::<game::ValidMove>()),
set_tile_hitbox.run_if(any_component_added::<game::Tile>()),
set_piece_position.run_if(any_component_changed::<BoardIndex>()),
set_piece_texture
.run_if(any_component_changed::<Side>)
.run_if(any_component_changed::<Side>())
.run_if(resource_exists::<tweak::GameTweaks>()),
select
.run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d))
.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>()),
switch_sides
.run_if(in_state(GameState::Play))
@ -76,12 +76,12 @@ impl Plugin for Display3dPlugin {
.run_if(on_event::<AssetEvent<Tweaks>>())
.run_if(resource_exists::<tweak::GameTweaks>()),
scale_lighting.run_if(
any_component_added::<DirectionalLight>
.or_else(any_component_added::<SpotLight>)
.or_else(any_component_added::<PointLight>)
any_component_added::<DirectionalLight>()
.or_else(any_component_added::<SpotLight>())
.or_else(any_component_added::<PointLight>())
.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>()),
skip_animation
.run_if(just_pressed(KeyCode::Return))

@ -23,12 +23,12 @@ impl Plugin for GamePlugin {
update_board
.run_if(on_event::<Move>())
.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)),
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>()),
manage_score.run_if(any_component_added::<Captured>),
manage_score.run_if(any_component_added::<Captured>()),
check_endgame.run_if(resource_changed::<Board>()),
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
// Updated for as long as there is scrolling text
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
// 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 {
!q.is_empty()
pub(crate) fn any_component_changed<C: Component>(
) -> 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 {
!q.is_empty()
pub(crate) fn any_component_added<C: Component>() -> impl Fn(Query<Entity, Added<C>>) -> bool + Clone
{
|q: Query<Entity, Added<C>>| -> bool { !q.is_empty() }
}
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
T: Copy + Eq + Hash + Send + Sync + 'static
T: Copy + Eq + Hash + Send + Sync + 'static,
{
Box::new(move |buttons: Res<Input<T>>| -> bool {
buttons.just_pressed(button)
})
Box::new(move |buttons: Res<Input<T>>| -> bool { buttons.just_pressed(button) })
}

@ -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::<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| {
// 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<State<GameState>>,
mut next_menu_state: ResMut<NextState<MenuState>>,
@ -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"),
}
}
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(
state_exists::<TutorialState>().and_then(
// A piece changes sides
any_component_changed::<game::Side>
any_component_changed::<game::Side>()
// 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
.or_else(any_component_removed::<game::Selected>())
// TEMP: The user hits 'enter'

@ -10,12 +10,12 @@ impl Plugin for UiPlugin {
Update,
(
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(
on_event::<AssetEvent<tweak::Tweaks>>()
.or_else(any_component_changed::<Window>)
.or_else(any_component_changed::<Window>())
.and_then(resource_exists::<tweak::GameTweaks>()),
),
),

Loading…
Cancel
Save