From f4e3f162ec010669273f5ad5110089fd09590a87 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Wed, 19 Oct 2022 20:54:21 +0200 Subject: [PATCH] Implement basic BTState --- bt/bt_state.cpp | 34 +++++++++++++++++++ bt/bt_state.h | 34 +++++++++++++++++++ .../icons_tree.svg => icon_b_t_state.svg} | 0 ...state_machine.svg => icon_limbo_h_s_m.svg} | 0 .../icons_state.svg => icon_limbo_state.svg} | 0 icons/unused/icons_subtree.svg | 1 - register_types.cpp | 2 ++ 7 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 bt/bt_state.cpp create mode 100644 bt/bt_state.h rename icons/{unused/icons_tree.svg => icon_b_t_state.svg} (100%) rename icons/{unused/icons_state_machine.svg => icon_limbo_h_s_m.svg} (100%) rename icons/{unused/icons_state.svg => icon_limbo_state.svg} (100%) delete mode 100644 icons/unused/icons_subtree.svg diff --git a/bt/bt_state.cpp b/bt/bt_state.cpp new file mode 100644 index 0000000..cbcafc7 --- /dev/null +++ b/bt/bt_state.cpp @@ -0,0 +1,34 @@ +/* bt_state.cpp */ + +#include "bt_state.h" +#include "core/variant.h" +#include "modules/limboai/bt/bt_task.h" + +void BTState::_setup() { + blackboard->prefetch_nodepath_vars(this); + root_task = behavior_tree->instance(get_owner(), blackboard); + root_task->initialize(get_owner(), blackboard); +} + +void BTState::_exit() { + root_task->cancel(); +} + +void BTState::_update(float p_delta) { + int status = root_task->execute(p_delta); + if (status == BTTask::SUCCESS) { + get_root()->dispatch("success", Variant()); + } else if (status == BTTask::FAILURE) { + get_root()->dispatch("failure", Variant()); + } +} + +void BTState::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_behavior_tree", "p_value"), &BTState::set_behavior_tree); + ClassDB::bind_method(D_METHOD("get_behavior_tree"), &BTState::get_behavior_tree); + // ClassDB::bind_method(D_METHOD("set_blackboard", "p_blackboard"), &BTState::set_blackboard); + ClassDB::bind_method(D_METHOD("get_blackboard"), &BTState::get_blackboard); + + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "behavior_tree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_behavior_tree", "get_behavior_tree"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "Blackboard", 0), "", "get_blackboard"); +} diff --git a/bt/bt_state.h b/bt/bt_state.h new file mode 100644 index 0000000..9ae1160 --- /dev/null +++ b/bt/bt_state.h @@ -0,0 +1,34 @@ +/* bt_state.h */ + +#ifndef BT_STATE_H +#define BT_STATE_H + +#include "core/object.h" +#include "modules/limboai/bt/behavior_tree.h" +#include "modules/limboai/bt/bt_task.h" +#include "modules/limboai/limbo_state.h" + +class BTState : public LimboState { + GDCLASS(BTState, LimboState); + +private: + Ref behavior_tree; + Ref blackboard; + Ref root_task; + +protected: + static void _bind_methods(); + + virtual void _setup(); + // virtual void _enter() {} + virtual void _exit(); + virtual void _update(float p_delta); + +public: + void set_behavior_tree(const Ref &p_value) { behavior_tree = p_value; } + Ref get_behavior_tree() const { return behavior_tree; } + // void set_blackboard(const Ref &p_value) { blackboard = p_value; } + Ref get_blackboard() const { return blackboard; } +}; + +#endif // BT_STATE_H \ No newline at end of file diff --git a/icons/unused/icons_tree.svg b/icons/icon_b_t_state.svg similarity index 100% rename from icons/unused/icons_tree.svg rename to icons/icon_b_t_state.svg diff --git a/icons/unused/icons_state_machine.svg b/icons/icon_limbo_h_s_m.svg similarity index 100% rename from icons/unused/icons_state_machine.svg rename to icons/icon_limbo_h_s_m.svg diff --git a/icons/unused/icons_state.svg b/icons/icon_limbo_state.svg similarity index 100% rename from icons/unused/icons_state.svg rename to icons/icon_limbo_state.svg diff --git a/icons/unused/icons_subtree.svg b/icons/unused/icons_subtree.svg deleted file mode 100644 index f8cd473..0000000 --- a/icons/unused/icons_subtree.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/register_types.cpp b/register_types.cpp index 6909dd1..d7ae6e9 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -13,6 +13,7 @@ #include "bt/actions/bt_wait_ticks.h" #include "bt/behavior_tree.h" #include "bt/bt_player.h" +#include "bt/bt_state.h" #include "bt/bt_task.h" #include "bt/composites/bt_composite.h" #include "bt/composites/bt_dynamic_selector.h" @@ -59,6 +60,7 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class();