menu.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. --[========================================================================[--
  2. Menu screen code for Thrust II Reloaded.
  3. Copyright © 2015-2018 Pedro Gimeno Fortea
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]========================================================================]--
  20. local menu = {}
  21. local la,le,lfs,lf,lg,li,lj,lk,lm,lmo,lp,ls,lsys,lth,lt,lw = require'ns'()
  22. local menuimage
  23. local has_save
  24. local t
  25. local playing
  26. local period = 1.9961678004535148 -- 88031 samples / 44100 Hz
  27. local bar1, bar2
  28. local barc
  29. local snd
  30. local activeMenu
  31. local sndstate = "stopped"
  32. local DIV = love_version >= 11000000 and 255 or 1
  33. function menu.load()
  34. menuimage = lg.newImage("img/mainmenu.png")
  35. -- playground~/images/ThrustII-newlogo-isolated.png")
  36. snd = la.newSource("snd/Thrust II - menu.ogg", "stream")
  37. snd:setVolume(0.5)
  38. snd:setLooping(true)
  39. end
  40. function menu.activate()
  41. has_save = main.isFile("saved.txt")
  42. playing = false
  43. activeMenu = "main"
  44. end
  45. function menu.deactivate()
  46. snd:stop()
  47. sndstate = "stopped"
  48. end
  49. function menu.pause(paused)
  50. if playing then
  51. if paused then
  52. if sndstate == "playing" then
  53. snd:pause()
  54. sndstate = "paused"
  55. end
  56. elseif sndstate == "paused" then
  57. snd:play()
  58. sndstate = "playing"
  59. end
  60. end
  61. end
  62. function menu.update(dt)
  63. -- This is obsolete and was used for synchronization of the audio effect with the bars.
  64. if not playing then
  65. -- start playing (not done earlier because we don't know how long it would take)
  66. playing = true
  67. t = period/2
  68. if main.music then
  69. snd:play()
  70. sndstate = "playing"
  71. end
  72. bar1 = main.wcy
  73. bar2 = bar1
  74. barc = 1
  75. else
  76. t = t + dt
  77. if t >= period then
  78. t = t - period
  79. end
  80. bar1 = main.wcy - (t-period/2)/period * main.wh*0.78
  81. bar2 = main.wcy + (t-period/2)/period * main.wh*0.78
  82. barc = barc + dt * 50
  83. end
  84. end
  85. function menu.draw()
  86. if playing then
  87. local c = math.floor(barc) % 6 + 1
  88. local b = c % 2 c = (c - b) / 2
  89. local r = c % 2 c = (c - r) / 2
  90. local g = c % 2
  91. lg.setColor(r*(192/DIV), g*(192/DIV), b*(192/DIV))
  92. lg.rectangle("fill", 0, bar1-2, main.ww, 5)
  93. lg.rectangle("fill", 0, bar2-2, main.ww, 5)
  94. end
  95. lg.setColor(255/DIV,255/DIV,255/DIV)
  96. main.drawaligned(5, menuimage, main.wcx, main.wcy)
  97. -- screens.game.tiles_draw(2176, 32)
  98. -- do return end
  99. lg.setColor(0,192/DIV,0)
  100. main.centertext("@^ A.ROGERS 1986", main.wcx, main.wcy)
  101. main.centertext("@^ P.GIMENO 2017", main.wcx, main.wcy+24)
  102. lg.setColor(192/DIV,192/DIV,192/DIV)
  103. if activeMenu == "main" then
  104. main.centertext("1....START GAME", main.wcx, main.wcy+32*3)
  105. main.centertext("2....DEFINE KEYS", main.wcx, main.wcy+32*4)
  106. main.centertext("3....SET OPTIONS", main.wcx, main.wcy+32*5)
  107. if has_save then
  108. main.centertext("4....LOAD GAME", main.wcx, main.wcy+32*6)
  109. end
  110. elseif activeMenu == "options" then
  111. local musOnOff
  112. if main.music then
  113. musOnOff = "- ON"
  114. else
  115. musOnOff = "- OFF"
  116. end
  117. main.centertext(string.format("1....ZOOM - %3.2gX", main.pixelsize), main.wcx, main.wcy+32*3)
  118. main.centertext(main.fullscreen and "2....WINDOW MODE" or "2....FULL SCREEN", main.wcx, main.wcy+32*4)
  119. main.centertext("3....MUSIC "..musOnOff, main.wcx, main.wcy+32*5)
  120. main.centertext("0....MAIN MENU", main.wcx, main.wcy+32*7)
  121. end
  122. lg.setColor(255/DIV,255/DIV,255/DIV)
  123. end
  124. function menu.keypressed(k)
  125. if k == "escape" then
  126. if activeMenu == "options" then
  127. activeMenu = "main"
  128. else
  129. le.quit()
  130. end
  131. end
  132. end
  133. function menu.textinput(c)
  134. if activeMenu == "main" then
  135. if c == "1" then
  136. main.restore = false
  137. screens.game.newgame()
  138. screens.getready.fromstart = true
  139. main.activate(screens.getready)
  140. return
  141. elseif c == "2" then
  142. main.activate(screens.redef)
  143. return
  144. elseif c == "3" then
  145. activeMenu = "options"
  146. return
  147. elseif c == "4" and has_save then
  148. main.restore = true
  149. screens.game.newgame()
  150. screens.getready.fromstart = true
  151. return main.activate(screens.getready)
  152. end
  153. elseif activeMenu == "options" then
  154. if c == "0" then
  155. activeMenu = "main"
  156. elseif c == "1" then
  157. main.pixelsize = main.pixelsize + 0.5
  158. if main.pixelsize > 4 then
  159. main.pixelsize = 1
  160. end
  161. love.resize(lg.getDimensions())
  162. main.saveScreen()
  163. elseif c == "2" then
  164. main.fullscreen = not main.fullscreen
  165. main.setMode()
  166. main.saveScreen()
  167. -- HACK: Apparently LÖVE neglects to call love.resize after entering/exiting fullscreen.
  168. love.resize(lg.getDimensions())
  169. elseif c == "3" then
  170. main.music = not main.music
  171. if main.music then
  172. snd:play()
  173. sndstate = "playing"
  174. else
  175. snd:stop()
  176. sndstate = "stopped"
  177. end
  178. main.setVolume(main.vol, true) -- save music setting
  179. end
  180. end
  181. end
  182. return menu