Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

203 rader
5.7 KiB

  1. practice = Class{}
  2. local levelLoaded = false
  3. local M = {}
  4. local currenctScore = 0
  5. function practice.update(dt)
  6. if not pauseStatus then
  7. if not levelLoaded then
  8. shipsleft = 1
  9. planetsleft = 10
  10. cannonsleft = 10
  11. gameStatus = "setup"
  12. playbutts = {}
  13. guibutts = {}
  14. XCAM = 0
  15. currentScore = 0
  16. cameraControl = true
  17. thrusterMax = 0
  18. YCAM = 0
  19. firstShip.fuel = 0
  20. explosions = {}
  21. shipIsHit = false
  22. guimenu = mainMenu()
  23. table.insert(playbutts, menu:addButton("Return to setup", function()
  24. gameStatus = "setup"
  25. practice.reset()
  26. end ))
  27. table.insert(guibutts, menu:addButton("Release brake!", function ()
  28. if shipsleft == 0 then
  29. selectedItem = "none"
  30. gameStatus = "play"
  31. end
  32. end
  33. ))
  34. table.insert(guibutts, menu:addButton("To menu", function ()
  35. practice.goBack()
  36. end
  37. ))
  38. levelLoaded = true
  39. end
  40. camera:update(dt)
  41. --print(camera.x .. " " .. camera.y)
  42. for i, explosion in ipairs(explosions) do
  43. explosion:update(dt)
  44. -- print("1 update done")
  45. if explosion.killed then
  46. table.remove(explosions, i)
  47. if shipIsHit then
  48. gameStatus = "setup"
  49. practice.reset()
  50. end
  51. end
  52. end
  53. if gameStatus == "play" then
  54. camera.x, camera.y = firstShip.x - firstShip.height*4, firstShip.y- firstShip.width
  55. --print(camera.x .. firstShip.x)
  56. if shipIsHit and not firstShip.exploded then
  57. table.insert(explosions, explosion(firstShip.x, firstShip.y, 100, {1,1,1,1}))
  58. explosions[#explosions].type = 0
  59. firstShip.exploded = true
  60. end
  61. firstShip:update(dt)
  62. for i in ipairs(planets) do
  63. planets[i]:update(dt)
  64. if math.sqrt((firstShip.x - planets[i].x)^2 + (firstShip.y - planets[i].y)^2) > planets[i].w/3 then
  65. currentScore = currentScore + math.sqrt(planets[i].attractionX^2 + planets[i].attractionY^2)*100
  66. --print(math.sqrt(planets[i].attractionX^2 + planets[i].attractionY^2))
  67. end
  68. end
  69. for i in ipairs(cannons) do
  70. cannons[i]:update(dt)
  71. end
  72. for i in ipairs(projectiles) do
  73. projectiles[i]:update(dt)
  74. end
  75. for i in ipairs(projectiles) do
  76. if projectiles[i].killed then
  77. table.remove(projectiles, i)
  78. --print("killing")
  79. end
  80. end
  81. else
  82. camera:follow(VCAM.x, VCAM.y)
  83. end
  84. practice.GUIControl()
  85. else
  86. settingsMenuUpdate(dt)
  87. end
  88. end
  89. function practice.draw()
  90. camera:attach()
  91. firstShip:draw()
  92. for i in ipairs(planets) do
  93. planets[i]:draw(dt)
  94. end
  95. for i in ipairs(cannons) do
  96. cannons[i]:draw(dt)
  97. end
  98. for i in ipairs(projectiles) do
  99. projectiles[i]:draw(dt)
  100. end
  101. --love.graphics.rectangle("fill",VCAM.x,VCAM.y,30,30)
  102. for i, explosion in ipairs(explosions) do
  103. if shipIsHit then
  104. explosion:render()
  105. else
  106. explosion:render("special")
  107. end
  108. --print("exploding")
  109. end
  110. camera:detach()
  111. camera:draw()
  112. if gameStatus == "setup" then
  113. GUIDraw("anywhere")
  114. love.graphics.setFont(tinyfont)
  115. local textW = tinyfont:getWidth("Top score: " .. math.floor(saveData.score/100))
  116. love.graphics.print("Top score: " .. math.floor(saveData.score/100), WINDOW_WIDTH/2-textW/2, 10)
  117. practice.hint()
  118. elseif gameStatus == "play" and not pauseStatus then
  119. local textW = tinyfont:getWidth("Score: " .. math.floor(currentScore/100))
  120. love.graphics.setFont(tinyfont)
  121. love.graphics.print("Score: " .. math.floor(currentScore/100), WINDOW_WIDTH/2-textW/2, 10)
  122. guimenu:butt(playbutts, WINDOW_WIDTH, WINDOW_HEIGHT, 1100, WINDOW_HEIGHT-50, 40, WINDOW_WIDTH/3)
  123. end
  124. if pauseStatus then
  125. drawPauseMenu()
  126. love.keyboard.mouseisReleased = false
  127. end
  128. end
  129. function practice.goBack()
  130. practice.reset()
  131. cameraControl = false
  132. gameStatus = "setup"
  133. levelLoaded = false
  134. gameState = "menu"
  135. cannons = {}
  136. end
  137. function practice.reset()
  138. firstShip:reset()
  139. camera.scale = 1
  140. projectiles = {}
  141. for i in ipairs(cannons) do
  142. cannons[i].timer = cannons[i].otimer
  143. end
  144. shipsleft = 1
  145. if currentScore > saveData.score then
  146. saveData.score = currentScore
  147. love.filesystem.write("save", serialize(saveData))
  148. end
  149. currentScore = 0
  150. shipIsHit = false
  151. firstShip.fuel = 99999
  152. end
  153. function practice.GUIControl()
  154. if (love.keyboard.isDown('w')) then
  155. VCAM.y = VCAM.y - 10
  156. end
  157. if (love.keyboard.isDown('a')) then
  158. VCAM.x = VCAM.x - 10
  159. end
  160. if (love.keyboard.isDown('s')) then
  161. VCAM.y = VCAM.y + 10
  162. end
  163. if (love.keyboard.isDown('d')) then
  164. VCAM.x = VCAM.x + 10
  165. end
  166. end
  167. function practice.hint()
  168. love.graphics.setFont(tinyfont)
  169. if love.keyboard.isDown('w') then
  170. love.graphics.setColor(1,0,0,1)
  171. end
  172. love.graphics.print("↑[W]",80,10)
  173. love.graphics.setColor(1,1,1,1)
  174. if love.keyboard.isDown('s') then
  175. love.graphics.setColor(1,0,0,1)
  176. end
  177. love.graphics.print("↓[S]",80,100)
  178. love.graphics.setColor(1,1,1,1)
  179. if love.keyboard.isDown('a') then
  180. love.graphics.setColor(1,0,0,1)
  181. end
  182. love.graphics.print("←[A]",10,50)
  183. love.graphics.setColor(1,1,1,1)
  184. if love.keyboard.isDown('d') then
  185. love.graphics.setColor(1,0,0,1)
  186. end
  187. love.graphics.print("→[D]",150,50)
  188. love.graphics.setColor(1,1,1,1)
  189. end
  190. return practice