behaviour-nodes-module/decorator_nodes.cpp

66 lines
1.8 KiB
C++

#include "decorator_nodes.h"
#include "behaviour_nodes/behaviour_node.h"
#include "core/variant/variant.h"
PackedStringArray BehaviourAlwaysSuccess::get_configuration_warnings() const {
PackedStringArray warnings{ super_type::get_configuration_warnings() };
if (get_child_behaviours().size() != 1) {
warnings.push_back("BehaviourAlwaysSuccess should have exactly one child");
}
return warnings;
}
void BehaviourAlwaysSuccess::execute() {
if (get_child_behaviours().is_empty()) {
set_status(Fail);
ERR_FAIL_EDMSG("BehaviourSequence executed with no children.");
} else if (get_status() == Running) {
set_status(get_child_behaviours().get(0)->get_status() == Running ? Running : Success);
} else {
set_status(Running);
}
}
BehaviourNode *BehaviourAlwaysSuccess::get_next() {
return get_status() == Running
? get_child_behaviours().get(0)
: cast_to<BehaviourNode>(get_parent());
}
PackedStringArray BehaviourInvertResult::get_configuration_warnings() const {
PackedStringArray warnings{ super_type::get_configuration_warnings() };
if (get_child_behaviours().size() != 1) {
warnings.push_back("BehaviourInvertResult should have exactly one child behaviour");
}
return warnings;
}
void BehaviourInvertResult::enter() {
set_status(Running);
}
void BehaviourInvertResult::execute() {
if (get_child_behaviours().is_empty()) {
set_status(Fail);
ERR_FAIL_EDMSG("BehaviourInvertResult executed with no children");
} else {
Status child_status{ get_child_behaviours().get(0)->get_status() };
switch (child_status) {
case Fail:
set_status(Success);
return;
case Success:
set_status(Fail);
return;
case Running:
set_status(Running);
return;
}
}
}
BehaviourNode *BehaviourInvertResult::get_next() {
return get_status() == Running
? get_child_behaviours().get(0)
: cast_to<BehaviourNode>(get_parent());
}