Neraphi

Elevator

Jan 4th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. -- Local Variables
  2. local sItem = 1
  3. local cFloor = 1
  4. local inFloorMenu = true
  5. local w, h = term.getSize()
  6.  
  7.  
  8.  
  9. -- Text Functions
  10. function printCentered( text ) -- Centers text on terminal screen
  11.     term.clear()
  12.     term.setCursorPos(w/2-#text/2, h/2)
  13.     print (text)
  14.     sleep(1)
  15. end
  16.  
  17. -- Handler Functions
  18. function s1() -- Sends rednet message to the control computer
  19.     rednet.open("back") -- Opens the wireless modem
  20.     rednet.send(2, "1", true) -- Sends the message
  21.     rednet.close("back") -- Closes the modem
  22.     cFloor = 1 -- Rewrites the current floor variable for later use
  23. end
  24.  
  25. function s2() -- Same as above
  26.     rednet.open("back")
  27.     rednet.send(2, "2", true)
  28.     rednet.close("back")
  29.     cFloor = 2
  30. end
  31.  
  32. function s3() -- Same as above
  33.     rednet.open("back")
  34.     rednet.send(2, "3", true)
  35.     rednet.close("back")
  36.     cFloor = 3
  37. end
  38. --[[ Menu Table
  39. Table containing menu text and the the corresponding functions
  40. ]]--
  41. floorMenu = {
  42. [1] = { text = "1st Floor", handler = s1 },
  43. [2] = { text = "2nd Floor", handler = s2 },
  44. [3] = { text = "3rd Floor", handler = s3 }
  45. }
  46.  
  47. -- Draw Menu
  48.  
  49. function printMenu( menu ) -- Displays the menu with >> << markers to select floors with.
  50.     for i=1,#menu do
  51.         term.setCursorPos(w/2-#menu[i].text/2-2, h/2-1+i) -- centers the menu
  52.         if i == sItem then
  53.             print (">> "..menu[i].text.." <<") -- Displays markers around the selected floor
  54.         else
  55.             print ("   "..menu[i].text) -- Displays the other floors
  56.         end
  57.     end
  58. end
  59. -- Handler
  60.  
  61. function onKey( key, menu ) -- Finds out which key was pressed
  62.     if key == keys.enter then -- starts the onItem function
  63.         onItem(menu)
  64.     elseif key == keys.up then -- Moves up in the menu, ie. from floor 3 to 2
  65.         if sItem > 1 then -- Unless it's already at the top
  66.             sItem = sItem - 1
  67.         end
  68.     elseif key == keys.down then -- Moves down in the menu
  69.         if sItem < #menu then -- Unless it's already on the last item in the menu
  70.             sItem = sItem + 1
  71.         end
  72.     end
  73. end
  74.  
  75. function onItem( menu ) -- starts the menu function(s1, s2 and s3 dependent on which was selected)
  76.     menu[sItem].handler()
  77. end
  78.  
  79. -- Run Program
  80.  
  81. function runProgram() -- Main function which starts the program
  82.     while inFloorMenu do -- Runs a loop as long as the inFloorMenu variable is true
  83.         term.clear()
  84.         term.setCursorPos(1,1)
  85.         printMenu(floorMenu) -- calls the print menu function for the floor menu specifically.
  86.        
  87.         event, key = os.pullEvent("key") -- Checks whether a key has been pressed
  88.         onKey(key,floorMenu) -- Runs the onKey function for the floor menu.
  89.     end
  90. end
  91.  
  92. runProgram()
Advertisement
Add Comment
Please, Sign In to add comment