CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUBehaviorAction.h
1 //
2 // CUBehaviorAction.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for an action, which is a user-defined result
6 // chosen by a behavior tree. Instead of requiring the user to subclass an
7 // interface, have elected to use functions pointers to allow the user more
8 // flexibilty.
9 //
10 // BehaviorAction objects are managed by BehaviorManager, and should never
11 // be allocated directly. Instead, you create a behavior action definition
12 // and pass it to a factor method in BehaviorManager.
13 //
14 // EXPERIMENTAL: This module is experimental. The API may change significantly
15 // in future CUGL releases.
16 //
17 // CUGL MIT License:
18 // This software is provided 'as-is', without any express or implied
19 // warranty. In no event will the authors be held liable for any damages
20 // arising from the use of this software.
21 //
22 // Permission is granted to anyone to use this software for any purpose,
23 // including commercial applications, and to alter it and redistribute it
24 // freely, subject to the following restrictions:
25 //
26 // 1. The origin of this software must not be misrepresented; you must not
27 // claim that you wrote the original software. If you use this software
28 // in a product, an acknowledgment in the product documentation would be
29 // appreciated but is not required.
30 //
31 // 2. Altered source versions must be plainly marked as such, and must not
32 // be misrepresented as being the original software.
33 //
34 // 3. This notice may not be removed or altered from any source distribution.
35 //
36 // Author: Apurv Sethi and Andrew Matsumoto (with Walker White)
37 // Version: 5/21/2018
38 //
39 #ifndef __CU_BEHAVIOR_ACTION_H__
40 #define __CU_BEHAVIOR_ACTION_H__
41 #include <cugl/util/CUDebug.h>
42 #include <functional>
43 #include <memory>
44 #include <string>
45 
46 namespace cugl {
47  namespace ai {
48 
49 #pragma mark -
50 #pragma mark Behavior Action Definition
51 
59 public:
61  std::string name;
62 
69  std::function<void()> start;
70 
78  std::function<bool(float dt)> update;
79 
87  std::function<void()> terminate;
88 
89 #pragma mark Methods
90 
98  BehaviorActionDef() : name(""), start(nullptr), update(nullptr), terminate(nullptr) {}
99 
107  static std::shared_ptr<BehaviorActionDef> alloc() {
108  return std::make_shared<BehaviorActionDef>();
109  }
110 };
111 
112 #pragma mark -
113 #pragma mark Behavior Action
114 
127 #pragma mark Values
128 public:
136  enum class State : unsigned int {
138  INACTIVE = 0,
140  RUNNING = 1,
142  PAUSED = 2,
144  FINISHED = 3
145  };
146 
147 protected:
149  std::string _name;
150 
153 
159  std::function<void()> _start;
160 
166  std::function<bool(float dt)> _update;
167 
175  std::function<void()> _terminate;
176 
177 public:
178 #pragma mark Constructors
179 
185  BehaviorAction();
186 
191 
199  bool init(const std::shared_ptr<BehaviorActionDef>& actiondef);
200 
207  void dispose();
208 
209 #pragma mark Attributes
210 
218  const std::string& getName() const { return _name; }
219 
226 
232  void setState(BehaviorAction::State state) { _state = state; }
233 
234 #pragma mark Action Management
235 
240  void start();
241 
250  void terminate();
251 
259  void pause();
260 
267  void resume();
268 
276  void reset();
277 
289  BehaviorAction::State update(float dt);
290 
291 };
292  }
293 }
294 #endif /* __CU_BEHAVIOR_ACTION_H__ */
BehaviorActionDef()
Definition: CUBehaviorAction.h:98
Definition: CUBehaviorAction.h:126
BehaviorAction::State update(float dt)
std::function< bool(float dt)> _update
Definition: CUBehaviorAction.h:166
~BehaviorAction()
Definition: CUBehaviorAction.h:190
State
Definition: CUBehaviorAction.h:136
std::function< void()> terminate
Definition: CUBehaviorAction.h:87
const std::string & getName() const
Definition: CUBehaviorAction.h:218
static std::shared_ptr< BehaviorActionDef > alloc()
Definition: CUBehaviorAction.h:107
BehaviorAction::State _state
Definition: CUBehaviorAction.h:152
std::function< void()> _terminate
Definition: CUBehaviorAction.h:175
std::function< bool(float dt)> update
Definition: CUBehaviorAction.h:78
void setState(BehaviorAction::State state)
Definition: CUBehaviorAction.h:232
std::function< void()> _start
Definition: CUBehaviorAction.h:159
std::function< void()> start
Definition: CUBehaviorAction.h:69
bool init(const std::shared_ptr< BehaviorActionDef > &actiondef)
Definition: CUAction.h:51
std::string _name
Definition: CUBehaviorAction.h:149
std::string name
Definition: CUBehaviorAction.h:61
Definition: CUBehaviorAction.h:58
BehaviorAction::State getState() const
Definition: CUBehaviorAction.h:225