diff --git a/src/bounds.rs b/src/bounds.rs index bf7d265..111f805 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -1,6 +1,6 @@ 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( mut player: Single<(&Collider, &mut Transform), With>, @@ -20,6 +20,6 @@ pub struct BoundsPlugin; impl Plugin for BoundsPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, constrain_player); + app.add_systems(Update, constrain_player.run_if(in_state(GameState::InGame))); } } diff --git a/src/debug.rs b/src/debug.rs index f5b41ca..c8357e0 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -2,11 +2,15 @@ use bevy::prelude::*; use crate::{camera::MainCamera, game_state::GameState, player::Player}; +#[derive(Component)] +pub struct Debug; + #[derive(Component)] pub struct DebugCamera; fn spawn_debug_camera(mut commands: Commands) { commands.spawn(( + Debug, DebugCamera, Camera2d, Camera { @@ -63,6 +67,7 @@ fn spawn_debug_frame(mut commands: Commands) { }; commands.spawn(( + Debug, FramePart(side), Sprite::from_color(Color::srgb(1.0, 0.0, 0.0), size), Visibility::Hidden, @@ -99,18 +104,24 @@ fn draw_view_frame( *visibility = visible; } } + +fn cleanup_debug(mut commands: Commands, entities: Query>) { + for entity in entities { + commands.entity(entity).despawn(); + } +} + pub struct DebugPlugin; impl Plugin for DebugPlugin { fn build(&self, app: &mut App) { app.add_systems( - Startup, + OnEnter(GameState::InGame), ( spawn_debug_camera, setup_debug_camera.after(spawn_debug_camera), spawn_debug_frame, - ) - .run_if(in_state(GameState::InGame)), + ), ) .add_systems( Update, diff --git a/src/game_over.rs b/src/game_over.rs index 9a08751..9ecc980 100644 --- a/src/game_over.rs +++ b/src/game_over.rs @@ -1,12 +1,13 @@ use bevy::prelude::*; -use crate::game_state::GameState; +use crate::{camera::MainCamera, game_state::GameState}; // To delete game over's components #[derive(Component)] pub struct GameOver; -fn game_over(mut commands: Commands) { +fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, With>) { + // Texts commands.spawn(( GameOver, 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>) { diff --git a/src/main_menu.rs b/src/main_menu.rs index 73cb157..4c6376f 100644 --- a/src/main_menu.rs +++ b/src/main_menu.rs @@ -7,6 +7,7 @@ use crate::game_state::GameState; pub struct MainMenu; fn main_menu(mut commands: Commands) { + // Texts commands.spawn(( MainMenu, 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>) { - for entity in menu { - commands.entity(entity).despawn(); - } -} - +// Wait player to press enter for starting the fall fn menu_input(input: Res>, mut next_state: ResMut>) { if input.just_pressed(KeyCode::Enter) { next_state.set(GameState::InGame); } } +// Clean all menu's entities +fn cleanup_menu(mut commands: Commands, menu: Query>) { + for entity in menu { + commands.entity(entity).despawn(); + } +} + pub struct MainMenuPlugin; impl Plugin for MainMenuPlugin { diff --git a/src/player.rs b/src/player.rs index 46a48c5..bb11c1b 100644 --- a/src/player.rs +++ b/src/player.rs @@ -111,11 +111,15 @@ pub fn reset_player( } } +fn cleanup_player(mut commands: Commands, player: Single>) { + commands.entity(*player).despawn(); +} + pub struct PlayerPlugin; impl Plugin for PlayerPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, spawn_player) + app.add_systems(OnEnter(GameState::InGame), spawn_player) .init_resource::() .add_systems( Update, @@ -126,6 +130,7 @@ impl Plugin for PlayerPlugin { .after(check_collision_with_player), ) .run_if(in_state(GameState::InGame)), - ); + ) + .add_systems(OnExit(GameState::InGame), cleanup_player); } } diff --git a/src/score.rs b/src/score.rs index 8e4d69c..e6ce074 100644 --- a/src/score.rs +++ b/src/score.rs @@ -46,16 +46,26 @@ fn update_depth_text( } } +fn cleanup_score(mut commands: Commands, entities: Query>) { + for entity in entities { + commands.entity(entity).despawn(); + } +} + pub struct ScorePlugin; impl Plugin for ScorePlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, spawn_depth_text) - .init_resource::() + app.init_resource::() + .add_systems(OnEnter(GameState::InGame), spawn_depth_text) .add_systems( 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)), - ); + ) + .add_systems(OnExit(GameState::InGame), cleanup_score); } } diff --git a/src/world.rs b/src/world.rs index 1c6b88a..1bc2f16 100644 --- a/src/world.rs +++ b/src/world.rs @@ -57,11 +57,19 @@ fn spawn_chunk(commands: &mut Commands, id: i64, run_seed: u64) { crate::background::spawn_background_chunk(commands, id); // crate::obstacle::spawn_obstacle_chunk(commands, id, run_seed); } + +fn cleanup_chunk(mut commands: Commands, entities: Query>) { + for entity in entities { + commands.entity(entity).despawn(); + } +} + pub struct WorldPlugin; impl Plugin for WorldPlugin { fn build(&self, app: &mut App) { app.insert_resource(RunSeed(rand::random::())) - .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); } }