Skip to content

Commit 1b44b6b

Browse files
committed
Draw health bar of player, and draw text in bars
1 parent ec56bc4 commit 1b44b6b

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Diff for: manager/game.lua

+14-12
Original file line numberDiff line numberDiff line change
@@ -236,29 +236,31 @@ function Game:draw()
236236
-- Replay the event log
237237
self.state = self.eventLog:play(self.time)
238238

239+
local player = self.state[self.playerId]
240+
239241
-- Move the viewport
240-
if self.state[self.playerId] then
241-
self.viewport:move(self.state[self.playerId].x, 300)
242+
if player then
243+
self.viewport:move(player.x, 300)
242244
end
243245

244246
-- Draw the world
245247
love.graphics.setColor({255,255,255,255})
246248
love.graphics.draw(self.bgimage)
247249
self.viewport:draw(self.state)
248250

251+
-- Draw the health bar
252+
if player then
253+
drawFilledBar(20, 20, 200, 20,
254+
player.health / player.maxHealth,
255+
{204,20,28}, nil, {255,255,255},
256+
player.health .. "/" .. player.maxHealth)
257+
end
258+
249259
-- Draw the time bar
250260
drawFilledBar(580, 20, 200, 20,
251261
(self.maxTime - self.time) / constants.playTime,
252-
{107,141,255}, nil, {255,255,255})
253-
254-
resetDraw(
255-
function ()
256-
love.graphics.setColor(255, 255, 255)
257-
258-
local str = math.ceil((self.maxTime - self.time) / constants.framerate)
259-
local strWidth = love.graphics.getFont():getWidth(str)
260-
love.graphics.print(str, 680 - strWidth/2, 23)
261-
end)
262+
{107,141,255}, nil, {255,255,255},
263+
math.ceil((self.maxTime - self.time) / constants.framerate))
262264
end
263265

264266
return Game

Diff for: utils.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ function uniqueId()
7373
return lastUnique
7474
end
7575

76-
function drawFilledBar(x, y, width, height, proportion, fgcolor, bgcolor, linecolor)
76+
function drawFilledBar(x, y, width, height, proportion, fgcolor, bgcolor, linecolor, caption, textcolor)
7777
bgcolor = bgcolor or {0,0,0,255}
7878
fgcolor = fgcolor or {255,255,255,255}
7979
linecolor = linecolor or bgcolor
80+
textcolor = textcolor or {255,255,255,255}
8081

8182
resetDraw(
8283
function ()
@@ -88,6 +89,11 @@ function drawFilledBar(x, y, width, height, proportion, fgcolor, bgcolor, lineco
8889

8990
love.graphics.setColor(linecolor)
9091
love.graphics.rectangle("line", x-1, y-1, width+1, height+1)
92+
93+
if caption then
94+
local strWidth = love.graphics.getFont():getWidth(caption)
95+
love.graphics.print(caption, x + width/2 - strWidth/2, y + height/2 - 7)
96+
end
9197
end)
9298
end
9399

0 commit comments

Comments
 (0)