This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
e88bf88
commit 0bd6895
Showing
6 changed files
with
109 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |