Move the title screen stuff into widgets.rs
This commit is contained in:
@@ -4,7 +4,6 @@ mod machinery;
|
|||||||
mod objects;
|
mod objects;
|
||||||
mod physics;
|
mod physics;
|
||||||
mod resources;
|
mod resources;
|
||||||
mod title_screen;
|
|
||||||
mod widgets;
|
mod widgets;
|
||||||
|
|
||||||
use crate::config::{
|
use crate::config::{
|
||||||
@@ -30,7 +29,7 @@ pub struct AsteroidPlugin;
|
|||||||
impl Plugin for AsteroidPlugin {
|
impl Plugin for AsteroidPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_plugins((
|
app.add_plugins((
|
||||||
title_screen::GameMenuPlugin,
|
widgets::GameMenuPlugin,
|
||||||
widgets::preparation_widget_plugin,
|
widgets::preparation_widget_plugin,
|
||||||
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
||||||
RapierDebugRenderPlugin::default(),
|
RapierDebugRenderPlugin::default(),
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
use crate::GameState;
|
|
||||||
|
|
||||||
use bevy::prelude::*;
|
|
||||||
|
|
||||||
pub struct GameMenuPlugin;
|
|
||||||
|
|
||||||
impl Plugin for GameMenuPlugin {
|
|
||||||
fn build(&self, app: &mut App) {
|
|
||||||
app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu)
|
|
||||||
.add_systems(OnExit(GameState::TitleScreen), despawn_menu)
|
|
||||||
.add_systems(
|
|
||||||
Update,
|
|
||||||
handle_spacebar.run_if(in_state(GameState::TitleScreen)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marker component for the title screen UI entity.
|
|
||||||
// This way, a query for the TitleUI can be used to despawn the title screen
|
|
||||||
#[derive(Component)]
|
|
||||||
struct TitleUI;
|
|
||||||
|
|
||||||
fn spawn_menu(mut commands: Commands) {
|
|
||||||
commands
|
|
||||||
.spawn((
|
|
||||||
TitleUI,
|
|
||||||
Node {
|
|
||||||
flex_direction: FlexDirection::Column,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
))
|
|
||||||
.with_children(|cmds| {
|
|
||||||
cmds.spawn((
|
|
||||||
Text::new("Robert's Bad Asteroids Game"),
|
|
||||||
TextFont::from_font_size(50.0),
|
|
||||||
));
|
|
||||||
cmds.spawn((
|
|
||||||
Text::new("Press space to begin"),
|
|
||||||
TextFont::from_font_size(40.0),
|
|
||||||
));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn despawn_menu(mut commands: Commands, to_despawn: Query<Entity, With<TitleUI>>) {
|
|
||||||
for entity in &to_despawn {
|
|
||||||
commands.entity(entity).despawn();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_spacebar(input: Res<ButtonInput<KeyCode>>, mut game_state: ResMut<NextState<GameState>>) {
|
|
||||||
if input.just_pressed(KeyCode::Space) {
|
|
||||||
game_state.set(GameState::GetReady);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -97,3 +97,54 @@ fn animate_get_ready_widget(
|
|||||||
game_state.set(GameState::Playing);
|
game_state.set(GameState::Playing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct GameMenuPlugin;
|
||||||
|
|
||||||
|
impl Plugin for GameMenuPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu)
|
||||||
|
.add_systems(OnExit(GameState::TitleScreen), despawn_menu)
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
handle_spacebar.run_if(in_state(GameState::TitleScreen)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marker component for the title screen UI entity.
|
||||||
|
// This way, a query for the TitleUI can be used to despawn the title screen
|
||||||
|
#[derive(Component)]
|
||||||
|
struct TitleUI;
|
||||||
|
|
||||||
|
fn spawn_menu(mut commands: Commands) {
|
||||||
|
commands
|
||||||
|
.spawn((
|
||||||
|
TitleUI,
|
||||||
|
Node {
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.with_children(|cmds| {
|
||||||
|
cmds.spawn((
|
||||||
|
Text::new("Robert's Bad Asteroids Game"),
|
||||||
|
TextFont::from_font_size(50.0),
|
||||||
|
));
|
||||||
|
cmds.spawn((
|
||||||
|
Text::new("Press space to begin"),
|
||||||
|
TextFont::from_font_size(40.0),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn despawn_menu(mut commands: Commands, to_despawn: Query<Entity, With<TitleUI>>) {
|
||||||
|
for entity in &to_despawn {
|
||||||
|
commands.entity(entity).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_spacebar(input: Res<ButtonInput<KeyCode>>, mut game_state: ResMut<NextState<GameState>>) {
|
||||||
|
if input.just_pressed(KeyCode::Space) {
|
||||||
|
game_state.set(GameState::GetReady);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user