CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUBehaviorParser.h
1 //
2 // CUBehaviorParser.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a behavior parser in order to create a
6 // BodyDefNode from a JSON. It is hear because we have not yet folded
7 // an official AI asset loader into this release.
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_PARSER_H__
46 #define __CU_BEHAVIOR_PARSER_H__
47 
48 #include <cugl/ai/behavior/CUBehaviorNode.h>
49 #include <cugl/ai/behavior/CUBehaviorAction.h>
50 #include <cugl/io/CUJsonReader.h>
51 #include <unordered_map>
52 #include <string>
53 #include <vector>
54 
55 namespace cugl {
56  namespace ai {
87 private:
89  std::unordered_map<std::string,std::function<float()>> _prioritizers;
91  std::unordered_map<std::string,std::shared_ptr<BehaviorActionDef>> _actions;
92 
93 #pragma mark Constructors
94 public:
102 
107 
116  void dispose();
117 
126  bool init() { return true; }
127 
136  static std::shared_ptr<BehaviorParser> alloc() {
137  std::shared_ptr<BehaviorParser> result = std::make_shared<BehaviorParser>();
138  return (result->init() ? result : nullptr);
139  }
140 
141 #pragma mark Parser State
142 
152  void addPrioritizer(const std::string& name, std::function<float()> prioritizer);
153 
164  void addPrioritizer(const char* name, std::function<float()> prioritizer) {
165  addPrioritizer(std::string(name),prioritizer);
166  }
167 
177  std::function<float()> getPrioritizer(const std::string& name) const;
178 
188  std::function<float()> getPrioritizer(const char* name) const {
189  return getPrioritizer(std::string(name));
190  }
191 
202  std::function<float()> removePrioritizer(const std::string& name);
203 
214  std::function<float()> removePrioritizer(const char* name) {
215  return removePrioritizer(std::string(name));
216  }
217 
226  void addAction(std::shared_ptr<BehaviorActionDef> actiondef) {
227  addAction(actiondef->name,actiondef);
228  }
229 
238  void addAction(const std::string& name, std::shared_ptr<BehaviorActionDef> actiondef);
239 
248  void addAction(const char* name, std::shared_ptr<BehaviorActionDef> actiondef) {
249  addAction(std::string(name),actiondef);
250  }
251 
261  std::shared_ptr<BehaviorActionDef> getAction(const std::string& name) const;
262 
272  std::shared_ptr<BehaviorActionDef> getAction(const char* name) const {
273  return getAction(std::string(name));
274  }
275 
286  std::shared_ptr<BehaviorActionDef> removeAction(const std::string& name);
287 
298  std::shared_ptr<BehaviorActionDef> removeAction(const char* name) {
299  return removeAction(std::string(name));
300  }
301 
302 #pragma mark Parsing Functions
303 
317  std::unordered_map<std::string, std::shared_ptr<BehaviorNodeDef>>
318  parseFile(const std::string& file) {
319  return parseFile(file.c_str());
320  }
321 
336  std::unordered_map<std::string, std::shared_ptr<BehaviorNodeDef>>
337  parseFile(const char* file);
338 
339 private:
350  std::shared_ptr<BehaviorNodeDef> parseJson(const std::shared_ptr<JsonValue>& json);
351 };
352 
353  }
354 }
355 #endif /* __CU_BEHAVIOR_PARSER_H__ */
cugl::ai::BehaviorParser::removePrioritizer
std::function< float()> removePrioritizer(const char *name)
Definition: CUBehaviorParser.h:214
cugl::ai::BehaviorParser::dispose
void dispose()
cugl::ai::BehaviorParser::getPrioritizer
std::function< float()> getPrioritizer(const std::string &name) const
cugl::ai::BehaviorParser::addPrioritizer
void addPrioritizer(const char *name, std::function< float()> prioritizer)
Definition: CUBehaviorParser.h:164
cugl::ai::BehaviorParser
Definition: CUBehaviorParser.h:86
cugl::ai::BehaviorParser::~BehaviorParser
~BehaviorParser()
Definition: CUBehaviorParser.h:106
cugl::ai::BehaviorParser::getAction
std::shared_ptr< BehaviorActionDef > getAction(const char *name) const
Definition: CUBehaviorParser.h:272
cugl::ai::BehaviorParser::addPrioritizer
void addPrioritizer(const std::string &name, std::function< float()> prioritizer)
cugl::ai::BehaviorParser::BehaviorParser
BehaviorParser()
Definition: CUBehaviorParser.h:101
cugl::ai::BehaviorParser::removePrioritizer
std::function< float()> removePrioritizer(const std::string &name)
cugl::ai::BehaviorParser::removeAction
std::shared_ptr< BehaviorActionDef > removeAction(const char *name)
Definition: CUBehaviorParser.h:298
cugl::ai::BehaviorParser::init
bool init()
Definition: CUBehaviorParser.h:126
cugl::ai::BehaviorParser::removeAction
std::shared_ptr< BehaviorActionDef > removeAction(const std::string &name)
cugl::ai::BehaviorParser::addAction
void addAction(const char *name, std::shared_ptr< BehaviorActionDef > actiondef)
Definition: CUBehaviorParser.h:248
cugl::ai::BehaviorParser::addAction
void addAction(std::shared_ptr< BehaviorActionDef > actiondef)
Definition: CUBehaviorParser.h:226
cugl::ai::BehaviorParser::getPrioritizer
std::function< float()> getPrioritizer(const char *name) const
Definition: CUBehaviorParser.h:188
cugl::ai::BehaviorParser::alloc
static std::shared_ptr< BehaviorParser > alloc()
Definition: CUBehaviorParser.h:136
cugl::ai::BehaviorParser::parseFile
std::unordered_map< std::string, std::shared_ptr< BehaviorNodeDef > > parseFile(const std::string &file)
Definition: CUBehaviorParser.h:318
cugl::ai::BehaviorParser::getAction
std::shared_ptr< BehaviorActionDef > getAction(const std::string &name) const