zeal/scripts/healthbar.lua
2026-04-08 23:27:01 +02:00

40 lines
1009 B
Lua

-- scripts/healthbar.lua
-- RectNode: the engine draws the green bar automatically.
-- This script drives the HP logic and recolors/resizes the bar.
local max_hp = 100
local hp = 100
---@param self RectNode
function on_init(self)
self.x = 20
self.y = 20
self.width = 200
self.height = 20
self.color = { r = 60, g = 200, b = 80, a = 255 }
log("HealthBar ready")
end
---@param self RectNode
---@param dt number
function on_update(self, dt)
-- Slow drain (replace with real damage events)
hp = math.max(0, hp - 4 * dt)
-- Shrink bar proportionally
self.width = (hp / max_hp) * 200
-- Green → red as HP drops
local t = hp / max_hp
self.color.r = math.floor((1 - t) * 220)
self.color.g = math.floor(t * 200)
self.color.b = 40
end
---@param self RectNode
function on_render(self)
-- Label drawn on top of the auto-rendered bar
local label = "HP: " .. (math.floor(hp)) .. (" / ") .. max_hp
draw_text(label, self.x + 4, self.y + 4, 12, 240, 240, 240)
end