Skip to main content

fyrox_impl/utils/behavior/
composite.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Composite node is a container for children nodes. Composite node could be either
22//! `Sequence` or `Selector`. `Sequence` node will execute children nodes consecutively
23//! until `Status::Failure` is returned from any descendant node. In other words `Sequence`
24//! implement AND logical function. `Selector` node will execute children until `Status::Success`
25//! is returned from any descendant node. In other worlds `Selector` implement OR logical
26//! function.
27
28use crate::utils::behavior::BaseBehavior;
29use crate::{
30    core::{pool::Handle, visitor::prelude::*},
31    utils::behavior::{BehaviorNode, BehaviorTree},
32};
33
34/// Defines exact behavior of the composite node.
35#[derive(Debug, PartialEq, Visit, Eq, Clone, Default)]
36pub enum CompositeNodeKind {
37    /// `Sequence` node will execute children nodes consecutively
38    /// until `Status::Failure` is returned from any descendant node. In other words `Sequence`
39    /// implement AND logical function.
40    #[default]
41    Sequence,
42    /// `Selector` node will execute children until `Status::Success`
43    /// is returned from any descendant node. In other worlds `Selector` implement OR logical
44    /// function.
45    Selector,
46}
47
48/// See module docs.
49#[derive(Debug, PartialEq, Visit, Eq, Clone)]
50pub struct CompositeNode<B>
51where
52    B: BaseBehavior,
53{
54    /// A set of children.
55    pub children: Vec<Handle<BehaviorNode<B>>>,
56    /// Current kind of the node.
57    pub kind: CompositeNodeKind,
58}
59
60impl<B> Default for CompositeNode<B>
61where
62    B: BaseBehavior,
63{
64    fn default() -> Self {
65        Self {
66            children: Default::default(),
67            kind: Default::default(),
68        }
69    }
70}
71
72impl<B> CompositeNode<B>
73where
74    B: BaseBehavior,
75{
76    /// Creates new composite node of given kind and set of children nodes.
77    pub fn new(kind: CompositeNodeKind, children: Vec<Handle<BehaviorNode<B>>>) -> Self {
78        Self { children, kind }
79    }
80
81    /// Creates new sequence composite node with a set of children nodes.
82    pub fn new_sequence(children: Vec<Handle<BehaviorNode<B>>>) -> Self {
83        Self {
84            children,
85            kind: CompositeNodeKind::Sequence,
86        }
87    }
88
89    /// Creates new selector composite node with a set of children nodes.
90    pub fn new_selector(children: Vec<Handle<BehaviorNode<B>>>) -> Self {
91        Self {
92            children,
93            kind: CompositeNodeKind::Selector,
94        }
95    }
96
97    /// Adds self to the tree and return handle to self.
98    pub fn add_to(self, tree: &mut BehaviorTree<B>) -> Handle<BehaviorNode<B>> {
99        tree.add_node(BehaviorNode::Composite(self))
100    }
101}