limboai/bt/tasks/scene/bt_stop_animation.cpp

90 lines
3.9 KiB
C++
Raw Permalink Normal View History

2023-08-14 10:43:12 +00:00
/**
* bt_stop_animation.cpp
* =============================================================================
* Copyright 2021-2024 Serhii Snitsaruk
2023-08-14 10:43:12 +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.
* =============================================================================
*/
2023-08-15 15:49:13 +00:00
#include "bt_stop_animation.h"
2023-08-14 10:43:12 +00:00
//**** Setters / Getters
void BTStopAnimation::set_animation_player(Ref<BBNode> p_animation_player) {
animation_player_param = p_animation_player;
emit_changed();
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 10:43:12 +00:00
}
}
void BTStopAnimation::set_animation_name(StringName p_animation_name) {
animation_name = p_animation_name;
emit_changed();
}
void BTStopAnimation::set_keep_state(bool p_keep_state) {
keep_state = p_keep_state;
emit_changed();
}
2023-08-14 10:43:12 +00:00
//**** Task Implementation
PackedStringArray BTStopAnimation::get_configuration_warnings() {
PackedStringArray warnings = BTAction::get_configuration_warnings();
2023-08-14 10:43:12 +00:00
if (animation_player_param.is_null()) {
warnings.append("Animation Player parameter is not set.");
2023-08-14 10:43:12 +00:00
} else {
if (animation_player_param->get_value_source() == BBParam::SAVED_VALUE && animation_player_param->get_saved_value() == Variant()) {
warnings.append("Path to AnimationPlayer node is not set.");
} else if (animation_player_param->get_value_source() == BBParam::BLACKBOARD_VAR && animation_player_param->get_variable() == StringName()) {
warnings.append("AnimationPlayer blackboard variable is not set.");
2023-08-14 10:43:12 +00:00
}
}
return warnings;
2023-08-14 10:43:12 +00:00
}
String BTStopAnimation::_generate_name() {
return "StopAnimation" +
(animation_name != StringName() ? vformat(" \"%s\"", animation_name) : "") +
(keep_state ? " keep_state: true" : "");
2023-08-14 10:43:12 +00:00
}
void BTStopAnimation::_setup() {
setup_failed = true;
ERR_FAIL_COND_MSG(animation_player_param.is_null(), "BTStopAnimation: 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 10:43:12 +00:00
ERR_FAIL_COND_MSG(animation_player == nullptr, "BTStopAnimation: Failed to get AnimationPlayer.");
if (animation_name != StringName()) {
ERR_FAIL_COND_MSG(!animation_player->has_animation(animation_name), vformat("BTStopAnimation: Animation not found: %s", animation_name));
}
setup_failed = false;
}
BT::Status BTStopAnimation::_tick(double p_delta) {
2023-08-14 10:43:12 +00:00
ERR_FAIL_COND_V_MSG(setup_failed == true, FAILURE, "BTStopAnimation: _setup() failed - returning FAILURE.");
if (animation_player->is_playing() && (animation_name == StringName() || animation_name == animation_player->get_assigned_animation())) {
animation_player->stop(keep_state);
2023-08-14 10:43:12 +00:00
}
return SUCCESS;
}
//**** Godot
void BTStopAnimation::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_animation_player", "animation_player"), &BTStopAnimation::set_animation_player);
2023-08-14 10:43:12 +00:00
ClassDB::bind_method(D_METHOD("get_animation_player"), &BTStopAnimation::get_animation_player);
ClassDB::bind_method(D_METHOD("set_animation_name", "name"), &BTStopAnimation::set_animation_name);
2023-08-14 10:43:12 +00:00
ClassDB::bind_method(D_METHOD("get_animation_name"), &BTStopAnimation::get_animation_name);
ClassDB::bind_method(D_METHOD("set_keep_state", "keep_state"), &BTStopAnimation::set_keep_state);
ClassDB::bind_method(D_METHOD("get_keep_state"), &BTStopAnimation::get_keep_state);
2023-08-14 10:43:12 +00:00
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::BOOL, "keep_state"), "set_keep_state", "get_keep_state");
2023-08-14 10:43:12 +00:00
}