You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 regels
3.9 KiB

  1. level3 = Class{}
  2. local levelLoaded = false
  3. local M = {}
  4. function level3.update(dt)
  5. if not levelLoaded then
  6. shipsleft = 1
  7. local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
  8. planetsleft = 3
  9. gameStatus = "setup"
  10. playbutts = {}
  11. guibutts = {}
  12. VCAM.x, VCAM.y = WINDOW_WIDTH/2, WINDOW_HEIGHT/2
  13. explosions = {}
  14. shipIsHit = false
  15. guimenu = mainMenu()
  16. reachedGoal = false
  17. lvlbase = base(900, 300)
  18. levelLoaded = true
  19. table.insert(playbutts, menu:addButton("Return to setup", function()
  20. gameStatus = "setup"
  21. level3.reset()
  22. end ))
  23. table.insert(guibutts, menu:addButton("Release brake!", function ()
  24. if shipsleft == 0 then
  25. selectedItem = "none"
  26. gameStatus = "play"
  27. end
  28. end
  29. ))
  30. table.insert(guibutts, menu:addButton("To menu", function ()
  31. level3.goBack()
  32. end))
  33. table.insert(planets, planet(900, 400, 50, 0.3, planetImage, "nodelete"))
  34. table.insert(planets, planet(700, 300, 50, 0.3, planetImage, "nodelete"))
  35. table.insert(planets, planet(900, 200, 50, 0.3, planetImage, "nodelete"))
  36. end
  37. if reachedGoal then
  38. if saveData.levelsBeaten < 3 then
  39. saveData.levelsBeaten = 3
  40. end
  41. --print("saveData.levelsBeaten is " .. saveData.levelsBeaten)
  42. love.filesystem.write("save", serialize(saveData))
  43. level3.goBack()
  44. end
  45. camera:update(dt)
  46. if lvlbase ~= nil then
  47. lvlbase:update(dt)
  48. end
  49. --print(camera.x .. " " .. camera.y)
  50. for i, explosion in ipairs(explosions) do
  51. explosion:update(dt)
  52. if explosion.killed then
  53. table.remove(explosions, i)
  54. gameStatus = "setup"
  55. level3.reset()
  56. end
  57. end
  58. if gameStatus == "play" then
  59. camera.x, camera.y = firstShip.x - firstShip.height*4, firstShip.y- firstShip.width
  60. --print(camera.x .. firstShip.x)
  61. if shipIsHit then
  62. if #explosions == 0 then
  63. table.insert(explosions, explosion(firstShip.x, firstShip.y, 100, {1,1,1,1}))
  64. end
  65. end
  66. firstShip:update(dt)
  67. for i in ipairs(planets) do
  68. planets[i]:update(dt)
  69. end
  70. else
  71. camera:follow(VCAM.x, VCAM.y)
  72. end
  73. level3.GUIControl()
  74. end
  75. function level3.draw()
  76. love.graphics.setColor(1,1,1,1)
  77. camera:attach()
  78. firstShip:draw()
  79. if lvlbase ~= nil then
  80. lvlbase:draw()
  81. end
  82. for i in ipairs(planets) do
  83. planets[i]:draw(dt)
  84. end
  85. --love.graphics.rectangle("fill",VCAM.x,VCAM.y,30,30)
  86. if shipIsHit then
  87. for i, explosion in ipairs(explosions) do
  88. explosion:render()
  89. --print("exploding")
  90. end
  91. end
  92. camera:detach()
  93. camera:draw()
  94. if gameStatus == "setup" then
  95. GUIDraw("left")
  96. elseif gameStatus == "play" then
  97. guimenu:butt(playbutts, WINDOW_WIDTH, WINDOW_HEIGHT, 1100, WINDOW_HEIGHT-50, 40, WINDOW_WIDTH/3)
  98. end
  99. end
  100. function level3.goBack()
  101. level3.reset()
  102. lvlbase = nil
  103. gameStatus = "setup"
  104. firstShip.path = {}
  105. levelLoaded = false
  106. for k in pairs(planets) do
  107. planets[k] = nil
  108. end
  109. gameState = "selectlv"
  110. end
  111. function level3.reset()
  112. firstShip:reset()
  113. for k in pairs(planets) do
  114. if planets[k].deletable then
  115. planets[k] = nil
  116. end
  117. end
  118. local planetImage = love.graphics.newImage("entities/planet/planet" .. math.random(1, 18) .. ".png")
  119. shipsleft = 1
  120. shipIsHit = false
  121. planetsleft = 3
  122. end
  123. function level3.GUIControl()
  124. if (love.keyboard.isDown('a') and VCAM.x > WINDOW_WIDTH/2) then
  125. VCAM.x = VCAM.x - 10
  126. end
  127. if (love.keyboard.isDown('d')) then
  128. VCAM.x = VCAM.x + 10
  129. end
  130. end
  131. return level3