chore: cleaned up rifleman class

This commit is contained in:
Sara Gerretsen 2026-02-08 18:16:31 +01:00
parent 8c2526b4e6
commit f7ce254249
2 changed files with 3 additions and 43 deletions

View file

@ -1,28 +1,16 @@
#include "enemy_rifleman.h"
#include "core/os/memory.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/macros.h"
#include "wave_survival/npc_unit.h"
#include "wave_survival/player_detector.h"
#include "wave_survival/state_machine.h"
void EnemyRifleman::_bind_methods() {
BIND_PROPERTY(Variant::FLOAT, chase_speed);
}
void EnemyRifleman::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 EnemyRifleman::ready() {
this->fsm->add_state(memnew(RiflemanPatrolState));
this->fsm->add_state(memnew(RiflemanChaseState));
this->fsm->add_state(memnew(RiflemanFireState));
get_fsm()->add_state(memnew(RiflemanPatrolState));
get_fsm()->add_state(memnew(RiflemanChaseState));
get_fsm()->add_state(memnew(RiflemanFireState));
}
void EnemyRifleman::_notification(int what) {
@ -32,29 +20,12 @@ void EnemyRifleman::_notification(int what) {
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
if (!is_ready()) {
connect("child_entered", callable_mp(this, &self_type::on_child_entered));
}
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void EnemyRifleman::set_chase_speed(float speed) {
this->chase_speed = speed;
}
float EnemyRifleman::get_chase_speed() const {
return this->chase_speed;
}
AnimationPlayer *EnemyRifleman::get_anim() const {
return this->anim;
}
EnemyRifleman *RiflemanState::get_target() const {
return cast_to<EnemyRifleman>(get_body());
}

View file

@ -10,21 +10,10 @@ class NavigationAgent3D;
class EnemyRifleman : public EnemyBody {
GDCLASS(EnemyRifleman, EnemyBody);
static void _bind_methods();
void on_child_entered(Node *node);
void ready();
protected:
void _notification(int what);
public:
void set_chase_speed(float speed);
float get_chase_speed() const;
AnimationPlayer *get_anim() const;
private:
float chase_speed{ 5.f };
StateMachine *fsm{ nullptr };
AnimationPlayer *anim{ nullptr };
};
/* ============================== STATES ============================== */