shrujupatch.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. local g = love.graphics
  2. ShrujuPatch = class()
  3. ShrujuPatch.width = 80
  4. ShrujuPatch.height = 40
  5. ShrujuPatch.depth = 1
  6. function ShrujuPatch:activate()
  7. self.y = ctx.map.height - ctx.map.groundHeight
  8. self.types = {}
  9. self.effects = {}
  10. for i = 1, #data.shruju do
  11. if not isa(data.shruju[i], Shruju) then
  12. table.insert(self.effects, data.shruju[i].code)
  13. end
  14. end
  15. for i = 1, #data.shruju do
  16. if isa(data.shruju[i], Shruju) then
  17. table.insert(self.types, data.shruju[i].code)
  18. end
  19. end
  20. self.timer = 0
  21. self.slot = nil
  22. self.highlight = 0
  23. self.prevHighlight = self.highlight
  24. self.animation = data.animation.shrujupatch()
  25. self.animation:on('complete', function(data)
  26. if data.state.name == 'spawn' then self.animation:set('idle') end
  27. end)
  28. self.shrujuAnimation = nil
  29. ctx.event:emit('view.register', {object = self})
  30. end
  31. function ShrujuPatch:deactivate()
  32. ctx.event:emit('view.unregister', {object = self})
  33. end
  34. function ShrujuPatch:update()
  35. self.timer = timer.rot(self.timer, function()
  36. do return end
  37. self:makeShruju()
  38. ctx.sound:play('shrujuSpawn')
  39. end)
  40. self.prevHighlight = self.highlight
  41. self.highlight = math.lerp(self.highlight, self:playerNearby() and 128 or 0, math.min(5 * ls.tickrate))
  42. end
  43. function ShrujuPatch:draw()
  44. self.animation:draw(self.x, self.y)
  45. local highlight = math.lerp(self.prevHighlight, self.highlight, ls.accum / ls.tickrate)
  46. table.each(self.animation.spine.skeleton.slots, function(slot)
  47. slot.a = highlight / 255
  48. slot.data.additiveBlending = true
  49. end)
  50. self.animation:draw(self.x, self.y)
  51. table.each(self.animation.spine.skeleton.slots, function(slot)
  52. slot.a = 1
  53. slot.data.additiveBlending = false
  54. end)
  55. g.setBlendMode('alpha')
  56. if self.shrujuAnimation then self.shrujuAnimation:draw(self.x, self.y + 15) end
  57. end
  58. function ShrujuPatch:grow(what)
  59. if not self:playerNearby() or not table.has(self.types, what) or self.growing or self.slot then return end
  60. self.timer = self:getGrowTime(what)
  61. self.growing = what
  62. end
  63. function ShrujuPatch:take()
  64. if not self:playerNearby() or not self.slot then return end
  65. local slot = self.slot
  66. self.slot = nil
  67. self.shrujuAnimation = nil
  68. return slot
  69. end
  70. function ShrujuPatch:playerNearby()
  71. local p = ctx.player
  72. return not p.dead and math.abs(p.x - self.x) <= self.width / 2 + p.width / 2
  73. end
  74. function ShrujuPatch:makeShruju()
  75. if self.growing and not self.slot then
  76. local shruju = data.shruju[self.growing]()
  77. -- Magic effect
  78. if love.math.random() < .25 then
  79. local effects = self.effects
  80. shruju.effect = setmetatable({timer = config.shruju.magicDuration}, {__index = data.shruju[effects[love.math.random(1, #effects)]]})
  81. end
  82. self.slot = shruju
  83. self.growing = nil
  84. self.shrujuAnimation = data.animation.shruju1()
  85. self.shrujuAnimation:on('complete', function(data)
  86. if data.state.name == 'spawn' then
  87. self.shrujuAnimation:set('idle')
  88. end
  89. end)
  90. end
  91. end
  92. function ShrujuPatch:getGrowTime(code)
  93. return math.max(config.shruju.growTime - (ctx.shrujuPatches.harvestLevel * config.shruju.harvestCooldownReduction), config.shruju.minGrowTime)
  94. end
  95. function ShrujuPatch:removeType(code)
  96. for i = 1, #self.types do
  97. if self.types[i] == code then table.remove(self.types, i) return end
  98. end
  99. end