
## Objective A major critique of Bevy at the moment is how boilerplatey it is to compose (and read) entity hierarchies: ```rust commands .spawn(Foo) .with_children(|p| { p.spawn(Bar).with_children(|p| { p.spawn(Baz); }); p.spawn(Bar).with_children(|p| { p.spawn(Baz); }); }); ``` There is also currently no good way to statically define and return an entity hierarchy from a function. Instead, people often do this "internally" with a Commands function that returns nothing, making it impossible to spawn the hierarchy in other cases (direct World spawns, ChildSpawner, etc). Additionally, because this style of API results in creating the hierarchy bits _after_ the initial spawn of a bundle, it causes ECS archetype changes (and often expensive table moves). Because children are initialized after the fact, we also can't count them to pre-allocate space. This means each time a child inserts itself, it has a high chance of overflowing the currently allocated capacity in the `RelationshipTarget` collection, causing literal worst-case reallocations. We can do better! ## Solution The Bundle trait has been extended to support an optional `BundleEffect`. This is applied directly to World immediately _after_ the Bundle has fully inserted. Note that this is [intentionally](https://github.com/bevyengine/bevy/discussions/16920) _not done via a deferred Command_, which would require repeatedly copying each remaining subtree of the hierarchy to a new command as we walk down the tree (_not_ good performance). This allows us to implement the new `SpawnRelated` trait for all `RelationshipTarget` impls, which looks like this in practice: ```rust world.spawn(( Foo, Children::spawn(( Spawn(( Bar, Children::spawn(Spawn(Baz)), )), Spawn(( Bar, Children::spawn(Spawn(Baz)), )), )) )) ``` `Children::spawn` returns `SpawnRelatedBundle<Children, L: SpawnableList>`, which is a `Bundle` that inserts `Children` (preallocated to the size of the `SpawnableList::size_hint()`). `Spawn<B: Bundle>(pub B)` implements `SpawnableList` with a size of 1. `SpawnableList` is also implemented for tuples of `SpawnableList` (same general pattern as the Bundle impl). There are currently three built-in `SpawnableList` implementations: ```rust world.spawn(( Foo, Children::spawn(( Spawn(Name::new("Child1")), SpawnIter(["Child2", "Child3"].into_iter().map(Name::new), SpawnWith(|parent: &mut ChildSpawner| { parent.spawn(Name::new("Child4")); parent.spawn(Name::new("Child5")); }) )), )) ``` We get the benefits of "structured init", but we have nice flexibility where it is required! Some readers' first instinct might be to try to remove the need for the `Spawn` wrapper. This is impossible in the Rust type system, as a tuple of "child Bundles to be spawned" and a "tuple of Components to be added via a single Bundle" is ambiguous in the Rust type system. There are two ways to resolve that ambiguity: 1. By adding support for variadics to the Rust type system (removing the need for nested bundles). This is out of scope for this PR :) 2. Using wrapper types to resolve the ambiguity (this is what I did in this PR). For the single-entity spawn cases, `Children::spawn_one` does also exist, which removes the need for the wrapper: ```rust world.spawn(( Foo, Children::spawn_one(Bar), )) ``` ## This works for all Relationships This API isn't just for `Children` / `ChildOf` relationships. It works for any relationship type, and they can be mixed and matched! ```rust world.spawn(( Foo, Observers::spawn(( Spawn(Observer::new(|trigger: Trigger<FuseLit>| {})), Spawn(Observer::new(|trigger: Trigger<Exploded>| {})), )), OwnerOf::spawn(Spawn(Bar)) Children::spawn(Spawn(Baz)) )) ``` ## Macros While `Spawn` is necessary to satisfy the type system, we _can_ remove the need to express it via macros. The example above can be expressed more succinctly using the new `children![X]` macro, which internally produces `Children::spawn(Spawn(X))`: ```rust world.spawn(( Foo, children![ ( Bar, children![Baz], ), ( Bar, children![Baz], ), ] )) ``` There is also a `related!` macro, which is a generic version of the `children!` macro that supports any relationship type: ```rust world.spawn(( Foo, related!(Children[ ( Bar, related!(Children[Baz]), ), ( Bar, related!(Children[Baz]), ), ]) )) ``` ## Returning Hierarchies from Functions Thanks to these changes, the following pattern is now possible: ```rust fn button(text: &str, color: Color) -> impl Bundle { ( Node { width: Val::Px(300.), height: Val::Px(100.), ..default() }, BackgroundColor(color), children![ Text::new(text), ] ) } fn ui() -> impl Bundle { ( Node { width: Val::Percent(100.0), height: Val::Percent(100.0), ..default(), }, children![ button("hello", BLUE), button("world", RED), ] ) } // spawn from a system fn system(mut commands: Commands) { commands.spawn(ui()); } // spawn directly on World world.spawn(ui()); ``` ## Additional Changes and Notes * `Bundle::from_components` has been split out into `BundleFromComponents::from_components`, enabling us to implement `Bundle` for types that cannot be "taken" from the ECS (such as the new `SpawnRelatedBundle`). * The `NoBundleEffect` trait (which implements `BundleEffect`) is implemented for empty tuples (and tuples of empty tuples), which allows us to constrain APIs to only accept bundles that do not have effects. This is critical because the current batch spawn APIs cannot efficiently apply BundleEffects in their current form (as doing so in-place could invalidate the cached raw pointers). We could consider allocating a buffer of the effects to be applied later, but that does have performance implications that could offset the balance and value of the batched APIs (and would likely require some refactors to the underlying code). I've decided to be conservative here. We can consider relaxing that requirement on those APIs later, but that should be done in a followup imo. * I've ported a few examples to illustrate real-world usage. I think in a followup we should port all examples to the `children!` form whenever possible (and for cases that require things like SpawnIter, use the raw APIs). * Some may ask "why not use the `Relationship` to spawn (ex: `ChildOf::spawn(Foo)`) instead of the `RelationshipTarget` (ex: `Children::spawn(Spawn(Foo))`)?". That _would_ allow us to remove the `Spawn` wrapper. I've explicitly chosen to disallow this pattern. `Bundle::Effect` has the ability to create _significant_ weirdness. Things in `Bundle` position look like components. For example `world.spawn((Foo, ChildOf::spawn(Bar)))` _looks and reads_ like Foo is a child of Bar. `ChildOf` is in Foo's "component position" but it is not a component on Foo. This is a huge problem. Now that `Bundle::Effect` exists, we should be _very_ principled about keeping the "weird and unintuitive behavior" to a minimum. Things that read like components _should be the components they appear to be". ## Remaining Work * The macros are currently trivially implemented using macro_rules and are currently limited to the max tuple length. They will require a proc_macro implementation to work around the tuple length limit. ## Next Steps * Port the remaining examples to use `children!` where possible and raw `Spawn` / `SpawnIter` / `SpawnWith` where the flexibility of the raw API is required. ## Migration Guide Existing spawn patterns will continue to work as expected. Manual Bundle implementations now require a `BundleEffect` associated type. Exisiting bundles would have no bundle effect, so use `()`. Additionally `Bundle::from_components` has been moved to the new `BundleFromComponents` trait. ```rust // Before unsafe impl Bundle for X { unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Self { } /* remaining bundle impl here */ } // After unsafe impl Bundle for X { type Effect = (); /* remaining bundle impl here */ } unsafe impl BundleFromComponents for X { unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Self { } } ``` --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Co-authored-by: Emerson Coskey <emerson@coskey.dev>
738 lines
28 KiB
Rust
738 lines
28 KiB
Rust
//! This example will display a simple menu using Bevy UI where you can start a new game,
|
|
//! change some settings or quit. There is no actual game, it will just display the current
|
|
//! settings for 5 seconds before going back to the menu.
|
|
|
|
use bevy::prelude::*;
|
|
|
|
const TEXT_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
|
|
|
|
// Enum that will be used as a global state for the game
|
|
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
|
|
enum GameState {
|
|
#[default]
|
|
Splash,
|
|
Menu,
|
|
Game,
|
|
}
|
|
|
|
// One of the two settings that can be set through the menu. It will be a resource in the app
|
|
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
|
|
enum DisplayQuality {
|
|
Low,
|
|
Medium,
|
|
High,
|
|
}
|
|
|
|
// One of the two settings that can be set through the menu. It will be a resource in the app
|
|
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
|
|
struct Volume(u32);
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
// Insert as resource the initial value for the settings resources
|
|
.insert_resource(DisplayQuality::Medium)
|
|
.insert_resource(Volume(7))
|
|
// Declare the game state, whose starting value is determined by the `Default` trait
|
|
.init_state::<GameState>()
|
|
.add_systems(Startup, setup)
|
|
// Adds the plugins for each state
|
|
.add_plugins((splash::splash_plugin, menu::menu_plugin, game::game_plugin))
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
commands.spawn(Camera2d);
|
|
}
|
|
|
|
mod splash {
|
|
use bevy::prelude::*;
|
|
|
|
use super::{despawn_screen, GameState};
|
|
|
|
// This plugin will display a splash screen with Bevy logo for 1 second before switching to the menu
|
|
pub fn splash_plugin(app: &mut App) {
|
|
// As this plugin is managing the splash screen, it will focus on the state `GameState::Splash`
|
|
app
|
|
// When entering the state, spawn everything needed for this screen
|
|
.add_systems(OnEnter(GameState::Splash), splash_setup)
|
|
// While in this state, run the `countdown` system
|
|
.add_systems(Update, countdown.run_if(in_state(GameState::Splash)))
|
|
// When exiting the state, despawn everything that was spawned for this screen
|
|
.add_systems(OnExit(GameState::Splash), despawn_screen::<OnSplashScreen>);
|
|
}
|
|
|
|
// Tag component used to tag entities added on the splash screen
|
|
#[derive(Component)]
|
|
struct OnSplashScreen;
|
|
|
|
// Newtype to use a `Timer` for this screen as a resource
|
|
#[derive(Resource, Deref, DerefMut)]
|
|
struct SplashTimer(Timer);
|
|
|
|
fn splash_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
let icon = asset_server.load("branding/icon.png");
|
|
// Display the logo
|
|
commands.spawn((
|
|
Node {
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
..default()
|
|
},
|
|
OnSplashScreen,
|
|
children![(
|
|
ImageNode::new(icon),
|
|
Node {
|
|
// This will set the logo to be 200px wide, and auto adjust its height
|
|
width: Val::Px(200.0),
|
|
..default()
|
|
},
|
|
)],
|
|
));
|
|
// Insert the timer as a resource
|
|
commands.insert_resource(SplashTimer(Timer::from_seconds(1.0, TimerMode::Once)));
|
|
}
|
|
|
|
// Tick the timer, and change state when finished
|
|
fn countdown(
|
|
mut game_state: ResMut<NextState<GameState>>,
|
|
time: Res<Time>,
|
|
mut timer: ResMut<SplashTimer>,
|
|
) {
|
|
if timer.tick(time.delta()).finished() {
|
|
game_state.set(GameState::Menu);
|
|
}
|
|
}
|
|
}
|
|
|
|
mod game {
|
|
use bevy::{
|
|
color::palettes::basic::{BLUE, LIME},
|
|
prelude::*,
|
|
};
|
|
|
|
use super::{despawn_screen, DisplayQuality, GameState, Volume, TEXT_COLOR};
|
|
|
|
// This plugin will contain the game. In this case, it's just be a screen that will
|
|
// display the current settings for 5 seconds before returning to the menu
|
|
pub fn game_plugin(app: &mut App) {
|
|
app.add_systems(OnEnter(GameState::Game), game_setup)
|
|
.add_systems(Update, game.run_if(in_state(GameState::Game)))
|
|
.add_systems(OnExit(GameState::Game), despawn_screen::<OnGameScreen>);
|
|
}
|
|
|
|
// Tag component used to tag entities added on the game screen
|
|
#[derive(Component)]
|
|
struct OnGameScreen;
|
|
|
|
#[derive(Resource, Deref, DerefMut)]
|
|
struct GameTimer(Timer);
|
|
|
|
fn game_setup(
|
|
mut commands: Commands,
|
|
display_quality: Res<DisplayQuality>,
|
|
volume: Res<Volume>,
|
|
) {
|
|
commands.spawn((
|
|
Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
// center children
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
OnGameScreen,
|
|
children![(
|
|
Node {
|
|
// This will display its children in a column, from top to bottom
|
|
flex_direction: FlexDirection::Column,
|
|
// `align_items` will align children on the cross axis. Here the main axis is
|
|
// vertical (column), so the cross axis is horizontal. This will center the
|
|
// children
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::BLACK),
|
|
children![
|
|
(
|
|
Text::new("Will be back to the menu shortly..."),
|
|
TextFont {
|
|
font_size: 67.0,
|
|
..default()
|
|
},
|
|
TextColor(TEXT_COLOR),
|
|
Node {
|
|
margin: UiRect::all(Val::Px(50.0)),
|
|
..default()
|
|
},
|
|
),
|
|
(
|
|
Text::default(),
|
|
Node {
|
|
margin: UiRect::all(Val::Px(50.0)),
|
|
..default()
|
|
},
|
|
children![
|
|
(
|
|
TextSpan(format!("quality: {:?}", *display_quality)),
|
|
TextFont {
|
|
font_size: 50.0,
|
|
..default()
|
|
},
|
|
TextColor(BLUE.into()),
|
|
),
|
|
(
|
|
TextSpan::new(" - "),
|
|
TextFont {
|
|
font_size: 50.0,
|
|
..default()
|
|
},
|
|
TextColor(TEXT_COLOR),
|
|
),
|
|
(
|
|
TextSpan(format!("volume: {:?}", *volume)),
|
|
TextFont {
|
|
font_size: 50.0,
|
|
..default()
|
|
},
|
|
TextColor(LIME.into()),
|
|
),
|
|
]
|
|
),
|
|
]
|
|
)],
|
|
));
|
|
// Spawn a 5 seconds timer to trigger going back to the menu
|
|
commands.insert_resource(GameTimer(Timer::from_seconds(5.0, TimerMode::Once)));
|
|
}
|
|
|
|
// Tick the timer, and change state when finished
|
|
fn game(
|
|
time: Res<Time>,
|
|
mut game_state: ResMut<NextState<GameState>>,
|
|
mut timer: ResMut<GameTimer>,
|
|
) {
|
|
if timer.tick(time.delta()).finished() {
|
|
game_state.set(GameState::Menu);
|
|
}
|
|
}
|
|
}
|
|
|
|
mod menu {
|
|
use bevy::{
|
|
app::AppExit,
|
|
color::palettes::css::CRIMSON,
|
|
ecs::spawn::{SpawnIter, SpawnWith},
|
|
prelude::*,
|
|
};
|
|
|
|
use super::{despawn_screen, DisplayQuality, GameState, Volume, TEXT_COLOR};
|
|
|
|
// This plugin manages the menu, with 5 different screens:
|
|
// - a main menu with "New Game", "Settings", "Quit"
|
|
// - a settings menu with two submenus and a back button
|
|
// - two settings screen with a setting that can be set and a back button
|
|
pub fn menu_plugin(app: &mut App) {
|
|
app
|
|
// At start, the menu is not enabled. This will be changed in `menu_setup` when
|
|
// entering the `GameState::Menu` state.
|
|
// Current screen in the menu is handled by an independent state from `GameState`
|
|
.init_state::<MenuState>()
|
|
.add_systems(OnEnter(GameState::Menu), menu_setup)
|
|
// Systems to handle the main menu screen
|
|
.add_systems(OnEnter(MenuState::Main), main_menu_setup)
|
|
.add_systems(OnExit(MenuState::Main), despawn_screen::<OnMainMenuScreen>)
|
|
// Systems to handle the settings menu screen
|
|
.add_systems(OnEnter(MenuState::Settings), settings_menu_setup)
|
|
.add_systems(
|
|
OnExit(MenuState::Settings),
|
|
despawn_screen::<OnSettingsMenuScreen>,
|
|
)
|
|
// Systems to handle the display settings screen
|
|
.add_systems(
|
|
OnEnter(MenuState::SettingsDisplay),
|
|
display_settings_menu_setup,
|
|
)
|
|
.add_systems(
|
|
Update,
|
|
(setting_button::<DisplayQuality>.run_if(in_state(MenuState::SettingsDisplay)),),
|
|
)
|
|
.add_systems(
|
|
OnExit(MenuState::SettingsDisplay),
|
|
despawn_screen::<OnDisplaySettingsMenuScreen>,
|
|
)
|
|
// Systems to handle the sound settings screen
|
|
.add_systems(OnEnter(MenuState::SettingsSound), sound_settings_menu_setup)
|
|
.add_systems(
|
|
Update,
|
|
setting_button::<Volume>.run_if(in_state(MenuState::SettingsSound)),
|
|
)
|
|
.add_systems(
|
|
OnExit(MenuState::SettingsSound),
|
|
despawn_screen::<OnSoundSettingsMenuScreen>,
|
|
)
|
|
// Common systems to all screens that handles buttons behavior
|
|
.add_systems(
|
|
Update,
|
|
(menu_action, button_system).run_if(in_state(GameState::Menu)),
|
|
);
|
|
}
|
|
|
|
// State used for the current menu screen
|
|
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
|
|
enum MenuState {
|
|
Main,
|
|
Settings,
|
|
SettingsDisplay,
|
|
SettingsSound,
|
|
#[default]
|
|
Disabled,
|
|
}
|
|
|
|
// Tag component used to tag entities added on the main menu screen
|
|
#[derive(Component)]
|
|
struct OnMainMenuScreen;
|
|
|
|
// Tag component used to tag entities added on the settings menu screen
|
|
#[derive(Component)]
|
|
struct OnSettingsMenuScreen;
|
|
|
|
// Tag component used to tag entities added on the display settings menu screen
|
|
#[derive(Component)]
|
|
struct OnDisplaySettingsMenuScreen;
|
|
|
|
// Tag component used to tag entities added on the sound settings menu screen
|
|
#[derive(Component)]
|
|
struct OnSoundSettingsMenuScreen;
|
|
|
|
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 HOVERED_PRESSED_BUTTON: Color = Color::srgb(0.25, 0.65, 0.25);
|
|
const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
|
|
|
|
// Tag component used to mark which setting is currently selected
|
|
#[derive(Component)]
|
|
struct SelectedOption;
|
|
|
|
// All actions that can be triggered from a button click
|
|
#[derive(Component)]
|
|
enum MenuButtonAction {
|
|
Play,
|
|
Settings,
|
|
SettingsDisplay,
|
|
SettingsSound,
|
|
BackToMainMenu,
|
|
BackToSettings,
|
|
Quit,
|
|
}
|
|
|
|
// This system handles changing all buttons color based on mouse interaction
|
|
fn button_system(
|
|
mut interaction_query: Query<
|
|
(&Interaction, &mut BackgroundColor, Option<&SelectedOption>),
|
|
(Changed<Interaction>, With<Button>),
|
|
>,
|
|
) {
|
|
for (interaction, mut background_color, selected) in &mut interaction_query {
|
|
*background_color = match (*interaction, selected) {
|
|
(Interaction::Pressed, _) | (Interaction::None, Some(_)) => PRESSED_BUTTON.into(),
|
|
(Interaction::Hovered, Some(_)) => HOVERED_PRESSED_BUTTON.into(),
|
|
(Interaction::Hovered, None) => HOVERED_BUTTON.into(),
|
|
(Interaction::None, None) => NORMAL_BUTTON.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
// This system updates the settings when a new value for a setting is selected, and marks
|
|
// the button as the one currently selected
|
|
fn setting_button<T: Resource + Component + PartialEq + Copy>(
|
|
interaction_query: Query<(&Interaction, &T, Entity), (Changed<Interaction>, With<Button>)>,
|
|
selected_query: Single<(Entity, &mut BackgroundColor), With<SelectedOption>>,
|
|
mut commands: Commands,
|
|
mut setting: ResMut<T>,
|
|
) {
|
|
let (previous_button, mut previous_button_color) = selected_query.into_inner();
|
|
for (interaction, button_setting, entity) in &interaction_query {
|
|
if *interaction == Interaction::Pressed && *setting != *button_setting {
|
|
*previous_button_color = NORMAL_BUTTON.into();
|
|
commands.entity(previous_button).remove::<SelectedOption>();
|
|
commands.entity(entity).insert(SelectedOption);
|
|
*setting = *button_setting;
|
|
}
|
|
}
|
|
}
|
|
|
|
fn menu_setup(mut menu_state: ResMut<NextState<MenuState>>) {
|
|
menu_state.set(MenuState::Main);
|
|
}
|
|
|
|
fn main_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
// Common style for all buttons on the screen
|
|
let button_node = Node {
|
|
width: Val::Px(300.0),
|
|
height: Val::Px(65.0),
|
|
margin: UiRect::all(Val::Px(20.0)),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
};
|
|
let button_icon_node = Node {
|
|
width: Val::Px(30.0),
|
|
// This takes the icons out of the flexbox flow, to be positioned exactly
|
|
position_type: PositionType::Absolute,
|
|
// The icon will be close to the left border of the button
|
|
left: Val::Px(10.0),
|
|
..default()
|
|
};
|
|
let button_text_font = TextFont {
|
|
font_size: 33.0,
|
|
..default()
|
|
};
|
|
|
|
let right_icon = asset_server.load("textures/Game Icons/right.png");
|
|
let wrench_icon = asset_server.load("textures/Game Icons/wrench.png");
|
|
let exit_icon = asset_server.load("textures/Game Icons/exitRight.png");
|
|
|
|
commands.spawn((
|
|
Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
OnMainMenuScreen,
|
|
children![(
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(CRIMSON.into()),
|
|
children![
|
|
// Display the game name
|
|
(
|
|
Text::new("Bevy Game Menu UI"),
|
|
TextFont {
|
|
font_size: 67.0,
|
|
..default()
|
|
},
|
|
TextColor(TEXT_COLOR),
|
|
Node {
|
|
margin: UiRect::all(Val::Px(50.0)),
|
|
..default()
|
|
},
|
|
),
|
|
// Display three buttons for each action available from the main menu:
|
|
// - new game
|
|
// - settings
|
|
// - quit
|
|
(
|
|
Button,
|
|
button_node.clone(),
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
MenuButtonAction::Play,
|
|
children![
|
|
(ImageNode::new(right_icon), button_icon_node.clone()),
|
|
(
|
|
Text::new("New Game"),
|
|
button_text_font.clone(),
|
|
TextColor(TEXT_COLOR),
|
|
),
|
|
]
|
|
),
|
|
(
|
|
Button,
|
|
button_node.clone(),
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
MenuButtonAction::Settings,
|
|
children![
|
|
(ImageNode::new(wrench_icon), button_icon_node.clone()),
|
|
(
|
|
Text::new("Settings"),
|
|
button_text_font.clone(),
|
|
TextColor(TEXT_COLOR),
|
|
),
|
|
]
|
|
),
|
|
(
|
|
Button,
|
|
button_node,
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
MenuButtonAction::Quit,
|
|
children![
|
|
(ImageNode::new(exit_icon), button_icon_node),
|
|
(Text::new("Quit"), button_text_font, TextColor(TEXT_COLOR),),
|
|
]
|
|
),
|
|
]
|
|
)],
|
|
));
|
|
}
|
|
|
|
fn settings_menu_setup(mut commands: Commands) {
|
|
let button_node = Node {
|
|
width: Val::Px(200.0),
|
|
height: Val::Px(65.0),
|
|
margin: UiRect::all(Val::Px(20.0)),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
};
|
|
|
|
let button_text_style = (
|
|
TextFont {
|
|
font_size: 33.0,
|
|
..default()
|
|
},
|
|
TextColor(TEXT_COLOR),
|
|
);
|
|
|
|
commands.spawn((
|
|
Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
OnSettingsMenuScreen,
|
|
children![(
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(CRIMSON.into()),
|
|
Children::spawn(SpawnIter(
|
|
[
|
|
(MenuButtonAction::SettingsDisplay, "Display"),
|
|
(MenuButtonAction::SettingsSound, "Sound"),
|
|
(MenuButtonAction::BackToMainMenu, "Back"),
|
|
]
|
|
.into_iter()
|
|
.map(move |(action, text)| {
|
|
(
|
|
Button,
|
|
button_node.clone(),
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
action,
|
|
children![(Text::new(text), button_text_style.clone())],
|
|
)
|
|
})
|
|
))
|
|
)],
|
|
));
|
|
}
|
|
|
|
fn display_settings_menu_setup(mut commands: Commands, display_quality: Res<DisplayQuality>) {
|
|
fn button_node() -> Node {
|
|
Node {
|
|
width: Val::Px(200.0),
|
|
height: Val::Px(65.0),
|
|
margin: UiRect::all(Val::Px(20.0)),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
}
|
|
}
|
|
fn button_text_style() -> impl Bundle {
|
|
(
|
|
TextFont {
|
|
font_size: 33.0,
|
|
..default()
|
|
},
|
|
TextColor(TEXT_COLOR),
|
|
)
|
|
}
|
|
|
|
let display_quality = *display_quality;
|
|
commands.spawn((
|
|
Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
OnDisplaySettingsMenuScreen,
|
|
children![(
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(CRIMSON.into()),
|
|
children![
|
|
// Create a new `Node`, this time not setting its `flex_direction`. It will
|
|
// use the default value, `FlexDirection::Row`, from left to right.
|
|
(
|
|
Node {
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(CRIMSON.into()),
|
|
Children::spawn((
|
|
// Display a label for the current setting
|
|
Spawn((Text::new("Display Quality"), button_text_style())),
|
|
SpawnWith(move |parent: &mut ChildSpawner| {
|
|
for quality_setting in [
|
|
DisplayQuality::Low,
|
|
DisplayQuality::Medium,
|
|
DisplayQuality::High,
|
|
] {
|
|
let mut entity = parent.spawn((
|
|
Button,
|
|
Node {
|
|
width: Val::Px(150.0),
|
|
height: Val::Px(65.0),
|
|
..button_node()
|
|
},
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
quality_setting,
|
|
children![(
|
|
Text::new(format!("{quality_setting:?}")),
|
|
button_text_style(),
|
|
)],
|
|
));
|
|
if display_quality == quality_setting {
|
|
entity.insert(SelectedOption);
|
|
}
|
|
}
|
|
})
|
|
))
|
|
),
|
|
// Display the back button to return to the settings screen
|
|
(
|
|
Button,
|
|
button_node(),
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
MenuButtonAction::BackToSettings,
|
|
children![(Text::new("Back"), button_text_style())]
|
|
)
|
|
]
|
|
)],
|
|
));
|
|
}
|
|
|
|
fn sound_settings_menu_setup(mut commands: Commands, volume: Res<Volume>) {
|
|
let button_node = Node {
|
|
width: Val::Px(200.0),
|
|
height: Val::Px(65.0),
|
|
margin: UiRect::all(Val::Px(20.0)),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
};
|
|
let button_text_style = (
|
|
TextFont {
|
|
font_size: 33.0,
|
|
..default()
|
|
},
|
|
TextColor(TEXT_COLOR),
|
|
);
|
|
|
|
let volume = *volume;
|
|
let button_node_clone = button_node.clone();
|
|
commands.spawn((
|
|
Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
OnSoundSettingsMenuScreen,
|
|
children![(
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(CRIMSON.into()),
|
|
children![
|
|
(
|
|
Node {
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(CRIMSON.into()),
|
|
Children::spawn((
|
|
Spawn((Text::new("Volume"), button_text_style.clone())),
|
|
SpawnWith(move |parent: &mut ChildSpawner| {
|
|
for volume_setting in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] {
|
|
let mut entity = parent.spawn((
|
|
Button,
|
|
Node {
|
|
width: Val::Px(30.0),
|
|
height: Val::Px(65.0),
|
|
..button_node_clone.clone()
|
|
},
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
Volume(volume_setting),
|
|
));
|
|
if volume == Volume(volume_setting) {
|
|
entity.insert(SelectedOption);
|
|
}
|
|
}
|
|
})
|
|
))
|
|
),
|
|
(
|
|
Button,
|
|
button_node,
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
MenuButtonAction::BackToSettings,
|
|
children![(Text::new("Back"), button_text_style)]
|
|
)
|
|
]
|
|
)],
|
|
));
|
|
}
|
|
|
|
fn menu_action(
|
|
interaction_query: Query<
|
|
(&Interaction, &MenuButtonAction),
|
|
(Changed<Interaction>, With<Button>),
|
|
>,
|
|
mut app_exit_events: EventWriter<AppExit>,
|
|
mut menu_state: ResMut<NextState<MenuState>>,
|
|
mut game_state: ResMut<NextState<GameState>>,
|
|
) {
|
|
for (interaction, menu_button_action) in &interaction_query {
|
|
if *interaction == Interaction::Pressed {
|
|
match menu_button_action {
|
|
MenuButtonAction::Quit => {
|
|
app_exit_events.send(AppExit::Success);
|
|
}
|
|
MenuButtonAction::Play => {
|
|
game_state.set(GameState::Game);
|
|
menu_state.set(MenuState::Disabled);
|
|
}
|
|
MenuButtonAction::Settings => menu_state.set(MenuState::Settings),
|
|
MenuButtonAction::SettingsDisplay => {
|
|
menu_state.set(MenuState::SettingsDisplay);
|
|
}
|
|
MenuButtonAction::SettingsSound => {
|
|
menu_state.set(MenuState::SettingsSound);
|
|
}
|
|
MenuButtonAction::BackToMainMenu => menu_state.set(MenuState::Main),
|
|
MenuButtonAction::BackToSettings => {
|
|
menu_state.set(MenuState::Settings);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Generic system that takes a component as a parameter, and will despawn all entities with that component
|
|
fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
|
|
for entity in &to_despawn {
|
|
commands.entity(entity).despawn();
|
|
}
|
|
}
|