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();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
fn menu(
|
||||||
struct MenuData {
|
mut next_state: ResMut<NextState<AppState>>,
|
||||||
root_entity: Entity,
|
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, PartialEq, Eq)]
|
#[derive(Component)]
|
||||||
enum MenuButton {
|
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)]
|
||||||
|
pub struct MenuData {
|
||||||
|
pub root_entity: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, PartialEq, Eq)]
|
||||||
|
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 {
|
||||||
@ -323,70 +453,13 @@ fn setup_menu(mut commands: Commands, tutorial_state: Res<State<TutorialState>>)
|
|||||||
commands.insert_resource(MenuData {
|
commands.insert_resource(MenuData {
|
||||||
root_entity: button_entity,
|
root_entity: button_entity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@ -394,29 +467,17 @@ fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear_state_bound_entities<S: States>(
|
const SPEED: f32 = 100.0;
|
||||||
state: S,
|
const TURBO_SPEED: f32 = 300.0;
|
||||||
) -> 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;
|
pub fn movement(
|
||||||
const TURBO_SPEED: f32 = 300.0;
|
|
||||||
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>>>,
|
||||||
mut query: Query<&mut Transform, With<Sprite>>,
|
mut query: Query<&mut Transform, With<Sprite>>,
|
||||||
) {
|
) {
|
||||||
for mut transform in &mut query {
|
for mut transform in &mut query {
|
||||||
let mut direction = Vec3::ZERO;
|
let mut direction = Vec3::ZERO;
|
||||||
if input.pressed(KeyCode::ArrowLeft) {
|
if input.pressed(KeyCode::ArrowLeft) {
|
||||||
@ -438,24 +499,9 @@ fn movement(
|
|||||||
* time.delta_seconds();
|
* time.delta_seconds();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 setup_paused_screen(mut commands: Commands) {
|
pub fn setup_paused_screen(mut commands: Commands) {
|
||||||
info!("Printing Pause");
|
info!("Printing Pause");
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
@ -504,24 +550,9 @@ fn setup_paused_screen(mut commands: Commands) {
|
|||||||
));
|
));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
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 setup_turbo_text(mut commands: Commands) {
|
pub fn setup_turbo_text(mut commands: Commands) {
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
StateBound(TurboMode),
|
StateBound(TurboMode),
|
||||||
@ -550,15 +581,9 @@ fn setup_turbo_text(mut commands: Commands) {
|
|||||||
},
|
},
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) {
|
pub 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,
|
||||||
@ -567,9 +592,9 @@ fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) {
|
|||||||
|
|
||||||
sprite.color = new_color.into();
|
sprite.color = new_color.into();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn movement_instructions(mut commands: Commands) {
|
pub fn movement_instructions(mut commands: Commands) {
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
StateBound(Tutorial::MovementInstructions),
|
StateBound(Tutorial::MovementInstructions),
|
||||||
@ -624,9 +649,9 @@ 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),
|
||||||
@ -664,23 +689,5 @@ 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),
|
||||||
@ -262,14 +277,5 @@ 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