From 74aead946cf3d56c67c0dec6ba8ea6577841700a Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 21 Jul 2025 23:17:37 +0200 Subject: [PATCH] feat: reworked enemy wretched logic --- .../wave_survival/enemies/enemy_wretched.cpp | 47 ++++++++++++++----- .../wave_survival/enemies/enemy_wretched.h | 14 +++++- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/modules/wave_survival/enemies/enemy_wretched.cpp b/modules/wave_survival/enemies/enemy_wretched.cpp index fa975de4..2f375b74 100644 --- a/modules/wave_survival/enemies/enemy_wretched.cpp +++ b/modules/wave_survival/enemies/enemy_wretched.cpp @@ -1,4 +1,5 @@ #include "enemy_wretched.h" +#include "scene/animation/animation_player.h" #include "wave_survival/npc_unit.h" #include "wave_survival/patrol_path.h" #include "wave_survival/player_body.h" @@ -7,12 +8,19 @@ void EnemyWretched::_bind_methods() { } -void EnemyWretched::ready() { - if (StateMachine * fsm{ cast_to(get_node(NodePath("%StateMachine"))) }) { - fsm->add_state(memnew(WretchedPatrolState)); - fsm->add_state(memnew(WretchedChaseState)); - fsm->add_state(memnew(WretchedAttackState)); +void EnemyWretched::on_child_entered(Node *node) { + if (StateMachine * fsm{ cast_to(node) }) { + this->fsm = fsm; } + if (node->has_node(NodePath("AnimationPlayer"))) { + this->anim = cast_to(node->get_node(NodePath("AnimationPlayer"))); + } +} +void EnemyWretched::ready() { + print_line("ready"); + fsm->add_state(memnew(WretchedPatrolState)); + fsm->add_state(memnew(WretchedChaseState)); + fsm->add_state(memnew(WretchedAttackState)); } void EnemyWretched::_notification(int what) { @@ -22,16 +30,21 @@ void EnemyWretched::_notification(int what) { switch (what) { default: return; + case NOTIFICATION_ENTER_TREE: + connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered)); + return; case NOTIFICATION_READY: ready(); return; } } +AnimationPlayer *EnemyWretched::get_anim() const { + return this->anim; +} + void WretchedState::set_target(Node *node) { this->target = cast_to(node); - this->nav = this->target->get_nav(); - this->unit = this->target->get_unit(); } EnemyWretched *WretchedState::get_target() const { @@ -39,11 +52,15 @@ EnemyWretched *WretchedState::get_target() const { } NpcUnit *WretchedState::get_unit() const { - return this->unit; + return this->target->get_unit(); } NavigationAgent3D *WretchedState::get_nav() const { - return this->nav; + return this->target->get_nav(); +} + +AnimationPlayer *WretchedState::get_anim() const { + return this->target->get_anim(); } void WretchedPatrolState::set_patrol_target(Vector3 path_point) { @@ -85,6 +102,7 @@ void WretchedChaseState::enter_state() { get_target()->set_movement_speed(get_unit()->get_patrol_speed() * 2.f); get_nav()->set_max_speed(get_unit()->get_patrol_speed() * 2.f); get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position()); + get_anim()->play("ready"); // TODO: replace this with "run" } void WretchedChaseState::process(double delta) { @@ -101,6 +119,13 @@ String WretchedChaseState::get_next_state() const { return get_class(); } -String WretchedAttackState::get_next_state() const { - return WretchedChaseState::get_class_static(); +void WretchedAttackState::enter_state() { + get_anim()->play("attack"); +} + +String WretchedAttackState::get_next_state() const { + if (get_anim()->get_current_animation().is_empty()) { + return WretchedChaseState::get_class_static(); + } + return get_class(); } diff --git a/modules/wave_survival/enemies/enemy_wretched.h b/modules/wave_survival/enemies/enemy_wretched.h index a9f5c36d..62bb4749 100644 --- a/modules/wave_survival/enemies/enemy_wretched.h +++ b/modules/wave_survival/enemies/enemy_wretched.h @@ -5,14 +5,24 @@ #include "wave_survival/state.h" class PatrolPath; class NpcUnit; +class AnimationPlayer; +class StateMachine; class EnemyWretched : public EnemyBody { GDCLASS(EnemyWretched, EnemyBody); static void _bind_methods(); + void on_child_entered(Node *node); void ready(); protected: void _notification(int what); + +public: + AnimationPlayer *get_anim() const; + +private: + StateMachine *fsm{ nullptr }; + AnimationPlayer *anim{ nullptr }; }; /* ============================== STATES ============================== */ @@ -26,11 +36,10 @@ public: EnemyWretched *get_target() const; NpcUnit *get_unit() const; NavigationAgent3D *get_nav() const; + AnimationPlayer *get_anim() const; private: - NpcUnit *unit{ nullptr }; EnemyWretched *target{ nullptr }; - NavigationAgent3D *nav{ nullptr }; }; class WretchedPatrolState : public WretchedState { @@ -63,6 +72,7 @@ class WretchedAttackState : public WretchedState { static void _bind_methods() {} public: + virtual void enter_state() override; virtual String get_next_state() const override; };