The_Dark_Side_of_Earth/ui/healthbar/healthbar.gd

44 lines
1,000 B
GDScript3
Raw Normal View History

extends HBoxContainer
@export var packedHeart : PackedScene;
2025-09-17 14:39:22 +02:00
var current_max_hp = 0
var current_health = 0
func _ready() -> void:
2025-09-17 14:39:22 +02:00
#for i in range(initial_health):
#add_child(packedHeart.instantiate())
pass
func set_health(health : int):
var i = 0
for heart : Heart in get_children():
i += 1
if i <= health:
heart.restore()
else:
heart.fade()
2025-09-17 14:39:22 +02:00
current_health = health
2025-09-17 14:39:22 +02:00
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_health = min(current_health, current_max_hp)
else: pass
current_max_hp = new_max_hp
set_health(current_max_hp)
func _on_player_health_changed(new_health: int) -> void:
set_health(new_health)
2025-09-17 14:39:22 +02:00
func _on_player_max_hp_changed(new_max_hp: int) -> void:
set_maxhealth(new_max_hp)