wave-survival/modules/wave_survival/enemies/enemy_wretched.cpp

56 lines
1.6 KiB
C++

#include "enemy_wretched.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() {
}
void EnemyWretched::ready() {
if (StateMachine * fsm{ cast_to<StateMachine>(get_node(NodePath("%StateMachine"))) }) {
fsm->add_state(memnew(WretchedPatrolState));
}
}
void EnemyWretched::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void WretchedState::set_target(Node *node) {
this->target = cast_to<EnemyWretched>(node);
}
EnemyWretched *WretchedState::get_target() const {
return this->target;
}
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() };
get_target()->set_movement_speed(max_speed);
this->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));
}
void WretchedPatrolState::process(double delta) {
if (this->nav->is_navigation_finished()) {
this->path_point += 1;
this->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()) };
get_target()->set_movement_direction(Vector2{ direction.x, direction.z }.normalized());
}