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.

267 lines
5.1 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. Menubutton(20, 100, 200, 50, gamestate.game, nil, "Start Game", {255, 255, 255}, {57, 45, 66}),
  20. 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. -- Buttons:
  27. button = {
  28. tutorial = Button(width - 70, 20, 50, 30, "help", {255, 255, 255}, {40, 40, 40}, false)
  29. }
  30. -- Textboxes:
  31. textbox = {
  32. tutorial = Textbox(40, 40, width-80, height-80, text.tutorial, "center", {255, 255, 255}, {0, 0, 0})
  33. }
  34. -- Camera:
  35. cam = Camera()
  36. zoomlevel = settings.zoom.reset
  37. --Simulation:
  38. warpspeed = 1
  39. warpCoolDown = 0
  40. -- Loading:
  41. ships = {} --Potentially add other starships in the future?
  42. planet = {}
  43. loadPlanets()
  44. local spawnPlanet = planet[1]
  45. player = Player(spawnPlanet.x, spawnPlanet.y-spawnPlanet.r-1, "orbiter")
  46. player.xSpeed, player.ySpeed = spawnPlanet.xSpeed, spawnPlanet.ySpeed
  47. gui = Gui(1)
  48. effects = {}
  49. end
  50. -- Planets:
  51. function loadPlanets()
  52. debug("Planets in planet table: "..#planetdata)
  53. for i, p in ipairs(planetdata) do
  54. debug(p.name.." is loading")
  55. table.insert(planet, i,
  56. Planet(
  57. -- Planet Data Assignment:
  58. p.x, p.y,
  59. p.r, p.m,
  60. p.xSpeed, p.ySpeed,
  61. p.name,
  62. p.colour,
  63. p.parent
  64. )
  65. )
  66. debug(p.name.." is loaded")
  67. end
  68. debug("Planets loaded: "..#planet)
  69. end
  70. function updatePlanets()
  71. planet[1]:update()
  72. end
  73. function drawPlanets()
  74. for i=1, #planet do
  75. planet[i]:draw()
  76. --debug("Drawing planet " .. i)
  77. end
  78. end
  79. -- Effects
  80. function drawEffects()
  81. for i=1, #effects do
  82. effects[i]:draw()
  83. end
  84. for i, effect in ipairs(effects) do --Separate functions because if I remove something while processing it it WILL lead to an error
  85. if effect.finished then
  86. table.remove(effects, i)
  87. end
  88. end
  89. end
  90. -- Camera
  91. function cameraControls()
  92. local step = settings.zoom.step
  93. function love.wheelmoved(x, y)
  94. if y > 0 then
  95. -- Zoom in:
  96. zoomlevel = zoomlevel + step*(zoomlevel*10)
  97. elseif y < 0 then
  98. -- Zoom out:
  99. zoomlevel = zoomlevel - step*(zoomlevel*10)
  100. end
  101. end
  102. -- Reset Zoom:
  103. if love.mouse.isDown(controls.camera.zoom.reset) then
  104. zoomlevel = settings.zoom.reset
  105. end
  106. -- Zoom Limit:
  107. local max, min = settings.zoom.max, settings.zoom.min
  108. if zoomlevel < min then
  109. zoomlevel = min
  110. end
  111. if zoomlevel > max then
  112. zoomlevel = max
  113. end
  114. --debug(zoomlevel)
  115. cam:zoomTo(zoomlevel)
  116. end
  117. -- Time Warp
  118. function timewarpControls()
  119. -- Time Warp Toggle Cooldowns:
  120. local maxCooldown = settings.warp.cooldown
  121. -- Time Warp Steps:
  122. local step = settings.warp.step
  123. -- Time Warp Limits:
  124. local min = settings.warp.min
  125. local max = settings.warp.max
  126. -- Decrease Warp
  127. if love.keyboard.isDown(controls.flight.warp.down) and warpCoolDown <= 0 then
  128. warpspeed = warpspeed - step
  129. warpCoolDown = maxCooldown
  130. end
  131. -- Increase Warp
  132. if love.keyboard.isDown(controls.flight.warp.up) and warpCoolDown <= 0 then
  133. warpspeed = warpspeed + step
  134. warpCoolDown = maxCooldown
  135. end
  136. -- Reset Warp
  137. if love.keyboard.isDown(controls.flight.warp.reset) then
  138. warpspeed = min
  139. end
  140. -- Value Correction
  141. if warpspeed < min then
  142. warpspeed = min
  143. elseif warpspeed > max then
  144. warpspeed = max
  145. end
  146. warpCoolDown = warpCoolDown - 1
  147. return warpspeed
  148. end
  149. -- Menubuttons
  150. function menubuttonUpdate()
  151. if GAMESTATE == gamestate.menu then
  152. for i, button in ipairs(menubutton.menu) do
  153. button:update()
  154. end
  155. elseif GAMESTATE == gamestate.game then
  156. for i, button in ipairs(menubutton.game) do
  157. button:update()
  158. end
  159. end
  160. end
  161. function menubuttonDraw()
  162. if GAMESTATE == gamestate.menu then
  163. for i, button in ipairs(menubutton.menu) do
  164. button:draw()
  165. end
  166. elseif GAMESTATE == gamestate.game then
  167. for i, button in ipairs(menubutton.game) do
  168. button:draw()
  169. end
  170. end
  171. end
  172. -- MAIN
  173. function love.update(dt)
  174. if GAMESTATE == gamestate.quit then
  175. debug("Game has been quit, goodbye :)")
  176. love.event.quit(0)
  177. elseif GAMESTATE == gamestate.menu then
  178. elseif GAMESTATE == gamestate.game then
  179. -- Game Objects:
  180. for i=1, timewarpControls() do
  181. -- Physics go in here:
  182. updatePlanets()
  183. player:update(dt)
  184. end
  185. player:throttleControls()
  186. -- Gui:
  187. gui:update(dt)
  188. button.tutorial:update()
  189. -- Camera:
  190. cam:lookAt(player.x, player.y)
  191. cameraControls()
  192. end
  193. menubuttonUpdate()
  194. end
  195. function love.draw()
  196. if GAMESTATE == gamestate.menu then
  197. -- Game Title:
  198. love.graphics.setColor(1, 1, 0.6)
  199. love.graphics.setFont(font.gametitle)
  200. love.graphics.printf(info.title, 20, 20, width, "left")
  201. elseif GAMESTATE == gamestate.game then
  202. cam:attach()
  203. -- Game Objects:
  204. drawPlanets()
  205. drawEffects()
  206. player:draw()
  207. cam:detach()
  208. -- Gui:
  209. player:drawPositionIndicator()
  210. gui:draw()
  211. if button.tutorial.isActive then
  212. textbox.tutorial:draw()
  213. end
  214. button.tutorial:draw()
  215. end
  216. menubuttonDraw()
  217. end