Implement basic BTState

This commit is contained in:
Serhii Snitsaruk 2022-10-19 20:54:21 +02:00
parent c43fac596c
commit f4e3f162ec
7 changed files with 70 additions and 1 deletions

34
bt/bt_state.cpp Normal file
View File

@ -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");
}

34
bt/bt_state.h Normal file
View File

@ -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<BehaviorTree> behavior_tree;
Ref<Blackboard> blackboard;
Ref<BTTask> 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<BehaviorTree> &p_value) { behavior_tree = p_value; }
Ref<BehaviorTree> get_behavior_tree() const { return behavior_tree; }
// void set_blackboard(const Ref<Blackboard> &p_value) { blackboard = p_value; }
Ref<Blackboard> get_blackboard() const { return blackboard; }
};
#endif // BT_STATE_H

View File

Before

Width:  |  Height:  |  Size: 301 B

After

Width:  |  Height:  |  Size: 301 B

View File

Before

Width:  |  Height:  |  Size: 375 B

After

Width:  |  Height:  |  Size: 375 B

View File

Before

Width:  |  Height:  |  Size: 141 B

After

Width:  |  Height:  |  Size: 141 B

View File

@ -1 +0,0 @@
<svg enable-background="new 0 0 16 16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="m16 10.03v-4h-6v1.5h-6.5v-3.53h2.5v-4h-6v4h2.5v10.5h.17.83 6.5v1.5h6v-4h-6v1.5h-6.5v-4.97h6.5v1.5z" fill="#cea4f1"/></svg>

Before

Width:  |  Height:  |  Size: 225 B

View File

@ -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<BTTask>();
ClassDB::register_class<BehaviorTree>();
ClassDB::register_class<BTPlayer>();
ClassDB::register_class<BTState>();
ClassDB::register_class<BTComposite>();
ClassDB::register_class<BTSequence>();