Randoloot/scripts/game/enemy_health_bar.gd

30 lines
918 B
GDScript

extends Node2D
var enemy: CharacterBody2D
func _ready() -> void:
enemy = get_parent() as CharacterBody2D
top_level = true
z_index = 30
_update_position()
func _process(_delta: float) -> void:
_update_position()
queue_redraw()
func _update_position() -> void:
global_position = enemy.global_position + Vector2(0, -enemy.get_visual_size() * 0.5 - 12.0)
func get_health_ratio() -> float:
return clampf(enemy.hull / maxf(enemy.max_hull, 0.001), 0.0, 1.0)
func _draw() -> void:
if not is_instance_valid(enemy) or enemy.hull <= 0.0:
return
var width := 72.0 if enemy.loot_tier == 2 else 44.0
var background := Rect2(-width * 0.5, -3.0, width, 6.0)
draw_rect(background.grow(1.0), Color("08111dee"))
draw_rect(background, Color("39232bee"))
var color := Color("ffac46") if enemy.loot_tier == 2 else Color("ef5967")
draw_rect(Rect2(background.position, Vector2(width * get_health_ratio(), 6.0)), color)