diff --git a/modules/wave_survival/health_status.cpp b/modules/wave_survival/health_status.cpp new file mode 100644 index 00000000..785b69f4 --- /dev/null +++ b/modules/wave_survival/health_status.cpp @@ -0,0 +1,46 @@ +#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"))); +} + +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; +} + +int HealthStatus::get_health() const { + return this->health; +} + +void HealthStatus::damage(int amount) { + amount = Math::abs(amount); + this->health -= amount; + emit_signal(sig_health_changed, this->health, -amount); + if (this->health <= 0) { + emit_signal(sig_death); + } +} diff --git a/modules/wave_survival/health_status.h b/modules/wave_survival/health_status.h new file mode 100644 index 00000000..496b446e --- /dev/null +++ b/modules/wave_survival/health_status.h @@ -0,0 +1,30 @@ +#ifndef HEALTH_STATUS_H +#define HEALTH_STATUS_H + +#include "scene/main/node.h" + +class HealthStatus : public Node { + GDCLASS(HealthStatus, Node); + static void _bind_methods(); + void ready(); + +protected: + void _notification(int what); + +public: + void set_health(int health); + int get_health() const; + + void damage(int amount); + void heal(int amount); + +private: + int max_health{ 1 }; + int health{ 1 }; + +public: + static String sig_death; + static String sig_health_changed; +}; + +#endif // !HEALTH_STATUS_H