2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_time_limit.cpp
|
|
|
|
* =============================================================================
|
|
|
|
* Copyright 2021-2023 Serhii Snitsaruk
|
|
|
|
*
|
|
|
|
* 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 13:34:04 +00:00
|
|
|
|
|
|
|
#include "bt_time_limit.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2023-04-10 05:54:02 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
2022-08-28 13:34:04 +00:00
|
|
|
|
|
|
|
String BTTimeLimit::_generate_name() const {
|
2023-04-10 05:54:02 +00:00
|
|
|
return vformat("TimeLimit %s sec", Math::snapped(time_limit, 0.001));
|
2022-08-28 13:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 08:08:11 +00:00
|
|
|
int BTTimeLimit::_tick(double 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.");
|
2022-08-28 13:34:04 +00:00
|
|
|
int status = get_child(0)->execute(p_delta);
|
2023-07-28 22:35:47 +00:00
|
|
|
if (status == RUNNING && get_elapsed_time() >= 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);
|
2022-12-15 07:26:52 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_limit"), "set_time_limit", "get_time_limit");
|
2022-08-28 13:34:04 +00:00
|
|
|
}
|