bubbles.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. local Bubbles = class()
  2. Bubbles.soundCount = 4
  3. function Bubbles:init()
  4. self.list = {}
  5. self.popped = 0
  6. self.particleCanvas = g.newCanvas(32, 32)
  7. g.setCanvas(self.particleCanvas)
  8. self.particleCanvas:clear(255, 255, 255, 0)
  9. g.setPointSize(31)
  10. g.setColor(255, 255, 255)
  11. g.point(16, 16)
  12. g.setCanvas()
  13. self.particles = g.newParticleSystem(self.particleCanvas, 256)
  14. self.particles:setOffset(32, 32)
  15. self.particles:setParticleLifetime(.3, .5)
  16. self.particles:setSpeed(100, 400)
  17. self.particles:setLinearDamping(4, 8)
  18. self.particles:setSpread(2 * math.pi)
  19. self.particles:setSizes(.4, 0)
  20. self.particles:setSizeVariation(.5)
  21. self.soundIndex = 1
  22. self.sounds = {}
  23. for i = 1, self.soundCount do
  24. self.sounds[i] = love.audio.newSource('sound/bubble.ogg')
  25. end
  26. for i = 1, 8 do
  27. local bubble = app.bubble()
  28. self.list[bubble] = bubble
  29. bubble.direction = -math.pi / 2
  30. bubble.speed = love.math.random(100, 200)
  31. end
  32. end
  33. function Bubbles:update(dt)
  34. if hud.tutorial then return end
  35. self.particles:update(dt)
  36. table.with(self.list, 'update', dt)
  37. if not hud.tutorial and not hud.dead and love.math.random() < .28 * dt then
  38. local bubble = app.bubble()
  39. self.list[bubble] = bubble
  40. end
  41. end
  42. function Bubbles:draw()
  43. g.setColor(255, 255, 255)
  44. g.draw(self.particles)
  45. table.with(self.list, 'draw')
  46. end
  47. function Bubbles:remove(instance, legit)
  48. self.particles:setPosition(instance.x, instance.y)
  49. for i = 1, 3 do
  50. local c = instance.color
  51. self.particles:setColors(c[1], c[2], c[3], 255, c[1], c[2], c[3], 0)
  52. self.particles:emit(10)
  53. end
  54. if legit then
  55. self.popped = self.popped + 1
  56. end
  57. self.list[instance] = nil
  58. end
  59. function Bubbles:playSound()
  60. local sound = self.sounds[self.soundIndex]
  61. if sound:isPlaying() then
  62. sound:rewind()
  63. else
  64. sound:play()
  65. end
  66. sound:setPitch(.9 + love.math.random() * .2)
  67. sound:setVolume(.3)
  68. self.soundIndex = self.soundIndex + 1
  69. if self.soundIndex > #self.sounds then
  70. self.soundIndex = 1
  71. end
  72. end
  73. return Bubbles