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 "enemy_body.h"
#include "player_body.h"
String const MapRegion::sig_difficulty_increased{ "difficulty_increased" };
String const MapRegion::sig_phase_changed{ "hunt_phase" };
@ -7,14 +8,31 @@ 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<EnemyBody>(node) }) {
if (!this->units.has(body->get_unit())) {
body->get_unit()->set_region(this);
void MapRegion::on_node_entered(Node3D *node) {
if (cast_to<PlayerBody>(node) != nullptr) {
this->has_player = true;
}
}
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() {
@ -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;

View file

@ -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<NpcUnit *> units{ nullptr };
bool has_player{ false };
public:
static String const sig_difficulty_increased;