feat(screens): add translucent pause screen #36

This commit is contained in:
Kristofers Solo 2025-01-18 16:25:16 +02:00
parent e7bdb37093
commit 5d50daf768
7 changed files with 59 additions and 34 deletions

View File

@ -15,7 +15,6 @@ use crate::{
components::{HexMaze, MazeConfig, Tile, Wall},
resources::GlobalMazeConfig,
},
screens::Screen,
theme::palette::rose_pine::RosePineDawn,
};
@ -62,7 +61,6 @@ pub fn spawn_maze(
config.clone(),
Transform::from_translation(Vec3::ZERO.with_y(y_offset)),
Visibility::Visible,
StateScoped(Screen::Gameplay),
))
.insert_if(CurrentFloor, || floor == 1) // Only floor 1 gets CurrentFloor
.id();

View File

@ -5,7 +5,6 @@ use crate::{
assets::{blue_material, generate_pill_mesh},
components::{CurrentPosition, Player},
},
screens::Screen,
};
use bevy::prelude::*;
@ -33,6 +32,5 @@ pub fn spawn_player(
Mesh3d(meshes.add(generate_pill_mesh(player_radius, player_height / 2.))),
MeshMaterial3d(materials.add(blue_material())),
Transform::from_xyz(start_pos.x, y_offset, start_pos.y),
StateScoped(Screen::Gameplay),
));
}

View File

@ -8,6 +8,7 @@ use crate::{
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
pub(super) fn plugin(app: &mut App) {
app.init_resource::<GameplayInitialized>();
app.add_systems(
OnEnter(Screen::Gameplay),
(
@ -16,8 +17,14 @@ pub(super) fn plugin(app: &mut App) {
spawn_hint_command,
spawn_stats_command,
)
.chain(),
.chain()
.run_if(not(resource_exists::<GameplayInitialized>)),
);
app.add_systems(OnEnter(Screen::Gameplay), |mut commands: Commands| {
commands.insert_resource(GameplayInitialized(true));
});
app.add_systems(OnEnter(Screen::Title), reset_gameplay_state);
app.add_systems(
Update,
@ -28,3 +35,11 @@ pub(super) fn plugin(app: &mut App) {
fn pause_game(mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Pause);
}
fn reset_gameplay_state(mut commands: Commands) {
commands.remove_resource::<GameplayInitialized>();
}
#[derive(Debug, Default, Reflect, Resource, DerefMut, Deref)]
#[reflect(Resource)]
pub struct GameplayInitialized(bool);

View File

@ -7,6 +7,7 @@ mod splash;
mod title;
use bevy::prelude::*;
pub use gameplay::GameplayInitialized;
pub(super) fn plugin(app: &mut App) {
app.init_state::<Screen>();

View File

@ -2,24 +2,38 @@ use bevy::{input::common_conditions::input_just_pressed, prelude::*};
use crate::theme::{
events::OnPress,
palette::rose_pine::RosePineDawn,
prelude::ColorScheme,
widgets::{Containers, Widgets},
};
use super::Screen;
pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Pause), spawn_title_screen);
app.add_systems(OnEnter(Screen::Pause), spawn_pause_overlay);
app.add_systems(
Update,
return_to_game.run_if(in_state(Screen::Pause).and(input_just_pressed(KeyCode::Escape))),
);
}
fn spawn_title_screen(mut commands: Commands) {
fn spawn_pause_overlay(mut commands: Commands) {
commands
.ui_root()
.insert(StateScoped(Screen::Pause))
.insert((
StateScoped(Screen::Pause),
BackgroundColor(RosePineDawn::Muted.to_color().with_alpha(0.5)),
))
.with_children(|parent| {
parent
.spawn(Node {
bottom: Val::Px(100.),
..default()
})
.with_children(|parent| {
parent.header("Paused");
});
parent.button("Continue").observe(return_to_game_trigger);
parent
.button("Exit")

View File

@ -13,23 +13,26 @@ use reset::reset_timers;
use score::{update_score, update_score_display};
use total_timer::{update_total_timer, update_total_timer_display};
use crate::screens::Screen;
use crate::screens::{GameplayInitialized, Screen};
pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Gameplay), reset_timers)
.add_systems(
Update,
app.add_systems(
OnEnter(Screen::Gameplay),
reset_timers.run_if(not(resource_exists::<GameplayInitialized>)),
);
app.add_systems(
Update,
(
(
(
update_score.before(update_floor_timer),
update_score_display,
)
.chain(),
(update_floor_timer, update_floor_timer_display).chain(),
(update_total_timer, update_total_timer_display).chain(),
update_floor_display,
update_highest_floor_display,
update_score.before(update_floor_timer),
update_score_display,
)
.run_if(in_state(Screen::Gameplay)),
);
.chain(),
(update_floor_timer, update_floor_timer_display).chain(),
(update_total_timer, update_total_timer_display).chain(),
update_floor_display,
update_highest_floor_display,
)
.run_if(in_state(Screen::Gameplay)),
);
}

View File

@ -1,7 +1,6 @@
use bevy::prelude::*;
use crate::{
screens::Screen,
stats::{
components::{
FloorDisplay, FloorTimerDisplay, HighestFloorDisplay, ScoreDisplay, TotalTimerDisplay,
@ -12,14 +11,11 @@ use crate::{
};
pub fn spawn_stats(mut commands: Commands) {
commands
.ui_stats()
.insert(StateScoped(Screen::Gameplay))
.with_children(|parent| {
parent.stats("Floor: 1", FloorDisplay);
parent.stats("Highest Floor: 1", HighestFloorDisplay);
parent.stats("Score: 0", ScoreDisplay);
parent.stats("Floor Timer", FloorTimerDisplay);
parent.stats("Total Timer", TotalTimerDisplay);
});
commands.ui_stats().with_children(|parent| {
parent.stats("Floor: 1", FloorDisplay);
parent.stats("Highest Floor: 1", HighestFloorDisplay);
parent.stats("Score: 0", ScoreDisplay);
parent.stats("Floor Timer", FloorTimerDisplay);
parent.stats("Total Timer", TotalTimerDisplay);
});
}