From 8dad8bbc1f08f6608ac6c40e6fc40d7109ee6cdb Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 28 Aug 2025 19:12:57 +0200 Subject: [PATCH] feat: refactored how mapregion tracks units and npcs --- modules/wave_survival/map_region.cpp | 29 ++++++++++++++++++++++++---- modules/wave_survival/map_region.h | 6 +++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/modules/wave_survival/map_region.cpp b/modules/wave_survival/map_region.cpp index 9475707c..65dbb122 100644 --- a/modules/wave_survival/map_region.cpp +++ b/modules/wave_survival/map_region.cpp @@ -1,5 +1,6 @@ #include "map_region.h" #include "enemy_body.h" +#include "player_body.h" String const MapRegion::sig_difficulty_increased{ "difficulty_increased" }; String const MapRegion::sig_phase_changed{ "hunt_phase" }; @@ -7,16 +8,33 @@ String const MapRegion::sig_phase_changed{ "hunt_phase" }; void MapRegion::_bind_methods() { ADD_SIGNAL(MethodInfo(sig_difficulty_increased)); ADD_SIGNAL(MethodInfo(sig_phase_changed, PropertyInfo(Variant::BOOL, "hunt"))); + ClassDB::bind_method(D_METHOD("raise_difficulty", "amount"), &self_type::raise_difficulty); } -void MapRegion::on_node_entered(Node *node) { - if (EnemyBody * body{ cast_to(node) }) { - if (!this->units.has(body->get_unit())) { - body->get_unit()->set_region(this); +void MapRegion::on_node_entered(Node3D *node) { + if (cast_to(node) != nullptr) { + this->has_player = true; + } +} + +void MapRegion::on_node_exited(Node3D *node) { + if (cast_to(node) != nullptr) { + this->has_player = false; + } +} + +void MapRegion::on_child_entered_tree(Node *node) { + if (NpcUnit * unit{ cast_to(node) }) { + if (!this->units.has(unit)) { + unit->set_region(this); } } } +void MapRegion::enter_tree() { + connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered_tree)); +} + void MapRegion::ready() { connect("body_entered", callable_mp(this, &self_type::on_node_entered)); } @@ -28,6 +46,9 @@ void MapRegion::_notification(int what) { switch (what) { default: return; + case NOTIFICATION_ENTER_TREE: + enter_tree(); + return; case NOTIFICATION_READY: ready(); return; diff --git a/modules/wave_survival/map_region.h b/modules/wave_survival/map_region.h index 39f0a6dc..4ad4968c 100644 --- a/modules/wave_survival/map_region.h +++ b/modules/wave_survival/map_region.h @@ -8,7 +8,10 @@ class MapRegion : public Area3D { GDCLASS(MapRegion, Area3D); static void _bind_methods(); - void on_node_entered(Node *node); + void on_node_entered(Node3D *node); + void on_node_exited(Node3D *node); + void on_child_entered_tree(Node *node); + void enter_tree(); void ready(); protected: @@ -24,6 +27,7 @@ private: double difficulty{ 0.f }; bool hunt_phase{ false }; HashSet units{ nullptr }; + bool has_player{ false }; public: static String const sig_difficulty_increased;