feat: simplifying enemy state logic
This commit is contained in:
parent
603b3afcc2
commit
710e9c7bde
6 changed files with 148 additions and 154 deletions
|
|
@ -53,24 +53,8 @@ AnimationPlayer *EnemyRifleman::get_anim() const {
|
|||
return this->anim;
|
||||
}
|
||||
|
||||
void RiflemanState::set_target(Node *node) {
|
||||
this->target = cast_to<EnemyRifleman>(node);
|
||||
}
|
||||
|
||||
EnemyRifleman *RiflemanState::get_target() const {
|
||||
return this->target;
|
||||
}
|
||||
|
||||
NpcUnit *RiflemanState::get_unit() const {
|
||||
return this->target->get_unit();
|
||||
}
|
||||
|
||||
NavigationAgent3D *RiflemanState::get_nav() const {
|
||||
return this->target->get_nav();
|
||||
}
|
||||
|
||||
AnimationPlayer *RiflemanState::get_anim() const {
|
||||
return this->target->get_anim();
|
||||
return cast_to<EnemyRifleman>(get_body());
|
||||
}
|
||||
|
||||
void RiflemanPatrolState::enter_state() {
|
||||
|
|
|
|||
|
|
@ -29,19 +29,12 @@ private:
|
|||
|
||||
/* ============================== STATES ============================== */
|
||||
|
||||
class RiflemanState : public State {
|
||||
class RiflemanState : public EnemyState {
|
||||
GDCLASS(RiflemanState, State);
|
||||
static void _bind_methods() {}
|
||||
|
||||
public:
|
||||
virtual void set_target(Node *target) override;
|
||||
EnemyRifleman *get_target() const;
|
||||
NpcUnit *get_unit() const;
|
||||
NavigationAgent3D *get_nav() const;
|
||||
AnimationPlayer *get_anim() const;
|
||||
|
||||
private:
|
||||
EnemyRifleman *target{ nullptr };
|
||||
};
|
||||
|
||||
class RiflemanPatrolState : public RiflemanState {
|
||||
|
|
|
|||
|
|
@ -1,27 +1,15 @@
|
|||
#include "enemy_wretched.h"
|
||||
#include "wave_survival/macros.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"
|
||||
#include "wave_survival/state_machine.h"
|
||||
|
||||
void EnemyWretched::_bind_methods() {
|
||||
BIND_PROPERTY(Variant::FLOAT, chase_speed);
|
||||
}
|
||||
|
||||
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::_bind_methods() {}
|
||||
|
||||
void EnemyWretched::ready() {
|
||||
this->fsm->add_state(memnew(WretchedPatrolState));
|
||||
this->fsm->add_state(memnew(WretchedChaseState));
|
||||
this->fsm->add_state(memnew(WretchedAttackState));
|
||||
get_fsm()->add_state(memnew(WretchedPatrolState));
|
||||
get_fsm()->add_state(memnew(WretchedChaseState));
|
||||
get_fsm()->add_state(memnew(WretchedAttackState));
|
||||
}
|
||||
|
||||
void EnemyWretched::_notification(int what) {
|
||||
|
|
@ -31,99 +19,26 @@ void EnemyWretched::_notification(int what) {
|
|||
switch (what) {
|
||||
default:
|
||||
return;
|
||||
case NOTIFICATION_ENTER_TREE:
|
||||
if (!is_ready()) {
|
||||
connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered));
|
||||
}
|
||||
return;
|
||||
case NOTIFICATION_READY:
|
||||
ready();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void EnemyWretched::set_chase_speed(float speed) {
|
||||
this->chase_speed = speed;
|
||||
}
|
||||
|
||||
float EnemyWretched::get_chase_speed() const {
|
||||
return this->chase_speed;
|
||||
}
|
||||
|
||||
AnimationPlayer *EnemyWretched::get_anim() const {
|
||||
return this->anim;
|
||||
}
|
||||
|
||||
void WretchedState::set_target(Node *node) {
|
||||
this->target = cast_to<EnemyWretched>(node);
|
||||
}
|
||||
|
||||
EnemyWretched *WretchedState::get_target() const {
|
||||
return this->target;
|
||||
}
|
||||
|
||||
NpcUnit *WretchedState::get_unit() const {
|
||||
return this->target->get_unit();
|
||||
}
|
||||
|
||||
NavigationAgent3D *WretchedState::get_nav() const {
|
||||
return this->target->get_nav();
|
||||
}
|
||||
|
||||
AnimationPlayer *WretchedState::get_anim() const {
|
||||
return this->target->get_anim();
|
||||
}
|
||||
|
||||
void WretchedPatrolState::set_patrol_target(Vector3 path_point) {
|
||||
get_nav()->set_target_position(path_point + get_target()->get_unit_offset_3d());
|
||||
}
|
||||
|
||||
void WretchedPatrolState::on_velocity_calculated(Vector3 direction) {
|
||||
get_target()->set_movement_direction(Vector2{ direction.x, direction.z } / get_target()->get_movement_speed());
|
||||
}
|
||||
|
||||
void WretchedPatrolState::enter_state() {
|
||||
this->path = get_target()->get_unit()->get_patrol_path();
|
||||
|
||||
float const max_speed{ get_unit()->get_patrol_speed() };
|
||||
get_target()->set_movement_speed(max_speed);
|
||||
get_nav()->set_max_speed(max_speed);
|
||||
if (this->path) {
|
||||
Vector3 const nav_target{ this->path->get_closest_point(get_target()->get_global_position(), &this->path_point) };
|
||||
set_patrol_target(nav_target);
|
||||
}
|
||||
get_nav()->connect("velocity_computed", this->mp_on_velocity_calculated);
|
||||
}
|
||||
|
||||
void WretchedPatrolState::process(double delta) {
|
||||
if (this->path) {
|
||||
if (get_nav()->is_navigation_finished()) {
|
||||
this->path_point += 1;
|
||||
set_patrol_target(this->path->point_at(this->path_point));
|
||||
}
|
||||
Vector3 const direction{ get_target()->get_global_position().direction_to(get_nav()->get_next_path_position()).normalized() };
|
||||
if (get_nav()->get_avoidance_enabled()) {
|
||||
get_nav()->set_velocity(direction);
|
||||
} else {
|
||||
on_velocity_calculated(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WretchedPatrolState::exit_state() {
|
||||
get_nav()->disconnect("velocity_computed", this->mp_on_velocity_calculated);
|
||||
return cast_to<EnemyWretched>(get_body());
|
||||
}
|
||||
|
||||
String WretchedPatrolState::get_next_state() const {
|
||||
if (get_target()->get_unit()->is_aware_of_player()) {
|
||||
if (get_body()->get_unit()->is_aware_of_player()) {
|
||||
return WretchedChaseState::get_class_static();
|
||||
}
|
||||
return get_class();
|
||||
}
|
||||
|
||||
void WretchedChaseState::enter_state() {
|
||||
get_target()->set_movement_speed(get_target()->get_chase_speed());
|
||||
get_nav()->set_max_speed(get_target()->get_chase_speed());
|
||||
get_target()->set_movement_speed(get_target()->get_max_speed());
|
||||
get_nav()->set_max_speed(get_target()->get_max_speed());
|
||||
get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
|
||||
get_anim()->play("ready"); // TODO: replace this with "run"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#ifndef ENEMY_WRETCHED_H
|
||||
#define ENEMY_WRETCHED_H
|
||||
|
||||
#include "scene/animation/animation_player.h"
|
||||
#include "wave_survival/enemy_body.h"
|
||||
#include "wave_survival/state.h"
|
||||
class PatrolPath;
|
||||
|
|
@ -11,56 +10,28 @@ 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:
|
||||
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 ============================== */
|
||||
|
||||
class WretchedState : public State {
|
||||
class WretchedState : public EnemyState {
|
||||
GDCLASS(WretchedState, State);
|
||||
static void _bind_methods() {}
|
||||
|
||||
public:
|
||||
virtual void set_target(Node *node) override;
|
||||
EnemyWretched *get_target() const;
|
||||
NpcUnit *get_unit() const;
|
||||
NavigationAgent3D *get_nav() const;
|
||||
AnimationPlayer *get_anim() const;
|
||||
|
||||
private:
|
||||
EnemyWretched *target{ nullptr };
|
||||
};
|
||||
|
||||
class WretchedPatrolState : public WretchedState {
|
||||
GDCLASS(WretchedPatrolState, WretchedState);
|
||||
class WretchedPatrolState : public PatrolState {
|
||||
GDCLASS(WretchedPatrolState, PatrolState);
|
||||
static void _bind_methods() {}
|
||||
void set_patrol_target(Vector3 path_point);
|
||||
void on_velocity_calculated(Vector3 direction);
|
||||
|
||||
public:
|
||||
virtual void enter_state() override;
|
||||
virtual void process(double delta) override;
|
||||
virtual String get_next_state() const override;
|
||||
virtual void exit_state() override;
|
||||
|
||||
private:
|
||||
int path_point{ 0 };
|
||||
PatrolPath *path{ nullptr };
|
||||
Callable mp_on_velocity_calculated{ callable_mp(this, &self_type::on_velocity_calculated) };
|
||||
String get_next_state() const override;
|
||||
};
|
||||
|
||||
class WretchedChaseState : public WretchedState {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue