Advertisement
VGToolBox

Elevator

Dec 1st, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.05 KB | None | 0 0
  1. --Touch pad control panel
  2.  
  3. local SIDES = {"left", "right", "top", "bottom", "front", "back"}
  4.  
  5. local OUTPUT_SIDE = "left"
  6.  
  7. local INPUT_CODE = "1973"
  8.  
  9. local BUTTON_PUSH = 9
  10. local COMBO_SUCCESS = {11,13}
  11. local COMBO_FAIL = {4,5}
  12.  
  13. local codeDoorOpen = false
  14. local CODE_DOOR = {}
  15. CODE_DOOR.CLOSE = colours.black
  16. CODE_DOOR.OPEN = colours.white
  17. CODE_DOOR.PULSES = 4
  18.  
  19. local passDoorOpen = false
  20. local PASS_DOOR = {}
  21. PASS_DOOR.CLOSE = colours.green
  22. PASS_DOOR.OPEN = colours.red
  23. PASS_DOOR.PULSES = 4
  24.  
  25. local colourUp = colours.white
  26. local colourDown = colours.black
  27.  
  28. local drawList = {} --Stores references to everything that needs to be drawn
  29. drawList.invalid = false;
  30.  
  31. local clickList = {} --Stores references to draw objects at each point in its 2D array
  32.  
  33. local outputs = {}
  34.  
  35. local count = 0
  36. local combo = ""
  37.  
  38. local defaultColour = colours.white
  39.  
  40. local currentColour = colours.white
  41.  
  42. local currentFloor = 4
  43. local elevatorImagePos = 0
  44. local floorDistances = 5
  45. local lastFloorMod = 2
  46.  
  47. local shaft = paintutils.loadImage("shaft")
  48. local elevImage = paintutils.loadImage("elevator")
  49. -------------------------------------------------Classes--
  50.  
  51. local function newBox(width, height, colour, label)
  52.    
  53.  
  54.     local function draw(self)
  55.  
  56.         print("in updraw")
  57.         if not self.visible then
  58.             return 0
  59.         end
  60.         print("setBG colour")
  61.  
  62.         for i, output in ipairs(outputs) do
  63.  
  64.             output.setBackgroundColour(self.colour)
  65.  
  66.             local x
  67.             local y
  68.             print("local x and y set")
  69.             --print(self.x)
  70.             --print(self.width)
  71.             --xMax = self.x + (self.width - 1)
  72.  
  73.             print("pre loop")
  74.  
  75.             for x = self.x, self.x + (self.width - 1) do
  76.                 for y = self.y, self.y + (self.height - 1) do
  77.                     print("in loop at "..x.." by "..y)
  78.                     output.setCursorPos(x,y)
  79.                     output.write(" ")
  80.                 end
  81.             end
  82.  
  83.             print("after box draw")
  84.  
  85.             x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
  86.             y = self.y + math.floor(self.height * 0.5)
  87.  
  88.             output.setCursorPos(x,y)
  89.             output.write(self.label)
  90.  
  91.             output.setBackgroundColour(currentColour)
  92.         end
  93.     end
  94.  
  95.     local function invalidate(self)
  96.         if self.parent then
  97.             self.parent.invalid = true
  98.         end
  99.     end
  100.  
  101.     box = {}
  102.     box.x = 0
  103.     box.y = 0
  104.     box.width = width or 1
  105.     box.height = height or 1
  106.     box.visible = true
  107.     box.colour = colour or colours.white
  108.     box.draw = draw
  109.     box.invalidate = invalidate
  110.     box.label = label
  111.     box.type = "Display Object"
  112.  
  113.     return box
  114. end
  115.  
  116. local function newButton(width, height, colour, downColour, label, value)
  117.    
  118.     box = newBox(width, height, colour, label)
  119.  
  120.     local function draw(self)
  121.         if self.downCounter > 0 then
  122.             self:downDraw()
  123.         else
  124.             print("upDraw")
  125.             self:upDraw()
  126.         end
  127.     end
  128.  
  129.     local function downDraw(self)
  130.  
  131.         for i, output in ipairs(outputs) do
  132.  
  133.             output.setBackgroundColour(self.downColour)
  134.             for x = self.x, self.x + (self.width - 1) do
  135.                 for y = self.y, self.y + (self.height - 1) do
  136.                     output.setCursorPos(x,y)
  137.                     output.write(" ")
  138.                 end
  139.             end
  140.  
  141.             x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
  142.             y = self.y + math.floor(self.height * 0.5)
  143.  
  144.             output.setCursorPos(x,y)
  145.             output.write(self.label)
  146.  
  147.             output.setBackgroundColour(currentColour)
  148.         end
  149.  
  150.         self.downCounter = self.downCounter - 1
  151.  
  152.         self:invalidate()
  153.     end
  154.  
  155.     local function click(self)
  156.         self.downCounter = 0
  157.         return value
  158.     end
  159.  
  160.  
  161.  
  162.     box.downColour = downColour or colours.blue
  163.     box.upDraw = box.draw
  164.     box.downDraw = downDraw
  165.     box.draw = draw
  166.     box.type = "Clickable"
  167.     box.downCounter = 0
  168.     box.value = value
  169.     box.click = click
  170.  
  171.     return box
  172. end
  173.  
  174.  
  175. local function getNoteBlock()
  176.     for i, v in ipairs(SIDES) do
  177.         if peripheral.getType(v) == "note" then
  178.             return peripheral.wrap(v)
  179.         end
  180.     end
  181. end
  182.  
  183. local function getMonitors()
  184.     local monitors = {}
  185.     for i, v in ipairs(SIDES) do
  186.         if peripheral.getType(v) == "monitor" then
  187.             table.insert(monitors, peripheral.wrap(v))
  188.         end
  189.     end
  190.  
  191.     return monitors
  192. end
  193.  
  194. local function clearEvents()
  195.     os.queueEvent("clear")
  196.     while true do
  197.         e = os.pullEvent()
  198.         if e == "clear" then
  199.             break
  200.         end
  201.     end
  202. end
  203.  
  204.  
  205. local function pulseRS(side, n, colour)
  206.    
  207.     if colour then
  208.         for i = 1, n do
  209.             rs.setBundledOutput(side, colours.combine(rs.getBundledOutput(side), colour))
  210.             sleep(0.45)
  211.             rs.setBundledOutput(side, colours.subtract(rs.getBundledOutput(side), colour))
  212.             sleep(0.45)
  213.         end
  214.     else
  215.         for i = 1, n do
  216.             rs.setOutput(side, true)
  217.             sleep(0.45)
  218.             rs.setOutput(side, false)
  219.             sleep(0.45)
  220.         end
  221.     end
  222. end
  223.  
  224. local function playNote(notes)
  225.     for i = 1, table.getn(notes) do
  226.         note.playNote(0,notes[i])
  227.         sleep(0.2)
  228.     end
  229. end
  230.  
  231. local function addToClickList(object)
  232.     for x = object.x, object.x + (object.width - 1) do
  233.         clickList[x] = clickList[x] or {}
  234.         for y = object.y, object.y + (object.height - 1) do
  235.             table.insert(clickList[x], y, object)
  236.         end
  237.     end
  238. end
  239.  
  240. local function clearScreen(colour, text)
  241.     print("num of monitors: "..table.getn(outputs))
  242.     for i, v in ipairs(outputs) do
  243.         v.setBackgroundColour(colour)
  244.         v.clear()
  245.         print("monitor"..i)
  246.         if text then
  247.             local x = 3 - math.floor((string.len(text) * 0.5))
  248.             local y = 3
  249.  
  250.             x = x > 0 and x or 1
  251.  
  252.             v.setCursorPos(x,y)
  253.             print("set pos")
  254.             v.write(text)
  255.             print("write String")
  256.         end
  257.         v.setBackgroundColour(defaultColour)
  258.         print("colour back to default")
  259.     end
  260. end
  261.  
  262. local function drawScreen()
  263.     if drawList.invalid then
  264.     --print("in draw")
  265.         drawList.invalid = true
  266.         clearScreen(defaultColour)
  267.  
  268.         for i, v in ipairs(drawList) do
  269.             --print("indraw list loop")
  270.             v:draw()
  271.             --print("objectDone")
  272.         end
  273.     end
  274. end
  275.  
  276. local function getObjectAt(x,y)
  277.     if clickList[x] then
  278.         if clickList[x][y] then
  279.             return clickList[x][y]
  280.         end
  281.     end
  282.     return false
  283. end
  284.  
  285. local function handleClick(x, y)
  286.     print(x)
  287.     print(y)
  288.  
  289.     object = getObjectAt(x, y)
  290.  
  291.     --print("object")
  292.  
  293.     if object and object.type == "Clickable" then
  294.         return object:click()
  295.     end
  296.  
  297. end
  298.  
  299. local function moveElevator(floor)
  300.     local distance = (floor - currentFloor) * floorDistances
  301.     if currentFloor == 0 then
  302.         distance = distance + 1
  303.     elseif floor == 0 then
  304.         distance = distance - 1
  305.     elseif currentFloor == 4 then
  306.         distance = distance - 2
  307.     elseif floor == 4 then
  308.         distance = distance + 2
  309.     end
  310.  
  311.     print("moveing elevator distance = "..distance)
  312.     if distance > 0 then
  313.  
  314.         pulseRS(OUTPUT_SIDE, distance, colourUp)
  315.  
  316.     elseif distance < 0 then
  317.         pulseRS(OUTPUT_SIDE, math.abs(distance), colourDown)
  318.     end
  319.  
  320.     elevatorImagePos = (height - 3) - (floor * 2)
  321.     currentFloor = floor
  322.  
  323. end
  324.  
  325. local function moveDoor(door, open)
  326.     if open then
  327.         pulseRS(OUTPUT_SIDE, door.PULSES, door.OPEN)
  328.     else
  329.         pulseRS(OUTPUT_SIDE, door.PULSES, door.CLOSE)
  330.     end
  331. end
  332.  
  333. local function addToDrawList(object, i)
  334.     i = i or table.getn(drawList) + 1
  335.     table.insert(drawList, i, object)
  336.     object.parent = drawList
  337.     print(object)
  338.     if object.type == "Clickable" then
  339.         addToClickList(object) 
  340.     end
  341.     drawList.invalid = true
  342.  
  343. end
  344.  
  345. local function drawElevator()
  346.     term.redirect(outputs[1])
  347.     paintutils.drawImage(shaft, 9, 1)
  348.     paintutils.drawImage(elevImage, 9, elevatorImagePos)
  349.     term.restore()
  350. end
  351.  
  352.  
  353. local function startingUpScreen()
  354.     clearScreen(colours.yellow, "Booting")
  355. end
  356.  
  357. local function successScreen()
  358.     clearScreen(colours.green, "Welcome")
  359. end
  360.  
  361. local function failScreen()
  362.     clearScreen(colours.red, "Wrong")
  363. end
  364.  
  365. local function openDoors()
  366.     rs.setOutput("bottom", true)
  367. end
  368.  
  369. local function closeDoors()
  370.     rs.setOutput("bottom", false)
  371. end
  372.  
  373. local function main()
  374.  
  375.     outputs = getMonitors()
  376.     note = getNoteBlock()
  377.  
  378.     width, height = outputs[1].getSize()
  379.  
  380.     startingUpScreen()
  381.  
  382.     --reset system just in case
  383.     --moveDoor(PASS_DOOR)
  384.     print("reset Elevator")
  385.     moveElevator(0)
  386.     sleep(1)
  387.  
  388.    
  389.  
  390.     local elevatorImagePos = height - 3
  391.  
  392.     clearScreen(defaultColour)
  393.  
  394.     --(width, height, colour, downColour, label, value)
  395.  
  396.     button1 = newButton(3,2,colours.yellow, colours.green, "G", "0")
  397.     button1.x = 1
  398.     button1.y = 8
  399.  
  400.     button2 = newButton(3,2,colours.yellow, colours.green, "1", "1")
  401.     button2.x = 1
  402.     button2.y = 5
  403.  
  404.     button3 = newButton(3,2,colours.yellow, colours.green, "2", "2")
  405.     button3.x = 5
  406.     button3.y = 5
  407.  
  408.     button4 = newButton(3,2,colours.yellow, colours.green, "3", "3")
  409.     button4.x = 1
  410.     button4.y = 2
  411.  
  412.     button5 = newButton(3,2,colours.yellow, colours.green, "R", "4")
  413.     button5.x = 5
  414.     button5.y = 2
  415.  
  416.     addToDrawList(button1)
  417.     addToDrawList(button2)
  418.     addToDrawList(button3)
  419.     addToDrawList(button4)
  420.     addToDrawList(button5)
  421.  
  422.     local doorTimer
  423.     local eventTimer
  424.  
  425.     while true do
  426.         print("tick")
  427.         drawScreen()
  428.         drawElevator()
  429.         --print("post draw")
  430.  
  431.         clearEvents()
  432.         --print("post clear Events")
  433.         while true do
  434.             --print("timerActivate")
  435.             eventTimer = os.startTimer(6)
  436.  
  437.             e, side, x, y = os.pullEvent()
  438.             if e == "monitor_touch" then
  439.                 floor = handleClick(x, y)
  440.             --  print("click activated")
  441.  
  442.                 if floor then
  443.                     closeDoors()
  444.                     moveElevator(floor)
  445.                     openDoors()
  446.                 end
  447.  
  448.  
  449.             end
  450.  
  451.             drawScreen()
  452.             drawElevator()
  453.  
  454.         --  print("timeOut")
  455.  
  456.         end
  457.     end
  458. end
  459.  
  460. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement