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:
Hennadii Chernyshchyk 2023-04-18 17:18:09 +03:00 committed by GitHub
parent 09df19bcad
commit 315f3cab36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -143,6 +143,7 @@ pub mod common_conditions {
change_detection::DetectChanges, change_detection::DetectChanges,
event::{Event, EventReader}, event::{Event, EventReader},
prelude::{Component, Query, With}, prelude::{Component, Query, With},
removal_detection::RemovedComponents,
schedule::{State, States}, schedule::{State, States},
system::{IntoSystem, Res, Resource, System}, system::{IntoSystem, Res, Resource, System},
}; };
@ -893,6 +894,17 @@ pub mod common_conditions {
move |query: Query<(), With<T>>| !query.is_empty() 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. /// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
/// ///
/// # Example /// # Example