wave-survival/modules/wave_survival/health_status.cpp

58 lines
1.3 KiB
C++

#include "health_status.h"
#include "macros.h"
String HealthStatus::sig_death{ "death" };
String HealthStatus::sig_health_changed{ "health_changed" };
void HealthStatus::_bind_methods() {
BIND_PROPERTY(Variant::INT, health);
ADD_SIGNAL(MethodInfo(sig_death));
ADD_SIGNAL(MethodInfo(sig_health_changed, PropertyInfo(Variant::INT, "remaining"), PropertyInfo(Variant::INT, "delta")));
ClassDB::bind_method(D_METHOD("get_max_health"), &self_type::get_max_health);
}
void HealthStatus::ready() {
this->max_health = this->health;
}
void HealthStatus::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void HealthStatus::set_health(int value) {
this->health = value;
if (!is_ready() || Engine::get_singleton()->is_editor_hint()) {
// if setting health as serialized, set max_health as well
this->max_health = value;
}
}
int HealthStatus::get_health() const {
return this->health;
}
int HealthStatus::get_max_health() const {
return this->max_health;
}
void HealthStatus::damage(int amount) {
if (this->health > 0) {
amount = Math::abs(amount);
this->health -= amount;
emit_signal(sig_health_changed, this->health, -amount);
if (this->health <= 0) {
emit_signal(sig_death);
}
}
}