@ -1,56 +1,48 @@
use bevy ::{
app ::AppExit ,
input ::{ keyboard ::KeyboardInput , ButtonState } ,
} ;
use crate ::prelude ::* ;
use crate ::prelude ::* ;
pub ( crate ) struct MenuPlugin ;
pub ( crate ) struct MenuPlugin ;
impl Plugin for MenuPlugin {
impl Plugin for MenuPlugin {
fn build ( & self , app : & mut App ) {
fn build ( & self , app : & mut App ) {
app . add_systems ( Startup , init_menu_ui )
app . add_state ::< MenuState > ( )
// Initialize menus
. add_systems (
. add_systems (
Update ,
OnExit ( GameState ::Loading ) ,
(
( init_play_menu , init_tutorial_menu , init_endgame_menu ) ,
handle_menu_button ::< GameState > , // Run in all states
handle_menu_quit . run_if ( in_state ( GameState ::Menu ) ) ,
bevy ::window ::close_on_esc . run_if ( in_state ( GameState ::Menu ) ) ,
) ,
)
)
// Manage visible/hidden states
. add_systems (
. add_systems (
OnEnter ( GameState ::Menu ) ,
Update ,
( activate ::< Menu > , activate ::< display3d ::Display3d > ) ,
manage_state_entities ::< MenuState > ( ) . run_if ( state_changed ::< MenuState > ( ) ) ,
)
) ;
. add_systems ( OnExit ( GameState ::Menu ) , deactivate ::< Menu > ) ;
}
}
}
}
#[ derive(Debug, Component) ]
#[ derive(Debug, States, Hash, Default, PartialEq, Eq, Clone, Component) ]
struct Menu ;
enum MenuState {
#[ default ]
#[ derive(Debug, Component) ]
None ,
struct Quit ;
Play ,
Tutorial ,
Endgame ,
}
fn init_menu_ui ( mut commands : Commands ) {
fn init_menu_ui ( mut commands : Commands ) {
commands
commands
. spawn ( (
. spawn ( ( NodeBundle {
Menu ,
style : Style {
NodeBundle {
width : Val ::Percent ( 100.0 ) ,
style : Style {
height : Val ::Percent ( 100.0 ) ,
width : Val ::Percent ( 100.0 ) ,
justify_content : JustifyContent ::Center ,
height : Val ::Percent ( 100.0 ) ,
align_items : AlignItems ::Center ,
justify_content : JustifyContent ::Center ,
flex_direction : FlexDirection ::Column ,
align_items : AlignItems ::Center ,
position_type : PositionType ::Absolute ,
flex_direction : FlexDirection ::Column ,
position_type : PositionType ::Absolute ,
.. default ( )
} ,
background_color : Color ::NONE . into ( ) ,
visibility : Visibility ::Hidden ,
.. default ( )
.. default ( )
} ,
} ,
) )
background_color : Color ::NONE . into ( ) ,
visibility : Visibility ::Hidden ,
.. default ( )
} , ) )
. with_children ( | parent | {
. with_children ( | parent | {
parent . spawn ( TextBundle ::from_section (
parent . spawn ( 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" ,
@ -115,71 +107,36 @@ fn init_menu_ui(mut commands: Commands) {
} ) ;
} ) ;
parent
parent
. spawn ( (
. spawn ( ( ButtonBundle {
Quit ,
style : Style {
ButtonBundle {
padding : UiRect ::all ( Val ::Px ( 5.0 ) ) ,
style : Style {
margin : UiRect ::all ( Val ::Px ( 5.0 ) ) ,
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 ( )
.. default ( )
} ,
} ,
) )
background_color : Color ::ORANGE . with_a ( 0.5 ) . into ( ) ,
.. default ( )
} , ) )
. with_children ( | parent | {
. with_children ( | parent | {
parent . spawn ( (
parent . spawn ( ( TextBundle ::from_section (
Quit ,
"Quit" ,
TextBundle ::from_section (
TextStyle {
"Quit" ,
color : Color ::BLACK ,
TextStyle {
font_size : 32.0 ,
color : Color ::BLACK ,
.. default ( )
font_size : 32.0 ,
} ,
.. default ( )
) , ) ) ;
} ,
) ,
) ) ;
} ) ;
} ) ;
} ) ;
} ) ;
}
}
pub ( crate ) fn handle_menu_button < T : Component + States > (
fn init_play_menu ( mut commands : Commands ) {
events : Query < ( & Interaction , & T ) , ( With < Button > , Changed < Interaction > ) > ,
todo! ( "Play Menu" ) ;
mut next_gs : ResMut < NextState < T > > ,
) {
events . iter ( ) . for_each ( | ( i , gs ) | match i {
Interaction ::Pressed = > {
next_gs . set ( gs . clone ( ) ) ;
}
_ = > ( ) ,
} ) ;
}
}
fn handle_menu_quit (
fn init_tutorial_menu ( mut commands : Commands ) {
events : Query < & Interaction , ( With < Quit > , With < Button > , Changed < Interaction > ) > ,
todo! ( "Tutorial Menu" ) ;
mut writer : EventWriter < AppExit > ,
) {
events
. iter ( )
. filter ( | & interaction | * interaction = = Interaction ::Pressed )
. for_each ( | _ | writer . send ( AppExit ) ) ;
}
}
/// Used to return to the menu
fn init_endgame_menu ( mut commands : Commands ) {
pub ( crate ) fn exit_to_menu (
todo! ( "Endgame Menu" ) ;
mut events : EventReader < KeyboardInput > ,
mut next_state : ResMut < NextState < GameState > > ,
) {
events
. read ( )
. filter (
| KeyboardInput {
key_code , state , ..
} | {
* key_code = = Some ( KeyCode ::Escape ) & & * state = = ButtonState ::Pressed
} ,
)
. for_each ( | _ | {
next_state . set ( GameState ::Menu ) ;
} )
}
}