feed.lua 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. local Feed = class()
  2. local g = love.graphics
  3. function Feed:init()
  4. self.entries = {}
  5. self.alpha = 0
  6. ctx.event:on(app.net.events.dead, function(data) self:insert(data) end)
  7. end
  8. function Feed:update()
  9. for i = 1, #self.entries do
  10. local k = self.entries[i]
  11. k.x = math.lerp(k.x, k.targetX, 30 * tickRate)
  12. k.y = math.lerp(k.y, k.targetY, 30 * tickRate)
  13. end
  14. if love.keyboard.isDown('tab') and self.alpha < 1.5 then
  15. self.alpha = math.lerp(self.alpha, 1, math.min(30 * tickRate, 1))
  16. else
  17. self.alpha = timer.rot(self.alpha)
  18. end
  19. end
  20. function Feed:draw()
  21. local u, v = ctx.hud.u, ctx.hud.v
  22. g.setFont('pixel', 8)
  23. local font = g.getFont()
  24. local alpha = math.min(self.alpha, 1)
  25. for i = 1, #self.entries do
  26. local k = self.entries[i]
  27. local killer = ctx.players:get(k.kill)
  28. local victim = ctx.players:get(k.id)
  29. local width = math.max(math.min(u * .14, 150), font:getWidth(killer.username) + font:getWidth(victim.username) + 24)
  30. local height = font:getHeight() + 8
  31. g.setColor(0, 0, 0, 180 * alpha)
  32. local xx, yy = math.round(k.x) + .5, math.round(k.y) + .5
  33. g.rectangle('fill', xx, yy, width, height)
  34. local val = 30 + ((k.kill == ctx.id or k.id == ctx.id) and 225 or 0)
  35. g.setColor(val, val, val, 180 * alpha)
  36. g.rectangle('line', xx, yy, width, height)
  37. if killer.team == purple then g.setColor(190, 160, 220, 255 * alpha)
  38. else g.setColor(240, 160, 140, 255 * alpha) end
  39. g.print(killer.username, k.x + 8, k.y + 4)
  40. if victim.team == purple then g.setColor(190, 160, 220, 255 * alpha)
  41. else g.setColor(240, 160, 140, 255 * alpha) end
  42. g.print(victim.username, k.x + width - font:getWidth(victim.username) - 9, k.y + 4)
  43. end
  44. end
  45. function Feed:insert(data)
  46. local u, v = ctx.hud.u, ctx.hud.v
  47. g.setFont('pixel', 8)
  48. local font = g.getFont()
  49. while #self.entries > 4 do table.remove(self.entries, 1) end
  50. if #self.entries == 4 then self.entries[1].targetX = u end
  51. for i = 1, #self.entries do self.entries[i].targetY = self.entries[i].targetY + font:getHeight() + 16 end
  52. local t = table.copy(data)
  53. t.x = u
  54. t.y = -v * .05
  55. local u1, u2 = ctx.players:get(data.kill).username, ctx.players:get(data.id).username
  56. t.u1 = u1
  57. t.u2 = u2
  58. local width = math.max(math.min(u * .14, 150), font:getWidth(u1) + font:getWidth(u2) + 24)
  59. t.targetX = u - width - 4
  60. t.targetY = 4 + v * .07
  61. table.insert(self.entries, t)
  62. self.alpha = 4
  63. end
  64. function Feed:resize()
  65. local u, v = ctx.hud.u, ctx.hud.v
  66. g.setFont('pixel', 8)
  67. local font = g.getFont()
  68. for i = 1, #self.entries do
  69. local entry = self.entries[i]
  70. local width = math.max(math.min(u * .14, 150), font:getWidth(entry.u1) + font:getWidth(entry.u2) + 24)
  71. entry.targetX = u - width - 4
  72. if i == 1 and #self.entries == 5 then
  73. entry.targetX = u
  74. end
  75. entry.x = entry.targetX
  76. entry.targetY = 4 + (v * .07) + ((#self.entries - i) * (font:getHeight() + 16))
  77. entry.y = entry.targetY
  78. end
  79. end
  80. return Feed