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.

practice.lua 5.3 KiB

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