feat: add game entity cleanup

This commit is contained in:
Kristofers Solo 2025-01-18 16:40:29 +02:00
parent 7a4bcd81f9
commit 7fa567d522
6 changed files with 33 additions and 10 deletions

View File

@ -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();

View File

@ -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,
));
}

View File

@ -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::<Screen>));
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<Entity, With<GameplayElement>>,
state: Res<State<Screen>>,
) {
if !matches!(*state.get(), Screen::Gameplay | Screen::Pause) {
for entity in query.iter() {
commands.entity(entity).despawn_recursive();
}
}
}

View File

@ -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::<Screen>();

View File

@ -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
}

View File

@ -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);
});
}