particles.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. local g = love.graphics
  2. Particles = extend(Manager)
  3. Particles.depth = -60
  4. function Particles:init()
  5. self.active = ctx.options.particles
  6. if not self.active then return end
  7. self.systems = {}
  8. self.modes = {draw = {}, gui = {}}
  9. for i = 1, #data.particle do
  10. local particle = data.particle[i]
  11. local system = g.newParticleSystem(particle.image, particle.max or 1024)
  12. system:setOffset(particle.image:getWidth() / 2, particle.image:getHeight() / 2)
  13. self.systems[particle.code] = system
  14. self.modes[particle.mode or 'draw'][particle.code] = system
  15. for option, value in pairs(particle.options) do
  16. self:apply(particle.code, option, value)
  17. end
  18. end
  19. ctx.event:emit('view.register', {object = self})
  20. ctx.event:emit('view.register', {object = self, mode = 'gui'})
  21. end
  22. function Particles:draw()
  23. if ctx.ded or not self.active then return end
  24. g.setColor(255, 255, 255)
  25. table.each(self.modes.draw, function(system, code)
  26. system:update(ls.dt)
  27. g.setBlendMode(data.particle[code].blendMode or 'alpha')
  28. g.draw(system)
  29. g.setBlendMode('alpha')
  30. end)
  31. end
  32. function Particles:gui()
  33. if not self.active then return end
  34. g.setColor(255, 255, 255)
  35. table.each(self.modes.gui, function(system, code)
  36. system:update(ls.dt)
  37. g.setBlendMode(data.particle[code].blendMode or 'alpha')
  38. g.draw(system)
  39. g.setBlendMode('alpha')
  40. end)
  41. end
  42. function Particles:emit(code, x, y, count, options)
  43. if not self.active or not data.particle[code] then return end
  44. if type(count) == 'table' then
  45. options = count
  46. count = 1
  47. end
  48. options = options or {}
  49. table.each(options, function(value, option)
  50. self:apply(code, option, value)
  51. end)
  52. self.systems[code]:setPosition(x, y)
  53. self.systems[code]:emit(count)
  54. table.each(options, function(value, option)
  55. if data.particle[code].options[option] then
  56. self:apply(code, option, data.particle[code].options[option])
  57. end
  58. end)
  59. end
  60. function Particles:apply(code, option, value)
  61. if not self.active then return end
  62. local system = self.systems[code]
  63. local setter = system['set' .. option:capitalize()]
  64. if type(value) == 'table' then setter(system, unpack(value))
  65. else setter(system, value) end
  66. end