Advertisement
natie3

Elevator

Aug 16th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. local button={}
  3. local send = peripheral.wrap("bottom")
  4. mon.setTextScale(1)
  5. mon.setTextColor(colors.white)
  6. mon.setBackgroundColor(colors.black)
  7.  
  8. function setTable(name, func, xmin, xmax, ymin, ymax)
  9.   button[name] = {}
  10.   button[name]["func"] = func
  11.   button[name]["active"] = false
  12.   button[name]["xmin"] = xmin
  13.   button[name]["ymin"] = ymin
  14.   button[name]["xmax"] = xmax
  15.   button[name]["ymax"] = ymax
  16. end
  17.  
  18. function fillTable()
  19.   setTable("Up",elevatorUp,1,7,1,5)
  20.   setTable("Down",elevatorDown,1,7,8,12)
  21. end
  22.  
  23. function fill(text, color, bData)
  24.   mon.setBackgroundColor(color)
  25.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  26.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  27.   for j = bData["ymin"], bData["ymax"] do
  28.     mon.setCursorPos(bData["xmin"], j)
  29.     if j == yspot then
  30.        for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  31.          if k == xspot then
  32.            mon.write(text)
  33.          else
  34.            mon.write(" ")
  35.          end
  36.        end
  37.     else
  38.       for i = bData["xmin"], bData["xmax"] do
  39.         mon.write(" ")
  40.       end
  41.     end
  42.   end
  43.   mon.setBackgroundColor(colors.black)
  44. end
  45.  
  46. function screen()
  47.   local currColor
  48.   for name,data in pairs(button) do
  49.     local on = data["active"]
  50.     if on == true then currColor = colors.lime else currColor = colors.red end
  51.     fill(name, currColor, data)
  52.   end
  53. end
  54.  
  55. function toggleButton(name)
  56.   button[name]["active"] = not button[name]["active"]
  57.   screen()
  58. end
  59.  
  60. function flash(name)
  61.   toggleButton(name)
  62.   sleep(0.15)
  63.   toggleButton(name)
  64. end
  65.  
  66. function checkxy(x, y)
  67.   for name, data in pairs(button) do
  68.     if y>=data["ymin"] and  y <= data["ymax"] then
  69.       if x>=data["xmin"] and x<= data["xmax"] then
  70.         data["func"]()
  71.       end
  72.     end
  73.   end
  74. end
  75.  
  76. function elevatorUp()
  77.   flash("Up")
  78.   send.setFreq(56)
  79.   redstone.setOutput("bottom", true)
  80.   sleep(0.1)
  81.   redstone.setOutput("bottom", false)
  82. end
  83.  
  84. function elevatorDown()
  85.   flash("Down")
  86.   send.setFreq(55)
  87.   redstone.setOutput("bottom", true)
  88.   sleep(0.1)
  89.   redstone.setOutput("bottom", false)
  90. end    
  91.  
  92. fillTable()
  93. while true do
  94.   mon.clear()
  95.   screen(colors.red)
  96.   local e,side,x,y = os.pullEvent("monitor_touch")
  97.   checkxy(x,y)
  98.   sleep(0.1)
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement