feat: implemented avoidance and exposed speed settings to editor

This commit is contained in:
Sara 2025-07-22 20:02:33 +02:00
parent fb9417a1d8
commit 2134fcae92
8 changed files with 61 additions and 14 deletions

View file

@ -1,11 +1,13 @@
#include "enemy_wretched.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/macros.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) {
@ -16,6 +18,7 @@ void EnemyWretched::on_child_entered(Node *node) {
this->anim = cast_to<AnimationPlayer>(node->get_node(NodePath("AnimationPlayer")));
}
}
void EnemyWretched::ready() {
fsm->add_state(memnew(WretchedPatrolState));
fsm->add_state(memnew(WretchedChaseState));
@ -38,6 +41,14 @@ void EnemyWretched::_notification(int what) {
}
}
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;
}
@ -66,6 +77,10 @@ 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();
@ -76,6 +91,7 @@ void WretchedPatrolState::enter_state() {
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) {
@ -84,11 +100,19 @@ void WretchedPatrolState::process(double delta) {
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()) };
get_target()->set_movement_direction(Vector2{ direction.x, direction.z }.normalized());
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);
}
String WretchedPatrolState::get_next_state() const {
if (get_target()->get_unit()->is_aware_of_player()) {
return WretchedChaseState::get_class_static();
@ -97,9 +121,8 @@ String WretchedPatrolState::get_next_state() const {
}
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_target()->set_movement_speed(get_target()->get_chase_speed());
get_nav()->set_max_speed(get_target()->get_chase_speed());
get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
get_anim()->play("ready"); // TODO: replace this with "run"
}