feat: player health status updates HUD

This commit is contained in:
Sara 2025-08-31 14:07:03 +02:00
parent 7b231bd0f3
commit e6af6557e0
3 changed files with 17 additions and 0 deletions

View file

@ -9,6 +9,7 @@ void HealthStatus::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_death)); ADD_SIGNAL(MethodInfo(sig_death));
ADD_SIGNAL(MethodInfo(sig_health_changed, PropertyInfo(Variant::INT, "remaining"), PropertyInfo(Variant::INT, "delta"))); 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() { void HealthStatus::ready() {
@ -30,12 +31,20 @@ void HealthStatus::_notification(int what) {
void HealthStatus::set_health(int value) { void HealthStatus::set_health(int value) {
this->health = 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 { int HealthStatus::get_health() const {
return this->health; return this->health;
} }
int HealthStatus::get_max_health() const {
return this->max_health;
}
void HealthStatus::damage(int amount) { void HealthStatus::damage(int amount) {
if (this->health > 0) { if (this->health > 0) {
amount = Math::abs(amount); amount = Math::abs(amount);

View file

@ -14,6 +14,7 @@ protected:
public: public:
void set_health(int health); void set_health(int health);
int get_health() const; int get_health() const;
int get_max_health() const;
void damage(int amount); void damage(int amount);
void heal(int amount); void heal(int amount);

View file

@ -10,6 +10,12 @@ script/source = "extends HealthStatus
func _on_death() -> void: func _on_death() -> void:
get_tree().change_scene_to_file.call_deferred(\"res://guis/main_menu.tscn\") get_tree().change_scene_to_file.call_deferred(\"res://guis/main_menu.tscn\")
func _on_health_changed(remaining: int, _delta: int) -> void:
var hud := HeadsUpDisplay.get_singleton()
if hud:
hud.set_health_percentage(float(remaining) / float(self.get_max_health()))
" "
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_bxedw"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_bxedw"]
@ -61,3 +67,4 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("CapsuleShape3D_eqqp1") shape = SubResource("CapsuleShape3D_eqqp1")
[connection signal="death" from="HealthStatus" to="HealthStatus" method="_on_death"] [connection signal="death" from="HealthStatus" to="HealthStatus" method="_on_death"]
[connection signal="health_changed" from="HealthStatus" to="HealthStatus" method="_on_health_changed"]