2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_repeat.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 12:49:02 +00:00
|
|
|
|
|
|
|
#include "bt_repeat.h"
|
2024-01-06 23:47:46 +00:00
|
|
|
#include "util/limbo_string_names.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
String BTRepeat::_generate_name() {
|
2023-08-06 10:45:19 +00:00
|
|
|
if (forever) {
|
2024-01-06 23:47:46 +00:00
|
|
|
return LSNAME(repeat_forever);
|
2023-08-06 10:45:19 +00:00
|
|
|
}
|
2022-08-28 12:49:02 +00:00
|
|
|
return vformat("Repeat x%s", times);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTRepeat::_enter() {
|
2022-12-17 10:47:10 +00:00
|
|
|
cur_iteration = 1;
|
2022-08-28 12:49:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTRepeat::_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.");
|
2023-09-19 11:43:26 +00:00
|
|
|
Status status = get_child(0)->execute(p_delta);
|
2023-08-06 10:45:19 +00:00
|
|
|
if (status == RUNNING || forever) {
|
2022-08-28 12:49:02 +00:00
|
|
|
return RUNNING;
|
|
|
|
} else if (status == FAILURE && abort_on_failure) {
|
|
|
|
return FAILURE;
|
2022-12-17 10:47:10 +00:00
|
|
|
} else if (cur_iteration >= times) {
|
2022-08-28 12:49:02 +00:00
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
2022-12-17 10:47:10 +00:00
|
|
|
cur_iteration += 1;
|
2022-08-28 12:49:02 +00:00
|
|
|
return RUNNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 10:45:19 +00:00
|
|
|
void BTRepeat::set_forever(bool p_forever) {
|
|
|
|
forever = p_forever;
|
|
|
|
notify_property_list_changed();
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTRepeat::set_times(int p_value) {
|
|
|
|
times = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTRepeat::set_abort_on_failure(bool p_value) {
|
|
|
|
abort_on_failure = p_value;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTRepeat::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
if (!forever) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "times", PROPERTY_HINT_RANGE, "1,65535"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, "abort_on_failure"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 12:49:02 +00:00
|
|
|
void BTRepeat::_bind_methods() {
|
2023-08-06 10:45:19 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_forever", "p_value"), &BTRepeat::set_forever);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_forever"), &BTRepeat::get_forever);
|
2022-08-28 12:49:02 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_times", "p_value"), &BTRepeat::set_times);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_times"), &BTRepeat::get_times);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_abort_on_failure", "p_value"), &BTRepeat::set_abort_on_failure);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_abort_on_failure"), &BTRepeat::get_abort_on_failure);
|
|
|
|
|
2023-08-06 10:45:19 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "forever"), "set_forever", "get_forever");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "times", PROPERTY_HINT_RANGE, "1,65535", PROPERTY_USAGE_NONE), "set_times", "get_times");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "abort_on_failure", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_abort_on_failure", "get_abort_on_failure");
|
2022-08-28 12:49:02 +00:00
|
|
|
}
|
2024-01-06 23:47:46 +00:00
|
|
|
|
|
|
|
BTRepeat::BTRepeat() {
|
|
|
|
}
|