mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 22:20:20 +00:00
Add any_component_removed
condition (#8326)
Added helper extracted from #7711. that PR contains some controversy conditions, but this one should be good to go. --- ## Changelog ### Added - `any_component_removed` condition. --------- Co-authored-by: François <mockersf@gmail.com>
This commit is contained in:
parent
09df19bcad
commit
315f3cab36
1 changed files with 12 additions and 0 deletions
|
@ -143,6 +143,7 @@ pub mod common_conditions {
|
|||
change_detection::DetectChanges,
|
||||
event::{Event, EventReader},
|
||||
prelude::{Component, Query, With},
|
||||
removal_detection::RemovedComponents,
|
||||
schedule::{State, States},
|
||||
system::{IntoSystem, Res, Resource, System},
|
||||
};
|
||||
|
@ -893,6 +894,17 @@ pub mod common_conditions {
|
|||
move |query: Query<(), With<T>>| !query.is_empty()
|
||||
}
|
||||
|
||||
/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
|
||||
/// if there are any entity with a component of the given type removed.
|
||||
pub fn any_component_removed<T: Component>() -> impl FnMut(RemovedComponents<T>) -> bool {
|
||||
// `RemovedComponents` based on events and therefore events need to be consumed,
|
||||
// so that there are no false positives on subsequent calls of the run condition.
|
||||
// Simply checking `is_empty` would not be enough.
|
||||
// PERF: note that `count` is efficient (not actually looping/iterating),
|
||||
// due to Bevy having a specialized implementation for events.
|
||||
move |mut removals: RemovedComponents<T>| !removals.iter().count() != 0
|
||||
}
|
||||
|
||||
/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
|
||||
///
|
||||
/// # Example
|
||||
|
|
Loading…
Reference in a new issue