CUGL 1.3
Cornell University Game Library
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__ */
cugl::ai::BehaviorAction::reset
void reset()
cugl::ai::BehaviorAction::terminate
void terminate()
cugl::ai::BehaviorAction::_terminate
std::function< void()> _terminate
Definition: CUBehaviorAction.h:175
cugl::ai::BehaviorAction::_state
BehaviorAction::State _state
Definition: CUBehaviorAction.h:152
cugl::ai::BehaviorActionDef::update
std::function< bool(float dt)> update
Definition: CUBehaviorAction.h:78
cugl::ai::BehaviorAction::_name
std::string _name
Definition: CUBehaviorAction.h:149
cugl::ai::BehaviorAction::getState
BehaviorAction::State getState() const
Definition: CUBehaviorAction.h:225
cugl::ai::BehaviorAction::State::PAUSED
cugl::ai::BehaviorAction::start
void start()
cugl::ai::BehaviorAction::update
BehaviorAction::State update(float dt)
cugl::ai::BehaviorAction::resume
void resume()
cugl::ai::BehaviorAction::init
bool init(const std::shared_ptr< BehaviorActionDef > &actiondef)
cugl::ai::BehaviorActionDef::terminate
std::function< void()> terminate
Definition: CUBehaviorAction.h:87
cugl::ai::BehaviorActionDef::name
std::string name
Definition: CUBehaviorAction.h:61
cugl::ai::BehaviorActionDef
Definition: CUBehaviorAction.h:58
cugl::ai::BehaviorAction::State
State
Definition: CUBehaviorAction.h:136
cugl::ai::BehaviorAction::BehaviorAction
BehaviorAction()
cugl::ai::BehaviorAction::State::INACTIVE
cugl::ai::BehaviorActionDef::start
std::function< void()> start
Definition: CUBehaviorAction.h:69
cugl::ai::BehaviorActionDef::BehaviorActionDef
BehaviorActionDef()
Definition: CUBehaviorAction.h:98
cugl::ai::BehaviorAction::getName
const std::string & getName() const
Definition: CUBehaviorAction.h:218
cugl::ai::BehaviorAction::setState
void setState(BehaviorAction::State state)
Definition: CUBehaviorAction.h:232
cugl::ai::BehaviorAction::State::RUNNING
cugl::ai::BehaviorAction::_update
std::function< bool(float dt)> _update
Definition: CUBehaviorAction.h:166
cugl::ai::BehaviorAction::_start
std::function< void()> _start
Definition: CUBehaviorAction.h:159
cugl::ai::BehaviorAction::dispose
void dispose()
cugl::ai::BehaviorAction::State::FINISHED
cugl::ai::BehaviorAction::pause
void pause()
cugl::ai::BehaviorActionDef::alloc
static std::shared_ptr< BehaviorActionDef > alloc()
Definition: CUBehaviorAction.h:107
cugl::ai::BehaviorAction::~BehaviorAction
~BehaviorAction()
Definition: CUBehaviorAction.h:190
cugl::ai::BehaviorAction
Definition: CUBehaviorAction.h:126