2023-08-14 15:24:27 +00:00
|
|
|
/**
|
|
|
|
* bt_await_animation.cpp
|
|
|
|
* =============================================================================
|
2024-03-04 20:36:16 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-08-14 15:24:27 +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.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bt_await_animation.h"
|
|
|
|
|
|
|
|
//**** Setters / Getters
|
|
|
|
|
|
|
|
void BTAwaitAnimation::set_animation_player(Ref<BBNode> p_animation_player) {
|
|
|
|
animation_player_param = p_animation_player;
|
|
|
|
emit_changed();
|
2024-08-02 08:07:42 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint() && animation_player_param.is_valid() &&
|
|
|
|
!animation_player_param->is_connected(LW_NAME(changed), callable_mp((Resource *)this, &Resource::emit_changed))) {
|
|
|
|
animation_player_param->connect(LW_NAME(changed), callable_mp((Resource *)this, &Resource::emit_changed));
|
2023-08-14 15:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 11:49:09 +00:00
|
|
|
void BTAwaitAnimation::set_animation_name(const StringName &p_animation_name) {
|
2023-08-14 15:24:27 +00:00
|
|
|
animation_name = p_animation_name;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTAwaitAnimation::set_max_time(double p_max_time) {
|
|
|
|
max_time = p_max_time;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** Task Implementation
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
PackedStringArray BTAwaitAnimation::get_configuration_warnings() {
|
2023-08-15 15:05:30 +00:00
|
|
|
PackedStringArray warnings = BTAction::get_configuration_warnings();
|
2023-08-14 15:24:27 +00:00
|
|
|
if (animation_player_param.is_null()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Animation Player parameter is not set.");
|
2023-08-14 15:24:27 +00:00
|
|
|
} else {
|
2024-01-06 23:47:46 +00:00
|
|
|
if (animation_player_param->get_value_source() == BBParam::SAVED_VALUE && animation_player_param->get_saved_value() == Variant()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Path to AnimationPlayer node is not set.");
|
2024-03-04 11:49:09 +00:00
|
|
|
} else if (animation_player_param->get_value_source() == BBParam::BLACKBOARD_VAR && animation_player_param->get_variable() == StringName()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("AnimationPlayer blackboard variable is not set.");
|
2023-08-14 15:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (animation_name == StringName()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Animation Name is required in order to wait for the animation to finish.");
|
2023-08-14 15:24:27 +00:00
|
|
|
}
|
|
|
|
if (max_time <= 0.0) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Max time should be greater than 0.0.");
|
2023-08-14 15:24:27 +00:00
|
|
|
}
|
2023-08-15 15:05:30 +00:00
|
|
|
return warnings;
|
2023-08-14 15:24:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
String BTAwaitAnimation::_generate_name() {
|
2023-08-14 15:24:27 +00:00
|
|
|
return "AwaitAnimation" +
|
|
|
|
(animation_name != StringName() ? vformat(" \"%s\"", animation_name) : " ???") +
|
|
|
|
vformat(" max_time: %ss", Math::snapped(max_time, 0.001));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTAwaitAnimation::_setup() {
|
|
|
|
setup_failed = true;
|
|
|
|
ERR_FAIL_COND_MSG(animation_player_param.is_null(), "BTAwaitAnimation: AnimationPlayer parameter is not set.");
|
2024-05-01 21:32:44 +00:00
|
|
|
animation_player = Object::cast_to<AnimationPlayer>(animation_player_param->get_value(get_scene_root(), get_blackboard()));
|
2023-08-14 15:24:27 +00:00
|
|
|
ERR_FAIL_COND_MSG(animation_player == nullptr, "BTAwaitAnimation: Failed to get AnimationPlayer.");
|
|
|
|
ERR_FAIL_COND_MSG(animation_name == StringName(), "BTAwaitAnimation: Animation Name is not set.");
|
|
|
|
ERR_FAIL_COND_MSG(!animation_player->has_animation(animation_name), vformat("BTAwaitAnimation: Animation not found: %s", animation_name));
|
|
|
|
setup_failed = false;
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTAwaitAnimation::_tick(double p_delta) {
|
2023-08-14 15:24:27 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(setup_failed == true, FAILURE, "BTAwaitAnimation: _setup() failed - returning FAILURE.");
|
|
|
|
|
|
|
|
// ! Doing this check instead of using signal due to a bug in Godot: https://github.com/godotengine/godot/issues/76127
|
|
|
|
if (animation_player->is_playing() && animation_player->get_assigned_animation() == animation_name) {
|
|
|
|
if (get_elapsed_time() < max_time) {
|
|
|
|
return RUNNING;
|
|
|
|
} else if (max_time > 0.0) {
|
|
|
|
WARN_PRINT(vformat("BTAwaitAnimation: Waiting time for the \"%s\" animation exceeded the allocated %s sec.", animation_name, max_time));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** Godot
|
|
|
|
|
|
|
|
void BTAwaitAnimation::_bind_methods() {
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_animation_player", "animation_player"), &BTAwaitAnimation::set_animation_player);
|
2023-08-14 15:24:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_animation_player"), &BTAwaitAnimation::get_animation_player);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_animation_name", "name"), &BTAwaitAnimation::set_animation_name);
|
2023-08-14 15:24:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_animation_name"), &BTAwaitAnimation::get_animation_name);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_max_time", "time_sec"), &BTAwaitAnimation::set_max_time);
|
2023-08-14 15:24:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_max_time"), &BTAwaitAnimation::get_max_time);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "animation_player", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_animation_player", "get_animation_player");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation_name"), "set_animation_name", "get_animation_name");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_time", PROPERTY_HINT_RANGE, "0.0,100.0"), "set_max_time", "get_max_time");
|
|
|
|
}
|