extends Node2D
class_name HealthBar

signal health_changed(new_value: int)

const MAX_HEALTH = 10

@export var health: int = 10:
	set(new_value):
		var new_health = clampi(new_value, 0, MAX_HEALTH)
		if new_health != health:
			health = new_health
			health_changed.emit(new_health)

@onready var foreground: AnimatedSprite2D = $Foreground

func _ready() -> void:
	update_foreground()

func _on_health_changed(_new_value: int) -> void:
	update_foreground()

func update_foreground():
	if health != 0:
		foreground.frame = MAX_HEALTH - health
		foreground.visible = true
	else:
		foreground.visible = false
