diff --git a/decorator_nodes.cpp b/decorator_nodes.cpp index 00bb62d..b46b41b 100644 --- a/decorator_nodes.cpp +++ b/decorator_nodes.cpp @@ -2,8 +2,6 @@ #include "behaviour_nodes/behaviour_node.h" #include "core/variant/variant.h" -void BehaviourAlwaysSuccess::_bind_methods() {} - PackedStringArray BehaviourAlwaysSuccess::get_configuration_warnings() const { PackedStringArray warnings{ super_type::get_configuration_warnings() }; if (get_child_behaviours().size() != 1) { @@ -28,3 +26,41 @@ BehaviourNode *BehaviourAlwaysSuccess::get_next() { ? get_child_behaviours().get(0) : cast_to(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 if (get_status() == Running) { + 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(get_parent()); +} diff --git a/decorator_nodes.h b/decorator_nodes.h index fc95249..96ac7f7 100644 --- a/decorator_nodes.h +++ b/decorator_nodes.h @@ -6,10 +6,21 @@ class BehaviourAlwaysSuccess : public BehaviourComposite { GDCLASS(BehaviourAlwaysSuccess, BehaviourComposite); - static void _bind_methods(); + static void _bind_methods() {} public: PackedStringArray get_configuration_warnings() const override; void execute() override; BehaviourNode *get_next() override; }; + +class BehaviourInvertResult : public BehaviourComposite { + GDCLASS(BehaviourInvertResult, BehaviourComposite); + static void _bind_methods() {} + +public: + PackedStringArray get_configuration_warnings() const override; + void enter() override; + void execute() override; + BehaviourNode *get_next() override; +}; diff --git a/register_types.cpp b/register_types.cpp index ffb0aea..cea03f3 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -18,6 +18,7 @@ void initialize_behaviour_nodes_module(ModuleInitializationLevel p_level) { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); } void uninitialize_behaviour_nodes_module(ModuleInitializationLevel p_level) {