scoreboard.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. local Scoreboard = class()
  2. local g = love.graphics
  3. function Scoreboard:init()
  4. ctx.event:on(app.net.events.class, function(data) self:refresh() end)
  5. ctx.event:on(app.net.events.dead, function(data) self:refresh() end)
  6. ctx.event:on(app.net.events.leave, function(data) self:refresh() end)
  7. self.names = {[0] = '', [1] = ''}
  8. self.ks = {[0] = '', [1] = ''}
  9. self.ds = {[0] = '', [1] = ''}
  10. self.nameWidth = {[0] = 0, [1] = 0}
  11. self.ksWidth = {[0] = 0, [1] = 0}
  12. self.dsWidth = {[0] = 0, [1] = 0}
  13. self.width = {[0] = 0, [1] = 0}
  14. self.height = {[0] = 0, [1] = 0}
  15. self:refresh()
  16. self.offset = {[0] = -love.graphics.getWidth(), [1] = love.graphics.getWidth() * 2}
  17. end
  18. function Scoreboard:update()
  19. if love.keyboard.isDown('tab') then
  20. self.offset[0] = math.lerp(self.offset[0], 0, math.min(30 * tickRate, 1))
  21. self.offset[1] = math.lerp(self.offset[1], ctx.hud.u - self.width[1], math.min(30 * tickRate, 1))
  22. if math.abs(self.offset[1] - (ctx.hud.u - self.width[1])) < 2 then
  23. self.offset[1] = ctx.hud.u - self.width[1]
  24. end
  25. else
  26. self.offset[0] = math.lerp(self.offset[0], -self.width[0] - 1, math.min(30 * tickRate, 1))
  27. self.offset[1] = math.lerp(self.offset[1], ctx.hud.u, math.min(30 * tickRate, 1))
  28. if math.abs(self.offset[1] - ctx.hud.u) < 2 then
  29. self.offset[1] = ctx.hud.u
  30. end
  31. end
  32. end
  33. function Scoreboard:draw()
  34. local u, v = ctx.hud.u, ctx.hud.v
  35. g.setFont('pixel', 8)
  36. local font = g.getFont()
  37. for i = 0, 1 do
  38. if (i == 0 and self.offset[0] > -self.width[0]) or (i == 1 and self.offset[1] < u) then
  39. local xx = self.offset[i]
  40. local yy = v * .5 - (self.height[i] / 2) + 1
  41. g.setColor(0, 0, 0, 180)
  42. g.rectangle('fill', xx, yy, self.width[i], self.height[i] - (#self.teams[i] == 0 and 1 or 0), false, true)
  43. g.setColor(30, 30, 30, 180)
  44. g.rectangle('line', xx, yy, self.width[i], self.height[i] - (#self.teams[i] == 0 and 1 or 0), false, true)
  45. g.setColor(255, 255, 255)
  46. g.print('username', xx + 10, yy + 4)
  47. g.print('k', xx + 10 + self.nameWidth[i] + 16, yy + 4)
  48. g.print('d', xx + 10 + self.nameWidth[i] + 16 + self.ksWidth[i] + 16, yy + 4)
  49. g.setColor(i == 0 and {190, 160, 220} or {240, 160, 140})
  50. g.print(self.names[i], xx + 10, yy + 4 + font:getHeight() + 2)
  51. g.print(self.ks[i], xx + 10 + self.nameWidth[i] + 16, yy + 4 + font:getHeight() + 2)
  52. g.print(self.ds[i], xx + 10 + self.nameWidth[i] + 16 + self.ksWidth[i] + 16, yy + 4 + font:getHeight() + 2)
  53. end
  54. end
  55. end
  56. function Scoreboard:refresh()
  57. self.teams = {[0] = {}, [1] = {}}
  58. ctx.players:each(function(p)
  59. table.insert(self.teams[p.team], {name = p.username, kills = p.kills, deaths = p.deaths})
  60. end)
  61. local comp = function(a, b) return (a.kills == b.kills) and (a.deaths < b.deaths) or (a.kills > b.kills) end
  62. table.sort(self.teams[0], comp)
  63. table.sort(self.teams[1], comp)
  64. g.setFont('pixel', 8)
  65. local font = g:getFont()
  66. for i = 0, 1 do
  67. self.names[i], self.ks[i], self.ds[i] = '', '', ''
  68. for j = 1, #self.teams[i] do
  69. self.names[i] = self.names[i] .. self.teams[i][j].name .. '\n'
  70. self.ks[i] = self.ks[i] .. self.teams[i][j].kills .. '\n'
  71. self.ds[i] = self.ds[i] .. self.teams[i][j].deaths .. '\n'
  72. end
  73. self.names[i] = self.names[i]:sub(1, #self.names[i] - 1)
  74. self.ks[i] = self.ks[i]:sub(1, #self.ks[i] - 1)
  75. self.ds[i] = self.ds[i]:sub(1, #self.ds[i] - 1)
  76. self.nameWidth[i] = font:getWrap(self.names[i], math.huge)
  77. self.nameWidth[i] = math.max(self.nameWidth[i], font:getWidth('username'))
  78. self.ksWidth[i] = font:getWrap(self.ks[i], math.huge)
  79. self.ksWidth[i] = math.max(self.ksWidth[i], font:getWidth('k'))
  80. self.dsWidth[i] = font:getWrap(self.ds[i], math.huge)
  81. self.dsWidth[i] = math.max(self.dsWidth[i], font:getWidth('d'))
  82. self.width[i] = self.nameWidth[i] + 16 + self.ksWidth[i] + 16 + self.dsWidth[i] + 18
  83. self.height[i] = (#self.teams[i] + 1) * font:getHeight() + 12
  84. end
  85. end
  86. return Scoreboard