pattern.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Pattern = class()
  2. function Pattern:init(update)
  3. self.done = false
  4. self.update = update
  5. end
  6. -- takes function(size, iteration, edge, world) and returns function(edge) end end
  7. -- if the function returns nil, end it all
  8. function newPattern(fn)
  9. return function(edge)
  10. edge = edge or ({'top', 'bottom', 'left', 'right'})[math.random(1, 4)]
  11. return { edge = edge, pattern = Pattern(function(inst, world)
  12. inst.counter = inst.counter and inst.counter + 1 or 1
  13. local size = ((edge == 'left' or edge == 'right') and HEIGHT or WIDTH)
  14. local result = fn(size, inst.counter, edge, world)
  15. if not result then
  16. inst.done = true
  17. return
  18. end
  19. for _, p in pairs(result) do
  20. if p > 0 and p < size then
  21. local bullet = ({
  22. top = Bullet(p, 1, 0, 1),
  23. bottom = Bullet(p, HEIGHT, 0, -1),
  24. left = Bullet(1, p, 1, 0),
  25. right = Bullet(WIDTH, p, -1, 0)
  26. })[edge]
  27. world.bullets[#world.bullets + 1] = bullet
  28. end
  29. end
  30. end)}
  31. end
  32. end
  33. EDGE_PATTERNS = {
  34. newPattern(function(size, iter, edge, world)
  35. if iter >= size then
  36. return nil
  37. elseif iter % 3 > 0 then
  38. return {}
  39. end
  40. local out = {}
  41. for i = math.min(iter, size), math.min(iter + 8, size), 2 do
  42. out[#out + 1] = i
  43. end
  44. return out
  45. end),
  46. newPattern(function(size, iter, edge, world)
  47. local pos = (edge == 'top' or edge == 'bottom') and world.player.x or world.player.y
  48. local out = {}
  49. if iter > 5 then
  50. return nil
  51. elseif iter == 5 or iter == 1 then
  52. for v = pos - 2, pos + 2 do
  53. out[#out + 1] = v
  54. end
  55. else
  56. out = {pos - 2, pos + 2}
  57. end
  58. return out
  59. end),
  60. newPattern(function(size, iter, edge, world)
  61. if iter > 7 then
  62. return nil
  63. end
  64. local pos = (edge == 'top' or edge == 'bottom') and world.player.x or world.player.y
  65. local out = {}
  66. out = {pos - iter + 1, pos + iter - 1}
  67. return out
  68. end),
  69. newPattern(function(size, iter, edge, world)
  70. if iter > 20 then
  71. return nil
  72. elseif iter > 6 then
  73. return {}
  74. elseif iter % 2 > 0 then
  75. return {}
  76. end
  77. local out = {}
  78. for i = 1, size do
  79. if math.floor((i + iter)/ 2) % 2 == 0 then
  80. out[#out + 1] = i
  81. end
  82. end
  83. return out
  84. end),
  85. }