simple_bt 1.0.0

minimal(-ish) behavior tree implementation
Documentation
use crate::{BehaviorArc, BehaviorNode, NodeResult};
use std::sync::Arc;

/// Always succeedes.
pub struct Succeeder<B> {
    child: Option<BehaviorArc<B>>,
}

impl<B> std::fmt::Debug for Succeeder<B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Succeeder")
            .field("child", &self.child)
            .finish()
    }
}

impl<B> Clone for Succeeder<B> {
    fn clone(&self) -> Self {
        Self {
            child: self.child.clone(),
        }
    }
}

impl<B> Default for Succeeder<B> {
    fn default() -> Self {
        Self { child: None }
    }
}

impl<B> Succeeder<B> {
    pub fn new(child: BehaviorArc<B>) -> Self {
        Self { child: Some(child) }
    }
}

impl<B: 'static> BehaviorNode<B> for Succeeder<B> {
    fn tick(mut self: Arc<Self>, blackboard: &mut B) -> crate::NodeResult<B> {
        if self.child.is_some() {
            let inner = Arc::make_mut(&mut self);
            if let Some(child) = inner.child.take() {
                match child.tick(blackboard) {
                    NodeResult::Failure | NodeResult::Success => NodeResult::Success,
                    NodeResult::Running(resume) => {
                        inner.child = Some(resume);
                        NodeResult::Running(self)
                    }
                }
            } else {
                unreachable!()
            }
        } else {
            NodeResult::Success
        }
    }
}

#[cfg(test)]
mod tests {}