Advertisement
Guest User

Computercraft Elevator

a guest
Aug 4th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. --[[ Local variables ]]
  2.  
  3. local termWidth, termHeight = term.getSize()
  4. local selectedItem = 1
  5. local a = 0 -- Current floor in block height
  6. local b = 0 -- Selected floor in block height
  7. local c = 0 -- Floor block height difference
  8. local d = 0 -- Absolute value of c for timer loop
  9. local running = true
  10.  
  11. --[[ Menu Methods ]]
  12.  
  13. function e() -- Math / Timer Function
  14.   if rs.getBundledOutput("back",colors.white)==true then
  15.     a = 0
  16.   elseif rs.getBundledOutput("back",colors.black)== true then
  17.     a = -5
  18.   elseif rs.getBundledOutput("back",colors.red)== true then
  19.     a = -10
  20.   elseif rs.getBundledOutput("back",colors.green)== true then
  21.     a = -15
  22.   end -- Value of a is negative to
  23.  
  24.   c = a + b
  25.   d = math.abs(c)
  26.  
  27.   if c > 0 then -- Timer loop to go down
  28.     for x=1, d do
  29.       rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"),colors.blue))
  30.       sleep(.2)
  31.       rs.setBundledOutput("back",colors.subtract(rs.getBundledOutput("back"),colors.blue))
  32.       sleep(.2)
  33.     end
  34.   elseif c < 0 then -- Timer loop to go up
  35.     for x=1, d do
  36.       rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"),colors.gray))
  37.       sleep(.2)
  38.       rs.setBundledOutput("back",colors.subtract(rs.getBundledOutput("back"),colors.gray))
  39.       sleep(.2)
  40.     end
  41.   elseif c == 0 then
  42.     return
  43.   end
  44. end
  45.  
  46. function C1() -- Floor selection to give wanted height variable
  47.   if rs.getBundledOutput("back",colors.white)==true then
  48.     return
  49.   else
  50.     b = 0
  51.     e(b)
  52.   end
  53. end
  54.  
  55. function C2()
  56.   if rs.getBundledOutput("back",colors.black)==true then
  57.     return
  58.   else
  59.     b = 5
  60.     e(b)
  61.   end
  62. end
  63.  
  64. function C3()
  65.   if rs.getBundledOutput("back",colors.red)==true then
  66.     return
  67.   else
  68.     b = 10
  69.     e(b)
  70.   end
  71. end
  72.  
  73. function C4()
  74.   if rs.getBundledOutput("back",colors.green)==true then
  75.     return
  76.   else
  77.     b = 15
  78.     e(b)
  79.   end
  80. end
  81.  
  82. --[[ Menu Definitions ]]
  83.  
  84. mainMenu = {
  85.   [1] = { text = "1st Floor", handler = C1},
  86.   [2] = { text = "2nd Floor", handler = C2},
  87.   [3] = { text = "3rd Floor", handler = C3},
  88.   [4] = { text = "4th Floor", handler = C4}
  89. }
  90.  
  91. --[[ Printing Methods ]]
  92.  
  93. function printMenu( menu )
  94.   term.clear()
  95.   term.setCursorPos(1,1)
  96.   print("-=Please Select a Floor=-")
  97.   term.setCursorPos(1,2)
  98.   for i=1,#menu do
  99.     if i == selectedItem then
  100.       print(">> "..menu[i].text)
  101.     else
  102.       print("   "..menu[i].text)
  103.     end
  104.   end
  105. end
  106.  
  107. --[[ Handler method ]]
  108.  
  109. function onKeyPressed(key, menu)
  110.   if key == 28 then
  111.     onItemSelected(menu)
  112.   elseif key == 200 then
  113.     if selectedItem > 1 then
  114.       selectedItem = selectedItem - 1
  115.     end
  116.   elseif key == 208 then
  117.     if selectedItem < #menu then
  118.       selectedItem = selectedItem + 1
  119.     end
  120.   end
  121. end
  122.  
  123. function onItemSelected( menu )
  124.   menu[selectedItem].handler()
  125. end
  126.  
  127. --[[ Main method ]]
  128.  
  129. function main()
  130.     while running do
  131.       printMenu(mainMenu)
  132.       event, key = os.pullEvent("key")
  133.       onKeyPressed(key,mainMenu)
  134.       e()
  135.     end
  136.   end
  137. end
  138. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement