From 7fa567d522a7f96ad4423d7c8ec8c252866b7edd Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sat, 18 Jan 2025 16:40:29 +0200 Subject: [PATCH] feat: add game entity cleanup --- src/maze/systems/spawn.rs | 2 ++ src/player/systems/spawn.rs | 2 ++ src/screens/gameplay.rs | 17 +++++++++++++++++ src/screens/mod.rs | 2 +- src/stats/systems/score.rs | 2 -- src/stats/systems/spawn.rs | 18 +++++++++++------- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/maze/systems/spawn.rs b/src/maze/systems/spawn.rs index 26b94b9..c8e2887 100644 --- a/src/maze/systems/spawn.rs +++ b/src/maze/systems/spawn.rs @@ -15,6 +15,7 @@ use crate::{ components::{HexMaze, MazeConfig, Tile, Wall}, resources::GlobalMazeConfig, }, + screens::GameplayElement, theme::palette::rose_pine::RosePineDawn, }; @@ -61,6 +62,7 @@ pub fn spawn_maze( config.clone(), Transform::from_translation(Vec3::ZERO.with_y(y_offset)), Visibility::Visible, + GameplayElement, )) .insert_if(CurrentFloor, || floor == 1) // Only floor 1 gets CurrentFloor .id(); diff --git a/src/player/systems/spawn.rs b/src/player/systems/spawn.rs index e37aa6b..41b9669 100644 --- a/src/player/systems/spawn.rs +++ b/src/player/systems/spawn.rs @@ -5,6 +5,7 @@ use crate::{ assets::{blue_material, generate_pill_mesh}, components::{CurrentPosition, Player}, }, + screens::GameplayElement, }; use bevy::prelude::*; @@ -32,5 +33,6 @@ 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), + GameplayElement, )); } diff --git a/src/screens/gameplay.rs b/src/screens/gameplay.rs index 61582b2..589847c 100644 --- a/src/screens/gameplay.rs +++ b/src/screens/gameplay.rs @@ -23,6 +23,7 @@ pub(super) fn plugin(app: &mut App) { app.add_systems(OnEnter(Screen::Gameplay), |mut commands: Commands| { commands.insert_resource(GameplayInitialized(true)); }); + app.add_systems(Update, cleanup_game.run_if(state_changed::)); app.add_systems(OnEnter(Screen::Title), reset_gameplay_state); @@ -43,3 +44,19 @@ fn reset_gameplay_state(mut commands: Commands) { #[derive(Debug, Default, Reflect, Resource, DerefMut, Deref)] #[reflect(Resource)] pub struct GameplayInitialized(bool); + +#[derive(Debug, Reflect, Component)] +#[reflect(Component)] +pub struct GameplayElement; + +fn cleanup_game( + mut commands: Commands, + query: Query>, + state: Res>, +) { + if !matches!(*state.get(), Screen::Gameplay | Screen::Pause) { + for entity in query.iter() { + commands.entity(entity).despawn_recursive(); + } + } +} diff --git a/src/screens/mod.rs b/src/screens/mod.rs index 643245b..8b0c2a8 100644 --- a/src/screens/mod.rs +++ b/src/screens/mod.rs @@ -7,7 +7,7 @@ mod splash; mod title; use bevy::prelude::*; -pub use gameplay::GameplayInitialized; +pub use gameplay::{GameplayElement, GameplayInitialized}; pub(super) fn plugin(app: &mut App) { app.init_state::(); diff --git a/src/stats/systems/score.rs b/src/stats/systems/score.rs index 9559902..6bb1864 100644 --- a/src/stats/systems/score.rs +++ b/src/stats/systems/score.rs @@ -59,8 +59,6 @@ fn calculate_score(floor_number: u8, completion_time: f32) -> usize { time_factor.max(MIN_TIME_MULTIPLIER) * TIME_BONUS_MULTIPLIER }; - dbg!(base_score * time_multiplier); - (base_score * time_multiplier) as usize } diff --git a/src/stats/systems/spawn.rs b/src/stats/systems/spawn.rs index 0ec6646..81d4b55 100644 --- a/src/stats/systems/spawn.rs +++ b/src/stats/systems/spawn.rs @@ -1,6 +1,7 @@ use bevy::prelude::*; use crate::{ + screens::GameplayElement, stats::{ components::{ FloorDisplay, FloorTimerDisplay, HighestFloorDisplay, ScoreDisplay, TotalTimerDisplay, @@ -11,11 +12,14 @@ use crate::{ }; pub fn spawn_stats(mut commands: Commands) { - 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); - }); + commands + .ui_stats() + .insert(GameplayElement) + .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); + }); }