41 lines
1.1 KiB
Lua
41 lines
1.1 KiB
Lua
local Healthbar = Object:extend()
|
|
|
|
function Healthbar:new(x, y, hp)
|
|
self.x = x or 100
|
|
self.y = y or 100
|
|
self.hp = hp or 100
|
|
self.max_width = 200
|
|
self.qwidth = (self.hp / 100) * self.max_width
|
|
self.lwidth = self.qwidth
|
|
end
|
|
|
|
function Healthbar:decrease(amount)
|
|
if self.hp ~= 0 then
|
|
if fg then timer:cancel(fg) end
|
|
if bg then timer:cancel(bg) end
|
|
if self.hp - amount <= 0 then
|
|
self.hp = 0
|
|
else
|
|
self.hp = self.hp - amount
|
|
end
|
|
local target_width = self.max_width * (self.hp / 100)
|
|
fg = timer:tween(0.3, self, {qwidth = target_width}, 'in-out-cubic')
|
|
bg = timer:tween(0.6, self, {lwidth = target_width}, 'in-out-cubic')
|
|
end
|
|
end
|
|
|
|
function Healthbar:update(dt)
|
|
if input:pressed('right') then self:decrease(20) end
|
|
end
|
|
|
|
function Healthbar:draw()
|
|
-- trailing hp
|
|
love.graphics.setColor(1, 0.5, 0.5)
|
|
love.graphics.rectangle("fill", self.x, self.y, self.lwidth, 20)
|
|
|
|
-- healthbar
|
|
love.graphics.setColor(1, 0.2, 0.2)
|
|
love.graphics.rectangle("fill", self.x, self.y, self.qwidth, 20)
|
|
end
|
|
|
|
return Healthbar
|