dungeon/src/BehaviourTreeNode.cpp

57 lines
1.1 KiB
C++
Raw Normal View History

2017-09-17 13:43:13 +02:00
#include "BehaviourTreeNode.h"
#include "BehaviourTree.h"
BehaviourTreeNode::BehaviourTreeNode(BehaviourTreeNode* parent) {
if (parent != nullptr) {
parent->AddChild(this);
}
isOpen = false;
}
BehaviourTreeNode::~BehaviourTreeNode() {
2017-09-17 20:07:38 +02:00
for (BehaviourTreeNode* child : children) {
2017-09-17 13:43:13 +02:00
delete child;
}
}
BehaviourTreeStatus BehaviourTreeNode::execute(BTTick* tick) {
enter(tick);
if (!isOpen) {
_open(tick);
}
BehaviourTreeStatus status = this->tick(tick);
if (status != BT_RUNNING) {
_close(tick);
}
exit(tick);
return status;
}
void BehaviourTreeNode::_open(BTTick * tick) {
if (!isOpen) {
isOpen = true;
tick->openNodes.push_back(this);
open(tick);
}
}
void BehaviourTreeNode::_close(BTTick * tick) {
if (isOpen) {
isOpen = false;
if (tick->openNodes.size() > 0) { // Patchwork, no idea why the vector is empty to begin with at times.
tick->openNodes.pop_back();
}
close(tick);
}
}
void BehaviourTreeNode::AddChild(BehaviourTreeNode* node) {
children.push_back(node);
}