2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_random_wait.cpp
|
|
|
|
* =============================================================================
|
2024-03-04 20:36:16 +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-29 12:06:48 +00:00
|
|
|
|
|
|
|
#include "bt_random_wait.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-10 21:45:42 +00:00
|
|
|
#include "../../../util/limbo_compat.h"
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
String BTRandomWait::_generate_name() {
|
2023-04-10 05:54:02 +00:00
|
|
|
return vformat("Wait %s to %s sec",
|
2023-04-10 08:08:11 +00:00
|
|
|
Math::snapped(min_duration, 0.001),
|
|
|
|
Math::snapped(max_duration, 0.001));
|
2022-08-29 12:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTRandomWait::_enter() {
|
2024-01-06 23:47:46 +00:00
|
|
|
duration = RAND_RANGE(min_duration, max_duration);
|
2022-08-29 12:06:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTRandomWait::_tick(double p_delta) {
|
2023-04-14 08:16:26 +00:00
|
|
|
if (get_elapsed_time() < duration) {
|
2022-08-29 12:06:48 +00:00
|
|
|
return RUNNING;
|
|
|
|
} else {
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:08:11 +00:00
|
|
|
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() {
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_min_duration", "duration_sec"), &BTRandomWait::set_min_duration);
|
2023-04-10 08:08:11 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_min_duration"), &BTRandomWait::get_min_duration);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_max_duration", "duration_sec"), &BTRandomWait::set_max_duration);
|
2023-04-10 08:08:11 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_max_duration"), &BTRandomWait::get_max_duration);
|
2022-08-29 12:06:48 +00:00
|
|
|
|
2023-04-10 08:08:11 +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
|
|
|
}
|