1
0
Fork 0

some entities don't have health

This commit is contained in:
Casey 2023-10-14 13:58:23 +03:00
parent 3cdeac3988
commit b3515c737f
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 14 additions and 4 deletions

View File

@ -11,12 +11,16 @@ local options = {
local function entityBox(ent)
local cube = canvas3d.addBox(ent.x - 0.25, ent.y - 0.25, ent.z - 0.25)
local hpbar = canvas3d.addFrame({ ent.x - 0.25, ent.y + 0.25, ent.z - 0.25 })
cube.setAlpha(0x20)
cube.setDepthTested(false)
hpbar.setDepthTested(false)
local hp_rect_bg = hpbar.addRectangle(-1, -0.25, 2, 0.25, options.hpbar.bg)
local hp_rect_fg = hpbar.addRectangle(-1, -0.25, 0, 0.25, options.hpbar.fg)
local hp_rect_bg, hp_rect_fg
if ent.health ~= nil then
hp_rect_bg = hpbar.addRectangle(-1, -0.25, 2, 0.25, options.hpbar.bg)
hp_rect_fg = hpbar.addRectangle(-1, -0.25, 0, 0.25, options.hpbar.fg)
end
local hp_txt = hpbar.addText({ -1, -0.2 }, ent.name, options.hpbar.txt)
return {
@ -28,8 +32,14 @@ local function entityBox(ent)
update = function(self, entity)
self._cube.setPosition(entity.x - 0.25, entity.y - 0.25, entity.z - 0.25)
self._hpbar.setPosition(entity.x, entity.y + 0.5, entity.z)
self._hp_rect_fg.setSize(2 * entity.health / entity.maxHealth, 0.25)
self._hp_txt.setText(string.format("%s (%.1f/%.1f)", entity.name, entity.health, entity.maxHealth))
if self._hp_rect_fg ~= nil then
self._hp_rect_fg.setSize(2 * entity.health / entity.maxHealth, 0.25)
end
if entity.health ~= nil then
self._hp_txt.setText(string.format("%s (%.1f/%.1f)", entity.name, entity.health, entity.maxHealth))
else
self._hp_txt.setText(string.format("%s", entity.name))
end
end,
destroy = function(self)
self._cube.remove()