From fbe9165d8e9ae6714be8e2144b1ff820ce855bc1 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sun, 28 Aug 2022 15:24:01 +0200 Subject: [PATCH] Add RunLimit --- limboai/bt_delay.h | 6 +++--- limboai/bt_run_limit.cpp | 23 +++++++++++++++++++++++ limboai/bt_run_limit.h | 30 ++++++++++++++++++++++++++++++ limboai/register_types.cpp | 2 ++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 limboai/bt_run_limit.cpp create mode 100644 limboai/bt_run_limit.h diff --git a/limboai/bt_delay.h b/limboai/bt_delay.h index 1919445..cb07065 100644 --- a/limboai/bt_delay.h +++ b/limboai/bt_delay.h @@ -1,7 +1,7 @@ /* bt_delay.h */ -#ifndef BT_DELAY -#define BT_DELAY +#ifndef BT_DELAY_H +#define BT_DELAY_H #include "bt_decorator.h" #include "core/object.h" @@ -28,4 +28,4 @@ public: float get_seconds() const { return seconds; }; }; -#endif // BT_DELAY \ No newline at end of file +#endif // BT_DELAY_H \ No newline at end of file diff --git a/limboai/bt_run_limit.cpp b/limboai/bt_run_limit.cpp new file mode 100644 index 0000000..4b616a9 --- /dev/null +++ b/limboai/bt_run_limit.cpp @@ -0,0 +1,23 @@ +/* bt_run_limit.cpp */ + +#include "bt_run_limit.h" + +String BTRunLimit::_generate_name() const { + return vformat("RunLimit x%d", run_limit); +} + +int BTRunLimit::_tick(float p_delta) { + if (get_child(0)->get_status() != RUNNING) { + if (_num_runs >= run_limit) { + return FAILURE; + } + _num_runs += 1; + } + return get_child(0)->execute(p_delta); +} + +void BTRunLimit::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_run_limit", "p_value"), &BTRunLimit::set_run_limit); + ClassDB::bind_method(D_METHOD("get_run_limit"), &BTRunLimit::get_run_limit); + ADD_PROPERTY(PropertyInfo(Variant::INT, "run_limit"), "set_run_limit", "get_run_limit"); +} diff --git a/limboai/bt_run_limit.h b/limboai/bt_run_limit.h new file mode 100644 index 0000000..b3c4ed9 --- /dev/null +++ b/limboai/bt_run_limit.h @@ -0,0 +1,30 @@ +/* bt_run_limit.h */ + +#ifndef BT_RUN_LIMIT_H +#define BT_RUN_LIMIT_H + +#include "bt_decorator.h" +#include "core/object.h" + +class BTRunLimit : public BTDecorator { + GDCLASS(BTRunLimit, BTDecorator); + +private: + int run_limit = 1; + int _num_runs = 0; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + virtual int _tick(float p_delta); + +public: + void set_run_limit(int p_value) { + run_limit = p_value; + emit_changed(); + }; + int get_run_limit() const { return run_limit; }; +}; + +#endif // BT_RUN_LIMIT_H \ No newline at end of file diff --git a/limboai/register_types.cpp b/limboai/register_types.cpp index 8c718d3..bb15515 100644 --- a/limboai/register_types.cpp +++ b/limboai/register_types.cpp @@ -16,6 +16,7 @@ #include "bt_repeat.h" #include "bt_repeat_until_failure.h" #include "bt_repeat_until_success.h" +#include "bt_run_limit.h" #include "bt_selector.h" #include "bt_sequence.h" #include "bt_task.h" @@ -38,6 +39,7 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); LimboStringNames::create(); }