Add "start" and "quit" buttons to title screen
I'm stealing the game-over plugin's button handling for use in the main menu. I guess it turns out that my widgets are more generic than intended.
This commit is contained in:
@@ -81,7 +81,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
.add_event::<events::AsteroidDestroy>()
|
.add_event::<events::AsteroidDestroy>()
|
||||||
.add_event::<events::ShipDestroy>()
|
.add_event::<events::ShipDestroy>()
|
||||||
.add_event::<events::BulletDestroy>();
|
.add_event::<events::BulletDestroy>();
|
||||||
app.insert_state(GameState::GameOver);
|
app.insert_state(GameState::TitleScreen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ impl Plugin for PluginGameOver {
|
|||||||
.add_systems(OnExit(GameState::GameOver), despawn::<MarkerGameOver>)
|
.add_systems(OnExit(GameState::GameOver), despawn::<MarkerGameOver>)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
operate_gameover_ui.run_if(in_state(GameState::GameOver)),
|
operate_gameover_ui //.run_if(in_state(GameState::GameOver)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,6 +72,7 @@ struct MarkerGameOver;
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
enum GameOverMenuAction {
|
enum GameOverMenuAction {
|
||||||
ToMainMenu,
|
ToMainMenu,
|
||||||
|
StartGame,
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +96,31 @@ fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Utility function for creating a standard button.
|
||||||
|
fn button_bundle(text: &str) -> impl Bundle {
|
||||||
|
(
|
||||||
|
Button,
|
||||||
|
// TODO: Generic action
|
||||||
|
Node {
|
||||||
|
width: Val::Px(150.0),
|
||||||
|
height: Val::Px(65.0),
|
||||||
|
border: UiRect::all(Val::Px(2.0)),
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
margin: UiRect::all(Val::Px(5.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BorderColor(Color::BLACK),
|
||||||
|
BorderRadius::MAX,
|
||||||
|
BackgroundColor(UI_BUTTON_NORMAL),
|
||||||
|
children![(
|
||||||
|
Text::new(text),
|
||||||
|
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||||
|
TextShadow::default(),
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn spawn_menu(mut commands: Commands) {
|
fn spawn_menu(mut commands: Commands) {
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
@@ -121,6 +147,12 @@ fn spawn_menu(mut commands: Commands) {
|
|||||||
TextColor(Color::srgb(0.7, 0.7, 0.7)),
|
TextColor(Color::srgb(0.7, 0.7, 0.7)),
|
||||||
TextShadow::default(),
|
TextShadow::default(),
|
||||||
));
|
));
|
||||||
|
cmds.spawn((
|
||||||
|
button_bundle("Start Game"), GameOverMenuAction::StartGame
|
||||||
|
));
|
||||||
|
cmds.spawn((
|
||||||
|
button_bundle("Quit"), GameOverMenuAction::Quit
|
||||||
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,48 +205,8 @@ fn spawn_gameover_ui(mut commands: Commands) {
|
|||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
children![
|
children![
|
||||||
(
|
(button_bundle("Main Menu"), GameOverMenuAction::ToMainMenu,),
|
||||||
Button,
|
(button_bundle("Quit"), GameOverMenuAction::Quit),
|
||||||
GameOverMenuAction::ToMainMenu,
|
|
||||||
Node {
|
|
||||||
width: Val::Px(150.0),
|
|
||||||
height: Val::Px(65.0),
|
|
||||||
border: UiRect::all(Val::Px(2.0)),
|
|
||||||
justify_content: JustifyContent::Center,
|
|
||||||
align_items: AlignItems::Center,
|
|
||||||
margin: UiRect::all(Val::Px(5.0)),
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
BorderColor(Color::BLACK),
|
|
||||||
BorderRadius::MAX,
|
|
||||||
BackgroundColor(UI_BUTTON_NORMAL),
|
|
||||||
children![(
|
|
||||||
Text::new("Main Menu"),
|
|
||||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
|
||||||
TextShadow::default(),
|
|
||||||
)]
|
|
||||||
),
|
|
||||||
(
|
|
||||||
Button,
|
|
||||||
GameOverMenuAction::Quit,
|
|
||||||
Node {
|
|
||||||
width: Val::Px(150.0),
|
|
||||||
height: Val::Px(65.0),
|
|
||||||
border: UiRect::all(Val::Px(2.0)),
|
|
||||||
justify_content: JustifyContent::Center,
|
|
||||||
align_items: AlignItems::Center,
|
|
||||||
margin: UiRect::all(Val::Px(5.0)),
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
BorderColor(Color::BLACK),
|
|
||||||
BorderRadius::MAX,
|
|
||||||
BackgroundColor(UI_BUTTON_NORMAL),
|
|
||||||
children![(
|
|
||||||
Text::new("Quit"),
|
|
||||||
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
|
||||||
TextShadow::default(),
|
|
||||||
)]
|
|
||||||
)
|
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -273,6 +265,9 @@ fn operate_gameover_ui(
|
|||||||
GameOverMenuAction::ToMainMenu => {
|
GameOverMenuAction::ToMainMenu => {
|
||||||
game_state.set(GameState::TitleScreen);
|
game_state.set(GameState::TitleScreen);
|
||||||
}
|
}
|
||||||
|
GameOverMenuAction::StartGame => {
|
||||||
|
game_state.set(GameState::Playing);
|
||||||
|
}
|
||||||
GameOverMenuAction::Quit => {
|
GameOverMenuAction::Quit => {
|
||||||
app_exit_events.write(AppExit::Success);
|
app_exit_events.write(AppExit::Success);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user