SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Sequence.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/Selector.h"
7 #include "AI.h"
8 
9 namespace ai {
10 
19 class Sequence: public Selector {
20 public:
21  SELECTOR_CLASS(Sequence)
22 
23  TreeNodeStatus execute(const AIPtr& entity, int64_t deltaMillis) override {
24  if (Selector::execute(entity, deltaMillis) == CANNOTEXECUTE)
25  return CANNOTEXECUTE;
26 
27  TreeNodeStatus result = FINISHED;
28  const int progress = std::max(0, getSelectorState(entity));
29 
30  const std::size_t size = _children.size();
31  for (std::size_t i = static_cast<std::size_t>(progress); i < size; ++i) {
32  TreeNodePtr& child = _children[i];
33 
34  result = child->execute(entity, deltaMillis);
35 
36  if (result == RUNNING) {
37  setSelectorState(entity, static_cast<int>(i));
38  break;
39  } else if (result == CANNOTEXECUTE || result == FAILED) {
40  resetState(entity);
41  break;
42  } else if (result == EXCEPTION) {
43  break;
44  }
45  }
46 
47  if (result != RUNNING) {
48  resetState(entity);
49  }
50 
51  return state(entity, result);
52  }
53 
54  void resetState(const AIPtr& entity) override {
55  setSelectorState(entity, AI_NOTHING_SELECTED);
56  TreeNode::resetState(entity);
57  }
58 };
59 
60 }
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
Base class for all type of TreeNode selectors.
Definition: Selector.h:25
void resetState(const AIPtr &entity) override
Reset the states in the node and also in the entity.
Definition: Sequence.h:54
The sequence continues to execute their children until one of the children returned a state that is n...
Definition: Sequence.h:19
virtual void resetState(const AIPtr &entity)
Reset the states in the node and also in the entity.
Definition: TreeNodeImpl.h:55
TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis) override
Definition: Sequence.h:23