limboai/bt/decorators/bt_time_limit.cpp

30 lines
882 B
C++
Raw Normal View History

2022-08-28 13:34:04 +00:00
/* bt_time_limit.cpp */
#include "bt_time_limit.h"
#include "core/math/math_funcs.h"
2022-08-28 13:34:04 +00:00
String BTTimeLimit::_generate_name() const {
return vformat("TimeLimit %s sec", Math::snapped(time_limit, 0.001));
2022-08-28 13:34:04 +00:00
}
void BTTimeLimit::_enter() {
time_passed = 0.0;
2022-08-28 13:34:04 +00:00
}
int BTTimeLimit::_tick(float p_delta) {
2022-09-21 10:37:19 +00:00
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child.");
time_passed += p_delta;
2022-08-28 13:34:04 +00:00
int status = get_child(0)->execute(p_delta);
if (status == RUNNING and time_passed >= time_limit) {
2022-08-28 13:34:04 +00:00
get_child(0)->cancel();
return FAILURE;
}
return status;
}
void BTTimeLimit::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_time_limit", "p_value"), &BTTimeLimit::set_time_limit);
ClassDB::bind_method(D_METHOD("get_time_limit"), &BTTimeLimit::get_time_limit);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_limit"), "set_time_limit", "get_time_limit");
2022-08-28 13:34:04 +00:00
}