exercise 23

This commit is contained in:
yuki 2025-11-09 06:26:15 -03:00
parent ed2eaabc98
commit ad7365cce0
4 changed files with 58 additions and 38 deletions

View file

@ -5,19 +5,35 @@ end
-- libraries --
Object = require 'lib/classic/classic'
Baton = require 'lib/baton/baton'
Timer = require 'lib/hump/timer'
-- objects --
Circle = require 'obj/Circle'
HyperCircle = require 'obj/HyperCircle'
Healthbar = require 'obj/Healthbar'
function love.load()
circle = HyperCircle(400, 300, 50, "fill", 10, 120)
input = Baton.new {
controls = {
left = {'key:left', 'key:a'},
right = {'key:right', 'key:d'},
up = {'key:up', 'key:w'},
down = {'key:down', 'key:s'},
action = {'key:z', 'key:space'}
},
pairs = {
move = {'left', 'right', 'up', 'down'}
}
}
timer = Timer()
health = Healthbar()
end
function love.update(dt)
circle:update(dt)
input:update(dt)
timer:update(dt)
health:update(dt)
end
function love.draw()
circle:draw()
health:draw()
end

View file

@ -1,16 +0,0 @@
local Circle = Object:extend()
function Circle:new(x, y, radius, mode)
self.x = x or 400
self.y = y or 300
self.radius = radius or 50
self.mode = mode or "fill"
end
function Circle:update(dt) end
function Circle:draw()
love.graphics.circle(self.mode, self.x, self.y, self.radius)
end
return Circle

37
obj/Healthbar.lua Normal file
View file

@ -0,0 +1,37 @@
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 - amount <= 0 then
self.hp = 0
else
self.hp = self.hp - amount
end
local target_width = self.max_width * (self.hp / 100)
timer:tween(0.3, self, {qwidth = target_width}, 'in-out-cubic')
timer:tween(0.6, self, {lwidth = target_width}, 'in-out-cubic')
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

View file

@ -1,17 +0,0 @@
local HyperCircle = Circle:extend()
function HyperCircle:new(x, y, radius, mode, lw, lradius)
HyperCircle.super.new(self, x, y, radius, mode)
self.lw = lw or 15
self.lradius = lradius or 80
end
function HyperCircle:update(dt) end
function HyperCircle:draw()
HyperCircle.super.draw(self)
love.graphics.setLineWidth(self.lw)
love.graphics.circle("line", self.x, self.y, self.lradius)
end
return HyperCircle