waves.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. local waves = lib.object.create()
  2. function waves:init()
  3. self.current = 1
  4. self.event = 1
  5. self.grace = 0
  6. end
  7. function waves:bind()
  8. self.waves = self.waves or {}
  9. self.current = 0
  10. self.event = 1
  11. self.grace = 10
  12. self.waveStart = lib.tick.index
  13. return {
  14. love.update
  15. :subscribe(function()
  16. if not self.waves[self.current] then return end
  17. local events = self.waves[self.current].events
  18. local event = events[self.event]
  19. if event and (lib.tick.index - self.waveStart) * lib.tick.rate >= (event.time or 0) then
  20. self:spawn(event.kind, event.count)
  21. self.event = self.event + 1
  22. end
  23. end),
  24. love.update
  25. :subscribe(function()
  26. local enemyCount = #util.filter(app.context.objects, 'isEnemy')
  27. if self.grace == 0 and self.current < #self.waves and self.event > #self.waves[self.current].events and enemyCount == 0 then
  28. self.grace = 10
  29. app.context.objects.muju:addJuju(1)
  30. end
  31. end),
  32. love.update
  33. :filter(function() return self.grace > 0 end)
  34. :subscribe(function()
  35. self.grace = math.max(self.grace - lib.tick.rate, 0)
  36. if self.grace == 0 then
  37. self:nextWave()
  38. end
  39. end),
  40. love.keypressed
  41. :filter(f.eq('space'))
  42. :filter(function() return self.grace > 0 end)
  43. :subscribe(function()
  44. self:nextWave()
  45. end)
  46. }
  47. end
  48. function waves:spawn(kind, count)
  49. count = count or 1
  50. for i = 1, count do
  51. local x = love.math.random() > .5 and app.context.scene.width - 50 or 50
  52. local y = 100 + love.math.random() * (app.context.scene.height - 200)
  53. app.context:addObject(app.enemies[kind].object, {
  54. position = {
  55. x = x,
  56. y = y
  57. }
  58. })
  59. end
  60. end
  61. function waves:nextWave()
  62. self.current = self.current + 1
  63. self.event = 1
  64. self.waveStart = lib.tick.index
  65. self.grace = 0
  66. end
  67. return waves