feat: fully implemented death in NpcUnit

This commit is contained in:
Sara Gerretsen 2025-10-25 13:18:04 +02:00
parent 5f61fafe7b
commit 28183fceb8
4 changed files with 43 additions and 8 deletions

View file

@ -11,18 +11,28 @@ void NpcUnit::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, region, PROPERTY_HINT_NODE_TYPE, "MapRegion");
}
void NpcUnit::remove_npc(EnemyBody *npc) {
Transform3D const tf{ npc->get_global_transform() };
remove_child(npc);
this->get_parent()->add_child(npc);
npc->set_global_transform(tf);
}
void NpcUnit::on_npc_death() {
Vector<EnemyBody>::Size living{ this->npcs.size() };
// remove any dead npcs from the list
// leaving their bodies as separate nodes part of the tree
for (EnemyBody *npc : this->npcs) {
for (Vector<EnemyBody>::Size i{ 0 }; i < this->npcs.size(); ++i) {
EnemyBody *npc{ this->npcs[i] };
if (npc->get_health()->get_health() <= 0) {
this->get_parent()->add_child(npc);
this->npcs.erase(npc);
break;
--living;
}
}
// remove unit from world once all npcs are dead
if (this->npcs.size() <= 0) {
if (living == 0) {
for (EnemyBody *npc : this->npcs) {
remove_npc(npc);
}
this->set_region(nullptr); // de-register from region
this->queue_free();
}