feat: refactored how mapregion tracks units and npcs

This commit is contained in:
Sara 2025-08-28 19:12:57 +02:00
parent 0ff5f4ae9c
commit 8dad8bbc1f
2 changed files with 30 additions and 5 deletions

View file

@ -1,5 +1,6 @@
#include "map_region.h" #include "map_region.h"
#include "enemy_body.h" #include "enemy_body.h"
#include "player_body.h"
String const MapRegion::sig_difficulty_increased{ "difficulty_increased" }; String const MapRegion::sig_difficulty_increased{ "difficulty_increased" };
String const MapRegion::sig_phase_changed{ "hunt_phase" }; String const MapRegion::sig_phase_changed{ "hunt_phase" };
@ -7,16 +8,33 @@ String const MapRegion::sig_phase_changed{ "hunt_phase" };
void MapRegion::_bind_methods() { void MapRegion::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_difficulty_increased)); ADD_SIGNAL(MethodInfo(sig_difficulty_increased));
ADD_SIGNAL(MethodInfo(sig_phase_changed, PropertyInfo(Variant::BOOL, "hunt"))); 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) { void MapRegion::on_node_entered(Node3D *node) {
if (EnemyBody * body{ cast_to<EnemyBody>(node) }) { if (cast_to<PlayerBody>(node) != nullptr) {
if (!this->units.has(body->get_unit())) { this->has_player = true;
body->get_unit()->set_region(this); }
}
void MapRegion::on_node_exited(Node3D *node) {
if (cast_to<PlayerBody>(node) != nullptr) {
this->has_player = false;
}
}
void MapRegion::on_child_entered_tree(Node *node) {
if (NpcUnit * unit{ cast_to<NpcUnit>(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() { void MapRegion::ready() {
connect("body_entered", callable_mp(this, &self_type::on_node_entered)); connect("body_entered", callable_mp(this, &self_type::on_node_entered));
} }
@ -28,6 +46,9 @@ void MapRegion::_notification(int what) {
switch (what) { switch (what) {
default: default:
return; return;
case NOTIFICATION_ENTER_TREE:
enter_tree();
return;
case NOTIFICATION_READY: case NOTIFICATION_READY:
ready(); ready();
return; return;

View file

@ -8,7 +8,10 @@
class MapRegion : public Area3D { class MapRegion : public Area3D {
GDCLASS(MapRegion, Area3D); GDCLASS(MapRegion, Area3D);
static void _bind_methods(); 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(); void ready();
protected: protected:
@ -24,6 +27,7 @@ private:
double difficulty{ 0.f }; double difficulty{ 0.f };
bool hunt_phase{ false }; bool hunt_phase{ false };
HashSet<NpcUnit *> units{ nullptr }; HashSet<NpcUnit *> units{ nullptr };
bool has_player{ false };
public: public:
static String const sig_difficulty_increased; static String const sig_difficulty_increased;