Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

229 строки
4.6 KiB

  1. function love.load()
  2. require "import"
  3. -- Debugging / Logging:
  4. debug = calc.debug
  5. calc.isDebug = true
  6. -- Declaration:
  7. love.window.setTitle(info.name.." - v"..info.version)
  8. width, height = love.graphics.getDimensions()
  9. -- Gamestate:
  10. gamestate = {
  11. quit = "stateQUIT",
  12. menu = "stateMENU",
  13. game = "stateGAME"
  14. }
  15. GAMESTATE = gamestate.menu
  16. -- Menubuttons:
  17. menubutton = {
  18. menu = {
  19. startGame = Menubutton(20, 100, 200, 50, gamestate.game, nil, "Start Game", {255, 255, 255}, {57, 45, 66}),
  20. quitGame = Menubutton(30, 170, 180, 40, gamestate.quit, nil, "Quit Game", {255, 255, 255}, {57, 45, 66})
  21. },
  22. game = {
  23. -- Pause button or something here in future?
  24. }
  25. }
  26. -- Camera:
  27. cam = Camera()
  28. zoomlevel = settings.zoom.reset
  29. --Simulation:
  30. warpspeed = 1
  31. warpCoolDown = 0
  32. -- Loading:
  33. ships = {} --Potentially add other starships in the future?
  34. planet = {}
  35. loadPlanets()
  36. local spawnPlanet = planet[1]
  37. player = Player(spawnPlanet.x, spawnPlanet.y-spawnPlanet.r-1, "orbiter")
  38. player.xSpeed, player.ySpeed = spawnPlanet.xSpeed, spawnPlanet.ySpeed
  39. gui = Gui(1)
  40. effects = {}
  41. end
  42. -- Planets:
  43. function loadPlanets()
  44. debug("Planets in planet table: "..#planetdata)
  45. for i, p in ipairs(planetdata) do
  46. debug(p.name.." is loading")
  47. table.insert(planet, i,
  48. Planet(
  49. -- Planet Data Assignment:
  50. p.x, p.y,
  51. p.r, p.m,
  52. p.xSpeed, p.ySpeed,
  53. p.name,
  54. p.colour,
  55. p.parent
  56. )
  57. )
  58. debug(p.name.." is loaded")
  59. end
  60. debug("Planets loaded: "..#planet)
  61. end
  62. function updatePlanets()
  63. planet[1]:update()
  64. end
  65. function drawPlanets()
  66. for i=1, #planet do
  67. planet[i]:draw()
  68. --debug("Drawing planet " .. i)
  69. end
  70. end
  71. function drawEffects()
  72. for i=1, #effects do
  73. effects[i]:draw()
  74. end
  75. for i, effect in ipairs(effects) do --Separate functions because if I remove something while processing it it WILL lead to an error
  76. if effect.finished then
  77. table.remove(effects, i)
  78. end
  79. end
  80. end
  81. -- Camera
  82. function cameraControls()
  83. local step = settings.zoom.step
  84. function love.wheelmoved(x, y)
  85. if y > 0 then
  86. -- Zoom in:
  87. zoomlevel = zoomlevel + step*(zoomlevel*10)
  88. elseif y < 0 then
  89. -- Zoom out:
  90. zoomlevel = zoomlevel - step*(zoomlevel*10)
  91. end
  92. end
  93. -- Reset Zoom:
  94. if love.mouse.isDown(controls.camera.zoom.reset) then
  95. zoomlevel = settings.zoom.reset
  96. end
  97. -- Zoom Limit:
  98. local max = settings.zoom.max
  99. local min = settings.zoom.min
  100. if zoomlevel < min then
  101. zoomlevel = min
  102. end
  103. if zoomlevel > max then
  104. zoomlevel = max
  105. end
  106. --debug(zoomlevel)
  107. cam:zoomTo(zoomlevel)
  108. end
  109. function timewarpControls()
  110. -- Time Warp Toggle Cooldowns:
  111. local maxCooldown = settings.warp.cooldown
  112. -- Time Warp Steps:
  113. local step = settings.warp.step
  114. -- Time Warp Limits:
  115. local min = settings.warp.min
  116. local max = settings.warp.max
  117. -- Decrease Warp
  118. if love.keyboard.isDown(controls.flight.warp.down) and warpCoolDown <= 0 then
  119. warpspeed = warpspeed - step
  120. warpCoolDown = maxCooldown
  121. end
  122. -- Increase Warp
  123. if love.keyboard.isDown(controls.flight.warp.up) and warpCoolDown <= 0 then
  124. warpspeed = warpspeed + step
  125. warpCoolDown = maxCooldown
  126. end
  127. -- Reset Warp
  128. if love.keyboard.isDown(controls.flight.warp.reset) then
  129. warpspeed = min
  130. end
  131. -- Value Correction
  132. if warpspeed < min then
  133. warpspeed = min
  134. elseif warpspeed > max then
  135. warpspeed = max
  136. end
  137. warpCoolDown = warpCoolDown - 1
  138. return warpspeed
  139. end
  140. -- MAIN
  141. function love.update(dt)
  142. if GAMESTATE == gamestate.quit then
  143. debug("Game has been quit, goodbye :)")
  144. love.event.quit(0)
  145. elseif GAMESTATE == gamestate.menu then
  146. menubutton.menu.startGame:update(dt)
  147. menubutton.menu.quitGame:update(dt)
  148. elseif GAMESTATE == gamestate.game then
  149. -- Game Objects:
  150. for i=1, timewarpControls() do
  151. -- Physics go in here:
  152. updatePlanets()
  153. player:update(dt)
  154. end
  155. player:throttleControls()
  156. -- Gui:
  157. gui:update(dt)
  158. -- Camera:
  159. cam:lookAt(player.x, player.y)
  160. cameraControls()
  161. --debug(player.x .. " " .. player.y)
  162. end
  163. end
  164. function love.draw()
  165. if GAMESTATE == gamestate.menu then
  166. -- Game Title:
  167. love.graphics.setColor(1, 1, 0.6)
  168. love.graphics.setFont(font.gametitle)
  169. love.graphics.printf(info.title, 20, 20, width, "left")
  170. -- Buttons:
  171. menubutton.menu.startGame:draw()
  172. menubutton.menu.quitGame:draw()
  173. elseif GAMESTATE == gamestate.game then
  174. cam:attach()
  175. -- Game Objects:
  176. drawPlanets()
  177. drawEffects()
  178. player:draw()
  179. -- Camera Zoom Player Location Indicator: OVERWORK SOON PLS KAY; IT UGLY
  180. if zoomlevel < 0.3 then
  181. love.graphics.setColor(1, 1, 1, 0.2)
  182. love.graphics.circle("fill", player.x, player.y, (1/zoomlevel)*10)
  183. end
  184. cam:detach()
  185. -- Gui:
  186. gui:draw()
  187. end
  188. end