2022-10-19 18:54:21 +00:00
|
|
|
/* bt_state.cpp */
|
|
|
|
|
|
|
|
#include "bt_state.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/object/class_db.h"
|
|
|
|
#include "core/variant/variant.h"
|
2022-10-19 18:54:21 +00:00
|
|
|
#include "modules/limboai/bt/bt_task.h"
|
2022-10-19 21:41:56 +00:00
|
|
|
#include "modules/limboai/limbo_state.h"
|
2022-10-19 18:54:21 +00:00
|
|
|
|
|
|
|
void BTState::_setup() {
|
2022-10-19 21:41:56 +00:00
|
|
|
root_task = behavior_tree->instance(get_agent(), get_blackboard());
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTState::_exit() {
|
|
|
|
root_task->cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTState::_update(float p_delta) {
|
|
|
|
int status = root_task->execute(p_delta);
|
|
|
|
if (status == BTTask::SUCCESS) {
|
2022-11-04 12:26:49 +00:00
|
|
|
get_root()->dispatch(success_event, Variant());
|
2022-10-19 18:54:21 +00:00
|
|
|
} else if (status == BTTask::FAILURE) {
|
2022-11-04 12:26:49 +00:00
|
|
|
get_root()->dispatch(failure_event, Variant());
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-11-04 12:26:49 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_success_event", "p_event_name"), &BTState::set_success_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_success_event"), &BTState::get_success_event);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_failure_event", "p_event_name"), &BTState::set_failure_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_failure_event"), &BTState::get_failure_event);
|
|
|
|
|
2022-10-19 18:54:21 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "behavior_tree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_behavior_tree", "get_behavior_tree");
|
2022-11-04 12:26:49 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "success_event"), "set_success_event", "get_success_event");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "failure_event"), "set_failure_event", "get_failure_event");
|
2022-10-19 21:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BTState::BTState() {
|
2022-11-04 12:26:49 +00:00
|
|
|
success_event = "success";
|
|
|
|
failure_event = "failure";
|
2022-10-19 18:54:21 +00:00
|
|
|
}
|