Skip to content

Commit ded6f04

Browse files
committed
Add pause menu
1 parent da3894c commit ded6f04

17 files changed

+616
-387
lines changed

assets/config/theme.ron

+2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@
2727
Srgba(Srgba(red: 0.384, green: 0.827, blue: 0.769, alpha: 1.000)),
2828
// CardBorder
2929
Srgba(Srgba(red: 0.071, green: 0.082, blue: 0.133, alpha: 1.000)),
30+
// Overlay
31+
Srgba(Srgba(red: 0.161, green: 0.157, blue: 0.231, alpha: 0.970)),
3032
)),
3133
)

src/animation/transition.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn apply_fade_out(
8686
.0
8787
.set_alpha(1.0 - (fade.remaining / fade.duration).max(0.0));
8888
if fade.remaining <= 0.0 {
89-
screen.enter(fade.to_screen);
89+
screen.trigger().enter(fade.to_screen);
9090
despawn.recursive(entity);
9191
}
9292
fade.remaining -= dt;

src/core/theme.rs

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub enum ThemeColor {
7171
Popup,
7272
Indicator,
7373
CardBorder,
74+
Overlay,
7475
}
7576

7677
impl ThemeColor {

src/game.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ pub mod spotlight;
99
pub mod sprite;
1010
pub mod wave;
1111

12+
use std::borrow::Cow;
13+
1214
use avian2d::prelude::*;
15+
use bevy::ecs::system::EntityCommand;
1316
use bevy::prelude::*;
17+
use pyri_state::prelude::*;
1418

19+
use crate::screen::Screen;
1520
use crate::util::prelude::*;
1621

1722
pub(super) fn plugin(app: &mut App) {
@@ -42,23 +47,16 @@ impl Configure for GameRoot {
4247
fn configure(app: &mut App) {
4348
app.register_type::<Self>();
4449
app.init_resource::<Self>();
50+
app.add_systems(StateFlush, Screen::ANY.on_exit(clear_game_root));
4551
}
4652
}
4753

4854
impl FromWorld for GameRoot {
4955
fn from_world(world: &mut World) -> Self {
50-
let players = world
51-
.spawn((Name::new("Players"), SpatialBundle::default()))
52-
.id();
53-
let enemies = world
54-
.spawn((Name::new("Enemies"), SpatialBundle::default()))
55-
.id();
56-
let projectiles = world
57-
.spawn((Name::new("Projectiles"), SpatialBundle::default()))
58-
.id();
59-
let vfx = world
60-
.spawn((Name::new("Vfx"), SpatialBundle::default()))
61-
.id();
56+
let players = world.spawn_with(root("Players")).id();
57+
let enemies = world.spawn_with(root("Enemies")).id();
58+
let projectiles = world.spawn_with(root("Projectiles")).id();
59+
let vfx = world.spawn_with(root("Vfx")).id();
6260

6361
Self {
6462
players,
@@ -69,12 +67,18 @@ impl FromWorld for GameRoot {
6967
}
7068
}
7169

72-
impl GameRoot {
73-
pub fn despawn_descendants(&self, commands: &mut Commands) {
74-
commands.entity(self.players).despawn_descendants();
75-
commands.entity(self.enemies).despawn_descendants();
76-
commands.entity(self.projectiles).despawn_descendants();
77-
commands.entity(self.vfx).despawn_descendants();
70+
fn clear_game_root(mut commands: Commands, game_root: Res<GameRoot>) {
71+
commands.entity(game_root.players).despawn_descendants();
72+
commands.entity(game_root.enemies).despawn_descendants();
73+
commands.entity(game_root.projectiles).despawn_descendants();
74+
commands.entity(game_root.vfx).despawn_descendants();
75+
}
76+
77+
fn root(name: impl Into<Cow<'static, str>>) -> impl EntityCommand<World> {
78+
let name = name.into();
79+
80+
move |mut entity: EntityWorldMut| {
81+
entity.insert((Name::new(name), SpatialBundle::default()));
7882
}
7983
}
8084

src/screen.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use pyri_state::prelude::*;
1010

1111
use crate::animation::transition::FadeIn;
1212
use crate::animation::transition::FadeOut;
13+
use crate::core::camera::CameraRoot;
1314
use crate::core::window::WindowReady;
1415
use crate::ui::prelude::*;
1516
use crate::util::prelude::*;
@@ -28,6 +29,7 @@ pub fn plugin(app: &mut App) {
2829

2930
#[derive(State, Copy, Clone, Eq, PartialEq, Hash, Debug, Reflect, Default)]
3031
#[state(after(WindowReady), entity_scope, bevy_state, log_flush)]
32+
#[reflect(Resource)]
3133
pub enum Screen {
3234
#[default]
3335
Splash,
@@ -39,11 +41,23 @@ pub enum Screen {
3941

4042
impl Configure for Screen {
4143
fn configure(app: &mut App) {
44+
app.register_type::<Self>();
4245
app.add_state::<Self>();
43-
app.add_systems(StateFlush, WindowReady.on_enter(Screen::enable_default));
46+
app.add_systems(
47+
StateFlush,
48+
(
49+
WindowReady.on_enter(Screen::enable_default),
50+
Screen::ANY.on_exit(reset_camera),
51+
),
52+
);
4453
}
4554
}
4655

56+
fn reset_camera(camera_root: Res<CameraRoot>, mut camera_query: Query<&mut Transform>) {
57+
let mut transform = r!(camera_query.get_mut(camera_root.primary));
58+
transform.translation = Vec2::ZERO.extend(transform.translation.z);
59+
}
60+
4761
const FADE_IN_SECS: f32 = 0.5;
4862

4963
fn fade_in(mut entity: EntityWorldMut) {

src/screen/end.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ fn enter_end(mut commands: Commands, ui_root: Res<UiRoot>) {
6464
);
6565
}
6666

67-
fn exit_end(mut commands: Commands, ui_root: Res<UiRoot>) {
67+
fn exit_end(mut commands: Commands) {
6868
commands.remove_resource::<InputMap<EndScreenAction>>();
69-
commands.entity(ui_root.body).despawn_descendants();
7069
}
7170

7271
fn end_screen(mut entity: EntityWorldMut) {
7372
entity
74-
.add(widget::column_mid)
73+
.add(Style::COLUMN_MID.div())
7574
.insert(Name::new("EndScreen"))
7675
.with_children(|children| {
7776
children.spawn_with(end_text);

src/screen/loading.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ pub(super) fn plugin(app: &mut App) {
2323
LoadingState::new(Screen::Loading.bevy()).load_collection::<PlayingAssets>(),
2424
);
2525
app.add_plugins(ProgressPlugin::new(Screen::Loading.bevy()));
26-
app.add_systems(
27-
StateFlush,
28-
Screen::Loading.on_edge(exit_loading, enter_loading),
29-
);
26+
app.add_systems(StateFlush, Screen::Loading.on_enter(enter_loading));
3027
app.add_systems(
3128
Update,
3229
// TODO: This is kinda silly. Find a better way later.
@@ -89,13 +86,9 @@ fn enter_loading(mut commands: Commands, ui_root: Res<UiRoot>) {
8986
commands.spawn_with(loading_screen).set_parent(ui_root.body);
9087
}
9188

92-
fn exit_loading(mut commands: Commands, ui_root: Res<UiRoot>) {
93-
commands.entity(ui_root.body).despawn_descendants();
94-
}
95-
9689
fn loading_screen(mut entity: EntityWorldMut) {
9790
entity
98-
.add(widget::column_center)
91+
.add(Style::COLUMN_CENTER.div())
9992
.insert(Name::new("LoadingScreen"))
10093
.with_children(|children| {
10194
children.spawn_with(loading_text);

0 commit comments

Comments
 (0)