From 936bf749d30e6caf64fb200a2ccb13cab4f6fbee Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Mon, 29 Aug 2022 14:06:48 +0200 Subject: [PATCH] Add Wait, WaitTicks and RandomWait --- limboai/bt/bt_random_wait.cpp | 28 ++++++++++++++++++++++ limboai/bt/bt_random_wait.h | 33 ++++++++++++++++++++++++++ limboai/bt/bt_wait.cpp | 30 +++++++++++++++++++++++ limboai/bt/bt_wait.h | 32 +++++++++++++++++++++++++ limboai/bt/bt_wait_ticks.cpp | 30 +++++++++++++++++++++++ limboai/bt/bt_wait_ticks.h | 32 +++++++++++++++++++++++++ limboai/icons/icon_b_t_random_wait.svg | 1 + limboai/icons/icon_b_t_wait_ticks.svg | 1 + limboai/register_types.cpp | 6 +++++ 9 files changed, 193 insertions(+) create mode 100644 limboai/bt/bt_random_wait.cpp create mode 100644 limboai/bt/bt_random_wait.h create mode 100644 limboai/bt/bt_wait.cpp create mode 100644 limboai/bt/bt_wait.h create mode 100644 limboai/bt/bt_wait_ticks.cpp create mode 100644 limboai/bt/bt_wait_ticks.h create mode 100644 limboai/icons/icon_b_t_random_wait.svg create mode 100644 limboai/icons/icon_b_t_wait_ticks.svg diff --git a/limboai/bt/bt_random_wait.cpp b/limboai/bt/bt_random_wait.cpp new file mode 100644 index 0000000..0e4759a --- /dev/null +++ b/limboai/bt/bt_random_wait.cpp @@ -0,0 +1,28 @@ +/* bt_random_wait.cpp */ + +#include "bt_random_wait.h" + +String BTRandomWait::_generate_name() const { + return vformat("Wait %s to %s sec", duration_min_max.x, duration_min_max.y); +} + +void BTRandomWait::_enter() { + _time_passed = 0.0; + _duration = Math::random(duration_min_max.x, duration_min_max.y); +} + +int BTRandomWait::_tick(float p_delta) { + _time_passed += p_delta; + if (_time_passed < _duration) { + return RUNNING; + } else { + return SUCCESS; + } +} + +void BTRandomWait::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_duration_min_max", "p_value"), &BTRandomWait::set_duration_min_max); + ClassDB::bind_method(D_METHOD("get_duration_min_max"), &BTRandomWait::get_duration_min_max); + + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "duration_min_max"), "set_duration_min_max", "get_duration_min_max"); +} diff --git a/limboai/bt/bt_random_wait.h b/limboai/bt/bt_random_wait.h new file mode 100644 index 0000000..9311e07 --- /dev/null +++ b/limboai/bt/bt_random_wait.h @@ -0,0 +1,33 @@ +/* bt_random_wait.h */ + +#ifndef BT_RANDOM_WAIT_H +#define BT_RANDOM_WAIT_H + +#include "bt_action.h" +#include "core/object.h" + +class BTRandomWait : public BTAction { + GDCLASS(BTRandomWait, BTAction); + +private: + Vector2 duration_min_max = Vector2(1.0, 2.0); + + float _time_passed = 0.0; + float _duration = 0.0; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + virtual void _enter(); + virtual int _tick(float p_delta); + +public: + void set_duration_min_max(Vector2 p_value) { + duration_min_max = p_value; + emit_changed(); + } + Vector2 get_duration_min_max() const { return duration_min_max; } +}; + +#endif // BT_RANDOM_WAIT_H \ No newline at end of file diff --git a/limboai/bt/bt_wait.cpp b/limboai/bt/bt_wait.cpp new file mode 100644 index 0000000..73c64fc --- /dev/null +++ b/limboai/bt/bt_wait.cpp @@ -0,0 +1,30 @@ +/* bt_wait.cpp */ + +#include "bt_wait.h" +#include "core/class_db.h" +#include "core/object.h" +#include "core/variant.h" + +String BTWait::_generate_name() const { + return vformat("Wait %ss", duration); +} + +void BTWait::_enter() { + _time_passed = 0.0; +} + +int BTWait::_tick(float p_delta) { + _time_passed += p_delta; + if (_time_passed < duration) { + return RUNNING; + } else { + return SUCCESS; + } +} + +void BTWait::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_duration", "p_value"), &BTWait::set_duration); + ClassDB::bind_method(D_METHOD("get_duration"), &BTWait::get_duration); + + ADD_PROPERTY(PropertyInfo(Variant::REAL, "duration"), "set_duration", "get_duration"); +} diff --git a/limboai/bt/bt_wait.h b/limboai/bt/bt_wait.h new file mode 100644 index 0000000..aadff9a --- /dev/null +++ b/limboai/bt/bt_wait.h @@ -0,0 +1,32 @@ +/* bt_wait.h */ + +#ifndef BT_WAIT_H +#define BT_WAIT_H + +#include "bt_action.h" +#include "core/object.h" + +class BTWait : public BTAction { + GDCLASS(BTWait, BTAction); + +private: + float duration = 1.0; + + float _time_passed = 0.0; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + virtual void _enter(); + virtual int _tick(float p_delta); + +public: + void set_duration(float p_value) { + duration = p_value; + emit_changed(); + } + float get_duration() const { return duration; } +}; + +#endif // BT_WAIT_H \ No newline at end of file diff --git a/limboai/bt/bt_wait_ticks.cpp b/limboai/bt/bt_wait_ticks.cpp new file mode 100644 index 0000000..099cec0 --- /dev/null +++ b/limboai/bt/bt_wait_ticks.cpp @@ -0,0 +1,30 @@ +/* bt_wait_ticks.cpp */ + +#include "bt_wait_ticks.h" +#include "core/class_db.h" +#include "core/object.h" +#include "core/variant.h" + +String BTWaitTicks::_generate_name() const { + return vformat("WaitTicks x%d", num_ticks); +} + +void BTWaitTicks::_enter() { + _num_passed = 0; +} + +int BTWaitTicks::_tick(float p_delta) { + _num_passed += 1; + if (_num_passed < num_ticks) { + return RUNNING; + } else { + return SUCCESS; + } +} + +void BTWaitTicks::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_num_ticks", "p_value"), &BTWaitTicks::set_num_ticks); + ClassDB::bind_method(D_METHOD("get_num_ticks"), &BTWaitTicks::get_num_ticks); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "num_ticks"), "set_num_ticks", "get_num_ticks"); +} diff --git a/limboai/bt/bt_wait_ticks.h b/limboai/bt/bt_wait_ticks.h new file mode 100644 index 0000000..3d6aa9a --- /dev/null +++ b/limboai/bt/bt_wait_ticks.h @@ -0,0 +1,32 @@ +/* bt_wait_num_ticks.h */ + +#ifndef BT_WAIT_TICKS_H +#define BT_WAIT_TICKS_H + +#include "bt_action.h" +#include "core/object.h" + +class BTWaitTicks : public BTAction { + GDCLASS(BTWaitTicks, BTAction); + +private: + int num_ticks = 1; + + int _num_passed = 0; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + virtual void _enter(); + virtual int _tick(float p_delta); + +public: + void set_num_ticks(int p_value) { + num_ticks = p_value; + emit_changed(); + } + int get_num_ticks() const { return num_ticks; } +}; + +#endif // BT_WAIT_TICKS_H \ No newline at end of file diff --git a/limboai/icons/icon_b_t_random_wait.svg b/limboai/icons/icon_b_t_random_wait.svg new file mode 100644 index 0000000..935727c --- /dev/null +++ b/limboai/icons/icon_b_t_random_wait.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/limboai/icons/icon_b_t_wait_ticks.svg b/limboai/icons/icon_b_t_wait_ticks.svg new file mode 100644 index 0000000..935727c --- /dev/null +++ b/limboai/icons/icon_b_t_wait_ticks.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/limboai/register_types.cpp b/limboai/register_types.cpp index c3f4f48..8ec1b90 100644 --- a/limboai/register_types.cpp +++ b/limboai/register_types.cpp @@ -20,6 +20,7 @@ #include "bt/bt_probability.h" #include "bt/bt_random_selector.h" #include "bt/bt_random_sequence.h" +#include "bt/bt_random_wait.h" #include "bt/bt_repeat.h" #include "bt/bt_repeat_until_failure.h" #include "bt/bt_repeat_until_success.h" @@ -28,6 +29,8 @@ #include "bt/bt_sequence.h" #include "bt/bt_task.h" #include "bt/bt_time_limit.h" +#include "bt/bt_wait.h" +#include "bt/bt_wait_ticks.h" #include "limbo_string_names.h" #include "limbo_utility.h" @@ -61,6 +64,9 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); LimboStringNames::create(); }