game states cleanup

This commit is contained in:
2026-09-19 10:53:36 +02:00
parent 9df830bf18
commit d42f5af75f
7 changed files with 74 additions and 20 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{camera::MainCamera, collision::Collider, player::Player}; use crate::{camera::MainCamera, collision::Collider, game_state::GameState, player::Player};
fn constrain_player( fn constrain_player(
mut player: Single<(&Collider, &mut Transform), With<Player>>, mut player: Single<(&Collider, &mut Transform), With<Player>>,
@@ -20,6 +20,6 @@ pub struct BoundsPlugin;
impl Plugin for BoundsPlugin { impl Plugin for BoundsPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, constrain_player); app.add_systems(Update, constrain_player.run_if(in_state(GameState::InGame)));
} }
} }
+14 -3
View File
@@ -2,11 +2,15 @@ use bevy::prelude::*;
use crate::{camera::MainCamera, game_state::GameState, player::Player}; use crate::{camera::MainCamera, game_state::GameState, player::Player};
#[derive(Component)]
pub struct Debug;
#[derive(Component)] #[derive(Component)]
pub struct DebugCamera; pub struct DebugCamera;
fn spawn_debug_camera(mut commands: Commands) { fn spawn_debug_camera(mut commands: Commands) {
commands.spawn(( commands.spawn((
Debug,
DebugCamera, DebugCamera,
Camera2d, Camera2d,
Camera { Camera {
@@ -63,6 +67,7 @@ fn spawn_debug_frame(mut commands: Commands) {
}; };
commands.spawn(( commands.spawn((
Debug,
FramePart(side), FramePart(side),
Sprite::from_color(Color::srgb(1.0, 0.0, 0.0), size), Sprite::from_color(Color::srgb(1.0, 0.0, 0.0), size),
Visibility::Hidden, Visibility::Hidden,
@@ -99,18 +104,24 @@ fn draw_view_frame(
*visibility = visible; *visibility = visible;
} }
} }
fn cleanup_debug(mut commands: Commands, entities: Query<Entity, With<Debug>>) {
for entity in entities {
commands.entity(entity).despawn();
}
}
pub struct DebugPlugin; pub struct DebugPlugin;
impl Plugin for DebugPlugin { impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems( app.add_systems(
Startup, OnEnter(GameState::InGame),
( (
spawn_debug_camera, spawn_debug_camera,
setup_debug_camera.after(spawn_debug_camera), setup_debug_camera.after(spawn_debug_camera),
spawn_debug_frame, spawn_debug_frame,
) ),
.run_if(in_state(GameState::InGame)),
) )
.add_systems( .add_systems(
Update, Update,
+12 -2
View File
@@ -1,12 +1,13 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::game_state::GameState; use crate::{camera::MainCamera, game_state::GameState};
// To delete game over's components // To delete game over's components
#[derive(Component)] #[derive(Component)]
pub struct GameOver; pub struct GameOver;
fn game_over(mut commands: Commands) { fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, With<MainCamera>>) {
// Texts
commands.spawn(( commands.spawn((
GameOver, GameOver,
Node { Node {
@@ -35,6 +36,15 @@ fn game_over(mut commands: Commands) {
) )
], ],
)); ));
main_camera.translation = Vec3::ZERO;
// Background
commands.spawn((
GameOver,
Sprite::from_color(Color::BLACK, Vec2::new(1920.0, 1080.0)),
Transform::from_xyz(0.0, 0.0, -1.0),
));
} }
fn cleanup_game_over(mut commands: Commands, game_over: Query<Entity, With<GameOver>>) { fn cleanup_game_over(mut commands: Commands, game_over: Query<Entity, With<GameOver>>) {
+16 -6
View File
@@ -7,6 +7,7 @@ use crate::game_state::GameState;
pub struct MainMenu; pub struct MainMenu;
fn main_menu(mut commands: Commands) { fn main_menu(mut commands: Commands) {
// Texts
commands.spawn(( commands.spawn((
MainMenu, MainMenu,
Node { Node {
@@ -35,20 +36,29 @@ fn main_menu(mut commands: Commands) {
) )
], ],
)); ));
// Background
commands.spawn((
MainMenu,
Sprite::from_color(Color::BLACK, Vec2::new(1920.0, 1080.0)),
Transform::from_xyz(0.0, 0.0, -1.0),
));
} }
fn cleanup_menu(mut commands: Commands, menu: Query<Entity, With<MainMenu>>) { // Wait player to press enter for starting the fall
for entity in menu {
commands.entity(entity).despawn();
}
}
fn menu_input(input: Res<ButtonInput<KeyCode>>, mut next_state: ResMut<NextState<GameState>>) { fn menu_input(input: Res<ButtonInput<KeyCode>>, mut next_state: ResMut<NextState<GameState>>) {
if input.just_pressed(KeyCode::Enter) { if input.just_pressed(KeyCode::Enter) {
next_state.set(GameState::InGame); next_state.set(GameState::InGame);
} }
} }
// Clean all menu's entities
fn cleanup_menu(mut commands: Commands, menu: Query<Entity, With<MainMenu>>) {
for entity in menu {
commands.entity(entity).despawn();
}
}
pub struct MainMenuPlugin; pub struct MainMenuPlugin;
impl Plugin for MainMenuPlugin { impl Plugin for MainMenuPlugin {
+7 -2
View File
@@ -111,11 +111,15 @@ pub fn reset_player(
} }
} }
fn cleanup_player(mut commands: Commands, player: Single<Entity, With<Player>>) {
commands.entity(*player).despawn();
}
pub struct PlayerPlugin; pub struct PlayerPlugin;
impl Plugin for PlayerPlugin { impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_player) app.add_systems(OnEnter(GameState::InGame), spawn_player)
.init_resource::<FallStarted>() .init_resource::<FallStarted>()
.add_systems( .add_systems(
Update, Update,
@@ -126,6 +130,7 @@ impl Plugin for PlayerPlugin {
.after(check_collision_with_player), .after(check_collision_with_player),
) )
.run_if(in_state(GameState::InGame)), .run_if(in_state(GameState::InGame)),
); )
.add_systems(OnExit(GameState::InGame), cleanup_player);
} }
} }
+14 -4
View File
@@ -46,16 +46,26 @@ fn update_depth_text(
} }
} }
fn cleanup_score(mut commands: Commands, entities: Query<Entity, With<DepthText>>) {
for entity in entities {
commands.entity(entity).despawn();
}
}
pub struct ScorePlugin; pub struct ScorePlugin;
impl Plugin for ScorePlugin { impl Plugin for ScorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_depth_text) app.init_resource::<Depth>()
.init_resource::<Depth>() .add_systems(OnEnter(GameState::InGame), spawn_depth_text)
.add_systems( .add_systems(
Update, Update,
(update_depth_text, update_depth.after(move_player)) (
update_depth_text.after(spawn_depth_text),
update_depth.after(move_player),
)
.run_if(in_state(GameState::InGame)), .run_if(in_state(GameState::InGame)),
); )
.add_systems(OnExit(GameState::InGame), cleanup_score);
} }
} }
+9 -1
View File
@@ -57,11 +57,19 @@ fn spawn_chunk(commands: &mut Commands, id: i64, run_seed: u64) {
crate::background::spawn_background_chunk(commands, id); crate::background::spawn_background_chunk(commands, id);
// crate::obstacle::spawn_obstacle_chunk(commands, id, run_seed); // crate::obstacle::spawn_obstacle_chunk(commands, id, run_seed);
} }
fn cleanup_chunk(mut commands: Commands, entities: Query<Entity, With<Chunk>>) {
for entity in entities {
commands.entity(entity).despawn();
}
}
pub struct WorldPlugin; pub struct WorldPlugin;
impl Plugin for WorldPlugin { impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(RunSeed(rand::random::<u64>())) app.insert_resource(RunSeed(rand::random::<u64>()))
.add_systems(Update, stream_world.run_if(in_state(GameState::InGame))); .add_systems(Update, stream_world.run_if(in_state(GameState::InGame)))
.add_systems(OnExit(GameState::InGame), cleanup_chunk);
} }
} }