Advertisement
AdhityaRimba

menu.lua

Nov 24th, 2020
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.12 KB | None | 0 0
  1. #include "game.lua"
  2. #include "options.lua"
  3. #include "score.lua"
  4. #include "debug.lua"
  5.  
  6. bgItems = {nil, nil}
  7. bgCurrent = 0
  8.  
  9. function bgLoad(i)
  10.     bg = {}
  11.     bg.i = i+1
  12.     bg.t = 0
  13.     bg.x = 0
  14.     bg.y = 0
  15.     bg.vx = 0
  16.     bg.vy = 0
  17.     bg.a = 0
  18.     return bg
  19. end
  20.  
  21. function bgDraw(bg)
  22.     if bg then
  23.         UiPush()
  24.             local dt = GetTimeStep()
  25.             bg.t = bg.t + dt
  26.             bg.a = math.min(bg.t*0.6, 1.0)
  27.             UiColor(1,1,1,bg.a)
  28.             UiScale(1.03 + bg.t*0.01)
  29.             UiTranslate(bg.x, bg.y)
  30.             UiImage(slideshowImages[bg.i])
  31.         UiPop()
  32.     end
  33. end
  34.  
  35. bgIndex = 0
  36. bgInterval = 6
  37. bgTimer = bgInterval
  38.  
  39. function initSlideshow()
  40.     slideshowImages = {}
  41.     for i=1, 5 do
  42.         slideshowImages[#slideshowImages+1] = "menu/hub"..i..".jpg"
  43.     end
  44.     if isLevelUnlocked("lee") then
  45.         for i=1, 3 do
  46.             slideshowImages[#slideshowImages+1] = "menu/lee"..i..".jpg"
  47.         end
  48.     end
  49.     if isLevelUnlocked("marina") then
  50.         for i=1, 7 do
  51.             slideshowImages[#slideshowImages+1] = "menu/marina"..i..".jpg"
  52.         end
  53.     end
  54.     if isLevelUnlocked("mansion") then
  55.         for i=1, 6 do
  56.             slideshowImages[#slideshowImages+1] = "menu/mansion"..i..".jpg"
  57.         end
  58.     end
  59.     if isLevelUnlocked("caveisland") then
  60.         for i=1, 5 do
  61.             slideshowImages[#slideshowImages+1] = "menu/caveisland"..i..".jpg"
  62.         end
  63.     end
  64.  
  65.     --Scramble order
  66.     for i=1, #slideshowImages do
  67.         local j = math.random(1, #slideshowImages)
  68.         local tmp = slideshowImages[j]
  69.         slideshowImages[j] = slideshowImages[i]
  70.         slideshowImages[i] = tmp
  71.     end
  72. end
  73.  
  74. function init()
  75.     initSlideshow()
  76.  
  77.     gOptionsScale = 0
  78.     gSandboxScale = 0
  79.     gPlayScale = 0
  80.  
  81.     gCreateScale = 0
  82.  
  83.     bgItems[bgCurrent] = bgLoad(bgIndex)
  84.     gDeploy = GetBool("game.deploy")
  85. end
  86.  
  87.  
  88. function isLevelUnlocked(level)
  89.     local missions = ListKeys("savegame.mission")
  90.     local levelMissions = {}
  91.     for i=1,#missions do
  92.         local missionId = missions[i]
  93.         if gMissions[missionId] and GetBool("savegame.mission."..missionId) then
  94.             if gMissions[missionId].level == level then
  95.                 return true
  96.             end
  97.         end
  98.     end
  99.     return false
  100. end
  101.  
  102.  
  103. function drawSandbox(scale)
  104.     local open = true
  105.     UiPush()
  106.         local w = 800
  107.         local h = 400
  108.         UiTranslate(UiCenter(), UiMiddle())
  109.         UiScale(scale)
  110.         UiColorFilter(1, 1, 1, scale)
  111.         UiColor(0,0,0, 0.5)
  112.         UiAlign("center middle")
  113.         UiImageBox("common/box-solid-shadow-50.png", w, h, -50, -50)
  114.         UiWindow(w, h)
  115.         UiAlign("left top")
  116.         UiColor(1,1,1)
  117.         if UiIsKeyPressed("esc") or (not UiIsMouseInRect(UiWidth(), UiHeight()) and UiIsMousePressed()) then
  118.             open = false
  119.         end
  120.  
  121.         UiPush()
  122.             UiFont("font/bold.ttf", 48)
  123.             UiColor(1,1,1)
  124.             UiAlign("center")
  125.             UiTranslate(UiCenter(), 80)
  126.             UiText("SANDBOX")
  127.         UiPop()
  128.        
  129.         UiPush()
  130.             UiFont("font/regular.ttf", 22)
  131.             UiTranslate(200, 90)
  132.             UiWordWrap(420)
  133.             UiColor(0.8, 0.8, 0.8)
  134.             UiText("Free roam sandbox play with unlimited resources and no challenge. Play the campaign to unlock more environments and tools.")
  135.         UiPop()
  136.    
  137.         UiTranslate(10 + UiWidth()/2-(150*#gSandbox)/2, 190)
  138.         UiFont("font/bold.ttf", 22)
  139.         for i=1, #gSandbox do
  140.             UiPush()
  141.                 local locked = not isLevelUnlocked(gSandbox[i].level)
  142.                 UiPush()
  143.                     if locked then
  144.                         UiDisableInput()
  145.                         UiColorFilter(.5, .5, .5)
  146.                     end
  147.                     if UiImageButton(gSandbox[i].image) then
  148.                         Command("game.startmission", gSandbox[i].id, gSandbox[i].file, gSandbox[i].layers)
  149.                     end
  150.                 UiPop()
  151.                 if locked then
  152.                     UiPush()
  153.                         UiTranslate(64, 64)
  154.                         UiAlign("center middle")
  155.                         UiImage("menu/locked.png")
  156.                     UiPop()
  157.                     if UiIsMouseInRect(128, 128) then
  158.                         UiPush()
  159.                             UiAlign("center middle")
  160.                             UiTranslate(64,  180)
  161.                             UiFont("font/regular.ttf", 20)
  162.                             UiColor(.8, .8, .8)
  163.                             UiText("Unlocked in\ncampaign")
  164.                         UiPop()
  165.                     end
  166.                 end
  167.  
  168.                 UiAlign("center")
  169.                 UiTranslate(64, 150)
  170.                 UiColor(0.8, 0.8, 0.8)
  171.                 UiText(gSandbox[i].name)
  172.             UiPop()
  173.             UiTranslate(150, 0)
  174.         end
  175.     UiPop()
  176.     return open
  177. end
  178.  
  179.  
  180.  
  181. function drawCreate(scale)
  182.     local open = true
  183.     UiPush()
  184.         local w = 800
  185.         local h = 530
  186.         UiTranslate(UiCenter(), UiMiddle())
  187.         UiScale(scale)
  188.         UiColorFilter(1, 1, 1, scale)
  189.         UiColor(0,0,0, 0.5)
  190.         UiAlign("center middle")
  191.         UiImageBox("common/box-solid-shadow-50.png", w, h, -50, -50)
  192.         UiWindow(w, h)
  193.         UiAlign("left top")
  194.         UiColor(1,1,1)
  195.         if UiIsKeyPressed("esc") or (not UiIsMouseInRect(UiWidth(), UiHeight()) and UiIsMousePressed()) then
  196.             open = false
  197.         end
  198.  
  199.         UiPush()
  200.             UiFont("font/bold.ttf", 48)
  201.             UiColor(1,1,1)
  202.             UiAlign("center")
  203.             UiTranslate(UiCenter(), 60)
  204.             UiText("CREATE")
  205.         UiPop()
  206.        
  207.         UiPush()
  208.             UiFont("font/regular.ttf", 22)
  209.             UiTranslate(UiCenter(), 100)
  210.             UiAlign("center")
  211.             UiWordWrap(600)
  212.             UiColor(0.8, 0.8, 0.8)
  213.             UiText("Create your own sandbox level using the free voxel modeling program MagicaVoxel. We have provided example levels that you can modify or replace with your own creation. Find out more on our web page:", true)
  214.             UiTranslate(0, 2)
  215.             UiFont("font/bold.ttf", 22)
  216.             UiColor(1, .8, .5)
  217.             if UiTextButton("www.teardowngame.com/create") then
  218.                 Command("game.openurl", "http://www.teardowngame.com/create")
  219.             end
  220.  
  221.             UiTranslate(0, 70)
  222.             UiPush()
  223.                 UiColor(1,1,1)
  224.                 UiFont("font/regular.ttf", 26)
  225.                 UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1)
  226.                 if UiTextButton("Basic level", 240, 40) then
  227.                     Command("game.startlevel", "../../create/basic.xml")
  228.                 end
  229.                 UiTranslate(0, 45)
  230.                 if UiTextButton("Island level", 240, 40) then
  231.                     Command("game.startlevel", "../../create/island.xml")
  232.                 end
  233.                 UiTranslate(0, 45)
  234.                 if UiTextButton("Castle level", 240, 40) then
  235.                     Command("game.startlevel", "../../create/castle.xml")
  236.                 end
  237.                 UiTranslate(0, 45)
  238.                 if UiTextButton("Vehicle level", 240, 40) then
  239.                     Command("game.startlevel", "../../create/vehicle.xml")
  240.                 end
  241.                 UiTranslate(0, 45)
  242.                 if UiTextButton("Custom level", 240, 40) then
  243.                     Command("game.startlevel", "../../create/custom.xml")
  244.                 end
  245.             UiPop()
  246.  
  247.             UiTranslate(0, 250)
  248.             UiFont("font/regular.ttf", 20)
  249.             UiColor(.6, .6, .6)
  250.             UiText("Files located at: " .. GetString("game.path") .. "/create")
  251.         UiPop()
  252.     UiPop()
  253.     return open
  254. end
  255.  
  256.  
  257. function mainMenu()
  258.     UiPush()
  259.         UiColor(0,0,0, 0.75)
  260.         UiRect(UiWidth(), 150)
  261.         UiColor(1,1,1)
  262.         UiPush()
  263.             UiTranslate(50, 20)
  264.             UiScale(0.8)
  265.             UiImage("menu/logo.png")
  266.         UiPop()
  267.         UiFont("font/regular.ttf", 36)
  268.         UiTranslate(800, 30)
  269.         UiTranslate(0, 50)
  270.         UiAlign("center middle")
  271.         UiPush()
  272.             UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1)
  273.  
  274.             local bh = 50
  275.             local bo = 56
  276.  
  277.             UiPush()
  278.                 if UiTextButton("Play", 250, bh) then
  279.                     UiSound("common/click.ogg")
  280.                     if gPlayScale == 0 then
  281.                         SetValue("gPlayScale", 1.0, "easeout", 0.25)
  282.                     else
  283.                         SetValue("gPlayScale", 0.0, "easein", 0.25)
  284.                     end
  285.                 end
  286.             UiPop()
  287.  
  288.             UiTranslate(300, 0)
  289.  
  290.             UiPush()
  291.                 if UiTextButton("Options", 250, bh) then
  292.                     UiSound("common/click.ogg")
  293.                     SetValue("gOptionsScale", 1.0, "easeout", 0.25)
  294.                     SetValue("gPlayScale", 0.0, "easein", 0.25)
  295.                 end
  296.             UiPop()
  297.  
  298.             UiTranslate(300, 0)
  299.  
  300.             UiPush()
  301.                 if UiTextButton("About", 250, bh) then
  302.                     UiSound("common/click.ogg")
  303.                     Command("game.startmission", "about", "about.xml")
  304.                     SetValue("gPlayScale", 0.0, "easein", 0.25)
  305.                 end
  306.             UiPop()
  307.                
  308.             UiTranslate(300, 0)
  309.  
  310.             UiPush()
  311.                 if UiTextButton("Quit", 250, bh) then
  312.                     UiSound("common/click.ogg")
  313.                     Command("game.quit")
  314.                     SetValue("gPlayScale", 0.0, "easein", 0.25)
  315.                 end
  316.             UiPop()
  317.         UiPop()
  318.     UiPop()
  319.  
  320.     if gPlayScale > 0 then
  321.         local bw = 200
  322.         local bh = 40
  323.         local bo = 48
  324.         UiPush()
  325.             UiTranslate(672, 160)
  326.             UiScale(1, gPlayScale)
  327.             UiColorFilter(1,1,1,gPlayScale)
  328.             if gPlayScale < 0.5 then
  329.                 UiColorFilter(1,1,1,gPlayScale*2)
  330.             end
  331.             UiColor(0,0,0,0.75)
  332.             UiFont("font/regular.ttf", 26)
  333.             UiImageBox("common/box-solid-10.png", 260, 210, 10, 10)
  334.             UiColor(1,1,1)
  335.             UiButtonImageBox("common/box-outline-6.png", 6, 6, 1, 1, 1)
  336.             UiAlign("top left")
  337.             UiTranslate(25, 25)
  338.  
  339.             if UiTextButton("Campaign", bw, bh) then
  340.                 UiSound("common/click.ogg")
  341.                 startHub()
  342.             end
  343.             UiTranslate(0, bo)
  344.  
  345.             if UiTextButton("Sandbox", bw, bh) then
  346.                 UiSound("common/click.ogg")
  347.                 SetValue("gSandboxScale", 1, "cosine", 0.25)
  348.             end        
  349.             UiTranslate(0, bo)
  350.  
  351.             UiTranslate(0, 20)
  352.             if UiTextButton("Create", bw, bh) then
  353.                 UiSound("common/click.ogg")
  354.                 SetValue("gCreateScale", 1, "cosine", 0.25)
  355.             end        
  356.         UiPop()
  357.     end
  358.     if gSandboxScale > 0 then
  359.         UiPush()
  360.             UiBlur(gSandboxScale)
  361.             UiColor(0.7,0.7,0.7, 0.25*gSandboxScale)
  362.             UiRect(UiWidth(), UiHeight())
  363.             UiModalBegin()
  364.             if not drawSandbox(gSandboxScale) then
  365.                 SetValue("gSandboxScale", 0, "cosine", 0.25)
  366.             end
  367.             UiModalEnd()
  368.         UiPop()
  369.     end
  370.     if gCreateScale > 0 then
  371.         UiPush()
  372.             UiBlur(gCreateScale)
  373.             UiColor(0.7,0.7,0.7, 0.25*gCreateScale)
  374.             UiRect(UiWidth(), UiHeight())
  375.             UiModalBegin()
  376.             if not drawCreate(gCreateScale) then
  377.                 SetValue("gCreateScale", 0, "cosine", 0.25)
  378.             end
  379.             UiModalEnd()
  380.         UiPop()
  381.     end
  382.     if gOptionsScale > 0 then
  383.         UiPush()
  384.             UiBlur(gOptionsScale)
  385.             UiColor(0.7,0.7,0.7, 0.25*gOptionsScale)
  386.             UiRect(UiWidth(), UiHeight())
  387.             UiModalBegin()
  388.             if not drawOptions(gOptionsScale, true) then
  389.                 SetValue("gOptionsScale", 0, "cosine", 0.25)
  390.             end
  391.             UiModalEnd()
  392.         UiPop()
  393.     end
  394. end
  395.  
  396.  
  397. function tick()
  398.     if GetTime() > 0.1 then
  399.         PlayMusic("music/menu.ogg")
  400.     end
  401. end
  402.  
  403.  
  404. function drawBackground()
  405.     UiPush()
  406.         if bgTimer >= 0 then
  407.             bgTimer = bgTimer - GetTimeStep()
  408.             if bgTimer < 0 then
  409.                 old = bgIndex
  410.                 while bgIndex == old do
  411.                     bgIndex = math.mod(bgIndex+1, #slideshowImages-1)
  412.                 end
  413.                 bgTimer = bgInterval
  414.  
  415.                 bgCurrent = 1-bgCurrent
  416.                 bgItems[bgCurrent] = bgLoad(bgIndex)
  417.             end
  418.         end
  419.  
  420.         UiTranslate(UiCenter(), UiMiddle())
  421.         UiAlign("center middle")
  422.         bgDraw(bgItems[1-bgCurrent])
  423.         bgDraw(bgItems[bgCurrent])
  424.     UiPop()
  425. end
  426.  
  427.  
  428. function draw()
  429.     UiButtonHoverColor(0.8,0.8,0.8,1)
  430.  
  431.     UiPush()
  432.         --Create a safe 1920x1080 window that will always be visible on screen
  433.         local x0,y0,x1,y1 = UiSafeMargins()
  434.         UiTranslate(x0,y0)
  435.         UiWindow(x1-x0,y1-y0, true)
  436.  
  437.         drawBackground()
  438.         mainMenu()
  439.        
  440.     UiPop()
  441.  
  442.     if not gDeploy and mainMenuDebug then
  443.         mainMenuDebug()
  444.     end
  445.  
  446.     UiPush()
  447.         UiTranslate(UiWidth()-50, UiHeight()-10)
  448.         UiFont("font/regular.ttf", 18)
  449.         UiColor(1,1,1,0.3)
  450.         UiText(GetString("game.version"))
  451.     UiPop()
  452.  
  453.     if GetBool("game.saveerror") then
  454.         UiPush()
  455.             UiFont("font/bold.ttf", 26)
  456.             UiTextOutline(0, 0, 0, 1, 0.1)
  457.             UiColor(1,.5,.5)
  458.             UiAlign("center")
  459.             UiTranslate(UiCenter(), UiHeight()-200)
  460.             UiWordWrap(800)
  461.             UiText("WARNING! Game failed to save progress and will not function correctly.\n\nThis may be caused by Windows Defender or other security software blocking access to Documents/Teardown in your home folder. Please see homepage FAQ for further details: http://teardowngame.com")
  462.         UiPop()
  463.     end
  464. end
  465.  
  466.  
  467. function handleCommand(cmd)
  468.     if cmd == "resolutionchanged" then
  469.         gOptionsScale = 1
  470.         optionsTab = "display"
  471.     end
  472.     if cmd == "activate" then
  473.         initSlideshow()
  474.         bgTimer = bgInterval
  475.     end
  476. end
  477.  
  478.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement