extends Control

@onready var hboxcontainer = $MarginContainer/HBoxContainer
var empty_heart_rsc
var full_heart_rsc
var icon_rsc

func _ready():
	empty_heart_rsc = load(Utils.HEALTH_BAR_ICONS[Utils.HEALTH_BAR_ICON_STATE.EMPTY])
	full_heart_rsc = load(Utils.HEALTH_BAR_ICONS[Utils.HEALTH_BAR_ICON_STATE.FULL])
	icon_rsc = load(Utils.HEALTH_BAR_ICON)
	init_health_bar(PlayerVariables.max_health)
	PlayerVariables.player_health_changed.connect(change_health_bar)

func init_health_bar(max_health):
	for i in range(max_health):
		var icon = icon_rsc.instantiate()
		if i < PlayerVariables.player_health:
			icon.texture = full_heart_rsc
		else:
			icon.texture = empty_heart_rsc
		hboxcontainer.add_child(icon)

func change_health_bar(new_health : int):
	for icon in hboxcontainer.get_children():
		if icon.get_index() < new_health:
			icon.texture = full_heart_rsc
		else:
			icon.texture = empty_heart_rsc
