From ec6fc76335942b582b2a346ee3aa1c4c56943592 Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 21 Jul 2025 16:50:58 +0200 Subject: [PATCH] feat: units hold formation while patrolling --- modules/wave_survival/enemies/enemy_wretched.cpp | 8 ++++++-- modules/wave_survival/enemies/enemy_wretched.h | 1 + modules/wave_survival/enemy_body.cpp | 12 ++++++++++++ modules/wave_survival/enemy_body.h | 4 ++++ modules/wave_survival/npc_unit.cpp | 7 +++++-- modules/wave_survival/npc_unit.h | 6 +++--- modules/wave_survival/state_machine.cpp | 4 ---- 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/modules/wave_survival/enemies/enemy_wretched.cpp b/modules/wave_survival/enemies/enemy_wretched.cpp index fe7ff827..08831a98 100644 --- a/modules/wave_survival/enemies/enemy_wretched.cpp +++ b/modules/wave_survival/enemies/enemy_wretched.cpp @@ -46,6 +46,10 @@ NavigationAgent3D *WretchedState::get_nav() const { return this->nav; } +void WretchedPatrolState::set_patrol_target(Vector3 path_point) { + get_nav()->set_target_position(path_point + get_target()->get_unit_offset_3d()); +} + void WretchedPatrolState::enter_state() { this->path = get_target()->get_unit()->get_patrol_path(); @@ -53,13 +57,13 @@ void WretchedPatrolState::enter_state() { get_target()->set_movement_speed(max_speed); get_nav()->set_max_speed(max_speed); Vector3 const nav_target{ this->path->get_closest_point(get_target()->get_global_position(), &this->path_point) }; - get_nav()->set_target_position(nav_target); + set_patrol_target(nav_target); } void WretchedPatrolState::process(double delta) { if (get_nav()->is_navigation_finished()) { this->path_point += 1; - get_nav()->set_target_position(this->path->point_at(this->path_point)); + 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()); diff --git a/modules/wave_survival/enemies/enemy_wretched.h b/modules/wave_survival/enemies/enemy_wretched.h index 9db4e2b4..a9f5c36d 100644 --- a/modules/wave_survival/enemies/enemy_wretched.h +++ b/modules/wave_survival/enemies/enemy_wretched.h @@ -36,6 +36,7 @@ private: class WretchedPatrolState : public WretchedState { GDCLASS(WretchedPatrolState, WretchedState); static void _bind_methods() {} + void set_patrol_target(Vector3 path_point); public: virtual void enter_state() override; diff --git a/modules/wave_survival/enemy_body.cpp b/modules/wave_survival/enemy_body.cpp index 6e721dfe..669c4b21 100644 --- a/modules/wave_survival/enemy_body.cpp +++ b/modules/wave_survival/enemy_body.cpp @@ -59,3 +59,15 @@ void EnemyBody::set_movement_speed(float value) { float EnemyBody::get_movement_speed() const { return this->movement_speed; } + +void EnemyBody::set_unit_offset(Vector2 offset) { + this->unit_offset = offset; +} + +Vector2 EnemyBody::get_unit_offset() const { + return this->unit_offset; +} + +Vector3 EnemyBody::get_unit_offset_3d() const { + return { this->unit_offset.x, 0, this->unit_offset.y }; +} diff --git a/modules/wave_survival/enemy_body.h b/modules/wave_survival/enemy_body.h index d7f62ecc..3949c109 100644 --- a/modules/wave_survival/enemy_body.h +++ b/modules/wave_survival/enemy_body.h @@ -23,6 +23,9 @@ public: NavigationAgent3D *get_nav() const; void set_movement_speed(float value); float get_movement_speed() const; + void set_unit_offset(Vector2 offset); + Vector2 get_unit_offset() const; + Vector3 get_unit_offset_3d() const; private: float movement_speed{ 1.f }; @@ -31,6 +34,7 @@ private: StateMachine *fsm{ nullptr }; NpcUnit *unit{ nullptr }; NavigationAgent3D *nav{ nullptr }; + Vector2 unit_offset{ 0, 0 }; }; #endif // !ENEMY_BODY_H diff --git a/modules/wave_survival/npc_unit.cpp b/modules/wave_survival/npc_unit.cpp index 00fcad3b..6d0a6d61 100644 --- a/modules/wave_survival/npc_unit.cpp +++ b/modules/wave_survival/npc_unit.cpp @@ -18,8 +18,11 @@ void NpcUnit::child_added(Node *node) { if (EnemyBody * npc{ cast_to(node) }) { this->npcs.push_back(npc); npc->set_unit(this); - PlayerDetector *detector{ cast_to(npc->get_node(NodePath("%PlayerDetector"))) }; - detector->connect(PlayerDetector::sig_awareness_changed, callable_mp(this, &self_type::on_npc_awareness_changed)); + Vector3 const offset{ npc->get_global_position() - get_global_position() }; + npc->set_unit_offset({ offset.x, offset.z }); + if (PlayerDetector * detector{ cast_to(npc->get_node(NodePath("%PlayerDetector"))) }) { + detector->connect(PlayerDetector::sig_awareness_changed, callable_mp(this, &self_type::on_npc_awareness_changed)); + } } } diff --git a/modules/wave_survival/npc_unit.h b/modules/wave_survival/npc_unit.h index 0737617c..9d489b85 100644 --- a/modules/wave_survival/npc_unit.h +++ b/modules/wave_survival/npc_unit.h @@ -1,13 +1,13 @@ #ifndef NPC_UNIT_H #define NPC_UNIT_H -#include "scene/main/node.h" +#include "scene/3d/node_3d.h" class StateMachine; class EnemyBody; class PatrolPath; -class NpcUnit : public Node { - GDCLASS(NpcUnit, Node); +class NpcUnit : public Node3D { + GDCLASS(NpcUnit, Node3D); static void _bind_methods(); void on_npc_awareness_changed(bool seen); void child_added(Node *node); diff --git a/modules/wave_survival/state_machine.cpp b/modules/wave_survival/state_machine.cpp index 7566ce54..393e5c52 100644 --- a/modules/wave_survival/state_machine.cpp +++ b/modules/wave_survival/state_machine.cpp @@ -56,8 +56,4 @@ void StateMachine::add_state(State *state) { if (this->current_state == nullptr) { this->switch_to_state(state); } - print_line("states:"); - for (KeyValue kv : this->states) { - print_line(vformat("\t\t| %s %s", kv.key, kv.value)); - } }