particles.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. local particles = lib.object.create()
  2. particles.config = {
  3. list = {'dust'}
  4. }
  5. function particles:init()
  6. self.systems = {}
  7. end
  8. function particles:bind()
  9. for i = 1, #self.config.list do
  10. local name = self.config.list[i]
  11. local particle = app.particles[name]
  12. local system = g.newParticleSystem(particle.image, particle.max or 1024)
  13. system:setOffset(particle.image:getWidth() / 2, particle.image:getHeight() / 2)
  14. self.systems[name] = system
  15. for option, value in pairs(particle.options) do
  16. self:apply(name, option, value)
  17. end
  18. end
  19. return {
  20. app.context.view.draw:subscribe(function()
  21. g.white()
  22. for code, system in pairs(self.systems) do
  23. system:update(lib.tick.delta)
  24. g.setBlendMode(app.particles[code].blendMode or 'alpha')
  25. g.draw(system)
  26. g.setBlendMode('alpha')
  27. end
  28. return -800
  29. end)
  30. }
  31. end
  32. function particles:emit(code, x, y, count, options)
  33. if type(count) == 'table' then
  34. options = count
  35. count = 1
  36. end
  37. options = options or {}
  38. if type(options) == 'function' then
  39. for i = 1, count do
  40. self:emit(code, x, y, 1, options())
  41. end
  42. else
  43. for option, value in pairs(options) do
  44. self:apply(code, option, value)
  45. end
  46. self.systems[code]:setPosition(x, y)
  47. self.systems[code]:emit(count)
  48. for option, value in pairs(options) do
  49. if app.particles[code].options[option] then
  50. self:apply(code, option, app.particles[code].options[option])
  51. end
  52. end
  53. end
  54. end
  55. function particles:apply(code, option, value)
  56. local system = self.systems[code]
  57. local capitalized = option:sub(1, 1):upper() .. option:sub(2)
  58. local setter = system['set' .. capitalized]
  59. if type(value) == 'table' then setter(system, unpack(value))
  60. else setter(system, value) end
  61. end
  62. return particles