bubble.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. local Bubble = class()
  2. function Bubble:init()
  3. self.size = 10 + love.math.random() * 10
  4. self.color = {love.math.random(20), love.math.random(150, 200), love.math.random(200, 255)}
  5. self.x = love.math.random() * g.getWidth()
  6. self.y = g.getHeight() + self.size
  7. self.floatSpeed = 3 + love.math.random(10)
  8. self.speed = 0
  9. self.direction = 0
  10. end
  11. function Bubble:update(dt)
  12. self.y = self.y - self.floatSpeed * dt
  13. if self.y < -self.size then
  14. if not hud.dead then
  15. hud.dead = true
  16. local lose = love.audio.newSource('sound/lose.ogg')
  17. lose:setVolume(.5)
  18. lose:play()
  19. table.each(bubbles.list, function(bubble)
  20. bubbles:remove(bubble)
  21. end)
  22. else
  23. bubbles:remove(self)
  24. end
  25. elseif self.y > g.getHeight() + self.size then
  26. self.y = g.getHeight() + self.size
  27. end
  28. if self.x < self.size + 5 then
  29. self.x = self.size + 5
  30. elseif self.x > g.getWidth() - self.size - 5 then
  31. self.x = g.getWidth() - self.size - 5
  32. end
  33. if self.speed > 0 then
  34. self.x = self.x + math.dx(self.speed * dt, self.direction)
  35. self.y = self.y + math.dy(self.speed * dt, self.direction)
  36. self.speed = math.lerp(self.speed, 0, math.min(2 * dt, 1))
  37. if self.speed < 1 then self.speed = 0 end
  38. end
  39. end
  40. function Bubble:draw()
  41. g.setLineWidth(3)
  42. g.setColor(self.color[1], self.color[2], self.color[3], 80)
  43. g.circle('fill', self.x, self.y, self.size, 40)
  44. g.setColor(self.color[1], self.color[2], self.color[3], 255)
  45. g.circle('line', self.x, self.y, self.size, 40)
  46. g.setLineWidth(1)
  47. end
  48. return Bubble