Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Fix panic when placing ghost (#765)
Browse files Browse the repository at this point in the history
* Add EntityCommands::try_remove and try_insert

* Swap to try_insert

* Add try_add_child

* Use try_add_child and fix the panic

* Clippy
  • Loading branch information
alice-i-cecile authored Apr 25, 2023
1 parent e88bf88 commit 0bd6895
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 41 deletions.
81 changes: 43 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion emergence_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
pub mod asset_management;
pub mod construction;
pub mod crafting;
pub mod curves;
pub mod enum_iter;
pub mod filtered_array_iter;
pub mod graphics;
Expand All @@ -25,6 +24,7 @@ pub mod structures;
pub mod terrain;
pub mod ui;
pub mod units;
pub mod utils;
pub mod world_gen;

/// Various app configurations, used for testing.
Expand Down
5 changes: 3 additions & 2 deletions emergence_lib/src/ui/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
goals::{Goal, GoalKind},
unit_manifest::Unit,
},
utils::fallible_commands::FallibleEntityCommandExt,
};

use super::ui_assets::Icons;
Expand Down Expand Up @@ -185,10 +186,10 @@ fn add_status_displays(
// - it will be hidden when the parent entity is hidden
commands
.entity(parent_entity)
.insert(StatusParent {
.try_insert(StatusParent {
entity: status_entity,
})
.add_child(status_entity);
.try_add_child(status_entity);
}
}

Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions emergence_lib/src/utils/fallible_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Contains [`FallibleEntityCommandExt`] and related code.
use bevy::{ecs::system::EntityCommands, prelude::*};

/// An extension trait for [`EntityCommands`] that has fallible versions of
/// the most commonly used commands.
pub trait FallibleEntityCommandExt<'w, 's, 'a> {
/// Attempts to add a component or bundle to the entity.
///
/// Fails silently (rather than panicking) if the entity does not exist.
///
/// Fallible version of [`EntityCommands::insert`].
fn try_insert(&mut self, bundle: impl Bundle) -> &mut EntityCommands<'w, 's, 'a>;

/// Attempts to remove a component or bundle from the entity.
///
/// Fails silently (rather than panicking) if the entity does not exist.
///
/// Fallible version of [`EntityCommands::remove`].
fn try_remove<B: Bundle>(&mut self) -> &mut EntityCommands<'w, 's, 'a>;

/// Attempts to add a child entity to the entity.
///
/// Fails silently (rather than panicking) if the entity does not exist.
///
/// Fallible version of [`BuildChildren::add_child`].
fn try_add_child(&mut self, child: Entity) -> &mut EntityCommands<'w, 's, 'a>;
}

impl<'w, 's, 'a> FallibleEntityCommandExt<'w, 's, 'a> for EntityCommands<'w, 's, 'a> {
fn try_insert(&mut self, bundle: impl Bundle) -> &mut Self {
self.add(|entity, world: &mut World| {
if let Some(mut entity_mut) = world.get_entity_mut(entity) {
entity_mut.insert(bundle);
}
});
self
}

fn try_remove<B: Bundle>(&mut self) -> &mut Self {
self.add(|entity, world: &mut World| {
if let Some(mut entity_mut) = world.get_entity_mut(entity) {
entity_mut.remove::<B>();
}
});
self
}

fn try_add_child(&mut self, child: Entity) -> &mut EntityCommands<'w, 's, 'a> {
self.add(move |entity, world: &mut World| {
if let Some(mut entity_mut) = world.get_entity_mut(entity) {
entity_mut.add_child(child);
}
});
self
}
}
4 changes: 4 additions & 0 deletions emergence_lib/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Simple gameplay-agnostic utilities.
pub mod curves;
pub mod fallible_commands;

0 comments on commit 0bd6895

Please sign in to comment.