From 89b861d5f866c9c8a456ec98207856078f33ccb1 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sun, 28 Aug 2022 15:34:04 +0200 Subject: [PATCH] Add TimeLimit --- limboai/bt_time_limit.cpp | 27 +++++++++++++++++++++++++++ limboai/bt_time_limit.h | 31 +++++++++++++++++++++++++++++++ limboai/register_types.cpp | 4 ++++ 3 files changed, 62 insertions(+) create mode 100644 limboai/bt_time_limit.cpp create mode 100644 limboai/bt_time_limit.h diff --git a/limboai/bt_time_limit.cpp b/limboai/bt_time_limit.cpp new file mode 100644 index 0000000..68612cc --- /dev/null +++ b/limboai/bt_time_limit.cpp @@ -0,0 +1,27 @@ +/* bt_time_limit.cpp */ + +#include "bt_time_limit.h" + +String BTTimeLimit::_generate_name() const { + return vformat("TimeLimit %ss", time_limit); +} + +void BTTimeLimit::_enter() { + _time_passed = 0.0; +} + +int BTTimeLimit::_tick(float p_delta) { + _time_passed += p_delta; + int status = get_child(0)->execute(p_delta); + if (status == RUNNING and _time_passed >= time_limit) { + get_child(0)->cancel(); + return FAILURE; + } + return status; +} + +void BTTimeLimit::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_time_limit", "p_value"), &BTTimeLimit::set_time_limit); + ClassDB::bind_method(D_METHOD("get_time_limit"), &BTTimeLimit::get_time_limit); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_limit"), "set_time_limit", "get_time_limit"); +} diff --git a/limboai/bt_time_limit.h b/limboai/bt_time_limit.h new file mode 100644 index 0000000..125557b --- /dev/null +++ b/limboai/bt_time_limit.h @@ -0,0 +1,31 @@ +/* bt_time_limit.h */ + +#ifndef BT_TIME_LIMIT_H +#define BT_TIME_LIMIT_H + +#include "bt_decorator.h" +#include "core/object.h" + +class BTTimeLimit : public BTDecorator { + GDCLASS(BTTimeLimit, BTDecorator); + +private: + float time_limit = 5.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_time_limit(float p_value) { + time_limit = p_value; + emit_changed(); + }; + float get_time_limit() const { return time_limit; }; +}; + +#endif // BT_TIME_LIMIT_H \ No newline at end of file diff --git a/limboai/register_types.cpp b/limboai/register_types.cpp index bb15515..8371f42 100644 --- a/limboai/register_types.cpp +++ b/limboai/register_types.cpp @@ -20,15 +20,18 @@ #include "bt_selector.h" #include "bt_sequence.h" #include "bt_task.h" +#include "bt_time_limit.h" #include "limbo_string_names.h" #include "limbo_utility.h" void register_limboai_types() { ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); @@ -40,6 +43,7 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); LimboStringNames::create(); }