Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Touch pad control panel
- local SIDES = {"left", "right", "top", "bottom", "front", "back"}
- local OUTPUT_SIDE = "left"
- local INPUT_CODE = "1973"
- local BUTTON_PUSH = 9
- local COMBO_SUCCESS = {11,13}
- local COMBO_FAIL = {4,5}
- local codeDoorOpen = false
- local CODE_DOOR = {}
- CODE_DOOR.CLOSE = colours.black
- CODE_DOOR.OPEN = colours.white
- CODE_DOOR.PULSES = 4
- local passDoorOpen = false
- local PASS_DOOR = {}
- PASS_DOOR.CLOSE = colours.green
- PASS_DOOR.OPEN = colours.red
- PASS_DOOR.PULSES = 4
- local colourUp = colours.white
- local colourDown = colours.black
- local drawList = {} --Stores references to everything that needs to be drawn
- drawList.invalid = false;
- local clickList = {} --Stores references to draw objects at each point in its 2D array
- local outputs = {}
- local count = 0
- local combo = ""
- local defaultColour = colours.white
- local currentColour = colours.white
- local currentFloor = 4
- local elevatorImagePos = 0
- local floorDistances = 5
- local lastFloorMod = 2
- local shaft = paintutils.loadImage("shaft")
- local elevImage = paintutils.loadImage("elevator")
- -------------------------------------------------Classes--
- local function newBox(width, height, colour, label)
- local function draw(self)
- print("in updraw")
- if not self.visible then
- return 0
- end
- print("setBG colour")
- for i, output in ipairs(outputs) do
- output.setBackgroundColour(self.colour)
- local x
- local y
- print("local x and y set")
- --print(self.x)
- --print(self.width)
- --xMax = self.x + (self.width - 1)
- print("pre loop")
- for x = self.x, self.x + (self.width - 1) do
- for y = self.y, self.y + (self.height - 1) do
- print("in loop at "..x.." by "..y)
- output.setCursorPos(x,y)
- output.write(" ")
- end
- end
- print("after box draw")
- x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
- y = self.y + math.floor(self.height * 0.5)
- output.setCursorPos(x,y)
- output.write(self.label)
- output.setBackgroundColour(currentColour)
- end
- end
- local function invalidate(self)
- if self.parent then
- self.parent.invalid = true
- end
- end
- box = {}
- box.x = 0
- box.y = 0
- box.width = width or 1
- box.height = height or 1
- box.visible = true
- box.colour = colour or colours.white
- box.draw = draw
- box.invalidate = invalidate
- box.label = label
- box.type = "Display Object"
- return box
- end
- local function newButton(width, height, colour, downColour, label, value)
- box = newBox(width, height, colour, label)
- local function draw(self)
- if self.downCounter > 0 then
- self:downDraw()
- else
- print("upDraw")
- self:upDraw()
- end
- end
- local function downDraw(self)
- for i, output in ipairs(outputs) do
- output.setBackgroundColour(self.downColour)
- for x = self.x, self.x + (self.width - 1) do
- for y = self.y, self.y + (self.height - 1) do
- output.setCursorPos(x,y)
- output.write(" ")
- end
- end
- x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
- y = self.y + math.floor(self.height * 0.5)
- output.setCursorPos(x,y)
- output.write(self.label)
- output.setBackgroundColour(currentColour)
- end
- self.downCounter = self.downCounter - 1
- self:invalidate()
- end
- local function click(self)
- self.downCounter = 0
- return value
- end
- box.downColour = downColour or colours.blue
- box.upDraw = box.draw
- box.downDraw = downDraw
- box.draw = draw
- box.type = "Clickable"
- box.downCounter = 0
- box.value = value
- box.click = click
- return box
- end
- local function getNoteBlock()
- for i, v in ipairs(SIDES) do
- if peripheral.getType(v) == "note" then
- return peripheral.wrap(v)
- end
- end
- end
- local function getMonitors()
- local monitors = {}
- for i, v in ipairs(SIDES) do
- if peripheral.getType(v) == "monitor" then
- table.insert(monitors, peripheral.wrap(v))
- end
- end
- return monitors
- end
- local function clearEvents()
- os.queueEvent("clear")
- while true do
- e = os.pullEvent()
- if e == "clear" then
- break
- end
- end
- end
- local function pulseRS(side, n, colour)
- if colour then
- for i = 1, n do
- rs.setBundledOutput(side, colours.combine(rs.getBundledOutput(side), colour))
- sleep(0.45)
- rs.setBundledOutput(side, colours.subtract(rs.getBundledOutput(side), colour))
- sleep(0.45)
- end
- else
- for i = 1, n do
- rs.setOutput(side, true)
- sleep(0.45)
- rs.setOutput(side, false)
- sleep(0.45)
- end
- end
- end
- local function playNote(notes)
- for i = 1, table.getn(notes) do
- note.playNote(0,notes[i])
- sleep(0.2)
- end
- end
- local function addToClickList(object)
- for x = object.x, object.x + (object.width - 1) do
- clickList[x] = clickList[x] or {}
- for y = object.y, object.y + (object.height - 1) do
- table.insert(clickList[x], y, object)
- end
- end
- end
- local function clearScreen(colour, text)
- print("num of monitors: "..table.getn(outputs))
- for i, v in ipairs(outputs) do
- v.setBackgroundColour(colour)
- v.clear()
- print("monitor"..i)
- if text then
- local x = 3 - math.floor((string.len(text) * 0.5))
- local y = 3
- x = x > 0 and x or 1
- v.setCursorPos(x,y)
- print("set pos")
- v.write(text)
- print("write String")
- end
- v.setBackgroundColour(defaultColour)
- print("colour back to default")
- end
- end
- local function drawScreen()
- if drawList.invalid then
- --print("in draw")
- drawList.invalid = true
- clearScreen(defaultColour)
- for i, v in ipairs(drawList) do
- --print("indraw list loop")
- v:draw()
- --print("objectDone")
- end
- end
- end
- local function getObjectAt(x,y)
- if clickList[x] then
- if clickList[x][y] then
- return clickList[x][y]
- end
- end
- return false
- end
- local function handleClick(x, y)
- print(x)
- print(y)
- object = getObjectAt(x, y)
- --print("object")
- if object and object.type == "Clickable" then
- return object:click()
- end
- end
- local function moveElevator(floor)
- local distance = (floor - currentFloor) * floorDistances
- if currentFloor == 0 then
- distance = distance + 1
- elseif floor == 0 then
- distance = distance - 1
- elseif currentFloor == 4 then
- distance = distance - 2
- elseif floor == 4 then
- distance = distance + 2
- end
- print("moveing elevator distance = "..distance)
- if distance > 0 then
- pulseRS(OUTPUT_SIDE, distance, colourUp)
- elseif distance < 0 then
- pulseRS(OUTPUT_SIDE, math.abs(distance), colourDown)
- end
- elevatorImagePos = (height - 3) - (floor * 2)
- currentFloor = floor
- end
- local function moveDoor(door, open)
- if open then
- pulseRS(OUTPUT_SIDE, door.PULSES, door.OPEN)
- else
- pulseRS(OUTPUT_SIDE, door.PULSES, door.CLOSE)
- end
- end
- local function addToDrawList(object, i)
- i = i or table.getn(drawList) + 1
- table.insert(drawList, i, object)
- object.parent = drawList
- print(object)
- if object.type == "Clickable" then
- addToClickList(object)
- end
- drawList.invalid = true
- end
- local function drawElevator()
- term.redirect(outputs[1])
- paintutils.drawImage(shaft, 9, 1)
- paintutils.drawImage(elevImage, 9, elevatorImagePos)
- term.restore()
- end
- local function startingUpScreen()
- clearScreen(colours.yellow, "Booting")
- end
- local function successScreen()
- clearScreen(colours.green, "Welcome")
- end
- local function failScreen()
- clearScreen(colours.red, "Wrong")
- end
- local function openDoors()
- rs.setOutput("bottom", true)
- end
- local function closeDoors()
- rs.setOutput("bottom", false)
- end
- local function main()
- outputs = getMonitors()
- note = getNoteBlock()
- width, height = outputs[1].getSize()
- startingUpScreen()
- --reset system just in case
- --moveDoor(PASS_DOOR)
- print("reset Elevator")
- moveElevator(0)
- sleep(1)
- local elevatorImagePos = height - 3
- clearScreen(defaultColour)
- --(width, height, colour, downColour, label, value)
- button1 = newButton(3,2,colours.yellow, colours.green, "G", "0")
- button1.x = 1
- button1.y = 8
- button2 = newButton(3,2,colours.yellow, colours.green, "1", "1")
- button2.x = 1
- button2.y = 5
- button3 = newButton(3,2,colours.yellow, colours.green, "2", "2")
- button3.x = 5
- button3.y = 5
- button4 = newButton(3,2,colours.yellow, colours.green, "3", "3")
- button4.x = 1
- button4.y = 2
- button5 = newButton(3,2,colours.yellow, colours.green, "R", "4")
- button5.x = 5
- button5.y = 2
- addToDrawList(button1)
- addToDrawList(button2)
- addToDrawList(button3)
- addToDrawList(button4)
- addToDrawList(button5)
- local doorTimer
- local eventTimer
- while true do
- print("tick")
- drawScreen()
- drawElevator()
- --print("post draw")
- clearEvents()
- --print("post clear Events")
- while true do
- --print("timerActivate")
- eventTimer = os.startTimer(6)
- e, side, x, y = os.pullEvent()
- if e == "monitor_touch" then
- floor = handleClick(x, y)
- -- print("click activated")
- if floor then
- closeDoors()
- moveElevator(floor)
- openDoors()
- end
- end
- drawScreen()
- drawElevator()
- -- print("timeOut")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement