class_name OverheadHealthBar extends Node2D @export var health_component_path: NodePath = ^"../HealthComponent" @export var bar_size := Vector2(58.0, 7.0) @export var border_width := 1.0 @export var background_color := Color(0.05, 0.04, 0.045, 0.82) @export var fill_color := Color(0.95, 0.16, 0.13, 0.95) @export var low_health_color := Color(1.0, 0.62, 0.15, 0.98) @export var border_color := Color(0.96, 0.9, 0.78, 0.88) @export var hide_when_depleted := true var _current := 1 var _maximum := 1 @onready var _health_component: Node = get_node_or_null(health_component_path) func _ready() -> void: z_index = max(z_index, 30) if _health_component != null: if _health_component.has_signal("health_changed") and not _health_component.is_connected("health_changed", _on_health_changed): _health_component.connect("health_changed", _on_health_changed) _on_health_changed(int(_health_component.get("current")), int(_health_component.get("maximum"))) else: queue_redraw() func health_ratio() -> float: return clampf(float(_current) / float(maxi(1, _maximum)), 0.0, 1.0) func _on_health_changed(current: int, maximum: int) -> void: _current = clampi(current, 0, maxi(1, maximum)) _maximum = maxi(1, maximum) visible = _current > 0 or not hide_when_depleted queue_redraw() func _draw() -> void: if hide_when_depleted and _current <= 0: return var rect := Rect2(-bar_size * 0.5, bar_size) draw_rect(rect, background_color, true) var ratio := health_ratio() var fill_rect := Rect2(rect.position + Vector2(border_width, border_width), Vector2(maxf(0.0, (bar_size.x - border_width * 2.0) * ratio), maxf(0.0, bar_size.y - border_width * 2.0))) draw_rect(fill_rect, low_health_color if ratio <= 0.32 else fill_color, true) draw_rect(rect, border_color, false, border_width)