70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
#include "enemy_wretched.h"
|
|
#include "scene/animation/animation_player.h"
|
|
#include "wave_survival/npc_unit.h"
|
|
#include "wave_survival/player_body.h"
|
|
#include "wave_survival/state_machine.h"
|
|
|
|
void EnemyWretched::_bind_methods() {}
|
|
|
|
void EnemyWretched::ready() {
|
|
get_fsm()->add_state(memnew(WretchedPatrolState));
|
|
get_fsm()->add_state(memnew(WretchedChaseState));
|
|
get_fsm()->add_state(memnew(WretchedAttackState));
|
|
}
|
|
|
|
void EnemyWretched::_notification(int what) {
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_READY:
|
|
ready();
|
|
return;
|
|
}
|
|
}
|
|
|
|
EnemyWretched *WretchedState::get_target() const {
|
|
return cast_to<EnemyWretched>(get_body());
|
|
}
|
|
|
|
String WretchedPatrolState::get_next_state() const {
|
|
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_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"
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
void WretchedAttackState::enter_state() {
|
|
get_anim()->play("attack");
|
|
get_target()->set_movement_direction(Vector2());
|
|
}
|
|
|
|
String WretchedAttackState::get_next_state() const {
|
|
if (get_anim()->get_current_animation().is_empty()) {
|
|
return WretchedChaseState::get_class_static();
|
|
}
|
|
return get_class();
|
|
}
|