feat: generalising of NPC logic

This commit is contained in:
Sara Gerretsen 2026-02-02 20:51:14 +01:00
parent 41bcf4cdf7
commit 1625a11c42
6 changed files with 74 additions and 37 deletions

View file

@ -3,6 +3,8 @@
#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/player_detector.h"
void EnemyBody::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_movement_direction", "direction"), &self_type::set_movement_direction);
@ -17,6 +19,9 @@ void EnemyBody::on_child_added(Node *node) {
if (NavigationAgent3D * nav{ cast_to<NavigationAgent3D>(node) }) {
this->nav = nav;
}
if (PlayerDetector * detector{ cast_to<PlayerDetector>(node) }) {
this->detector = detector;
}
if (node->has_node(NodePath("AnimationPlayer"))) {
this->anim = cast_to<AnimationPlayer>(node->get_node(NodePath("AnimationPlayer")));
}
@ -49,7 +54,9 @@ void EnemyBody::_notification(int what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
connect("child_entered_tree", callable_mp(this, &self_type::on_child_added));
if (!is_ready()) {
connect("child_entered_tree", callable_mp(this, &self_type::on_child_added));
}
return;
case NOTIFICATION_READY:
set_physics_process(true);
@ -97,6 +104,10 @@ StateMachine *EnemyBody::get_fsm() const {
return this->fsm;
}
PlayerDetector *EnemyBody::get_detector() const {
return this->detector;
}
void EnemyBody::set_movement_speed(float value) {
this->movement_speed = value;
}
@ -138,15 +149,15 @@ EnemyBody *EnemyState::get_body() const {
return this->body;
}
void PatrolState::set_patrol_target(Vector3 path_point) {
void EnemyPatrolState::set_patrol_target(Vector3 path_point) {
get_nav()->set_target_position(path_point + get_body()->get_unit_offset_3d());
}
void PatrolState::on_velocity_calculated(Vector3 direction) {
void EnemyPatrolState::on_velocity_calculated(Vector3 direction) {
get_body()->set_movement_direction(Vector2{ direction.x, direction.z } / get_body()->get_movement_speed());
}
void PatrolState::enter_state() {
void EnemyPatrolState::enter_state() {
this->path = get_body()->get_unit()->get_patrol_path();
float const max_speed{ get_unit()->get_patrol_speed() };
@ -159,7 +170,7 @@ void PatrolState::enter_state() {
get_nav()->connect("velocity_computed", this->mp_on_velocity_calculated);
}
void PatrolState::process(double delta) {
void EnemyPatrolState::process(double delta) {
if (this->path) {
if (get_nav()->is_navigation_finished()) {
this->path_point += 1;
@ -174,6 +185,20 @@ void PatrolState::process(double delta) {
}
}
void PatrolState::exit_state() {
void EnemyPatrolState::exit_state() {
get_nav()->disconnect("velocity_computed", this->mp_on_velocity_calculated);
}
void EnemyChaseState::enter_state() {
get_body()->set_movement_speed(get_body()->get_max_speed());
get_nav()->set_max_speed(get_body()->get_max_speed());
get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
get_anim()->play("ready"); // TODO: replace this with "run"
}
void EnemyChaseState::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_body()->get_global_position().direction_to(get_nav()->get_next_path_position()) };
get_body()->set_movement_direction(Vector2{ direction.x, direction.z }.normalized());
}