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

@ -19,11 +19,15 @@ void EnemyBody::on_child_added(Node *node) {
void EnemyBody::ready() {
this->fsm = cast_to<StateMachine>(get_node(NodePath("%StateMachine")));
this->nav = cast_to<NavigationAgent3D>(get_node(NodePath("%NavigationAgent3D")));
this->health = cast_to<HealthStatus>(get_node(NodePath("%HealthStatus")));
}
void EnemyBody::physics_process(double delta) {
GETSET(velocity, {
velocity = Vector3{ this->movement_direction.x * this->movement_speed, velocity.y, this->movement_direction.y * this->movement_speed } + (velocity * delta);
if (!is_on_floor() && this->health->get_health() > 0) {
velocity += get_gravity() * delta;
}
});
if (!this->movement_direction.is_zero_approx()) {
look_at(get_global_position() + Vector3{ this->movement_direction.x, 0.f, this->movement_direction.y });

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();
}

View file

@ -10,6 +10,7 @@ class MapRegion;
class NpcUnit : public Node3D {
GDCLASS(NpcUnit, Node3D);
static void _bind_methods();
void remove_npc(EnemyBody *unit);
void on_npc_death();
void on_npc_awareness_changed(bool seen);
void child_added(Node *node);