summaryrefslogtreecommitdiff
path: root/hud.lua
diff options
context:
space:
mode:
authorcursed22bc <admin@pixeldawn.org>2026-03-10 19:27:58 +0200
committercursed22bc <admin@pixeldawn.org>2026-03-10 19:27:58 +0200
commit9d95968c3e732be915f10841d1d659e37b3b5d03 (patch)
tree1932c4fef554a350303c75cda830503efdaa144c /hud.lua
parentdb9454cb632a3ba0b39018513b0d5bd444c8ffd3 (diff)
collision death state and basic HUD
Diffstat (limited to 'hud.lua')
-rw-r--r--hud.lua146
1 files changed, 146 insertions, 0 deletions
diff --git a/hud.lua b/hud.lua
new file mode 100644
index 0000000..ce877bb
--- /dev/null
+++ b/hud.lua
@@ -0,0 +1,146 @@
+local Textbox = require("textbox")
+
+local HUD = {}
+HUD.__index = HUD
+
+HUD.slots = {
+ { enabled = false },
+ { enabled = false },
+ { enabled = false },
+ { enabled = false },
+}
+
+local SLOT_SIZE = 16
+local SLOT_SPACING = 8
+local LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
+
+local function createMissingTexture()
+ local size = SLOT_SIZE
+ local half = size / 2
+ local imgData = love.image.newImageData(size, size)
+ for y = 0, size - 1 do
+ for x = 0, size - 1 do
+ local inBlack = (x < half and y < half) or (x >= half and y >= half)
+ if inBlack then
+ imgData:setPixel(x, y, 0, 0, 0, 1)
+ else
+ imgData:setPixel(x, y, 1, 0, 1, 1)
+ end
+ end
+ end
+ local img = love.graphics.newImage(imgData)
+ img:setFilter("nearest", "nearest")
+ return img
+end
+
+function HUD:new()
+ local self = setmetatable({}, HUD)
+ self.missingTexture = createMissingTexture()
+ self.textbox = Textbox:new()
+ self.hoveredSlot = nil
+ self.slotRects = {}
+ self.screenW = 0
+ self.screenH = 0
+ self.drawScale = 1
+ return self
+end
+
+function HUD:computeSlotRects(screenW, screenH, drawScale)
+ local slotW = SLOT_SIZE * drawScale
+ local slotH = SLOT_SIZE * drawScale
+ local spacing = SLOT_SPACING * drawScale
+ local totalW = 4 * slotW + 3 * spacing
+ local startX = math.floor((screenW - totalW) / 2)
+ local startY = math.floor(screenH / 2 - slotH / 2)
+ local rects = {}
+ for i = 1, 4 do
+ rects[i] = {
+ x = startX + (i - 1) * (slotW + spacing),
+ y = startY,
+ w = slotW,
+ h = slotH,
+ }
+ end
+ return rects
+end
+
+function HUD:update(dt, screenW, screenH, drawScale)
+ self.screenW = screenW
+ self.screenH = screenH
+ self.drawScale = drawScale
+ self.slotRects = self:computeSlotRects(screenW, screenH, drawScale)
+
+ local mx, my = love.mouse.getPosition()
+ local prevHovered = self.hoveredSlot
+ self.hoveredSlot = nil
+ for i, rect in ipairs(self.slotRects) do
+ if mx >= rect.x and mx < rect.x + rect.w
+ and my >= rect.y and my < rect.y + rect.h then
+ self.hoveredSlot = i
+ break
+ end
+ end
+
+ if self.hoveredSlot and self.hoveredSlot ~= prevHovered then
+ local rect = self.slotRects[self.hoveredSlot]
+ local centerX = screenW / 2
+ local topY = rect.y + rect.h + 8 * drawScale
+ self.textbox:show(LOREM, { centerX, topY, "center" }, "show", {
+ fontSize = 16,
+ centeredText = true,
+ wrapToFit = true,
+ maxChars = 40,
+ })
+ self.textbox.y = topY + self.textbox.height / 2
+ elseif not self.hoveredSlot and prevHovered then
+ self.textbox:hide()
+ end
+
+ self.textbox:update(dt)
+end
+
+function HUD:mousepressed(mx, my, button)
+ if button ~= 1 then return end
+ for i, rect in ipairs(self.slotRects) do
+ if mx >= rect.x and mx < rect.x + rect.w
+ and my >= rect.y and my < rect.y + rect.h then
+ HUD.slots[i].enabled = not HUD.slots[i].enabled
+ break
+ end
+ end
+end
+
+function HUD:draw()
+ local screenW = self.screenW
+ local screenH = self.screenH
+ local drawScale = self.drawScale
+
+ love.graphics.setColor(0, 0, 0, 0.4)
+ love.graphics.rectangle("fill", 0, 0, screenW, screenH)
+
+ for i, rect in ipairs(self.slotRects) do
+ love.graphics.setColor(1, 1, 1, 1)
+ love.graphics.draw(self.missingTexture, rect.x, rect.y, 0, drawScale, drawScale)
+
+ if HUD.slots[i].enabled then
+ love.graphics.setColor(0, 1, 0, 0.3)
+ love.graphics.rectangle("fill", rect.x, rect.y, rect.w, rect.h)
+ end
+
+ if self.hoveredSlot == i then
+ love.graphics.setColor(1, 1, 0, 1)
+ love.graphics.setLineWidth(2)
+ else
+ love.graphics.setColor(1, 1, 1, 0.5)
+ love.graphics.setLineWidth(1)
+ end
+ love.graphics.rectangle("line", rect.x, rect.y, rect.w, rect.h)
+ end
+
+ love.graphics.setLineWidth(1)
+ love.graphics.setColor(1, 1, 1, 1)
+
+ self.textbox:draw()
+end
+
+return HUD