use crate::{
self as behaviortree, Action, BehaviorDescription, BehaviorKind, BehaviorTreeFactory, EMPTY_STR,
behavior::{Behavior, BehaviorCreationFn, BehaviorData, BehaviorResult, BehaviorState},
input_port,
port::PortList,
port_list,
tree::BehaviorTreeElementList,
};
use alloc::{
boxed::Box,
string::{String, ToString},
};
use core::{fmt::Debug, marker::PhantomData, str::FromStr};
use tinyscript::SharedRuntime;
const KEY: &str = "key";
#[derive(Action, Debug, Default)]
#[behavior(no_create, no_register, no_register_with)]
pub struct UnsetBlackboard<T>
where
T: Clone + Debug + Default + FromStr + ToString + Send + Sync + 'static,
{
_marker: PhantomData<T>,
}
#[async_trait::async_trait]
impl<T> Behavior for UnsetBlackboard<T>
where
T: Clone + Debug + Default + FromStr + ToString + Send + Sync,
{
async fn tick(
&mut self,
behavior: &mut BehaviorData,
_children: &mut BehaviorTreeElementList,
_runtime: &SharedRuntime,
) -> BehaviorResult {
let key = behavior.get::<String>(KEY)?;
behavior.delete::<String>(&key)?;
Ok(BehaviorState::Success)
}
fn provided_ports() -> PortList {
port_list![input_port!(
String,
KEY,
EMPTY_STR,
"Key of the entry to remove"
),]
}
}
impl<T> UnsetBlackboard<T>
where
T: Clone + Debug + Default + FromStr + ToString + Send + Sync,
{
#[must_use]
pub fn create_fn() -> Box<BehaviorCreationFn> {
Box::new(move || Box::new(Self { _marker: PhantomData }))
}
pub fn register_with(
factory: &mut BehaviorTreeFactory,
name: &str,
groot2: bool,
) -> Result<(), crate::factory::error::Error> {
let bhvr_desc = BehaviorDescription::new(name, name, BehaviorKind::Action, groot2, Self::provided_ports());
let bhvr_creation_fn = Self::create_fn();
factory
.registry_mut()
.add_behavior(bhvr_desc, bhvr_creation_fn)
}
}