fyrox_impl/utils/behavior/inverter.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
//! A node, that inverts its child state ([`super::Status::Failure`] becomes [`super::Status::Success`] and vice versa, [`super::Status::Running`] remains
//! unchanged)
use crate::{
core::{pool::Handle, visitor::prelude::*},
utils::behavior::{BehaviorNode, BehaviorTree},
};
/// See module docs.
#[derive(Debug, PartialEq, Visit, Eq, Clone)]
pub struct Inverter<B>
where
B: Clone,
{
/// A handle of child node, the state of which will be inverted.
pub child: Handle<BehaviorNode<B>>,
}
impl<B> Default for Inverter<B>
where
B: Clone,
{
fn default() -> Self {
Self {
child: Default::default(),
}
}
}
impl<B> Inverter<B>
where
B: Clone + 'static,
{
/// Creates new inverter node with given action.
pub fn new(child: Handle<BehaviorNode<B>>) -> Self {
Self { child }
}
/// Adds self to given behavior tree and returns handle to self.
pub fn add_to(self, tree: &mut BehaviorTree<B>) -> Handle<BehaviorNode<B>> {
tree.add_node(BehaviorNode::Inverter(self))
}
}