35 lines
964 B
GDScript
35 lines
964 B
GDScript
@tool
|
|
class_name Hearts
|
|
extends Control
|
|
|
|
@export_range(0, 10, 1) var health: int = 10:
|
|
set(value):
|
|
health = clamp(value, 0, max_health)
|
|
update_health_textures()
|
|
|
|
var max_health: int = 10
|
|
var heart_textures: Array[Node]
|
|
|
|
func _ready() -> void:
|
|
heart_textures = $HeartContainer.get_children()
|
|
for heart_texture in heart_textures:
|
|
heart_texture.texture = heart_texture.texture.duplicate()
|
|
update_health_textures()
|
|
|
|
func update_health_textures() -> void:
|
|
@warning_ignore("integer_division")
|
|
var full_hearts: int = health / 2
|
|
var half_heart: bool = health % 2 == 1
|
|
|
|
for i in range(heart_textures.size()):
|
|
var heart_texture: TextureRect = heart_textures[i]
|
|
var atlas: AtlasTexture = heart_texture.texture as AtlasTexture
|
|
if i < full_hearts:
|
|
atlas.region.position.x = 1
|
|
heart_texture.visible = true
|
|
elif i == full_hearts and half_heart:
|
|
atlas.region.position.x = 17
|
|
heart_texture.visible = true
|
|
else:
|
|
heart_texture.visible = false
|