TimberWoods / src /ServerScriptService /WireLogicManager.server.lua
algorembrant's picture
Upload 88 files
0712d5f verified
-- src/ServerScriptService/WireLogicManager.server.lua
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WireLogicConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("WireLogicConfig"))
local WireConnectEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("WireConnectEvent")
-- Store connections globally
local connections = {} -- [sourcePart] = {targetPart1, targetPart2...}
local componentStates = {} -- [part] = true/false (Power state)
-- Update the state of a specific component
local function updateState(part, newState)
if componentStates[part] == newState then return end
componentStates[part] = newState
-- Visual changes mapping
if newState then
if part:IsA("BasePart") then
-- Optional visual effect (glow)
part.Material = Enum.Material.Neon
end
else
if part:IsA("BasePart") then
part.Material = Enum.Material.SmoothPlastic
end
end
-- Trigger logic if it's a Target component
local compType = part:GetAttribute("ComponentType")
if compType == "Target" then
local targetFunc = part:GetAttribute("TargetFunction")
if targetFunc == "ToggleDoor" then
-- Basic door tweening logic here
-- if newState then openDoor() else closeDoor() end
print("Door toggled to " .. tostring(newState))
elseif targetFunc == "ToggleConveyor" then
-- Turn on/off conveyor velocity
local conveyorVelocity = part:GetAttribute("ConveyorSpeed") or 5
part.AssemblyLinearVelocity = newState and (part.CFrame.LookVector * conveyorVelocity) or Vector3.new()
end
end
-- Propagate signal to connected children
local targets = connections[part]
if targets then
for _, targetPart in pairs(targets) do
-- Basic signal pass. Real implementation requires Logic Gate (AND/OR) processing here.
updateState(targetPart, newState)
end
end
end
-- Hook up interactions for Source components (Buttons, Levers)
local function bindSource(sourcePart)
local sourceType = sourcePart:GetAttribute("SourceType")
if sourceType == "Button" then
-- We'll use a ProximityPrompt for interaction
local prompt = sourcePart:FindFirstChild("ProximityPrompt")
if not prompt then
prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Press"
prompt.Parent = sourcePart
end
prompt.Triggered:Connect(function(player)
-- Buttons pulse signal for 1 second
updateState(sourcePart, true)
task.delay(1, function()
updateState(sourcePart, false)
end)
end)
elseif sourceType == "Lever" then
local prompt = sourcePart:FindFirstChild("ProximityPrompt")
if not prompt then
prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Toggle"
prompt.Parent = sourcePart
end
prompt.Triggered:Connect(function(player)
-- Levers toggle exact state
local currentState = componentStates[sourcePart] or false
updateState(sourcePart, not currentState)
end)
end
end
-- Initialize Sources
CollectionService:GetInstanceAddedSignal("LogicSource"):Connect(bindSource)
for _, src in pairs(CollectionService:GetTagged("LogicSource")) do
bindSource(src)
end
-- Handle Players wiring things together
WireConnectEvent.OnServerEvent:Connect(function(player, sourcePart, targetPart)
-- Security checks
if not sourcePart or not targetPart then return end
-- Distance logic validation
if (sourcePart.Position - targetPart.Position).Magnitude > WireLogicConfig.MaxWireLength then
print("Wire too long!")
return
end
-- Register connection
if not connections[sourcePart] then
connections[sourcePart] = {}
end
table.insert(connections[sourcePart], targetPart)
-- Draw physical wire (Visual) using Beam or RopeConstraint
local attachment0 = sourcePart:FindFirstChild("WireAtt") or Instance.new("Attachment", sourcePart)
attachment0.Name = "WireAtt"
local attachment1 = targetPart:FindFirstChild("WireAtt") or Instance.new("Attachment", targetPart)
attachment1.Name = "WireAtt"
local beam = Instance.new("Beam")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
beam.FaceCamera = true
beam.Width0 = 0.1
beam.Width1 = 0.1
beam.Color = ColorSequence.new(Color3.new(0,0,0))
beam.Parent = sourcePart
print("Wire connected from", sourcePart.Name, "to", targetPart.Name)
end)