Separate unrelated code to submodules in compute/sub-state examples (#13188)
Large part of the code is UI spawning which contributes nothing to the code example.
This commit is contained in:
		
							parent
							
								
									6ca1b0728a
								
							
						
					
					
						commit
						777bb8cfef
					
				| @ -18,6 +18,8 @@ | |||||||
| 
 | 
 | ||||||
| use bevy::prelude::*; | use bevy::prelude::*; | ||||||
| 
 | 
 | ||||||
|  | use ui::*; | ||||||
|  | 
 | ||||||
| // To begin, we want to define our state objects.
 | // To begin, we want to define our state objects.
 | ||||||
| #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] | #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] | ||||||
| enum AppState { | enum AppState { | ||||||
| @ -221,30 +223,158 @@ fn main() { | |||||||
|         .run(); |         .run(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | fn menu( | ||||||
|  |     mut next_state: ResMut<NextState<AppState>>, | ||||||
|  |     tutorial_state: Res<State<TutorialState>>, | ||||||
|  |     mut next_tutorial: ResMut<NextState<TutorialState>>, | ||||||
|  |     mut interaction_query: Query< | ||||||
|  |         (&Interaction, &mut UiImage, &MenuButton), | ||||||
|  |         (Changed<Interaction>, With<Button>), | ||||||
|  |     >, | ||||||
|  | ) { | ||||||
|  |     for (interaction, mut image, menu_button) in &mut interaction_query { | ||||||
|  |         let color = &mut image.color; | ||||||
|  |         match *interaction { | ||||||
|  |             Interaction::Pressed => { | ||||||
|  |                 *color = if menu_button == &MenuButton::Tutorial | ||||||
|  |                     && tutorial_state.get() == &TutorialState::Active | ||||||
|  |                 { | ||||||
|  |                     PRESSED_ACTIVE_BUTTON | ||||||
|  |                 } else { | ||||||
|  |                     PRESSED_BUTTON | ||||||
|  |                 }; | ||||||
|  | 
 | ||||||
|  |                 match menu_button { | ||||||
|  |                     MenuButton::Play => next_state.set(AppState::InGame { | ||||||
|  |                         paused: false, | ||||||
|  |                         turbo: false, | ||||||
|  |                     }), | ||||||
|  |                     MenuButton::Tutorial => next_tutorial.set(match tutorial_state.get() { | ||||||
|  |                         TutorialState::Active => TutorialState::Inactive, | ||||||
|  |                         TutorialState::Inactive => TutorialState::Active, | ||||||
|  |                     }), | ||||||
|  |                 }; | ||||||
|  |             } | ||||||
|  |             Interaction::Hovered => { | ||||||
|  |                 if menu_button == &MenuButton::Tutorial | ||||||
|  |                     && tutorial_state.get() == &TutorialState::Active | ||||||
|  |                 { | ||||||
|  |                     *color = HOVERED_ACTIVE_BUTTON; | ||||||
|  |                 } else { | ||||||
|  |                     *color = HOVERED_BUTTON; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             Interaction::None => { | ||||||
|  |                 if menu_button == &MenuButton::Tutorial | ||||||
|  |                     && tutorial_state.get() == &TutorialState::Active | ||||||
|  |                 { | ||||||
|  |                     *color = ACTIVE_BUTTON; | ||||||
|  |                 } else { | ||||||
|  |                     *color = NORMAL_BUTTON; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[derive(Component)] | ||||||
|  | struct StateBound<S: States>(S); | ||||||
|  | 
 | ||||||
|  | fn clear_state_bound_entities<S: States>( | ||||||
|  |     state: S, | ||||||
|  | ) -> impl Fn(Commands, Query<(Entity, &StateBound<S>)>) { | ||||||
|  |     info!("Clearing entities for {state:?}"); | ||||||
|  |     move |mut commands, query| { | ||||||
|  |         for (entity, bound) in &query { | ||||||
|  |             if bound.0 == state { | ||||||
|  |                 commands.entity(entity).despawn_recursive(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn toggle_pause( | ||||||
|  |     input: Res<ButtonInput<KeyCode>>, | ||||||
|  |     current_state: Res<State<AppState>>, | ||||||
|  |     mut next_state: ResMut<NextState<AppState>>, | ||||||
|  | ) { | ||||||
|  |     if input.just_pressed(KeyCode::Space) { | ||||||
|  |         if let AppState::InGame { paused, turbo } = current_state.get() { | ||||||
|  |             next_state.set(AppState::InGame { | ||||||
|  |                 paused: !*paused, | ||||||
|  |                 turbo: *turbo, | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn toggle_turbo( | ||||||
|  |     input: Res<ButtonInput<KeyCode>>, | ||||||
|  |     current_state: Res<State<AppState>>, | ||||||
|  |     mut next_state: ResMut<NextState<AppState>>, | ||||||
|  | ) { | ||||||
|  |     if input.just_pressed(KeyCode::KeyT) { | ||||||
|  |         if let AppState::InGame { paused, turbo } = current_state.get() { | ||||||
|  |             next_state.set(AppState::InGame { | ||||||
|  |                 paused: *paused, | ||||||
|  |                 turbo: !*turbo, | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn quit_to_menu(input: Res<ButtonInput<KeyCode>>, mut next_state: ResMut<NextState<AppState>>) { | ||||||
|  |     if input.just_pressed(KeyCode::Escape) { | ||||||
|  |         next_state.set(AppState::Menu); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /// print when either an `AppState` transition or a `TutorialState` transition happens
 | ||||||
|  | fn log_transitions( | ||||||
|  |     mut transitions: EventReader<StateTransitionEvent<AppState>>, | ||||||
|  |     mut tutorial_transitions: EventReader<StateTransitionEvent<TutorialState>>, | ||||||
|  | ) { | ||||||
|  |     for transition in transitions.read() { | ||||||
|  |         info!( | ||||||
|  |             "transition: {:?} => {:?}", | ||||||
|  |             transition.before, transition.after | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  |     for transition in tutorial_transitions.read() { | ||||||
|  |         info!( | ||||||
|  |             "tutorial transition: {:?} => {:?}", | ||||||
|  |             transition.before, transition.after | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | mod ui { | ||||||
|  |     use crate::*; | ||||||
|  | 
 | ||||||
|     #[derive(Resource)] |     #[derive(Resource)] | ||||||
| struct MenuData { |     pub struct MenuData { | ||||||
|     root_entity: Entity, |         pub root_entity: Entity, | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #[derive(Component, PartialEq, Eq)] |     #[derive(Component, PartialEq, Eq)] | ||||||
| enum MenuButton { |     pub enum MenuButton { | ||||||
|         Play, |         Play, | ||||||
|         Tutorial, |         Tutorial, | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); |     pub const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); | ||||||
| const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25); |     pub const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25); | ||||||
| const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); |     pub const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); | ||||||
| 
 | 
 | ||||||
| const ACTIVE_BUTTON: Color = Color::srgb(0.15, 0.85, 0.15); |     pub const ACTIVE_BUTTON: Color = Color::srgb(0.15, 0.85, 0.15); | ||||||
| const HOVERED_ACTIVE_BUTTON: Color = Color::srgb(0.25, 0.55, 0.25); |     pub const HOVERED_ACTIVE_BUTTON: Color = Color::srgb(0.25, 0.55, 0.25); | ||||||
| const PRESSED_ACTIVE_BUTTON: Color = Color::srgb(0.35, 0.95, 0.35); |     pub const PRESSED_ACTIVE_BUTTON: Color = Color::srgb(0.35, 0.95, 0.35); | ||||||
| 
 | 
 | ||||||
| fn setup(mut commands: Commands) { |     pub fn setup(mut commands: Commands) { | ||||||
|         commands.spawn(Camera2dBundle::default()); |         commands.spawn(Camera2dBundle::default()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn setup_menu(mut commands: Commands, tutorial_state: Res<State<TutorialState>>) { |     pub fn setup_menu(mut commands: Commands, tutorial_state: Res<State<TutorialState>>) { | ||||||
|         let button_entity = commands |         let button_entity = commands | ||||||
|             .spawn(NodeBundle { |             .spawn(NodeBundle { | ||||||
|                 style: Style { |                 style: Style { | ||||||
| @ -325,68 +455,11 @@ fn setup_menu(mut commands: Commands, tutorial_state: Res<State<TutorialState>>) | |||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn menu( |     pub fn cleanup_menu(mut commands: Commands, menu_data: Res<MenuData>) { | ||||||
|     mut next_state: ResMut<NextState<AppState>>, |  | ||||||
|     tutorial_state: Res<State<TutorialState>>, |  | ||||||
|     mut next_tutorial: ResMut<NextState<TutorialState>>, |  | ||||||
|     mut interaction_query: Query< |  | ||||||
|         (&Interaction, &mut UiImage, &MenuButton), |  | ||||||
|         (Changed<Interaction>, With<Button>), |  | ||||||
|     >, |  | ||||||
| ) { |  | ||||||
|     for (interaction, mut image, menu_button) in &mut interaction_query { |  | ||||||
|         let color = &mut image.color; |  | ||||||
|         match *interaction { |  | ||||||
|             Interaction::Pressed => { |  | ||||||
|                 *color = if menu_button == &MenuButton::Tutorial |  | ||||||
|                     && tutorial_state.get() == &TutorialState::Active |  | ||||||
|                 { |  | ||||||
|                     PRESSED_ACTIVE_BUTTON |  | ||||||
|                 } else { |  | ||||||
|                     PRESSED_BUTTON |  | ||||||
|                 }; |  | ||||||
| 
 |  | ||||||
|                 match menu_button { |  | ||||||
|                     MenuButton::Play => next_state.set(AppState::InGame { |  | ||||||
|                         paused: false, |  | ||||||
|                         turbo: false, |  | ||||||
|                     }), |  | ||||||
|                     MenuButton::Tutorial => next_tutorial.set(match tutorial_state.get() { |  | ||||||
|                         TutorialState::Active => TutorialState::Inactive, |  | ||||||
|                         TutorialState::Inactive => TutorialState::Active, |  | ||||||
|                     }), |  | ||||||
|                 }; |  | ||||||
|             } |  | ||||||
|             Interaction::Hovered => { |  | ||||||
|                 if menu_button == &MenuButton::Tutorial |  | ||||||
|                     && tutorial_state.get() == &TutorialState::Active |  | ||||||
|                 { |  | ||||||
|                     *color = HOVERED_ACTIVE_BUTTON; |  | ||||||
|                 } else { |  | ||||||
|                     *color = HOVERED_BUTTON; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|             Interaction::None => { |  | ||||||
|                 if menu_button == &MenuButton::Tutorial |  | ||||||
|                     && tutorial_state.get() == &TutorialState::Active |  | ||||||
|                 { |  | ||||||
|                     *color = ACTIVE_BUTTON; |  | ||||||
|                 } else { |  | ||||||
|                     *color = NORMAL_BUTTON; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn cleanup_menu(mut commands: Commands, menu_data: Res<MenuData>) { |  | ||||||
|         commands.entity(menu_data.root_entity).despawn_recursive(); |         commands.entity(menu_data.root_entity).despawn_recursive(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| #[derive(Component)] |     pub fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { | ||||||
| struct StateBound<S: States>(S); |  | ||||||
| 
 |  | ||||||
| fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { |  | ||||||
|         commands.spawn(( |         commands.spawn(( | ||||||
|             StateBound(InGame), |             StateBound(InGame), | ||||||
|             SpriteBundle { |             SpriteBundle { | ||||||
| @ -396,22 +469,10 @@ fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { | |||||||
|         )); |         )); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn clear_state_bound_entities<S: States>( |  | ||||||
|     state: S, |  | ||||||
| ) -> impl Fn(Commands, Query<(Entity, &StateBound<S>)>) { |  | ||||||
|     info!("Clearing entities for {state:?}"); |  | ||||||
|     move |mut commands, query| { |  | ||||||
|         for (entity, bound) in &query { |  | ||||||
|             if bound.0 == state { |  | ||||||
|                 commands.entity(entity).despawn_recursive(); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
|     const SPEED: f32 = 100.0; |     const SPEED: f32 = 100.0; | ||||||
|     const TURBO_SPEED: f32 = 300.0; |     const TURBO_SPEED: f32 = 300.0; | ||||||
| fn movement( | 
 | ||||||
|  |     pub fn movement( | ||||||
|         time: Res<Time>, |         time: Res<Time>, | ||||||
|         input: Res<ButtonInput<KeyCode>>, |         input: Res<ButtonInput<KeyCode>>, | ||||||
|         turbo: Option<Res<State<TurboMode>>>, |         turbo: Option<Res<State<TurboMode>>>, | ||||||
| @ -440,22 +501,7 @@ fn movement( | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn toggle_pause( |     pub fn setup_paused_screen(mut commands: Commands) { | ||||||
|     input: Res<ButtonInput<KeyCode>>, |  | ||||||
|     current_state: Res<State<AppState>>, |  | ||||||
|     mut next_state: ResMut<NextState<AppState>>, |  | ||||||
| ) { |  | ||||||
|     if input.just_pressed(KeyCode::Space) { |  | ||||||
|         if let AppState::InGame { paused, turbo } = current_state.get() { |  | ||||||
|             next_state.set(AppState::InGame { |  | ||||||
|                 paused: !*paused, |  | ||||||
|                 turbo: *turbo, |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn setup_paused_screen(mut commands: Commands) { |  | ||||||
|         info!("Printing Pause"); |         info!("Printing Pause"); | ||||||
|         commands |         commands | ||||||
|             .spawn(( |             .spawn(( | ||||||
| @ -506,22 +552,7 @@ fn setup_paused_screen(mut commands: Commands) { | |||||||
|             }); |             }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn toggle_turbo( |     pub fn setup_turbo_text(mut commands: Commands) { | ||||||
|     input: Res<ButtonInput<KeyCode>>, |  | ||||||
|     current_state: Res<State<AppState>>, |  | ||||||
|     mut next_state: ResMut<NextState<AppState>>, |  | ||||||
| ) { |  | ||||||
|     if input.just_pressed(KeyCode::KeyT) { |  | ||||||
|         if let AppState::InGame { paused, turbo } = current_state.get() { |  | ||||||
|             next_state.set(AppState::InGame { |  | ||||||
|                 paused: *paused, |  | ||||||
|                 turbo: !*turbo, |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn setup_turbo_text(mut commands: Commands) { |  | ||||||
|         commands |         commands | ||||||
|             .spawn(( |             .spawn(( | ||||||
|                 StateBound(TurboMode), |                 StateBound(TurboMode), | ||||||
| @ -552,13 +583,7 @@ fn setup_turbo_text(mut commands: Commands) { | |||||||
|             }); |             }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn quit_to_menu(input: Res<ButtonInput<KeyCode>>, mut next_state: ResMut<NextState<AppState>>) { |     pub fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) { | ||||||
|     if input.just_pressed(KeyCode::Escape) { |  | ||||||
|         next_state.set(AppState::Menu); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) { |  | ||||||
|         for mut sprite in &mut query { |         for mut sprite in &mut query { | ||||||
|             let new_color = LinearRgba { |             let new_color = LinearRgba { | ||||||
|                 blue: (time.elapsed_seconds() * 0.5).sin() + 2.0, |                 blue: (time.elapsed_seconds() * 0.5).sin() + 2.0, | ||||||
| @ -569,7 +594,7 @@ fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn movement_instructions(mut commands: Commands) { |     pub fn movement_instructions(mut commands: Commands) { | ||||||
|         commands |         commands | ||||||
|             .spawn(( |             .spawn(( | ||||||
|                 StateBound(Tutorial::MovementInstructions), |                 StateBound(Tutorial::MovementInstructions), | ||||||
| @ -626,7 +651,7 @@ fn movement_instructions(mut commands: Commands) { | |||||||
|             }); |             }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| fn pause_instructions(mut commands: Commands) { |     pub fn pause_instructions(mut commands: Commands) { | ||||||
|         commands |         commands | ||||||
|             .spawn(( |             .spawn(( | ||||||
|                 StateBound(Tutorial::PauseInstructions), |                 StateBound(Tutorial::PauseInstructions), | ||||||
| @ -665,22 +690,4 @@ fn pause_instructions(mut commands: Commands) { | |||||||
|                 )); |                 )); | ||||||
|             }); |             }); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| /// print when either an `AppState` transition or a `TutorialState` transition happens
 |  | ||||||
| fn log_transitions( |  | ||||||
|     mut transitions: EventReader<StateTransitionEvent<AppState>>, |  | ||||||
|     mut tutorial_transitions: EventReader<StateTransitionEvent<TutorialState>>, |  | ||||||
| ) { |  | ||||||
|     for transition in transitions.read() { |  | ||||||
|         info!( |  | ||||||
|             "transition: {:?} => {:?}", |  | ||||||
|             transition.before, transition.after |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
|     for transition in tutorial_transitions.read() { |  | ||||||
|         info!( |  | ||||||
|             "tutorial transition: {:?} => {:?}", |  | ||||||
|             transition.before, transition.after |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -9,6 +9,8 @@ | |||||||
| 
 | 
 | ||||||
| use bevy::prelude::*; | use bevy::prelude::*; | ||||||
| 
 | 
 | ||||||
|  | use ui::*; | ||||||
|  | 
 | ||||||
| #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] | #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] | ||||||
| enum AppState { | enum AppState { | ||||||
|     #[default] |     #[default] | ||||||
| @ -61,62 +63,6 @@ fn main() { | |||||||
|         .run(); |         .run(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Resource)] |  | ||||||
| struct MenuData { |  | ||||||
|     button_entity: Entity, |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); |  | ||||||
| const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25); |  | ||||||
| const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); |  | ||||||
| 
 |  | ||||||
| fn setup(mut commands: Commands) { |  | ||||||
|     commands.spawn(Camera2dBundle::default()); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn setup_menu(mut commands: Commands) { |  | ||||||
|     let button_entity = commands |  | ||||||
|         .spawn(NodeBundle { |  | ||||||
|             style: Style { |  | ||||||
|                 // center button
 |  | ||||||
|                 width: Val::Percent(100.), |  | ||||||
|                 height: Val::Percent(100.), |  | ||||||
|                 justify_content: JustifyContent::Center, |  | ||||||
|                 align_items: AlignItems::Center, |  | ||||||
|                 ..default() |  | ||||||
|             }, |  | ||||||
|             ..default() |  | ||||||
|         }) |  | ||||||
|         .with_children(|parent| { |  | ||||||
|             parent |  | ||||||
|                 .spawn(ButtonBundle { |  | ||||||
|                     style: Style { |  | ||||||
|                         width: Val::Px(150.), |  | ||||||
|                         height: Val::Px(65.), |  | ||||||
|                         // horizontally center child text
 |  | ||||||
|                         justify_content: JustifyContent::Center, |  | ||||||
|                         // vertically center child text
 |  | ||||||
|                         align_items: AlignItems::Center, |  | ||||||
|                         ..default() |  | ||||||
|                     }, |  | ||||||
|                     image: UiImage::default().with_color(NORMAL_BUTTON), |  | ||||||
|                     ..default() |  | ||||||
|                 }) |  | ||||||
|                 .with_children(|parent| { |  | ||||||
|                     parent.spawn(TextBundle::from_section( |  | ||||||
|                         "Play", |  | ||||||
|                         TextStyle { |  | ||||||
|                             font_size: 40.0, |  | ||||||
|                             color: Color::srgb(0.9, 0.9, 0.9), |  | ||||||
|                             ..default() |  | ||||||
|                         }, |  | ||||||
|                     )); |  | ||||||
|                 }); |  | ||||||
|         }) |  | ||||||
|         .id(); |  | ||||||
|     commands.insert_resource(MenuData { button_entity }); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn menu( | fn menu( | ||||||
|     mut next_state: ResMut<NextState<AppState>>, |     mut next_state: ResMut<NextState<AppState>>, | ||||||
|     mut interaction_query: Query< |     mut interaction_query: Query< | ||||||
| @ -145,13 +91,6 @@ fn cleanup_menu(mut commands: Commands, menu_data: Res<MenuData>) { | |||||||
|     commands.entity(menu_data.button_entity).despawn_recursive(); |     commands.entity(menu_data.button_entity).despawn_recursive(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { |  | ||||||
|     commands.spawn(SpriteBundle { |  | ||||||
|         texture: asset_server.load("branding/icon.png"), |  | ||||||
|         ..default() |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| const SPEED: f32 = 100.0; | const SPEED: f32 = 100.0; | ||||||
| fn movement( | fn movement( | ||||||
|     time: Res<Time>, |     time: Res<Time>, | ||||||
| @ -218,7 +157,83 @@ fn clear_state_bound_entities<S: States>( | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn setup_paused_screen(mut commands: Commands) { | /// print when an `AppState` transition happens
 | ||||||
|  | fn log_transitions(mut transitions: EventReader<StateTransitionEvent<AppState>>) { | ||||||
|  |     for transition in transitions.read() { | ||||||
|  |         info!( | ||||||
|  |             "transition: {:?} => {:?}", | ||||||
|  |             transition.before, transition.after | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | mod ui { | ||||||
|  |     use crate::*; | ||||||
|  | 
 | ||||||
|  |     #[derive(Resource)] | ||||||
|  |     pub struct MenuData { | ||||||
|  |         pub button_entity: Entity, | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); | ||||||
|  |     pub const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25); | ||||||
|  |     pub const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); | ||||||
|  | 
 | ||||||
|  |     pub fn setup(mut commands: Commands) { | ||||||
|  |         commands.spawn(Camera2dBundle::default()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn setup_menu(mut commands: Commands) { | ||||||
|  |         let button_entity = commands | ||||||
|  |             .spawn(NodeBundle { | ||||||
|  |                 style: Style { | ||||||
|  |                     // center button
 | ||||||
|  |                     width: Val::Percent(100.), | ||||||
|  |                     height: Val::Percent(100.), | ||||||
|  |                     justify_content: JustifyContent::Center, | ||||||
|  |                     align_items: AlignItems::Center, | ||||||
|  |                     ..default() | ||||||
|  |                 }, | ||||||
|  |                 ..default() | ||||||
|  |             }) | ||||||
|  |             .with_children(|parent| { | ||||||
|  |                 parent | ||||||
|  |                     .spawn(ButtonBundle { | ||||||
|  |                         style: Style { | ||||||
|  |                             width: Val::Px(150.), | ||||||
|  |                             height: Val::Px(65.), | ||||||
|  |                             // horizontally center child text
 | ||||||
|  |                             justify_content: JustifyContent::Center, | ||||||
|  |                             // vertically center child text
 | ||||||
|  |                             align_items: AlignItems::Center, | ||||||
|  |                             ..default() | ||||||
|  |                         }, | ||||||
|  |                         image: UiImage::default().with_color(NORMAL_BUTTON), | ||||||
|  |                         ..default() | ||||||
|  |                     }) | ||||||
|  |                     .with_children(|parent| { | ||||||
|  |                         parent.spawn(TextBundle::from_section( | ||||||
|  |                             "Play", | ||||||
|  |                             TextStyle { | ||||||
|  |                                 font_size: 40.0, | ||||||
|  |                                 color: Color::srgb(0.9, 0.9, 0.9), | ||||||
|  |                                 ..default() | ||||||
|  |                             }, | ||||||
|  |                         )); | ||||||
|  |                     }); | ||||||
|  |             }) | ||||||
|  |             .id(); | ||||||
|  |         commands.insert_resource(MenuData { button_entity }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { | ||||||
|  |         commands.spawn(SpriteBundle { | ||||||
|  |             texture: asset_server.load("branding/icon.png"), | ||||||
|  |             ..default() | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn setup_paused_screen(mut commands: Commands) { | ||||||
|         commands |         commands | ||||||
|             .spawn(( |             .spawn(( | ||||||
|                 StateBound(IsPaused::Paused), |                 StateBound(IsPaused::Paused), | ||||||
| @ -263,13 +278,4 @@ fn setup_paused_screen(mut commands: Commands) { | |||||||
|                     }); |                     }); | ||||||
|             }); |             }); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| /// print when an `AppState` transition happens
 |  | ||||||
| fn log_transitions(mut transitions: EventReader<StateTransitionEvent<AppState>>) { |  | ||||||
|     for transition in transitions.read() { |  | ||||||
|         info!( |  | ||||||
|             "transition: {:?} => {:?}", |  | ||||||
|             transition.before, transition.after |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 MiniaczQ
						MiniaczQ