From 8e3c4c757069e8eff1659be2c2f67724c0c7bfd4 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Mon, 14 Aug 2023 14:08:10 +0200 Subject: [PATCH] Add BTPauseAnimation action --- bt/actions/bt_pause_animation.cpp | 70 +++++++++++++++++++++++++++++++ bt/actions/bt_pause_animation.h | 44 +++++++++++++++++++ config.py | 1 + register_types.cpp | 2 + 4 files changed, 117 insertions(+) create mode 100644 bt/actions/bt_pause_animation.cpp create mode 100644 bt/actions/bt_pause_animation.h diff --git a/bt/actions/bt_pause_animation.cpp b/bt/actions/bt_pause_animation.cpp new file mode 100644 index 0000000..ee1b142 --- /dev/null +++ b/bt/actions/bt_pause_animation.cpp @@ -0,0 +1,70 @@ +/** + * bt_pause_animation.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. + * ============================================================================= + */ + +#include "bt_pause_animation.h" + +//**** Setters / Getters + +void BTPauseAnimation::set_animation_player(Ref 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->connect(SNAME("changed"), Callable(this, SNAME("emit_changed"))); + } +} + +//**** Task Implementation + +String BTPauseAnimation::get_configuration_warning() const { + String warning = BTAction::get_configuration_warning(); + if (!warning.is_empty()) { + warning += "\n"; + } + + if (animation_player_param.is_null()) { + warning += "Animation Player parameter is not set.\n"; + } else { + if (animation_player_param->get_value_source() == BBParam::SAVED_VALUE && animation_player_param->get_saved_value().is_zero()) { + warning += "Path to AnimationPlayer node is not set.\n"; + } else if (animation_player_param->get_value_source() == BBParam::BLACKBOARD_VAR && animation_player_param->get_variable().is_empty()) { + warning += "AnimationPlayer blackboard variable is not set.\n"; + } + } + + return warning; +} + +String BTPauseAnimation::_generate_name() const { + return "PauseAnimation"; +} + +void BTPauseAnimation::_setup() { + setup_failed = true; + ERR_FAIL_COND_MSG(animation_player_param.is_null(), "BTPauseAnimation: AnimationPlayer parameter is not set."); + animation_player = Object::cast_to(animation_player_param->get_value(get_agent(), get_blackboard())); + ERR_FAIL_COND_MSG(animation_player == nullptr, "BTPauseAnimation: Failed to get AnimationPlayer."); + setup_failed = false; +} + +int BTPauseAnimation::_tick(double p_delta) { + ERR_FAIL_COND_V_MSG(setup_failed == true, FAILURE, "BTPauseAnimation: _setup() failed - returning FAILURE."); + animation_player->pause(); + return SUCCESS; +} + +//**** Godot + +void BTPauseAnimation::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_animation_player", "p_anim_player"), &BTPauseAnimation::set_animation_player); + ClassDB::bind_method(D_METHOD("get_animation_player"), &BTPauseAnimation::get_animation_player); + + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "animation_player", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_animation_player", "get_animation_player"); +} diff --git a/bt/actions/bt_pause_animation.h b/bt/actions/bt_pause_animation.h new file mode 100644 index 0000000..a54a436 --- /dev/null +++ b/bt/actions/bt_pause_animation.h @@ -0,0 +1,44 @@ +/** + * bt_pause_animation.h + * ============================================================================= + * 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. + * ============================================================================= + */ + +#ifndef BT_PAUSE_ANIMATION_H +#define BT_PAUSE_ANIMATION_H + +#include "bt_action.h" + +#include "modules/limboai/blackboard/bb_param/bb_node.h" + +#include "scene/animation/animation_player.h" + +class BTPauseAnimation : public BTAction { + GDCLASS(BTPauseAnimation, BTAction); + +private: + Ref animation_player_param; + + AnimationPlayer *animation_player = nullptr; + bool setup_failed = false; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const override; + virtual void _setup() override; + virtual int _tick(double p_delta) override; + +public: + void set_animation_player(Ref p_animation_player); + Ref get_animation_player() const { return animation_player_param; } + + virtual String get_configuration_warning() const override; +}; + +#endif // BT_PAUSE_ANIMATION diff --git a/config.py b/config.py index f4db882..b94b37a 100644 --- a/config.py +++ b/config.py @@ -76,6 +76,7 @@ def get_doc_classes(): "BTInvert", "BTNewScope", "BTParallel", + "BTPauseAnimation", "BTPlayAnimation", "BTPlayer", "BTProbability", diff --git a/register_types.cpp b/register_types.cpp index 3b8b8fe..25d36cc 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -47,6 +47,7 @@ #include "bt/actions/bt_action.h" #include "bt/actions/bt_console_print.h" #include "bt/actions/bt_fail.h" +#include "bt/actions/bt_pause_animation.h" #include "bt/actions/bt_play_animation.h" #include "bt/actions/bt_random_wait.h" #include "bt/actions/bt_set_agent_property.h" @@ -144,6 +145,7 @@ void initialize_limboai_module(ModuleInitializationLevel p_level) { GDREGISTER_CLASS(BTConsolePrint); GDREGISTER_CLASS(BTFail); GDREGISTER_CLASS(BTNewScope); + GDREGISTER_CLASS(BTPauseAnimation); GDREGISTER_CLASS(BTPlayAnimation); GDREGISTER_CLASS(BTRandomWait); GDREGISTER_CLASS(BTSetAgentProperty);