Advertisement
CaptainResu

elevator_gui

Dec 25th, 2022 (edited)
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. local monitor = peripheral.find("monitor");
  2. local bg1 = colors.gray;
  3. local bg2 = colors.lightGray;
  4. local bgclick = colors.red;
  5. local bgsuccess = colors.green;
  6. -- term.redirect(monitor);
  7.  
  8.  
  9. peripheral.find("modem", rednet.open);
  10. function gethost()
  11.     while true do
  12.         local tmphost = rednet.lookup("elevator", "elevator.api");
  13.  
  14.         if (type(tmphost) == "number") then
  15.             return tmphost;
  16.         end
  17.     end
  18. end
  19.  
  20. local host = gethost();
  21. print("start");
  22. function iam()
  23.     -- host = rednet.lookup("elevator", "elevator.api");
  24.     rednet.send(host, "iamelevator");
  25. end
  26. iam();
  27.  
  28. monitor.setCursorPos(1, 1);
  29. monitor.setTextScale(0.5, 0.5);
  30. monitor.setBackgroundColor(colors.gray);
  31. monitor.clear();
  32.  
  33. -- FLOOR CLASS
  34. Floor = { name = "floor", click = "none", x = 0, y = 0, width = 5,  };
  35.  
  36. function Floor:new (o, name, click, x, y)
  37.     o = o or {}
  38.     setmetatable(o, {__index = self});
  39.     -- o.__index = self;
  40.     o.name = name or "floor";
  41.     o.click = click or "none";
  42.     o.x = x or 0;
  43.     o.y = y or 0;
  44.  
  45.     o.width = string.len(name);
  46.  
  47.     return o;
  48. end
  49. function Floor:render(col)
  50.     monitor.setBackgroundColor(col);
  51.     monitor.setCursorPos(self.x, self.y);
  52.     monitor.write(self.name, colors.white, col);
  53. end
  54. function Floor:clicked (x, y)
  55.     return (x >= self.x and x <= (self.x + self.width) and y == self.y);
  56. end
  57. -- FLOOR CLASS
  58.  
  59. local floors = {};
  60. floors[1] = Floor:new(nil, "top", "requesttop", 1, 1);
  61. floors[2] = Floor:new(nil, "engine", "requestpower", 10, 1);
  62. floors[3] = Floor:new(nil, "-", "elevatorstop", 1, 3);
  63. floors[4] = Floor:new(nil, "+", "elevatorstart", 10,3);
  64. floors[5] = Floor:new(nil, "/\\", "elevatorup", 1, 5);
  65. floors[6] = Floor:new(nil, "\\/", "elevatordown", 10, 5);
  66. floors[7] = Floor:new(nil, "<|>", "dooropen", 1, 7);
  67. floors[8] = Floor:new(nil, ">|<", "doorclose", 10, 7);
  68.  
  69.  
  70. for it, val in pairs(floors) do
  71.     print("it: " .. tostring(it));
  72.     val:render(bg2);
  73. end
  74.  
  75.  
  76. while true do
  77.     iam();
  78.     event, side, xPos, yPos = os.pullEvent( "monitor_touch" );
  79.  
  80.     monitor.setBackgroundColor(bg1);
  81.     monitor.clear();
  82.     for it, val in pairs(floors) do
  83.         val:render(bg2);
  84.         if val:clicked(xPos, yPos) then
  85.             -- rednet.send(7, "dooropen");
  86.             rednet.send(host, val.click);
  87.             val:render(bgclick);
  88.         end
  89.     end
  90.  
  91.  
  92.     -- monitor.clear();
  93.     -- monitor.setCursorPos(1, 1);
  94.     -- monitor.write("Clicked: " .. tostring(objects[1]:clicked(xPos, yPos)));
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement