lightning.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local Lightning = class()
  2. Lightning.maxHealth = .2
  3. function Lightning:activate()
  4. self.range = 50
  5. self.health = self.maxHealth
  6. self.prevHealth = self.health
  7. self.sparked = false
  8. self:lightning()
  9. ctx.event:emit('view.register', {object = self})
  10. end
  11. function Lightning:deactivate()
  12. ctx.event:emit('view.unregister', {object = self})
  13. end
  14. function Lightning:lightning()
  15. local function t(x, y)
  16. table.insert(self.path, x)
  17. table.insert(self.path, y)
  18. end
  19. self.path = {}
  20. self.distance = 0
  21. if not self.target then return end
  22. local x, y = self.x, self.y
  23. t(x, y)
  24. for i = 0, 1, .1 do
  25. if i == 1 then t(self.target.x, self.target.y) break end
  26. x, y = math.lerp(self.x, self.target.x, i), math.lerp(self.y, self.target.y, i)
  27. local n1, n2 = love.math.noise(x * y * i), love.math.noise(x / (1 + y) / (1 + i))
  28. n1, n2 = 2 * n1 - 1, 2 * n2 - 1
  29. x = x + 50 * n1
  30. y = y + 50 * n2
  31. if #self.path > 0 then
  32. self.distance = self.distance + math.distance(self.path[#self.path - 1], self.path[#self.path], x, y)
  33. end
  34. t(x, y)
  35. end
  36. end
  37. function Lightning:update()
  38. self.prevHealth = self.health
  39. self.health = timer.rot(self.health, function()
  40. ctx.spells:remove(self)
  41. end)
  42. if not self.sparked and self.health < self.maxHealth / 2 then
  43. for i = 1, 12 do
  44. ctx.event:emit('particles.add', {kind = 'spark', x = self.path[#self.path - 1], y = self.path[#self.path]})
  45. end
  46. self.sparked = true
  47. end
  48. end
  49. function Lightning:draw()
  50. local g = love.graphics
  51. local hp = math.lerp(self.prevHealth, self.health, ls.accum / ls.tickrate)
  52. local dis = self.distance * (1 - math.max((hp / self.maxHealth) - .5, 0) * 2)
  53. for i = 1, #self.path - 2, 2 do
  54. g.setLineWidth(3 * (1 - (dis / self.distance)))
  55. g.setColor(255, 255, 220, math.min(2 * hp / self.maxHealth, 1) * 255)
  56. local x1, y1 = self.path[i], self.path[i + 1]
  57. local x2, y2 = self.path[i + 2], self.path[i + 3]
  58. local d = math.distance(x1, y1, x2, y2)
  59. if d < dis then
  60. g.line(x1, y1, x2, y2)
  61. dis = dis - d
  62. else
  63. local dir = math.direction(x1, y1, x2, y2)
  64. g.line(x1, y1, x1 + math.dx(dis, dir), y1 + math.dy(dis, dir))
  65. break
  66. end
  67. end
  68. g.setLineWidth(1)
  69. end
  70. return Lightning