feat: NPC units drop demo packs
This commit is contained in:
parent
dd6a639eb1
commit
6b7ca195ee
3 changed files with 32 additions and 12 deletions
|
|
@ -4,11 +4,13 @@
|
|||
#include "map_region.h"
|
||||
#include "patrol_path.h"
|
||||
#include "player_detector.h"
|
||||
#include "scene/resources/packed_scene.h"
|
||||
|
||||
void NpcUnit::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::OBJECT, patrol_path, PROPERTY_HINT_NODE_TYPE, "PatrolPath");
|
||||
BIND_PROPERTY(Variant::FLOAT, patrol_speed);
|
||||
BIND_HPROPERTY(Variant::OBJECT, region, PROPERTY_HINT_NODE_TYPE, "MapRegion");
|
||||
BIND_HPROPERTY(Variant::OBJECT, drop_on_clear, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
|
||||
}
|
||||
|
||||
void NpcUnit::remove_npc(EnemyBody *npc) {
|
||||
|
|
@ -18,18 +20,23 @@ void NpcUnit::remove_npc(EnemyBody *npc) {
|
|||
npc->set_global_transform(tf);
|
||||
}
|
||||
|
||||
void NpcUnit::on_npc_death() {
|
||||
void NpcUnit::on_npc_death(EnemyBody *npc) {
|
||||
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 (Vector<EnemyBody>::Size i{ 0 }; i < this->npcs.size(); ++i) {
|
||||
EnemyBody *npc{ this->npcs[i] };
|
||||
if (npc->get_health()->get_health() <= 0) {
|
||||
--living;
|
||||
}
|
||||
}
|
||||
// remove unit from world once all npcs are dead
|
||||
// all member NPCs are dead, remove self (leaving NPC bodies) and drop reward for player
|
||||
if (living == 0) {
|
||||
if (this->drop_on_clear.is_valid()) {
|
||||
Node *node{ this->drop_on_clear->instantiate() };
|
||||
if (Node3D * node3d{ cast_to<Node3D>(node) }) {
|
||||
node3d->set_position(npc->get_global_position());
|
||||
this->get_parent()->add_child(node3d);
|
||||
}
|
||||
}
|
||||
for (EnemyBody *npc : this->npcs) {
|
||||
remove_npc(npc);
|
||||
}
|
||||
|
|
@ -63,7 +70,7 @@ void NpcUnit::ready() {
|
|||
detector->connect(PlayerDetector::sig_awareness_changed, callable_mp(this, &self_type::on_npc_awareness_changed));
|
||||
}
|
||||
if (HealthStatus * health{ npc->get_health() }) {
|
||||
health->connect(HealthStatus::sig_death, callable_mp(this, &self_type::on_npc_death));
|
||||
health->connect(HealthStatus::sig_death, callable_mp(this, &self_type::on_npc_death).bind(npc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,3 +130,11 @@ void NpcUnit::set_region(MapRegion *region) {
|
|||
MapRegion *NpcUnit::get_region() const {
|
||||
return this->region;
|
||||
}
|
||||
|
||||
void NpcUnit::set_drop_on_clear(Ref<PackedScene> object) {
|
||||
this->drop_on_clear = object;
|
||||
}
|
||||
|
||||
Ref<PackedScene> NpcUnit::get_drop_on_clear() const {
|
||||
return this->drop_on_clear;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue