Add non-active counter & flip-flop components

This commit is contained in:
2022-06-30 15:17:54 +01:00
parent b7f74ccb1a
commit 96b4176f8b
8 changed files with 121 additions and 30 deletions

View File

@@ -1,6 +1,8 @@
local moduleNames = {
"flipflopcomponent",
-- "activeflipflopcomponent",
"countercomponent",
-- "activecountercomponent",
"reactorcontrollercomponent",
}

View File

@@ -0,0 +1,36 @@
local allItems = {}
Hook.Add("item.created", "activecountercomponent.init", function(item)
if item.Prefab.Identifier == "activecountercomponent" then
allItems[item] = true
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = this.Components[2]
if signal.value == "" then return end
local input = tonumber(signal.value) or 1
if input > 0 then
if tonumber(mem.value) >= this.Components[3].clampMax then
mem.value = tostring(this.Components[3].clampMin)
else
mem.value = tostring(tonumber(mem.value) + 1)
end
elseif input == 0 then
mem.value = "0"
elseif input < 0 then
mem.value = tostring(-input)
end
end
local think = function()
for component, _ in pairs(allItems) do
component.SendSignal(component.Components[2].value, "state_out")
end
end
Hook.Add("think", "activecountercomponent.think", think)
Hook.Add("signalreceived.activecountercomponent", "activecountercomponent.signalReceived", signalReceived)

View File

@@ -0,0 +1,32 @@
local allItems = {}
Hook.Add("item.created", "activeflipflopcomponent.init", function(item)
if item.Prefab.Identifier == "activeflipflopcomponent" then
allItems[item] = true
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = this.Components[2]
if signal.value == "" then return end
local input = tonumber(signal.value) or 1
if input == 1 then
if mem.value == "0" then mem.value = "1" else mem.value = "0" end
elseif input == 0 then
mem.value = "0"
elseif input == -1 then
mem.value = "1"
end
end
local think = function()
for component, _ in pairs(allItems) do
component.SendSignal(component.Components[2].value, "state_out")
end
end
Hook.Add("think", "activeflipflopcomponent.think", think)
Hook.Add("signalreceived.activeflipflopcomponent", "activeflipflopcomponent.signalReceived", signalReceived)

View File

@@ -1,11 +1,3 @@
local allItems = {}
Hook.Add("item.created", "countercomponent.init", function(item)
if item.Prefab.Identifier == "countercomponent" then
allItems[item] = true
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = this.Components[2]
@@ -24,13 +16,8 @@ local signalReceived = function(signal, connection)
elseif input < 0 then
mem.value = tostring(-input)
end
this.SendSignal(mem.value, "state_out")
end
local think = function()
for component, _ in pairs(allItems) do
component.SendSignal(component.Components[2].value, "state_out")
end
end
Hook.Add("think", "countercomponent.think", think)
Hook.Add("signalreceived.countercomponent", "countercomponent.signalReceived", signalReceived)

View File

@@ -1,11 +1,3 @@
local allItems = {}
Hook.Add("item.created", "flipflopcomponent.init", function(item)
if item.Prefab.Identifier == "flipflopcomponent" then
allItems[item] = true
end
end)
local signalReceived = function(signal, connection)
local this = connection.Item
local mem = this.Components[2]
@@ -20,13 +12,8 @@ local signalReceived = function(signal, connection)
elseif input == -1 then
mem.value = "1"
end
this.sendSignal(mem.value, "state_out")
end
local think = function()
for component, _ in pairs(allItems) do
component.SendSignal(component.Components[2].value, "state_out")
end
end
Hook.Add("think", "flipflopcomponent.think", think)
Hook.Add("signalreceived.flipflopcomponent", "flipflopcomponent.signalReceived", signalReceived)