Advertisement
Guest User

wiring

a guest
Aug 1st, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. -- API designed for implementing more advanced colored wire functions.
  2. -- v1.1.1
  3.  
  4. --------------------------------------------------
  5.  
  6. WireTable = {}
  7.  
  8.   WireTable.register = function (self, side, color, name)
  9.     if self[name] then
  10.       print('Wire name already taken.')
  11.       return false
  12.     else
  13.       self[name] = {}
  14.         self[name].side, self[name].color = side, color
  15.         self[name].on = function () rs.setBundledOutput(self[name].side, colors.combine(rs.getBundledOutput(self[name].side), self[name].color)) return true end
  16.         self[name].off = function () rs.setBundledOutput(self[name].side, colors.subtract(rs.getBundledOutput(self[name].side), self[name].color)) return true end
  17.         self[name].switch = function () if rs.testBundledInput(self[name].side, self[name].color) then self[name].off() else self[name].on() end return true end
  18.       return true
  19.     end
  20.   end
  21.  
  22.   WireTable.unregister = function (self, name)
  23.     if not self[name] then
  24.       print('This wire doesn\'t exist')
  25.       return false
  26.     else
  27.       self[name] = nil
  28.       return true
  29.     end
  30.   end
  31.  
  32.   WireTable.callOnAll = function (self, wireMethod)
  33.     for _, wireTable in pairs(self) do
  34.       wireTable[wireMethod]()
  35.     end
  36.     return true
  37.   end
  38.  
  39.   WireTable.onAll = function(self)
  40.     return self:callOnAll('on')
  41.   end
  42.  
  43.   WireTable.offAll = function(self)
  44.     return self:callOnAll('off')
  45.   end
  46.  
  47.   WireTable.switchAll = function(self)
  48.     return self:callOnAll('switch')
  49.   end
  50.  
  51.   WireTable.new = function ()
  52.     newTable = setmetatable({}, WireTable)
  53.     return newTable
  54.   end
  55.  
  56. WireTable.__index = WireTable
  57. setmetatable(WireTable, {__call = WireTable.new})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement