From ad7365cce0bc3b6c699fc67d16d3380289957a3e Mon Sep 17 00:00:00 2001 From: yuki Date: Sun, 9 Nov 2025 06:26:15 -0300 Subject: [PATCH] exercise 23 --- main.lua | 26 +++++++++++++++++++++----- obj/Circle.lua | 16 ---------------- obj/Healthbar.lua | 37 +++++++++++++++++++++++++++++++++++++ obj/HyperCircle.lua | 17 ----------------- 4 files changed, 58 insertions(+), 38 deletions(-) delete mode 100644 obj/Circle.lua create mode 100644 obj/Healthbar.lua delete mode 100644 obj/HyperCircle.lua diff --git a/main.lua b/main.lua index e377663..25f2189 100644 --- a/main.lua +++ b/main.lua @@ -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 diff --git a/obj/Circle.lua b/obj/Circle.lua deleted file mode 100644 index f47c29d..0000000 --- a/obj/Circle.lua +++ /dev/null @@ -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 diff --git a/obj/Healthbar.lua b/obj/Healthbar.lua new file mode 100644 index 0000000..2e578ac --- /dev/null +++ b/obj/Healthbar.lua @@ -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 diff --git a/obj/HyperCircle.lua b/obj/HyperCircle.lua deleted file mode 100644 index ba9575c..0000000 --- a/obj/HyperCircle.lua +++ /dev/null @@ -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