CUGL 1.3
Cornell University Game Library
CUCompositeNode.h
1 //
2 // CUCompositeNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a composite behavior node. It is used
6 // to select from one or more children, according to priority.
7 //
8 // BehaviorNode objects are managed by BehaviorManager, and should never
9 // be allocated directly. Instead, you create a behavior node definition
10 // and pass it to a factor method in BehaviorManager.
11 //
12 // EXPERIMENTAL: This module is experimental. The API may change significantly
13 // in future CUGL releases.
14 //
15 // CUGL MIT License:
16 // This software is provided 'as-is', without any express or implied
17 // warranty. In no event will the authors be held liable for any damages
18 // arising from the use of this software.
19 //
20 // Permission is granted to anyone to use this software for any purpose,
21 // including commercial applications, and to alter it and redistribute it
22 // freely, subject to the following restrictions:
23 //
24 // 1. The origin of this software must not be misrepresented; you must not
25 // claim that you wrote the original software. If you use this software
26 // in a product, an acknowledgment in the product documentation would be
27 // appreciated but is not required.
28 //
29 // 2. Altered source versions must be plainly marked as such, and must not
30 // be misrepresented as being the original software.
31 //
32 // 3. This notice may not be removed or altered from any source distribution.
33 //
34 // Author: Apurv Sethi and Andrew Matsumoto (with Walker White)
35 // Version: 5/21/2018
36 //
37 #ifndef __CU_COMPOSITE_NODE_H__
38 #define __CU_COMPOSITE_NODE_H__
39 
40 #include <cugl/ai/behavior/CUBehaviorNode.h>
41 #include <string>
42 #include <vector>
43 
44 namespace cugl {
45  namespace ai {
64 class CompositeNode : public BehaviorNode {
65 #pragma mark Values
66 protected:
69 
70 #pragma mark Internal Helpers
71 
79  virtual int selectChild() const = 0;
80 
81 public:
82 #pragma mark Constructors
83 
89  CompositeNode();
90 
95 
103  virtual void dispose() override;
104 
105 #pragma mark Attributes
106 
116  bool isPreemptive() const { return _preemptive; }
117 
128  void setPreemptive(bool preemptive) { _preemptive = preemptive; }
129 
130 #pragma mark Tree Management
131 
146  const BehaviorNode* getChildByPriorityIndex(Uint32 index) const;
147 
165  template <typename T>
166  const T* getChildByPriorityIndex(Uint32 index) const {
167  return dynamic_cast<const T*>(getChildByPriorityIndex(index));
168  }
169 
179  const BehaviorNode* getActiveChild() const;
180 
192  template <typename T>
193  const T* getActiveChild() const {
194  return dynamic_cast<const T*>(getActiveChild());
195  }
196 
197 #pragma mark Behavior Management
198 
212  virtual void query(float dt) override;
213 
229  BehaviorNode::State update(float dt) override;
230 
231 };
232 
233  }
234 }
235 #endif /* __CU_COMPOSITE_NODE_H__ */
cugl::ai::CompositeNode::isPreemptive
bool isPreemptive() const
Definition: CUCompositeNode.h:116
cugl::ai::CompositeNode::update
BehaviorNode::State update(float dt) override
cugl::ai::CompositeNode::CompositeNode
CompositeNode()
cugl::ai::BehaviorNode
Definition: CUBehaviorNode.h:280
cugl::ai::CompositeNode::query
virtual void query(float dt) override
cugl::ai::CompositeNode::selectChild
virtual int selectChild() const =0
cugl::ai::CompositeNode::getChildByPriorityIndex
const BehaviorNode * getChildByPriorityIndex(Uint32 index) const
cugl::ai::CompositeNode::~CompositeNode
~CompositeNode()
Definition: CUCompositeNode.h:94
cugl::ai::BehaviorNode::State
State
Definition: CUBehaviorNode.h:290
cugl::ai::CompositeNode::setPreemptive
void setPreemptive(bool preemptive)
Definition: CUCompositeNode.h:128
cugl::ai::CompositeNode::_preemptive
bool _preemptive
Definition: CUCompositeNode.h:68
cugl::ai::CompositeNode::dispose
virtual void dispose() override
cugl::ai::CompositeNode::getActiveChild
const BehaviorNode * getActiveChild() const
cugl::ai::CompositeNode
Definition: CUCompositeNode.h:64