Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Incorrectly despawned inspect labels (#137)
Browse files Browse the repository at this point in the history
* Adding names to entities for debugging purposes

* Despawning recursively to avoid lingering references in Children
  • Loading branch information
porkbrain authored Apr 2, 2024
1 parent 3348bf8 commit d57a99e
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 45 deletions.
1 change: 1 addition & 0 deletions bevy_magic_light_2d/src/gi/compositing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub fn setup_post_processing_quad<T: LightScene>(
let layer = RenderLayers::layer(T::render_layer_index());

cmd.spawn((
Name::new("PostProcessingQuad"),
T::default(),
PostProcessingQuad,
MaterialMesh2dBundle {
Expand Down
2 changes: 1 addition & 1 deletion common/story/src/dialog/fe/portrait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ impl PortraitDialog {
camera: Entity,
root: Entity,
) {
cmd.entity(camera).despawn();
cmd.entity(camera).despawn_recursive();
cmd.entity(root).despawn_recursive();
cmd.remove_resource::<PortraitDialog>();

Expand Down
2 changes: 1 addition & 1 deletion common/story/src/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn play_next(
first,
last: event.emoji.animation_last_frame(),
play: AtlasAnimationStep::Forward,
on_last_frame: AtlasAnimationEnd::DespawnItself,
on_last_frame: AtlasAnimationEnd::DespawnRecursiveItself,
extra_steps: event.emoji.extra_steps(),
})
.insert(AtlasAnimationTimer::new_fps(event.emoji.fps()));
Expand Down
3 changes: 2 additions & 1 deletion common/visuals/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy::{
query::With,
system::{Commands, Query},
},
hierarchy::DespawnRecursiveExt,
render::{camera::Camera, view::RenderLayers},
utils::default,
};
Expand Down Expand Up @@ -92,5 +93,5 @@ pub fn spawn(mut cmd: Commands) {

/// System to despawn 2D camera with component [`MainCamera`].
pub fn despawn(mut cmd: Commands, camera: Query<Entity, With<MainCamera>>) {
cmd.entity(camera.single()).despawn();
cmd.entity(camera.single()).despawn_recursive();
}
1 change: 1 addition & 0 deletions common/visuals/src/fps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) fn spawn(mut cmd: Commands) {
// create our text
let text_fps = cmd
.spawn((
Name::new("FPS text"),
FpsText,
TextBundle {
// use two sections, so it is easy to update just the number
Expand Down
16 changes: 8 additions & 8 deletions common/visuals/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub fn advance_atlas_animation(
AtlasAnimationStep::Backward => animation.last,
};
}
AtlasAnimationEnd::DespawnItself => {
cmd.entity(entity).despawn();
AtlasAnimationEnd::DespawnRecursiveItself => {
cmd.entity(entity).despawn_recursive();
}
AtlasAnimationEnd::RemoveTimer => {
cmd.entity(entity).remove::<AtlasAnimationTimer>();
Expand Down Expand Up @@ -218,8 +218,8 @@ pub fn interpolate(
Some(OnInterpolationFinished::Custom(fun)) => {
fun(&mut cmd);
}
Some(OnInterpolationFinished::DespawnItself) => {
cmd.entity(entity).despawn();
Some(OnInterpolationFinished::DespawnRecursiveItself) => {
cmd.entity(entity).despawn_recursive();
}
None => {}
}
Expand Down Expand Up @@ -267,8 +267,8 @@ pub fn interpolate(
Some(OnInterpolationFinished::Custom(fun)) => {
fun(&mut cmd);
}
Some(OnInterpolationFinished::DespawnItself) => {
cmd.entity(entity).despawn();
Some(OnInterpolationFinished::DespawnRecursiveItself) => {
cmd.entity(entity).despawn_recursive();
}
None => {}
}
Expand Down Expand Up @@ -304,8 +304,8 @@ pub fn interpolate(
Some(OnInterpolationFinished::Custom(fun)) => {
fun(&mut cmd);
}
Some(OnInterpolationFinished::DespawnItself) => {
cmd.entity(entity).despawn();
Some(OnInterpolationFinished::DespawnRecursiveItself) => {
cmd.entity(entity).despawn_recursive();
}
None => {}
}
Expand Down
8 changes: 4 additions & 4 deletions common/visuals/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub enum AtlasAnimationEnd {
/// Keeps the entity visible and on the last frame.
RemoveTimer,
/// Despawns the animated entity.
DespawnItself,
DespawnRecursiveItself,
/// Can optionally mutate state.
Custom {
/// The function to call when the last frame is reached.
Expand Down Expand Up @@ -162,7 +162,7 @@ pub struct BeginInterpolationEvent {
#[derive(Clone)]
pub(crate) enum OnInterpolationFinished {
/// Despawn the entity.
DespawnItself,
DespawnRecursiveItself,
/// Can schedule commands.
Custom(Arc<dyn Fn(&mut Commands) + Send + Sync>),
}
Expand Down Expand Up @@ -370,8 +370,8 @@ impl BeginInterpolationEvent {
}

/// Despawn the entity when interpolation is done.
pub fn when_finished_despawn_itself(self) -> Self {
self.when_finished(OnInterpolationFinished::DespawnItself)
pub fn when_finished_despawn_recursive_itself(self) -> Self {
self.when_finished(OnInterpolationFinished::DespawnRecursiveItself)
}

/// Any extra logic when interpolation is done?
Expand Down
6 changes: 3 additions & 3 deletions main_game_lib/src/cutscene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,9 @@ impl Cutscene {
trace!("Despawning cutscene");
cmd.remove_resource::<Cutscene>();

cmd.entity(entities[0]).despawn();
cmd.entity(entities[1]).despawn();
cmd.entity(entities[2]).despawn();
cmd.entity(entities[0]).despawn_recursive();
cmd.entity(entities[1]).despawn_recursive();
cmd.entity(entities[2]).despawn_recursive();
})
.insert(cmd);
} else {
Expand Down
4 changes: 1 addition & 3 deletions main_game_lib/src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ impl bevy::app::Plugin for Plugin {

#[cfg(feature = "devtools")]
{
use bevy_inspector_egui::quick::ResourceInspectorPlugin;

app.register_type::<daybar::DayBar>()
.add_plugins(ResourceInspectorPlugin::<daybar::DayBar>::new());
.add_systems(Update, daybar::change_progress);
}
}
}
37 changes: 34 additions & 3 deletions main_game_lib/src/hud/daybar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//!
//! See wiki for more information about day progress.
use bevy::ui::RelativeCursorPosition;
use common_ext::QueryExt;
use common_visuals::camera::MainCamera;

Expand All @@ -23,6 +24,11 @@ pub enum IncreaseDayBarEvent {
ChangedScene,
/// Finished meditating.
Meditated,
/// Sets the daybar progress to 0.
Reset,
/// Custom amount of increase in the day bar.
/// The final progress is clamped between 0 and 1.
Custom(f32),
}

/// What sort of things are dependent on status of the daybar.
Expand All @@ -47,6 +53,8 @@ pub(crate) fn spawn(
Name::new("DayBar"),
DayBarRoot,
TargetCamera(camera.single()),
Interaction::default(),
RelativeCursorPosition::default(),
NodeBundle {
style: Style {
position_type: PositionType::Absolute,
Expand Down Expand Up @@ -97,12 +105,35 @@ pub(crate) fn increase(
let amount = match event {
IncreaseDayBarEvent::ChangedScene => 0.01,
IncreaseDayBarEvent::Meditated => 0.05,
IncreaseDayBarEvent::Custom(amount) => *amount,
IncreaseDayBarEvent::Reset => -daybar.progress,
};

daybar.progress = (daybar.progress + amount).clamp(0.0, 1.0);
}

if let Some(mut progress) = progress.get_single_mut_or_none() {
progress.width = Val::Percent(daybar.progress * 100.0);
}
}

#[cfg(feature = "devtools")]
pub(crate) fn change_progress(
mut events: EventWriter<IncreaseDayBarEvent>,

root: Query<
(&Interaction, &RelativeCursorPosition),
(Changed<Interaction>, With<DayBarRoot>),
>,
) {
for (interaction, cursor_position) in root.iter() {
if !matches!(interaction, Interaction::Pressed) {
continue;
}

if let Some(mut progress) = progress.get_single_mut_or_none() {
progress.width = Val::Percent(daybar.progress * 100.0);
if let Some(position) = cursor_position.normalized {
events.send(IncreaseDayBarEvent::Reset);
events.send(IncreaseDayBarEvent::Custom(position.x));
}
}
}
Expand All @@ -122,7 +153,7 @@ impl DayBar {
/// Whether it's time for something to happen.
pub fn is_it_time_for(&self, what: DayBarDependent) -> bool {
let range = match what {
DayBarDependent::MallOpenHours => 0.05..0.75,
DayBarDependent::MallOpenHours => ..0.75,
};

range.contains(&self.progress)
Expand Down
6 changes: 4 additions & 2 deletions main_game_lib/src/rscn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ use std::borrow::Cow;

use bevy::{
asset::{Asset, AssetServer, Assets, Handle},
core::Name,
ecs::{
component::Component,
entity::Entity,
system::{Commands, Query, Res},
},
hierarchy::DespawnRecursiveExt,
math::{Rect, Vec2},
reflect::TypePath,
render::color::Color,
Expand Down Expand Up @@ -205,7 +207,7 @@ pub fn start_loading_tscn<T: TscnInBevy>(
mut cmd: Commands,
asset_server: Res<AssetServer>,
) {
let mut e = cmd.spawn_empty();
let mut e = cmd.spawn(Name::new(".tscn tree handle"));
e.insert(TscnTreeHandle::<T> {
entity: e.id(),
handle: Some(asset_server.load(T::tscn_asset_path())),
Expand Down Expand Up @@ -239,7 +241,7 @@ impl<T> TscnTreeHandle<T> {
) -> TscnTree {
let handle = self.handle.take().expect("Handle already consumed");
let tscn = assets.remove(handle).expect("Handle not loaded");
cmd.entity(self.entity).despawn();
cmd.entity(self.entity).despawn_recursive();
tscn
}
}
Expand Down
1 change: 1 addition & 0 deletions main_game_lib/src/rscn/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,6 @@ fn node_to_entity<T: TscnSpawner>(
..default()
});

bevy::log::trace!("Spawning {entity:?} {name:?} from scene file",);
spawner.on_spawned(cmd, entity, name, translation);
}
16 changes: 8 additions & 8 deletions main_game_lib/src/top_down/inspect_and_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ pub(crate) fn change_highlighted_label(
return;
};

cmd.entity(displayed.bg).despawn();
cmd.entity(displayed.text).despawn();
cmd.entity(displayed.bg).despawn_recursive();
cmd.entity(displayed.text).despawn_recursive();

let displayed =
spawn_label_bg_and_text(&mut cmd, &asset_server, label, spawn_params);
Expand Down Expand Up @@ -273,8 +273,8 @@ pub(crate) fn highlight_what_would_be_interacted_with(
cmd.entity(highlighted)
.remove::<HighlightedForInteraction>();

cmd.entity(old_displayed.bg).despawn();
cmd.entity(old_displayed.text).despawn();
cmd.entity(old_displayed.bg).despawn_recursive();
cmd.entity(old_displayed.text).despawn_recursive();

let mut new_displayed = spawn_label_bg_and_text(
&mut cmd,
Expand Down Expand Up @@ -335,8 +335,8 @@ pub(crate) fn highlight_what_would_be_interacted_with(
remove_old_highlight_if_present();

if let Some(InspectLabelDisplayed { bg, text, .. }) = displayed {
cmd.entity(*bg).despawn();
cmd.entity(*text).despawn();
cmd.entity(*bg).despawn_recursive();
cmd.entity(*text).despawn_recursive();
}

let displayed = spawn_label_bg_and_text(
Expand Down Expand Up @@ -575,15 +575,15 @@ impl InspectLabelDisplayed {
.with_animation_curve(TEXT_ANIMATION_CURVE.clone())
.when_finished_do(move |cmd| {
cmd.entity(label_entity).remove::<Self>();
cmd.entity(text).despawn();
cmd.entity(text).despawn_recursive();
}),
);

begin_interpolation.send(
BeginInterpolationEvent::of_color(bg, None, Color::NONE)
.over(FADE_OUT_IN)
.with_animation_curve(BG_ANIMATION_CURVE.clone())
.when_finished_despawn_itself(),
.when_finished_despawn_recursive_itself(),
);
}

Expand Down
4 changes: 2 additions & 2 deletions main_game_lib/src/top_down/layout/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn start_loading_map<T: TopDownScene>(
let asset_path = format!("maps/{}.ron", T::name());
debug!("Loading map {} from {}", T::type_path(), asset_path);
let handle: Handle<TileMap<T>> = assets.load(asset_path);
cmd.spawn(handle);
cmd.spawn((Name::new(format!("TileMap<{}>", T::type_path())), handle));
}

/// Run this to wait for the map to be loaded and insert it as a resource.
Expand Down Expand Up @@ -52,7 +52,7 @@ pub(crate) fn try_insert_map_as_resource<T: TopDownScene>(

cmd.insert_resource(loaded_map);
cmd.init_resource::<crate::top_down::actor::ActorZoneMap<T::LocalTileKind>>();
cmd.entity(entity).despawn();
cmd.entity(entity).despawn_recursive();
}
}

Expand Down
2 changes: 0 additions & 2 deletions scenes/building1_basement1/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ impl<'a> TscnSpawner for Spawner<'a> {
}
_ => {}
}

trace!("Spawned {name:?} as {who:?} from scene file");
}

fn add_texture_atlas(
Expand Down
2 changes: 0 additions & 2 deletions scenes/building1_player_floor/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ impl<'a> TscnSpawner for Spawner<'a> {
}
_ => {}
}

trace!("Spawned {name:?} as {who:?} from scene file");
}

fn handle_plain_node(
Expand Down
6 changes: 4 additions & 2 deletions scenes/downtown/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ fn despawn(mut cmd: Commands, root: Query<Entity, With<LayoutEntity>>) {

let root = root.single();
cmd.entity(root).despawn_recursive();

cmd.remove_resource::<ZoneToInspectLabelEntity<
<Downtown as TopDownScene>::LocalTileKind,
>>();
}

impl<'a> TscnSpawner for Spawner<'a> {
Expand Down Expand Up @@ -156,8 +160,6 @@ impl<'a> TscnSpawner for Spawner<'a> {
}
_ => {}
}

trace!("Spawned {name:?} as {who:?} from scene file");
}

fn add_texture_atlas(
Expand Down
2 changes: 0 additions & 2 deletions scenes/mall/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ impl<'a> TscnSpawner for Spawner<'a> {
}
_ => {}
}

trace!("Spawned {name:?} as {who:?} from scene file");
}

fn add_texture_atlas(
Expand Down
2 changes: 1 addition & 1 deletion scenes/meditation/src/polpos/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) mod bolt {
let lives_for = bolt.spawned_at.elapsed();

if lives_for > BOLT_LIFETIME {
cmd.entity(entity).despawn();
cmd.entity(entity).despawn_recursive();
} else {
let lerp_factor =
lives_for.as_secs_f32() / BOLT_LIFETIME.as_secs_f32();
Expand Down

0 comments on commit d57a99e

Please sign in to comment.