Skip to content

Commit

Permalink
Clean up entities after game exit
Browse files Browse the repository at this point in the history
Fixes #135.
  • Loading branch information
Indy2222 committed Feb 7, 2023
1 parent 8fe3a81 commit f668ed1
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 25 deletions.
13 changes: 9 additions & 4 deletions crates/camera/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::f32::consts::FRAC_PI_2;
use bevy::prelude::*;
use de_conf::{CameraConf, Configuration};
use de_core::{
cleanup::DespawnOnGameExit,
events::ResendEventPlugin,
projection::ToAltitude,
stages::GameStage,
Expand Down Expand Up @@ -313,10 +314,14 @@ fn setup(mut commands: Commands, conf: Res<Configuration>) {
point: Vec3::ZERO,
distance,
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, distance.into(), 0.0).looking_at(Vec3::ZERO, -Vec3::Z),
..Default::default()
});
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, distance.into(), 0.0)
.looking_at(Vec3::ZERO, -Vec3::Z),
..Default::default()
},
DespawnOnGameExit,
));
}

fn cleanup(mut commands: Commands) {
Expand Down
2 changes: 2 additions & 0 deletions crates/combat/src/trail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bevy::{
},
};
use de_core::{
cleanup::DespawnOnGameExit,
stages::GameStage,
state::{AppState, GameState},
};
Expand Down Expand Up @@ -131,6 +132,7 @@ fn spawn(
..Default::default()
},
Trail::default(),
DespawnOnGameExit,
));
}
}
Expand Down
17 changes: 11 additions & 6 deletions crates/controller/src/draft.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use de_core::{
cleanup::DespawnOnGameExit,
gconfig::GameConfig,
objects::{BuildingType, ObjectType},
stages::GameStage,
Expand Down Expand Up @@ -93,6 +94,7 @@ fn spawn(
commands.spawn((
SpawnBundle::new(object_type, transform),
game_config.player(),
DespawnOnGameExit,
));
}
}
Expand All @@ -112,12 +114,15 @@ fn new_drafts(
commands.entity(entity).despawn_recursive();
}

commands.spawn(DraftBundle::new(
event.building_type(),
Transform {
translation: event.point(),
..Default::default()
},
commands.spawn((
DraftBundle::new(
event.building_type(),
Transform {
translation: event.point(),
..Default::default()
},
),
DespawnOnGameExit,
));
}

Expand Down
4 changes: 2 additions & 2 deletions crates/controller/src/hud/minimap/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{prelude::*, render::texture::TextureFormatPixelInfo};
use de_core::{stages::GameStage, state::GameState};
use de_core::{cleanup::DespawnOnGameExit, stages::GameStage, state::GameState};
use iyes_loopless::prelude::*;
use wgpu_types::{Extent3d, TextureDimension, TextureFormat};

Expand Down Expand Up @@ -43,7 +43,7 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
background_color: HUD_COLOR.into(),
..default()
})
.insert(InteractionBlocker)
.insert((InteractionBlocker, DespawnOnGameExit))
.with_children(|parent| {
parent
.spawn(ImageBundle {
Expand Down
4 changes: 3 additions & 1 deletion crates/controller/src/hud/panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use de_core::state::GameState;
use de_core::{cleanup::DespawnOnGameExit, state::GameState};
use iyes_loopless::prelude::*;

use super::{interaction::InteractionBlocker, HUD_COLOR};
Expand Down Expand Up @@ -33,6 +33,7 @@ fn spawn_details(mut commands: Commands) {
background_color: HUD_COLOR.into(),
..default()
},
DespawnOnGameExit,
InteractionBlocker,
));
}
Expand All @@ -57,6 +58,7 @@ fn spawn_action_bar(mut commands: Commands) {
background_color: HUD_COLOR.into(),
..default()
},
DespawnOnGameExit,
InteractionBlocker,
));
}
5 changes: 4 additions & 1 deletion crates/controller/src/hud/selection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;
use de_core::{screengeom::ScreenRect, stages::GameStage, state::AppState};
use de_core::{
cleanup::DespawnOnGameExit, screengeom::ScreenRect, stages::GameStage, state::AppState,
};
use iyes_loopless::prelude::*;

const SELECTION_BOX_COLOR: Color = Color::rgba(0., 0.5, 0.8, 0.2);
Expand Down Expand Up @@ -67,6 +69,7 @@ fn process_events(
..Default::default()
},
SelectionBox,
DespawnOnGameExit,
));
}
}
Expand Down
23 changes: 23 additions & 0 deletions crates/core/src/cleanup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use bevy::prelude::*;
use iyes_loopless::prelude::*;

use crate::state::AppState;

pub(crate) struct CleanupPlugin;

impl Plugin for CleanupPlugin {
fn build(&self, app: &mut App) {
app.add_exit_system(AppState::InGame, cleanup);
}
}

/// Mark all entities which should be recursively despawned after the game is
/// exited with this component.
#[derive(Component)]
pub struct DespawnOnGameExit;

fn cleanup(mut commands: Commands, query: Query<Entity, With<DespawnOnGameExit>>) {
for entity in query.iter() {
commands.entity(entity).despawn_recursive();
}
}
3 changes: 3 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bevy::{app::PluginGroupBuilder, prelude::PluginGroup};
use cleanup::CleanupPlugin;
use iyes_progress::prelude::*;
use stages::StagesPlugin;
use state::{AppState, GameState};
use visibility::VisibilityPlugin;

pub mod assets;
pub mod cleanup;
mod errors;
pub mod events;
pub mod frustum;
Expand All @@ -26,5 +28,6 @@ impl PluginGroup for CorePluginGroup {
.add(ProgressPlugin::new(GameState::Loading).continue_to(GameState::Playing))
.add(StagesPlugin)
.add(VisibilityPlugin)
.add(CleanupPlugin)
}
}
29 changes: 18 additions & 11 deletions crates/loader/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy::{
use de_camera::MoveFocusEvent;
use de_core::{
assets::asset_path,
cleanup::DespawnOnGameExit,
gconfig::GameConfig,
log_full_error,
objects::{ActiveObjectType, BuildingType, ObjectType},
Expand Down Expand Up @@ -102,7 +103,10 @@ fn spawn_map(
}

setup_light(&mut commands);
commands.spawn(TerrainBundle::flat(map.metadata().bounds()));
commands.spawn((
TerrainBundle::flat(map.metadata().bounds()),
DespawnOnGameExit,
));

for object in map.content().objects() {
let mut entity_commands = commands.spawn_empty();
Expand All @@ -113,9 +117,9 @@ fn spawn_map(
}
InnerObject::Inactive(object) => ObjectType::Inactive(object.object_type()),
};
entity_commands.insert(SpawnBundle::new(
object_type,
object.placement().to_transform(),
entity_commands.insert((
SpawnBundle::new(object_type, object.placement().to_transform()),
DespawnOnGameExit,
));
}

Expand All @@ -131,13 +135,16 @@ fn setup_light(commands: &mut Commands) {

let mut transform = Transform::IDENTITY;
transform.look_at(Vec3::new(1., -1., 0.), Vec3::new(1., 1., 0.));
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::WHITE,
illuminance: 30000.,
commands.spawn((
DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::WHITE,
illuminance: 30000.,
..Default::default()
},
transform,
..Default::default()
},
transform,
..Default::default()
});
DespawnOnGameExit,
));
}

0 comments on commit f668ed1

Please sign in to comment.