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::{
29    core::{pool::Handle, visitor::prelude::*},
30    utils::behavior::{BehaviorNode, BehaviorTree},
31};
32
33/// Defines exact behavior of the composite node.
34#[derive(Debug, PartialEq, Visit, Eq, Clone)]
35pub enum CompositeNodeKind {
36    /// `Sequence` node will execute children nodes consecutively
37    /// until `Status::Failure` is returned from any descendant node. In other words `Sequence`
38    /// implement AND logical function.
39    Sequence,
40    /// `Selector` node will execute children until `Status::Success`
41    /// is returned from any descendant node. In other worlds `Selector` implement OR logical
42    /// function.
43    Selector,
44}
45
46impl Default for CompositeNodeKind {
47    fn default() -> Self {
48        Self::Sequence
49    }
50}
51
52/// See module docs.
53#[derive(Debug, PartialEq, Visit, Eq, Clone)]
54pub struct CompositeNode<B>
55where
56    B: Clone,
57{
58    /// A set of children.
59    pub children: Vec<Handle<BehaviorNode<B>>>,
60    /// Current kind of the node.
61    pub kind: CompositeNodeKind,
62}
63
64impl<B> Default for CompositeNode<B>
65where
66    B: Clone,
67{
68    fn default() -> Self {
69        Self {
70            children: Default::default(),
71            kind: Default::default(),
72        }
73    }
74}
75
76impl<B> CompositeNode<B>
77where
78    B: Clone + 'static,
79{
80    /// Creates new composite node of given kind and set of children nodes.
81    pub fn new(kind: CompositeNodeKind, children: Vec<Handle<BehaviorNode<B>>>) -> Self {
82        Self { children, kind }
83    }
84
85    /// Creates new sequence composite node with a set of children nodes.
86    pub fn new_sequence(children: Vec<Handle<BehaviorNode<B>>>) -> Self {
87        Self {
88            children,
89            kind: CompositeNodeKind::Sequence,
90        }
91    }
92
93    /// Creates new selector composite node with a set of children nodes.
94    pub fn new_selector(children: Vec<Handle<BehaviorNode<B>>>) -> Self {
95        Self {
96            children,
97            kind: CompositeNodeKind::Selector,
98        }
99    }
100
101    /// Adds self to the tree and return handle to self.
102    pub fn add_to(self, tree: &mut BehaviorTree<B>) -> Handle<BehaviorNode<B>> {
103        tree.add_node(BehaviorNode::Composite(self))
104    }
105}