2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_cooldown.cpp
|
|
|
|
* =============================================================================
|
2024-03-04 11:49:09 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-08-28 20:24:33 +00:00
|
|
|
|
|
|
|
#include "bt_cooldown.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2022-08-28 20:24:33 +00:00
|
|
|
#include "scene/main/scene_tree.h"
|
2024-01-06 23:47:46 +00:00
|
|
|
#endif
|
2024-01-13 16:10:42 +00:00
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
#include <godot_cpp/classes/scene_tree.hpp>
|
|
|
|
#endif
|
2022-08-28 20:24:33 +00:00
|
|
|
|
2023-08-15 15:05:30 +00:00
|
|
|
//**** Setters / Getters
|
|
|
|
|
|
|
|
void BTCooldown::set_duration(double p_value) {
|
|
|
|
duration = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTCooldown::set_process_pause(bool p_value) {
|
|
|
|
process_pause = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTCooldown::set_start_cooled(bool p_value) {
|
|
|
|
start_cooled = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTCooldown::set_trigger_on_failure(bool p_value) {
|
|
|
|
trigger_on_failure = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2024-03-04 11:49:09 +00:00
|
|
|
void BTCooldown::set_cooldown_state_var(const StringName &p_value) {
|
2023-08-15 15:05:30 +00:00
|
|
|
cooldown_state_var = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** Task Implementation
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
String BTCooldown::_generate_name() {
|
2023-04-10 05:54:02 +00:00
|
|
|
return vformat("Cooldown %s sec", Math::snapped(duration, 0.001));
|
2022-08-28 20:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTCooldown::_setup() {
|
2024-03-04 11:49:09 +00:00
|
|
|
if (cooldown_state_var == StringName()) {
|
2024-06-23 10:14:15 +00:00
|
|
|
cooldown_state_var = vformat("cooldown_%d", get_instance_id());
|
2022-08-28 20:24:33 +00:00
|
|
|
}
|
2022-09-21 10:37:38 +00:00
|
|
|
get_blackboard()->set_var(cooldown_state_var, false);
|
2022-08-28 20:24:33 +00:00
|
|
|
if (start_cooled) {
|
|
|
|
_chill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTCooldown::_tick(double p_delta) {
|
2022-09-21 10:37:38 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child.");
|
2022-09-08 13:56:51 +00:00
|
|
|
if (get_blackboard()->get_var(cooldown_state_var, true)) {
|
2022-08-28 20:24:33 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2023-09-19 11:43:26 +00:00
|
|
|
Status status = get_child(0)->execute(p_delta);
|
2022-08-28 20:24:33 +00:00
|
|
|
if (status == SUCCESS || (trigger_on_failure && status == FAILURE)) {
|
|
|
|
_chill();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTCooldown::_chill() {
|
2022-09-21 10:37:38 +00:00
|
|
|
get_blackboard()->set_var(cooldown_state_var, true);
|
2022-12-17 10:47:10 +00:00
|
|
|
if (timer.is_valid()) {
|
|
|
|
timer->set_time_left(duration);
|
2022-08-28 20:24:33 +00:00
|
|
|
} else {
|
2024-01-13 16:10:42 +00:00
|
|
|
timer = SCENE_TREE()->create_timer(duration, process_pause);
|
|
|
|
ERR_FAIL_NULL(timer);
|
2024-01-09 12:42:54 +00:00
|
|
|
timer->connect(LW_NAME(timeout), callable_mp(this, &BTCooldown::_on_timeout), CONNECT_ONE_SHOT);
|
2022-08-28 20:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTCooldown::_on_timeout() {
|
2022-09-21 10:37:38 +00:00
|
|
|
get_blackboard()->set_var(cooldown_state_var, false);
|
2022-12-17 10:47:10 +00:00
|
|
|
timer.unref();
|
2022-08-28 20:24:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:05:30 +00:00
|
|
|
//**** Godot
|
|
|
|
|
2022-08-28 20:24:33 +00:00
|
|
|
void BTCooldown::_bind_methods() {
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_duration", "duration"), &BTCooldown::set_duration);
|
2022-08-28 20:24:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_duration"), &BTCooldown::get_duration);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_process_pause", "enable"), &BTCooldown::set_process_pause);
|
2022-08-28 20:24:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_process_pause"), &BTCooldown::get_process_pause);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_start_cooled", "enable"), &BTCooldown::set_start_cooled);
|
2022-08-28 20:24:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_start_cooled"), &BTCooldown::get_start_cooled);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_trigger_on_failure", "enable"), &BTCooldown::set_trigger_on_failure);
|
2022-08-28 20:24:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_trigger_on_failure"), &BTCooldown::get_trigger_on_failure);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_cooldown_state_var", "variable"), &BTCooldown::set_cooldown_state_var);
|
2022-08-28 20:24:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_cooldown_state_var"), &BTCooldown::get_cooldown_state_var);
|
2022-09-21 10:37:38 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_on_timeout"), &BTCooldown::_on_timeout);
|
2022-08-28 20:24:33 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "duration"), "set_duration", "get_duration");
|
2022-08-28 20:24:33 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "process_pause"), "set_process_pause", "get_process_pause");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "start_cooled"), "set_start_cooled", "get_start_cooled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trigger_on_failure"), "set_trigger_on_failure", "get_trigger_on_failure");
|
2024-03-04 11:49:09 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "cooldown_state_var"), "set_cooldown_state_var", "get_cooldown_state_var");
|
2022-08-28 20:24:33 +00:00
|
|
|
}
|