The_Dark_Side_of_Earth/ui/healthbar/healthbar.gd

33 lines
900 B
GDScript

extends HBoxContainer
@export var packedHeart : PackedScene;
var current_max_hp = 0
# Set number of hearts to red corresponding to input health
func set_health(health : int):
var i = 0
for heart : Heart in get_children():
i += 1
if i <= health:
heart.restore()
else:
heart.fade()
# Adjust the number of children to max HP
func set_maxhealth(new_max_hp : int):
if new_max_hp > current_max_hp:
for i in range(new_max_hp - current_max_hp):
add_child(packedHeart.instantiate())
elif new_max_hp < current_max_hp:
var children = get_children()
for i in range(children.size()):
if new_max_hp < i+1:
children[i].queue_free()
current_max_hp = new_max_hp
# Update display on (max) HP changes for the player
func _on_player_health_changed(new_health: int) -> void:
set_health(new_health)
func _on_player_max_hp_changed(new_max_hp: int) -> void:
set_maxhealth(new_max_hp)