feat: reworked enemy wretched logic

This commit is contained in:
Sara 2025-07-21 23:17:37 +02:00
parent 2cd1cba04f
commit 74aead946c
2 changed files with 48 additions and 13 deletions

View file

@ -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<StateMachine>(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<StateMachine>(node) }) {
this->fsm = fsm;
}
if (node->has_node(NodePath("AnimationPlayer"))) {
this->anim = cast_to<AnimationPlayer>(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<EnemyWretched>(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();
}

View file

@ -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;
};