fyrox_impl/utils/behavior/
leaf.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
44
//! Leaf is a "final" node of a behavior tree. It contains user-defined action which
//! is able to mutate given context.

use crate::{
    core::{pool::Handle, visitor::prelude::*},
    utils::behavior::{BehaviorNode, BehaviorTree},
};
use std::cell::RefCell;

/// See module docs.
#[derive(Debug, PartialEq, Visit, Eq, Clone)]
pub struct LeafNode<B>
where
    B: Clone,
{
    /// User-defined behavior.
    pub behavior: Option<RefCell<B>>,
}

impl<B> Default for LeafNode<B>
where
    B: Clone,
{
    fn default() -> Self {
        Self { behavior: None }
    }
}

impl<B> LeafNode<B>
where
    B: Clone + 'static,
{
    /// Creates new leaf node with given action.
    pub fn new(behavior: B) -> Self {
        Self {
            behavior: Some(RefCell::new(behavior)),
        }
    }

    /// 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::Leaf(self))
    }
}