feat: implemented enemy wretched patrol and chase states
This commit is contained in:
parent
1b0e4384b9
commit
9517588415
12 changed files with 224 additions and 17 deletions
|
|
@ -10,6 +10,8 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,28 +30,69 @@ void EnemyWretched::_notification(int what) {
|
|||
|
||||
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 {
|
||||
return this->target;
|
||||
}
|
||||
|
||||
NpcUnit *WretchedState::get_unit() const {
|
||||
return this->unit;
|
||||
}
|
||||
|
||||
NavigationAgent3D *WretchedState::get_nav() const {
|
||||
return this->nav;
|
||||
}
|
||||
|
||||
void WretchedPatrolState::enter_state() {
|
||||
this->nav = get_target()->get_nav();
|
||||
this->path = get_target()->get_unit()->get_patrol_path();
|
||||
|
||||
float const max_speed{ get_target()->get_unit()->get_patrol_speed() };
|
||||
float const max_speed{ get_unit()->get_patrol_speed() };
|
||||
get_target()->set_movement_speed(max_speed);
|
||||
this->nav->set_max_speed(max_speed);
|
||||
get_nav()->set_max_speed(max_speed);
|
||||
this->path_point = this->path->get_closest_point(get_target()->get_global_position());
|
||||
this->nav->set_target_position(this->path->point_at(this->path_point));
|
||||
get_nav()->set_target_position(this->path->point_at(this->path_point));
|
||||
}
|
||||
|
||||
void WretchedPatrolState::process(double delta) {
|
||||
if (this->nav->is_navigation_finished()) {
|
||||
if (get_nav()->is_navigation_finished()) {
|
||||
this->path_point += 1;
|
||||
this->nav->set_target_position(this->path->point_at(this->path_point));
|
||||
get_nav()->set_target_position(this->path->point_at(this->path_point));
|
||||
}
|
||||
Vector3 const direction{ get_target()->get_global_position().direction_to(this->nav->get_next_path_position()) };
|
||||
Vector3 const direction{ get_target()->get_global_position().direction_to(get_nav()->get_next_path_position()) };
|
||||
get_target()->set_movement_direction(Vector2{ direction.x, direction.z }.normalized());
|
||||
}
|
||||
|
||||
String WretchedPatrolState::get_next_state() const {
|
||||
if (get_target()->get_unit()->is_aware_of_player()) {
|
||||
return WretchedChaseState::get_class_static();
|
||||
}
|
||||
return get_class();
|
||||
}
|
||||
|
||||
void WretchedChaseState::enter_state() {
|
||||
// TODO: replace this with a setting somewhere
|
||||
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());
|
||||
}
|
||||
|
||||
void WretchedChaseState::process(double delta) {
|
||||
// TODO: optimize this with some checks to avoid re-pathing every frame
|
||||
get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
|
||||
Vector3 const direction{ get_target()->get_global_position().direction_to(get_nav()->get_next_path_position()) };
|
||||
get_target()->set_movement_direction(Vector2{ direction.x, direction.z }.normalized());
|
||||
}
|
||||
|
||||
String WretchedChaseState::get_next_state() const {
|
||||
if (get_target()->get_global_position().distance_to(PlayerBody::get_singleton()->get_global_position()) < 2.f) {
|
||||
return WretchedAttackState::get_class_static();
|
||||
}
|
||||
return get_class();
|
||||
}
|
||||
|
||||
String WretchedAttackState::get_next_state() const {
|
||||
return WretchedChaseState::get_class_static();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "wave_survival/enemy_body.h"
|
||||
#include "wave_survival/state.h"
|
||||
class PatrolPath;
|
||||
class NpcUnit;
|
||||
|
||||
class EnemyWretched : public EnemyBody {
|
||||
GDCLASS(EnemyWretched, EnemyBody);
|
||||
|
|
@ -23,9 +24,13 @@ class WretchedState : public State {
|
|||
public:
|
||||
virtual void set_target(Node *node) override;
|
||||
EnemyWretched *get_target() const;
|
||||
NpcUnit *get_unit() const;
|
||||
NavigationAgent3D *get_nav() const;
|
||||
|
||||
private:
|
||||
NpcUnit *unit{ nullptr };
|
||||
EnemyWretched *target{ nullptr };
|
||||
NavigationAgent3D *nav{ nullptr };
|
||||
};
|
||||
|
||||
class WretchedPatrolState : public WretchedState {
|
||||
|
|
@ -35,11 +40,29 @@ class WretchedPatrolState : public WretchedState {
|
|||
public:
|
||||
virtual void enter_state() override;
|
||||
virtual void process(double delta) override;
|
||||
virtual String get_next_state() const override;
|
||||
|
||||
private:
|
||||
NavigationAgent3D *nav{ nullptr };
|
||||
int path_point{ 0 };
|
||||
PatrolPath *path{ nullptr };
|
||||
};
|
||||
|
||||
class WretchedChaseState : public WretchedState {
|
||||
GDCLASS(WretchedChaseState, WretchedState);
|
||||
static void _bind_methods() {}
|
||||
|
||||
public:
|
||||
virtual void enter_state() override;
|
||||
virtual void process(double delta) override;
|
||||
virtual String get_next_state() const override;
|
||||
};
|
||||
|
||||
class WretchedAttackState : public WretchedState {
|
||||
GDCLASS(WretchedAttackState, WretchedState);
|
||||
static void _bind_methods() {}
|
||||
|
||||
public:
|
||||
virtual String get_next_state() const override;
|
||||
};
|
||||
|
||||
#endif // !ENEMY_WRETCHED_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue