hp-bar-the-dumb-way #22
@ -4,6 +4,96 @@ local Element = require "lib.simple_ui.element"
|
||||
local Rect = require "lib.simple_ui.rect"
|
||||
local SkillRow = require "lib.simple_ui.level.skill_row"
|
||||
|
||||
--- @class BarElement : UIElement
|
||||
--- @field getter fun() : number
|
||||
--- @field value number
|
||||
--- @field maxValue number
|
||||
local barElement = setmetatable({}, Element)
|
||||
barElement.__index = barElement
|
||||
|
||||
function barElement:update(dt)
|
||||
local val = self.getter()
|
||||
self.value = val < 0 and 0 or val > self.maxValue and self.maxValue or val
|
||||
end
|
||||
|
||||
function barElement:draw()
|
||||
love.graphics.push()
|
||||
love.graphics.applyTransform(self.transform)
|
||||
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
|
||||
love.graphics.rectangle("fill", self.bounds.x, self.bounds.y, self.bounds.width * self.value / self.maxValue,
|
||||
self.bounds.height)
|
||||
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
love.graphics.pop()
|
||||
end
|
||||
|
||||
--- @class BottomBars : UIElement
|
||||
--- @field children BarElement[]
|
||||
local bottomBars = setmetatable({}, Element)
|
||||
bottomBars.__index = bottomBars;
|
||||
|
||||
--- @param cid Id
|
||||
function bottomBars.new(cid)
|
||||
local t = setmetatable({}, bottomBars)
|
||||
|
||||
t.children = {
|
||||
barElement:new {
|
||||
getter = function()
|
||||
local char = Tree.level.characters[cid]
|
||||
return char:try(Tree.behaviors.stats, function(stats)
|
||||
return stats.hp or 0
|
||||
end)
|
||||
end,
|
||||
maxValue = 20
|
||||
},
|
||||
|
||||
barElement:new {
|
||||
getter = function()
|
||||
local char = Tree.level.characters[cid]
|
||||
return char:try(Tree.behaviors.stats, function(stats)
|
||||
return stats.mana or 0
|
||||
end)
|
||||
end,
|
||||
maxValue = 10
|
||||
}
|
||||
}
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
function bottomBars:update(dt)
|
||||
local padding = 16
|
||||
local screenW, screenH = love.graphics.getDimensions()
|
||||
self.bounds = Rect {
|
||||
height = 10,
|
||||
width = 200 + 200 + padding,
|
||||
y = screenH - 96 - padding
|
||||
}
|
||||
|
||||
self.transform = love.math.newTransform():translate(-self.bounds.width / 2 + screenW / 2, self.bounds.y)
|
||||
|
||||
for i = 1, #self.children do
|
||||
self.children[i].bounds = Rect {
|
||||
width = 200,
|
||||
height = 20
|
||||
}
|
||||
self.children[i].transform = self.transform:clone():translate(padding * (i - 1) + (i - 1) * 200, 0)
|
||||
end
|
||||
|
||||
|
||||
|
||||
for _, el in ipairs(self.children) do
|
||||
el:update(dt)
|
||||
end
|
||||
end
|
||||
|
||||
function bottomBars:draw()
|
||||
for _, el in ipairs(self.children) do
|
||||
el:draw()
|
||||
end
|
||||
end
|
||||
|
||||
local layout = {}
|
||||
function layout:update(dt)
|
||||
@ -11,14 +101,18 @@ function layout:update(dt)
|
||||
if cid then
|
||||
self.skillRow = SkillRow(cid)
|
||||
self.skillRow:show()
|
||||
self.bottomBars = bottomBars.new(cid)
|
||||
elseif Tree.level.selector:deselected() then
|
||||
self.skillRow:hide()
|
||||
self.bottomBars = nil
|
||||
end
|
||||
if self.skillRow then self.skillRow:update(dt) end
|
||||
if self.bottomBars then self.bottomBars:update(dt) end
|
||||
end
|
||||
|
||||
function layout:draw()
|
||||
if self.skillRow then self.skillRow:draw() end
|
||||
if self.bottomBars then self.bottomBars:draw() end
|
||||
end
|
||||
|
||||
return layout
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user