dirt.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. local Dirt = extend(Spell)
  2. function Dirt:activate()
  3. self.image = data.media.graphics.particles.linering
  4. self.prevx = self.x
  5. self.prevy = self.y
  6. self.vx = love.math.random(-150, 150)
  7. self.vy = love.math.random(-500, -250)
  8. self.alpha = .9
  9. self.size = 1.5 + love.math.random() * 1.5
  10. self.targety = ctx.map.height - ctx.map.groundHeight + love.math.random(-18, 18)
  11. self.bounced = love.math.random() > .8
  12. self.r = 100 + love.math.random(-20, 20)
  13. self.g = 50 + love.math.random(-10, 10)
  14. self.b = love.math.random(10)
  15. ctx.event:emit('view.register', {object = self})
  16. end
  17. function Dirt:deactivate()
  18. ctx.event:emit('view.unregister', {object = self})
  19. end
  20. function Dirt:update()
  21. self.prevx = self.x
  22. self.prevy = self.y
  23. self.x = self.x + self.vx * ls.tickrate
  24. if self.vy ~= math.huge then
  25. self.y = self.y + self.vy * ls.tickrate
  26. self.vy = self.vy + 1000 * ls.tickrate
  27. if self.y > self.targety and self.vy > 0 then
  28. if self.bounced then
  29. self.vy = math.huge
  30. else
  31. self.vy = -self.vy * .5
  32. self.bounced = true
  33. end
  34. end
  35. else
  36. self.vx = math.lerp(self.vx, 0, 8 * ls.tickrate)
  37. if self.vx < 10 then
  38. self.alpha = math.lerp(self.alpha, 0, 2 * ls.tickrate)
  39. if self.alpha < .05 then ctx.spells:remove(self) end
  40. end
  41. end
  42. end
  43. function Dirt:draw()
  44. local g = love.graphics
  45. local x, y = math.lerp(self.prevx, self.x, ls.accum / ls.tickrate), math.lerp(self.prevy, self.y, ls.accum / ls.tickrate)
  46. local scale = self.size * 3 / self.image:getWidth()
  47. g.setColor(self.r, self.g, self.b, 255 * self.alpha)
  48. g.draw(self.image, x, y, 0, scale, scale, self.image:getWidth() / 2, self.image:getHeight() / 2)
  49. end
  50. function Dirt:paused()
  51. self.prevx = self.x
  52. self.prevy = self.y
  53. end
  54. return Dirt