File size: 4,993 Bytes
0712d5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | -- 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)
|