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.

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