This commit is contained in:
Serhii Snitsaruk 2022-10-26 23:12:29 +02:00
parent ffeb7a2e5e
commit b68dc0a36a
6 changed files with 22 additions and 19 deletions

View File

@ -6,13 +6,8 @@
#include "modules/limboai/bt/bt_task.h"
#include "modules/limboai/limbo_state.h"
// void BTState::initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard) {
void BTState::_setup() {
// blackboard->prefetch_nodepath_vars(this);
// blackboard->set_parent_scope(p_blackboard);
root_task = behavior_tree->instance(get_agent(), get_blackboard());
// LimboState::initialize(p_agent, blackboard);
}
void BTState::_exit() {
@ -31,13 +26,9 @@ void BTState::_update(float p_delta) {
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("_get_blackboard_data"), &BTState::_get_blackboard_data);
// ClassDB::bind_method(D_METHOD("_set_blackboard_data", "p_data"), &BTState::_set_blackboard_data);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "behavior_tree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_behavior_tree", "get_behavior_tree");
// ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "blackboard_data"), "_set_blackboard_data", "_get_blackboard_data");
}
BTState::BTState() {
// blackboard = Ref<Blackboard>(memnew(Blackboard));
}

View File

@ -13,7 +13,6 @@ class BTState : public LimboState {
private:
Ref<BehaviorTree> behavior_tree;
// Ref<Blackboard> blackboard;
Ref<BTTask> root_task;
protected:
@ -24,15 +23,10 @@ protected:
virtual void _exit();
virtual void _update(float p_delta);
// void _set_blackboard_data(Dictionary p_value) { blackboard->set_data(p_value); }
// Dictionary _get_blackboard_data() const { return blackboard->get_data(); }
public:
void set_behavior_tree(const Ref<BehaviorTree> &p_value) { behavior_tree = p_value; }
Ref<BehaviorTree> get_behavior_tree() const { return behavior_tree; }
// virtual void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
BTState();
};

View File

@ -113,6 +113,11 @@ LimboState *LimboHSM::get_leaf_state() const {
}
}
void LimboHSM::set_initial_state(Node *p_state) {
ERR_FAIL_COND(p_state == nullptr || !p_state->is_class("LimboState"));
initial_state = Object::cast_to<LimboState>(p_state);
}
bool LimboHSM::dispatch(const String &p_event, const Variant &p_cargo) {
ERR_FAIL_COND_V(p_event.empty(), false);
@ -209,6 +214,10 @@ void LimboHSM::_notification(int p_what) {
void LimboHSM::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_update_mode", "p_mode"), &LimboHSM::set_update_mode);
ClassDB::bind_method(D_METHOD("get_update_mode"), &LimboHSM::get_update_mode);
ClassDB::bind_method(D_METHOD("set_initial_state", "p_state"), &LimboHSM::set_initial_state);
ClassDB::bind_method(D_METHOD("get_initial_state"), &LimboHSM::get_initial_state);
ClassDB::bind_method(D_METHOD("get_active_state"), &LimboHSM::get_active_state);
ClassDB::bind_method(D_METHOD("get_leaf_state"), &LimboHSM::get_leaf_state);
ClassDB::bind_method(D_METHOD("set_active", "p_active"), &LimboHSM::set_active);
@ -221,7 +230,8 @@ void LimboHSM::_bind_methods() {
BIND_ENUM_CONSTANT(MANUAL);
ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Idle, Physics, Manual"), "set_update_mode", "get_update_mode");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ANYSTATE"), "", "anystate");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ANYSTATE", PROPERTY_HINT_NONE, "", 0), "", "anystate");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "initial_state", PROPERTY_HINT_RESOURCE_TYPE, "LimboState", 0), "set_initial_state", "get_initial_state");
ADD_SIGNAL(MethodInfo("state_changed", PropertyInfo(Variant::OBJECT, "p_state", PROPERTY_HINT_NONE, "", 0, "LimboState")));
}
@ -230,4 +240,4 @@ LimboHSM::LimboHSM() {
update_mode = UpdateMode::IDLE;
active_state = nullptr;
initial_state = nullptr;
}
}

View File

@ -48,6 +48,9 @@ public:
LimboState *get_leaf_state() const;
void set_active(bool p_active);
void set_initial_state(Node *p_state);
LimboState *get_initial_state() const { return initial_state; }
virtual void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
virtual bool dispatch(const String &p_event, const Variant &p_cargo);

View File

@ -13,7 +13,7 @@ const String LimboState::EVENT_FINISHED = "finished";
LimboState *LimboState::get_root() const {
const LimboState *state = this;
while (get_parent() && get_parent()->is_class("LimboState")) {
while (state->get_parent() && state->get_parent()->is_class("LimboState")) {
state = Object::cast_to<LimboState>(get_parent());
}
return const_cast<LimboState *>(state);
@ -137,6 +137,7 @@ void LimboState::_notification(int p_what) {
void LimboState::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_root"), &LimboState::get_root);
ClassDB::bind_method(D_METHOD("get_agent"), &LimboState::get_agent);
ClassDB::bind_method(D_METHOD("set_agent", "p_agent"), &LimboState::set_agent);
ClassDB::bind_method(D_METHOD("event_finished"), &LimboState::event_finished);
ClassDB::bind_method(D_METHOD("is_active"), &LimboState::is_active);
ClassDB::bind_method(D_METHOD("_setup"), &LimboState::_setup);
@ -159,7 +160,9 @@ void LimboState::_bind_methods() {
BIND_VMETHOD(MethodInfo("_exit"));
BIND_VMETHOD(MethodInfo("_update", PropertyInfo(Variant::REAL, "p_delta")));
ADD_PROPERTY(PropertyInfo(Variant::STRING, "EVENT_FINISHED"), "", "event_finished");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "EVENT_FINISHED", PROPERTY_HINT_NONE, "", 0), "", "event_finished");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "agent", PROPERTY_HINT_RESOURCE_TYPE, "Object", 0), "set_agent", "get_agent");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_RESOURCE_TYPE, "Blackboard", 0), "", "get_blackboard");
ADD_SIGNAL(MethodInfo("setup"));
ADD_SIGNAL(MethodInfo("entered"));

View File

@ -47,7 +47,9 @@ public:
static const String EVENT_FINISHED;
Ref<Blackboard> get_blackboard() const { return blackboard; }
Object *get_agent() const { return agent; }
void set_agent(Object *p_agent) { agent = p_agent; }
virtual void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
virtual bool dispatch(const String &p_event, const Variant &p_cargo);