BTStopAnimation: Add `keep_state` and fixes

This commit is contained in:
Serhii Snitsaruk 2023-08-14 13:06:00 +02:00
parent abe1117055
commit 0b1d249ebe
2 changed files with 16 additions and 5 deletions

View File

@ -26,6 +26,11 @@ void BTStopAnimation::set_animation_name(StringName p_animation_name) {
emit_changed(); emit_changed();
} }
void BTStopAnimation::set_keep_state(bool p_keep_state) {
keep_state = p_keep_state;
emit_changed();
}
//**** Task Implementation //**** Task Implementation
String BTStopAnimation::get_configuration_warning() const { String BTStopAnimation::get_configuration_warning() const {
@ -48,10 +53,9 @@ String BTStopAnimation::get_configuration_warning() const {
} }
String BTStopAnimation::_generate_name() const { String BTStopAnimation::_generate_name() const {
if (animation_name == StringName() || animation_player_param.is_null()) { return "StopAnimation" +
return "StopAnimation"; (animation_name != StringName() ? vformat(" \"%s\"", animation_name) : "") +
} (keep_state ? " keep_state: true" : "");
return vformat("StopAnimation \"%s\"", animation_name);
} }
void BTStopAnimation::_setup() { void BTStopAnimation::_setup() {
@ -68,7 +72,7 @@ void BTStopAnimation::_setup() {
int BTStopAnimation::_tick(double p_delta) { int BTStopAnimation::_tick(double p_delta) {
ERR_FAIL_COND_V_MSG(setup_failed == true, FAILURE, "BTStopAnimation: _setup() failed - returning FAILURE."); 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())) { if (animation_player->is_playing() && (animation_name == StringName() || animation_name == animation_player->get_assigned_animation())) {
animation_player->stop(); animation_player->stop(keep_state);
} }
return SUCCESS; return SUCCESS;
} }
@ -80,7 +84,10 @@ void BTStopAnimation::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_animation_player"), &BTStopAnimation::get_animation_player); ClassDB::bind_method(D_METHOD("get_animation_player"), &BTStopAnimation::get_animation_player);
ClassDB::bind_method(D_METHOD("set_animation_name", "p_anim_name"), &BTStopAnimation::set_animation_name); ClassDB::bind_method(D_METHOD("set_animation_name", "p_anim_name"), &BTStopAnimation::set_animation_name);
ClassDB::bind_method(D_METHOD("get_animation_name"), &BTStopAnimation::get_animation_name); ClassDB::bind_method(D_METHOD("get_animation_name"), &BTStopAnimation::get_animation_name);
ClassDB::bind_method(D_METHOD("set_keep_state", "p_keep_state"), &BTStopAnimation::set_keep_state);
ClassDB::bind_method(D_METHOD("get_keep_state"), &BTStopAnimation::get_keep_state);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "animation_player", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_animation_player", "get_animation_player"); 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::STRING_NAME, "animation_name"), "set_animation_name", "get_animation_name");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_state"), "set_keep_state", "get_keep_state");
} }

View File

@ -24,6 +24,7 @@ class BTStopAnimation : public BTAction {
private: private:
Ref<BBNode> animation_player_param; Ref<BBNode> animation_player_param;
StringName animation_name; StringName animation_name;
bool keep_state = false;
AnimationPlayer *animation_player = nullptr; AnimationPlayer *animation_player = nullptr;
bool setup_failed = false; bool setup_failed = false;
@ -42,6 +43,9 @@ public:
void set_animation_name(StringName p_animation_name); void set_animation_name(StringName p_animation_name);
StringName get_animation_name() const { return animation_name; } StringName get_animation_name() const { return animation_name; }
void set_keep_state(bool p_keep_state);
bool get_keep_state() const { return keep_state; }
virtual String get_configuration_warning() const override; virtual String get_configuration_warning() const override;
}; };