shruju.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. local g = love.graphics
  2. Shruju = class()
  3. Shruju.width = 64
  4. Shruju.height = 64
  5. Shruju.depth = -5
  6. function Shruju:activate()
  7. self.timer = config.shruju.lifetime
  8. self.y = ctx.map.height - ctx.map.groundHeight
  9. self.index = love.math.random(1, 5)
  10. self.animation = data.animation['shruju' .. self.index]()
  11. self.animation:on('complete', function(data)
  12. if data.state.name == 'spawn' then self.animation:set('idle') end
  13. end)
  14. ctx.event:emit('view.register', {object = self})
  15. ctx.sound:play('shrujuSpawn')
  16. end
  17. function Shruju:update()
  18. self.timer = timer.rot(self.timer, function()
  19. if not self.juju or self.juju > 0 then
  20. local amount = self.juju or 100
  21. ctx.jujus:add({
  22. x = self.x,
  23. y = self.y,
  24. amount = amount,
  25. vx = 0,
  26. vy = love.math.random(-200, -100)
  27. })
  28. ctx.particles:emit('jujusex', self.x, self.y, 30)
  29. end
  30. ctx.shrujus:remove(self)
  31. end)
  32. if love.math.random() < 4 * ls.tickrate then
  33. ctx.particles:emit('magicshruju', self.x, self.y - 30, 1)
  34. end
  35. end
  36. function Shruju:deactivate()
  37. ctx.event:emit('view.unregister', {object = self})
  38. end
  39. function Shruju:draw()
  40. local multiplier = self.timer < 10 and (self.timer < 3 and 6 or 3) or 0
  41. if math.floor(self.timer * multiplier) % 2 == 0 then
  42. self.animation:draw(self.x, self.y)
  43. end
  44. end
  45. function Shruju:pickup()
  46. ctx.player.shruju = self
  47. f.exe(self.apply, self)
  48. ctx.shrujus:remove(self)
  49. ctx.event:emit('view.unregister', {object = self})
  50. end
  51. function Shruju:drop()
  52. f.exe(self.remove, self)
  53. ctx.shrujus.objects[self] = self
  54. self.x = ctx.player.x
  55. self.animation:set('spawn')
  56. ctx.event:emit('view.register', {object = self})
  57. end
  58. function Shruju:playerNearby()
  59. local p = ctx.player
  60. return not p.dead and math.abs(p.x - self.x) <= self.width / 2 + p.width / 2
  61. end