#[allow(unused_imports)]
use crate::events_impl::{
adapt::action,
core::{Bhv, Status},
events::{Event, EventExt, EventKind},
};
pub struct Sel<C>(pub(crate) Box<[Box<dyn Bhv<Context=C>>]>);
pub struct Seq<C>(pub(crate) Box<[Box<dyn Bhv<Context=C>>]>);
impl<C> Bhv for Sel<C> {
type Context = C;
#[inline]
fn react(&mut self, event: &dyn Event, ctx: &mut Self::Context) -> Status {
let et = event.event_type();
for node in self
.0
.iter_mut()
.take_while(|n| n.should_react_to(et)) {
let s = node.react(event, ctx);
if s != Status::Failure {
return s;
}
}
Status::Failure
}
}
impl<C> Bhv for Seq<C> {
type Context = C;
#[inline]
fn react(&mut self, event: &dyn Event, ctx: &mut Self::Context) -> Status {
let et = event.event_type();
let mut count = 0;
for node in self.0
.iter_mut()
.take_while(|n| n.should_react_to(et)) {
let s = node.react(event, ctx);
if s != Status::Success {
return s;
}
count += 1;
}
if count != self.0.len() {
Status::Failure
} else {
Status::Success
}
}
}
#[macro_export]
macro_rules! sel {
() => {
compile_error!("`sel` should have at least one argument!")
};
($($x:expr),+$(,)?) => {
$crate::Sel(
Box::new([$(Box::new($x)),+]),
)
};
}
#[macro_export]
macro_rules! seq {
() => {
compile_error!("`seq` should have at least one argument!")
};
($($x:expr),+$(,)?) => {
$crate::Seq(
Box::new([$(Box::new($x)),+]),
)
};
}