Skip to content

Commit b89dc65

Browse files
committed
bit of a restructure, moved some stuff into mod.rs and stop redefining the same thing all over the place
1 parent b9c0486 commit b89dc65

File tree

4 files changed

+24
-37
lines changed

4 files changed

+24
-37
lines changed

src/main.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ pub(crate) enum GameState {
8787
Splash,
8888
}
8989

90-
// a label component to tell us which things are loaded in the Game GameState
91-
#[derive(Component)]
92-
struct OnGameScreen;
93-
9490
#[derive(Component)]
9591
struct Player {
9692
health: i32
@@ -114,7 +110,6 @@ fn setup_sys(
114110
Mesh2d(meshes.add(Annulus::new(25.0, 50.0))),
115111
MeshMaterial2d(materials.add(Color::WHITE)),
116112
Transform::from_xyz(0., - window_height / 2. + 75., 0.),
117-
OnGameScreen,
118113
));
119114
}
120115

@@ -134,12 +129,4 @@ fn player_move_sys(
134129
player_tf.translation.x = (player_tf.translation.x + move_distance).min(width / 2.)
135130
}
136131
}
137-
}
138-
139-
// stole this directly from an example but it seems a sensible way of removing
140-
// unneeded Entities with a given Component indiscriminantly
141-
fn despawn_entities<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
142-
for entity in &to_despawn {
143-
commands.entity(entity).despawn_recursive();
144-
}
145-
}
132+
}

src/ui/main_menu.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::{
1010
MenuContainerNode,
1111
MenuButtonAction,
1212
MenuFont,
13+
OnMenuScreen,
1314
MENU_TEXT_COLOR
1415
};
1516

@@ -38,16 +39,7 @@ impl Plugin for MenuPlugin {
3839
}
3940
}
4041

41-
/// Tag Entities with this if they appear on any menu screen
42-
///
43-
/// Can be useful to despawn (or otherwise affect)
44-
/// the entire Menu regardless of where you are in it
45-
/// e.g. if you hit Esc while in [`GameState::Menu`] it should despawn all
46-
/// [`OnMenuScreen`] entities and switch to [`GameState::Gam`e], which would be difficult to do
47-
/// if we used only [`OnMainMenuScreen`] and [`OnSettingsMenuScreen`]
48-
#[derive(Component)]
49-
struct OnMenuScreen;
50-
/// Tag Entities with this if they are visible on [MenuState::MainMenu]
42+
// Tag Entities with this if they are visible on [MenuState::MainMenu]
5143
#[derive(Component)]
5244
struct OnMainMenuScreen;
5345
/// Tag Entities with this if they are visible on [MenuState::Settings]

src/ui/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ pub mod main_menu;
44

55
use bevy::prelude::*;
66
use std::{path::PathBuf, sync::LazyLock};
7-
use super::{
8-
GameState,
9-
despawn_entities
10-
};
7+
use super::GameState;
118
use main_menu::{
129
MenuState,
1310
MenuPlugin
@@ -43,6 +40,17 @@ impl Plugin for UiPlugin {
4340
}
4441
}
4542

43+
/// Tag Entities with this if they appear on any menu screen
44+
///
45+
/// Can be useful to despawn (or otherwise affect)
46+
/// the entire Menu regardless of where you are in it
47+
/// e.g. if you hit Esc while in [`GameState::Menu`] it should despawn all
48+
/// [`OnMenuScreen`] entities and switch to [`GameState::Gam`e], which would be difficult to do
49+
/// if we used only [`OnMainMenuScreen`] and [`OnSettingsMenuScreen`]
50+
51+
#[derive (Component)]
52+
pub struct OnMenuScreen;
53+
4654
/// Enum of all the actions a [Button] should be able to perform,
4755
/// with [MenuButtonAction] and [PauseButtonAction] variants
4856
/// To use the variants:
@@ -253,4 +261,12 @@ pub fn pause_menu_listener_sys(
253261
}
254262
}
255263
}
264+
}
265+
266+
// stole this directly from an example but it seems a sensible way of removing
267+
// unneeded Entities with a given Component indiscriminantly
268+
fn despawn_entities<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
269+
for entity in &to_despawn {
270+
commands.entity(entity).despawn_recursive();
271+
}
256272
}

src/ui/pause.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::{
1010
MenuContainerNode,
1111
ButtonNode,
1212
MenuFont,
13+
OnMenuScreen,
1314
MENU_TEXT_COLOR
1415
};
1516
use super::main_menu::{
@@ -38,15 +39,6 @@ impl Plugin for PausePlugin {
3839
}
3940
}
4041

41-
/// Tag Entities with this if they appear on any menu screen
42-
///
43-
/// Can be useful to despawn (or otherwise affect)
44-
/// the entire Menu regardless of where you are in it
45-
/// e.g. if you hit Esc while in [`GameState::Menu`] it should despawn all
46-
/// [`OnMenuScreen`] entities and switch to [`GameState::Game`], which would be difficult to do
47-
/// if we used only [`OnPauseMenuScreen`] and [`OnSettingsMenuScreen`]
48-
#[derive(Component)]
49-
struct OnMenuScreen;
5042
/// Tag Entities with this if they are visible on [PauseMenuState::PauseMenu]
5143
#[derive(Component)]
5244
struct OnPauseMenuScreen;

0 commit comments

Comments
 (0)