CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUBehaviorManager.h
1 //
2 // CUBehaviorManager.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a behavior tree manager. The behavior tree
6 // manager controls the creation and execution of behavior trees. It is akin
7 // to a world object in Box2d.
8 //
9 // EXPERIMENTAL: This module is experimental. The API may change significantly
10 // in future CUGL releases.
11 //
12 // This class uses our standard shared-pointer architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 // CUGL MIT License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Apurv Sethi and Andrew Matsumoto (with Walker White)
43 // Version: 5/22/2018
44 //
45 #ifndef __CU_BEHAVIOR_MANAGER_H__
46 #define __CU_BEHAVIOR_MANAGER_H__
47 #include <cugl/ai/behavior/CUBehaviorNode.h>
48 #include <cugl/ai/behavior/CUBehaviorAction.h>
49 #include <unordered_map>
50 #include <functional>
51 #include <random>
52 #include <string>
53 
54 namespace cugl {
55  namespace ai {
75 #pragma mark Values
76 protected:
78  std::unordered_map<std::string, std::shared_ptr<BehaviorNode>> _trees;
79 
81  std::minstd_rand _random;
82 
83 #pragma mark -
84 #pragma mark Constructors
85 public:
93 
100 
107  void dispose();
108 
117  bool init();
118 
128  bool init(Uint32 seed);
129 
138  static std::shared_ptr<BehaviorManager> alloc() {
139  std::shared_ptr<BehaviorManager> result = std::make_shared<BehaviorManager>();
140  return (result->init() ? result : nullptr);
141  }
142 
152  static std::shared_ptr<BehaviorManager> alloc(Uint32 seed) {
153  std::shared_ptr<BehaviorManager> result = std::make_shared<BehaviorManager>();
154  return (result->init(seed) ? result : nullptr);
155  }
156 
157 #pragma mark -
158 #pragma mark Tree Management
159 
169  bool containsTree(const std::string& name) const;
170 
181  bool containsTree(const char* name) const {
182  return containsTree(std::string(name));
183  }
184 
198  const BehaviorNode* getTree(const std::string& name) const;
199 
213  const BehaviorNode* getTree(const char* name) const {
214  return getTree(std::string(name));
215  }
216 
236  bool addTree(const std::shared_ptr<BehaviorNodeDef>& treedef);
237 
258  bool addTree(const std::string& name, const std::shared_ptr<BehaviorNodeDef>& treedef);
259 
280  bool addTree(const char* name, const std::shared_ptr<BehaviorNodeDef>& treedef) {
281  return addTree(std::string(name),treedef);
282  }
283 
297  BehaviorNode::State getTreeState(const std::string& name) const {
298  return getTree(name)->getState();
299  }
300 
301 #pragma mark -
302 #pragma mark Behavior Management
303 
317  void startTree(const std::string& name);
318 
332  void pauseTree(const std::string& name);
333 
345  void resumeTree(const std::string& name);
346 
361  void removeTree(const std::string& name);
362 
378  void resetTree(const std::string& name);
379 
388  void update(float dt);
389 
390 private:
403  std::shared_ptr<BehaviorNode> createTree(const std::shared_ptr<BehaviorNodeDef>& treedef);
404 };
405  }
406 }
407 #endif /* __CU_BEHAVIOR_MANAGER_H__ */
~BehaviorManager()
Definition: CUBehaviorManager.h:99
Definition: CUBehaviorNode.h:280
Definition: CUBehaviorManager.h:74
State
Definition: CUBehaviorNode.h:290
void removeTree(const std::string &name)
void pauseTree(const std::string &name)
bool addTree(const char *name, const std::shared_ptr< BehaviorNodeDef > &treedef)
Definition: CUBehaviorManager.h:280
bool containsTree(const std::string &name) const
std::minstd_rand _random
Definition: CUBehaviorManager.h:81
std::unordered_map< std::string, std::shared_ptr< BehaviorNode > > _trees
Definition: CUBehaviorManager.h:78
BehaviorNode::State getState() const
Definition: CUBehaviorNode.h:395
static std::shared_ptr< BehaviorManager > alloc()
Definition: CUBehaviorManager.h:138
const BehaviorNode * getTree(const char *name) const
Definition: CUBehaviorManager.h:213
static std::shared_ptr< BehaviorManager > alloc(Uint32 seed)
Definition: CUBehaviorManager.h:152
const BehaviorNode * getTree(const std::string &name) const
void startTree(const std::string &name)
void resumeTree(const std::string &name)
Definition: CUAction.h:51
BehaviorManager()
Definition: CUBehaviorManager.h:92
BehaviorNode::State getTreeState(const std::string &name) const
Definition: CUBehaviorManager.h:297
bool addTree(const std::shared_ptr< BehaviorNodeDef > &treedef)
void resetTree(const std::string &name)
bool containsTree(const char *name) const
Definition: CUBehaviorManager.h:181