wave-survival/modules/wave_survival/map_region.cpp

88 lines
2.1 KiB
C++

#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" };
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(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() {
connect("body_entered", callable_mp(this, &self_type::on_node_entered));
}
void MapRegion::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
enter_tree();
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void MapRegion::register_unit(NpcUnit *unit) {
if (!this->units.has(unit)) {
this->units.insert(unit);
}
}
void MapRegion::remove_unit(NpcUnit *unit) {
if (this->units.has(unit)) {
this->units.erase(unit);
}
}
void MapRegion::raise_difficulty(double amount) {
if (this->hunt_phase) {
return;
}
double const new_difficulty{ this->difficulty + amount };
int const new_trunc{ (int)Math::floor(new_difficulty) };
int const old_trunc{ (int)Math::floor(this->difficulty) };
if (new_trunc != old_trunc) {
emit_signal(sig_difficulty_increased);
emit_signal(sig_phase_changed, true);
this->hunt_phase = true;
}
}
int MapRegion::get_current_difficulty() const {
int difficulty{ (int)Math::floor(this->difficulty) };
return difficulty;
}