limboai/bt/actions/bt_random_wait.cpp

51 lines
1.4 KiB
C++
Raw Normal View History

2022-08-29 12:06:48 +00:00
/* bt_random_wait.cpp */
#include "bt_random_wait.h"
#include "core/math/math_funcs.h"
2022-08-29 12:06:48 +00:00
String BTRandomWait::_generate_name() const {
return vformat("Wait %s to %s sec",
Math::snapped(min_duration, 0.001),
Math::snapped(max_duration, 0.001));
2022-08-29 12:06:48 +00:00
}
void BTRandomWait::_enter() {
2022-12-17 10:26:48 +00:00
time_passed = 0.0;
duration = Math::random(min_duration, max_duration);
2022-08-29 12:06:48 +00:00
}
int BTRandomWait::_tick(double p_delta) {
2022-12-17 10:26:48 +00:00
time_passed += p_delta;
if (time_passed < duration) {
2022-08-29 12:06:48 +00:00
return RUNNING;
} else {
return SUCCESS;
}
}
void BTRandomWait::set_min_duration(double p_max_duration) {
min_duration = p_max_duration;
if (max_duration < min_duration) {
set_max_duration(min_duration);
}
emit_changed();
}
void BTRandomWait::set_max_duration(double p_max_duration) {
max_duration = p_max_duration;
if (min_duration > max_duration) {
set_min_duration(max_duration);
}
emit_changed();
}
2022-08-29 12:06:48 +00:00
void BTRandomWait::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_min_duration"), &BTRandomWait::set_min_duration);
ClassDB::bind_method(D_METHOD("get_min_duration"), &BTRandomWait::get_min_duration);
ClassDB::bind_method(D_METHOD("set_max_duration"), &BTRandomWait::set_max_duration);
ClassDB::bind_method(D_METHOD("get_max_duration"), &BTRandomWait::get_max_duration);
2022-08-29 12:06:48 +00:00
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_duration"), "set_min_duration", "get_min_duration");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_duration"), "set_max_duration", "get_max_duration");
2022-08-29 12:06:48 +00:00
}