Skip to content

Commit

Permalink
Add condition negation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Feb 8, 2023
1 parent 09cb590 commit 504e39d
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ mod sealed {
}

pub mod common_conditions {
use crate::schedule::{State, States};
use crate::system::{Res, Resource};
use super::Condition;
use crate::{
schedule::{State, States},
system::{In, IntoPipeSystem, ReadOnlySystem, Res, Resource},
};

/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
/// if the first time the condition is run and false every time after
Expand Down Expand Up @@ -105,4 +108,37 @@ pub mod common_conditions {
None => false,
}
}

/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
///
/// # Examples
///
/// ```
/// use bevy_ecs::prelude::*;
/// // Building a new schedule/app...
/// let mut sched = Schedule::default();
/// sched.add_system(
/// // This system will always run.
/// my_system.run_if(not(always_false))
/// )
/// // ...
/// # ;
/// # let mut world = World::new();
/// # sched.run(&mut world);
///
/// // A condition that always returns false.
/// fn always_false() -> bool {
/// false
/// }
///
/// # fn my_system() {}
/// ```
pub fn not<Params, C: Condition<Params>>(
condition: C,
) -> impl ReadOnlySystem<In = (), Out = bool>
where
C::System: ReadOnlySystem,
{
condition.pipe(|In(val): In<bool>| !val)
}
}

0 comments on commit 504e39d

Please sign in to comment.