diff --git a/limboai/bt_repeat.cpp b/limboai/bt_repeat.cpp new file mode 100644 index 0000000..fb61449 --- /dev/null +++ b/limboai/bt_repeat.cpp @@ -0,0 +1,37 @@ +/* bt_repeat.cpp */ + +#include "bt_repeat.h" +#include "core/object.h" +#include "core/variant.h" + +String BTRepeat::_generate_name() const { + return vformat("Repeat x%s", times); +} + +void BTRepeat::_enter() { + _cur_iteration = 1; +} + +int BTRepeat::_tick(float p_delta) { + int status = get_child(0)->execute(p_delta); + if (status == RUNNING) { + return RUNNING; + } else if (status == FAILURE && abort_on_failure) { + return FAILURE; + } else if (_cur_iteration >= times) { + return SUCCESS; + } else { + _cur_iteration += 1; + return RUNNING; + } +} + +void BTRepeat::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_times", "p_value"), &BTRepeat::set_times); + ClassDB::bind_method(D_METHOD("get_times"), &BTRepeat::get_times); + ClassDB::bind_method(D_METHOD("set_abort_on_failure", "p_value"), &BTRepeat::set_abort_on_failure); + ClassDB::bind_method(D_METHOD("get_abort_on_failure"), &BTRepeat::get_abort_on_failure); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "times", PROPERTY_HINT_RANGE, "1,65535"), "set_times", "get_times"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "abort_on_failure"), "set_abort_on_failure", "get_abort_on_failure"); +} diff --git a/limboai/bt_repeat.h b/limboai/bt_repeat.h new file mode 100644 index 0000000..92322b2 --- /dev/null +++ b/limboai/bt_repeat.h @@ -0,0 +1,37 @@ +/* bt_repeat.h */ + +#ifndef BT_REPEAT_H +#define BT_REPEAT_H + +#include "bt_decorator.h" +#include "core/object.h" + +class BTRepeat : public BTDecorator { + GDCLASS(BTRepeat, BTDecorator); + +private: + int times = 1; + bool abort_on_failure = false; + int _cur_iteration = 0; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + virtual void _enter(); + virtual int _tick(float p_delta); + +public: + void set_times(int p_value) { + times = p_value; + emit_changed(); + }; + int get_times() const { return times; }; + void set_abort_on_failure(bool p_value) { + abort_on_failure = p_value; + emit_changed(); + }; + bool get_abort_on_failure() const { return abort_on_failure; }; +}; + +#endif // BT_REPEAT_H \ No newline at end of file diff --git a/limboai/bt_repeat_until_failure.cpp b/limboai/bt_repeat_until_failure.cpp new file mode 100644 index 0000000..bd911f0 --- /dev/null +++ b/limboai/bt_repeat_until_failure.cpp @@ -0,0 +1,10 @@ +/* bt_repeat_until_failure.cpp */ + +#include "bt_repeat_until_failure.h" + +int BTRepeatUntilFailure::_tick(float p_delta) { + if (get_child(0)->execute(p_delta) == FAILURE) { + return SUCCESS; + } + return RUNNING; +} diff --git a/limboai/bt_repeat_until_failure.h b/limboai/bt_repeat_until_failure.h new file mode 100644 index 0000000..12de583 --- /dev/null +++ b/limboai/bt_repeat_until_failure.h @@ -0,0 +1,16 @@ +/* bt_repeat_until_failure.h */ + +#ifndef BT_REPEAT_UNTIL_FAILURE_H +#define BT_REPEAT_UNTIL_FAILURE_H + +#include "bt_decorator.h" +#include "core/object.h" + +class BTRepeatUntilFailure : public BTDecorator { + GDCLASS(BTRepeatUntilFailure, BTDecorator); + +protected: + virtual int _tick(float p_delta); +}; + +#endif // BT_REPEAT_UNTIL_FAILURE_H \ No newline at end of file diff --git a/limboai/bt_repeat_until_success.cpp b/limboai/bt_repeat_until_success.cpp new file mode 100644 index 0000000..aa2ad59 --- /dev/null +++ b/limboai/bt_repeat_until_success.cpp @@ -0,0 +1,10 @@ +/* bt_repeat_until_success.cpp */ + +#include "bt_repeat_until_success.h" + +int BTRepeatUntilSuccess::_tick(float p_delta) { + if (get_child(0)->execute(p_delta) == SUCCESS) { + return SUCCESS; + } + return RUNNING; +} diff --git a/limboai/bt_repeat_until_success.h b/limboai/bt_repeat_until_success.h new file mode 100644 index 0000000..db83ea6 --- /dev/null +++ b/limboai/bt_repeat_until_success.h @@ -0,0 +1,16 @@ +/* bt_repeat_until_success.h */ + +#ifndef BT_REPEAT_UNTIL_SUCCESS_H +#define BT_REPEAT_UNTIL_SUCCESS_H + +#include "bt_decorator.h" +#include "core/object.h" + +class BTRepeatUntilSuccess : public BTDecorator { + GDCLASS(BTRepeatUntilSuccess, BTDecorator); + +protected: + virtual int _tick(float p_delta); +}; + +#endif // BT_REPEAT_UNTIL_SUCCESS_H \ No newline at end of file diff --git a/limboai/register_types.cpp b/limboai/register_types.cpp index 050b873..8c718d3 100644 --- a/limboai/register_types.cpp +++ b/limboai/register_types.cpp @@ -13,6 +13,9 @@ #include "bt_delay.h" #include "bt_invert.h" #include "bt_parallel.h" +#include "bt_repeat.h" +#include "bt_repeat_until_failure.h" +#include "bt_repeat_until_success.h" #include "bt_selector.h" #include "bt_sequence.h" #include "bt_task.h" @@ -32,6 +35,9 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); LimboStringNames::create(); }