feat(rs): introduce basic redstone library
This commit is contained in:
parent
a4cc3d4c8e
commit
ce90d6c69c
@ -29,6 +29,7 @@ local LIST_CLIENT_FILES = {
|
|||||||
local LIST_CLIENT_CONFIG_FILES = {}
|
local LIST_CLIENT_CONFIG_FILES = {}
|
||||||
|
|
||||||
local LIST_LIBS_FILES = {
|
local LIST_LIBS_FILES = {
|
||||||
|
'libs/rs.lua',
|
||||||
'libs/net.lua',
|
'libs/net.lua',
|
||||||
'libs/utils.lua',
|
'libs/utils.lua',
|
||||||
'libs/turtle-utils.lua',
|
'libs/turtle-utils.lua',
|
||||||
|
|||||||
68
libs/rs.lua
Normal file
68
libs/rs.lua
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
local rs = {}
|
||||||
|
|
||||||
|
rs.listenInput = function(side, onChangeCallback)
|
||||||
|
local state = redstone.getInput(side)
|
||||||
|
|
||||||
|
while true do
|
||||||
|
os.pullEvent('redstone')
|
||||||
|
local newState = redstone.getInput(side)
|
||||||
|
|
||||||
|
if state ~= newState then
|
||||||
|
state = newState
|
||||||
|
onChangeCallback(newState)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
rs.createBundledOutput = function(side, initialColorState)
|
||||||
|
local bundledOutput = {}
|
||||||
|
|
||||||
|
local _colorState = initialColorState or 0
|
||||||
|
|
||||||
|
local function getState()
|
||||||
|
return _colorState
|
||||||
|
end
|
||||||
|
|
||||||
|
local function setState(newColorState)
|
||||||
|
|
||||||
|
if newColorState == _colorState then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
_colorState = newColorState
|
||||||
|
redstone.setBundledOutpout(side, newColorState)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
bundledOutput.getStateColor = getState
|
||||||
|
bundledOutput.setColorState = setState
|
||||||
|
|
||||||
|
bundledOutput.test = function(color)
|
||||||
|
return colors.test(getState(), color)
|
||||||
|
end
|
||||||
|
|
||||||
|
bundledOutput.setOn = function(color)
|
||||||
|
local colorState = getState()
|
||||||
|
local newColorState = color.combine(colorState, color)
|
||||||
|
return setState(newColorState)
|
||||||
|
end
|
||||||
|
|
||||||
|
bundledOutput.setOff = function(color)
|
||||||
|
local colorState = getState()
|
||||||
|
local newColorState = color.subtract(colorState, color)
|
||||||
|
return setState(newColorState)
|
||||||
|
end
|
||||||
|
|
||||||
|
bundledOutput.toggle = function(color)
|
||||||
|
if colors.test(getState(), color) then
|
||||||
|
return bundledOutput.setOff(color)
|
||||||
|
end
|
||||||
|
|
||||||
|
return bundledOutput.setOn(color)
|
||||||
|
end
|
||||||
|
|
||||||
|
return bundledOutput
|
||||||
|
end
|
||||||
|
|
||||||
|
return rs
|
||||||
Loading…
Reference in New Issue
Block a user