Indirected player to Scoreboard to allow switching it

This commit is contained in:
Erkki Seppälä 2014-10-19 13:38:25 +03:00
parent 1c7054e1aa
commit 6a40153426
2 changed files with 12 additions and 8 deletions

View file

@ -55,7 +55,10 @@ function level_state:enter(previous, level_file)
local i0 = i - 1 local i0 = i - 1
local x = 20 + (i0 * SCOREBOARD_WIDTH) + (i0 * SCOREBOARD_MARGIN) local x = 20 + (i0 * SCOREBOARD_WIDTH) + (i0 * SCOREBOARD_MARGIN)
scoreboards[i] = Scoreboard(x, 20, players[i]) scoreboards[i] = Scoreboard(x, 20,
function()
return players[i]
end)
end end
end end

View file

@ -5,14 +5,15 @@ require 'utils'
local SCOREBOARD_FONT = love.graphics.newFont(16) local SCOREBOARD_FONT = love.graphics.newFont(16)
Scoreboard = Class{ Scoreboard = Class{
init = function(self, x, y, player) init = function(self, x, y, getPlayer)
self.player = player self.getPlayer = getPlayer
self.x = x self.x = x
self.y = y self.y = y
end; end;
draw = function(self) draw = function(self)
if self.player.plane == nil then local player = self.getPlayer()
if player.plane == nil then
return return
end end
@ -23,13 +24,13 @@ Scoreboard = Class{
love.graphics.setLineWidth(3) love.graphics.setLineWidth(3)
love.graphics.rectangle("line", self.x, self.y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT) love.graphics.rectangle("line", self.x, self.y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT)
love.graphics.print(self.player.name, self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN) love.graphics.print(player.name, self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN)
love.graphics.print("Score", self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 20) love.graphics.print("Score", self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 20)
love.graphics.printf(self.player.score, self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 20, SCOREBOARD_WIDTH - 2 * SCOREBOARD_MARGIN, "right") love.graphics.printf(player.score, self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 20, SCOREBOARD_WIDTH - 2 * SCOREBOARD_MARGIN, "right")
love.graphics.setLineWidth(origWidth) love.graphics.setLineWidth(origWidth)
local health_ratio = self.player.plane.health / PLANE_HEALTH local health_ratio = player.plane.health / PLANE_HEALTH
local start_color = {0, 255, 0} local start_color = {0, 255, 0}
local end_color = {255, 0, 0} local end_color = {255, 0, 0}
love.graphics.setColor(colorSlide(start_color, end_color, health_ratio)) love.graphics.setColor(colorSlide(start_color, end_color, health_ratio))
@ -37,7 +38,7 @@ Scoreboard = Class{
love.graphics.setColor({0, 0, 0, 255}) love.graphics.setColor({0, 0, 0, 255})
love.graphics.print("Health", self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 45) love.graphics.print("Health", self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 45)
local engine_ratio = self.player.plane.motorPower / ENGINE_MAX local engine_ratio = player.plane.motorPower / ENGINE_MAX
local start_color = {0, 255, 0} local start_color = {0, 255, 0}
local end_color = {255, 0, 0} local end_color = {255, 0, 0}
love.graphics.setColor(colorSlide(start_color, end_color, engine_ratio)) love.graphics.setColor(colorSlide(start_color, end_color, engine_ratio))