game.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Game = class()
  2. Game.categories = {
  3. ground = 1,
  4. building = 2,
  5. person = 3,
  6. pigeon = 4,
  7. oneWayPlatform = 5,
  8. debris = 6
  9. }
  10. function Game:load(level, levelIndex)
  11. self.stats = {
  12. buildingsDestroyed = 0,
  13. peopleKilled = 0,
  14. maxCombo = 0
  15. }
  16. self.startTick = ls.tick
  17. self.event = Event()
  18. self.world = love.physics.newWorld(0, 1000)
  19. self.view = View()
  20. self.map = _G[level](levelIndex)
  21. self.pigeon = Pigeon()
  22. self.enemies = Manager()
  23. self.buildings = Manager()
  24. self.projectiles = Manager()
  25. self.hud = Hud()
  26. self.goal = Goal()
  27. self.particles = Particles()
  28. self.sound = Sound()
  29. self.paused = false
  30. self.map:spawnHuts()
  31. ctx.stats.maxMaxCombo = table.count(ctx.enemies.objects) + table.count(ctx.buildings.objects) + (table.count(ctx.buildings.objects) * 6)
  32. ctx.stats.originalBuildings = table.count(ctx.buildings.objects)
  33. ctx.stats.originalPeople = table.count(ctx.buildings.objects) * 6 + table.count(ctx.enemies.objects)
  34. self.backgroundSound = ctx.sound:loop('background', function(sound)
  35. sound:setVolume(.5)
  36. end)
  37. self.world:setContactFilter(function(fixtureA, fixtureB)
  38. local a, b = fixtureA:getBody():getUserData(), fixtureB:getBody():getUserData()
  39. if not a or not b then return true end
  40. local resultA = f.exe(a.collideWith, a, b, fixtureA, fixtureB)
  41. local resultB = f.exe(b.collideWith, b, a, fixtureB, fixtureA)
  42. return resultA or resultB
  43. end)
  44. end
  45. function Game:update()
  46. self.debug = false--love.keyboard.isDown('`')
  47. if self.paused then
  48. self.pigeon:paused()
  49. self.view:update()
  50. self.buildings:paused()
  51. self.enemies:paused()
  52. self.projectiles:paused()
  53. self.hud:paused()
  54. return
  55. end
  56. self.pigeon:update()
  57. self.enemies:update()
  58. self.buildings:update()
  59. self.projectiles:update()
  60. self.map:update()
  61. self.world:update(ls.tickrate)
  62. self.view:update()
  63. self.hud:update()
  64. ls.timescale = 1--love.keyboard.isDown('r') and 5 or 1
  65. if joystick then
  66. if ctx.hud.win.active and joystick:isGamepadDown('a') then
  67. self:keypressed('return')
  68. elseif joystick:isGamepadDown('back') then
  69. self:keypressed('escape')
  70. elseif joystick:isGamepadDown('start') then
  71. if ctx.hud.win.active then
  72. ctx.hud:share()
  73. else
  74. self:keypressed('p')
  75. end
  76. end
  77. end
  78. lurker.update()
  79. end
  80. function Game:draw()
  81. flux.update(ls.dt)
  82. self.view:draw()
  83. self.goal:draw()
  84. end
  85. function Game:keypressed(key)
  86. if key == 'escape' then
  87. Context:remove(ctx)
  88. Context:add(Menu)
  89. elseif key == 'm' then
  90. ctx.sound:mute()
  91. elseif key == 'p' then
  92. self.paused = not self.paused
  93. elseif key == 'r' and ctx.hud.win.active then
  94. Context:remove(ctx)
  95. local world = (ctx.map.name == 'dinoland') and 'Dinoland' or 'Kingdumb'
  96. local index = ctx.map.index
  97. Context:add(Game, world, index)
  98. elseif ctx.hud.win.active and (key == 'return' or key == ' ') then
  99. if self.backgroundSound then self.backgroundSound:stop() end
  100. Context:remove(ctx)
  101. local world
  102. local index
  103. if ctx.map.name == 'kingdumb' and ctx.map.index == 3 then
  104. Context:add(Menu)
  105. return
  106. elseif ctx.map.name == 'dinoland' and ctx.map.index == 3 then
  107. world = 'Kingdumb'
  108. index = 1
  109. else
  110. world = (ctx.map.name == 'dinoland') and 'Dinoland' or 'Kingdumb'
  111. index = ctx.map.index + 1
  112. end
  113. Context:add(Game, world, index)
  114. elseif ctx.hud.win.active and key == 't' then
  115. ctx.hud:share()
  116. end
  117. self.pigeon:keypressed(key)
  118. end