84 lines
3.7 KiB
Luau
84 lines
3.7 KiB
Luau
-- GarlandTwinkle (LocalScript, StarterPlayerScripts): makes every garland in the world blink like a real one.
|
|
-- A garland = any parts named "Bulb" with an "Index" attribute (order along the wire): the big tree in the centre
|
|
-- of the map (workspace.CenterTree.Garland) and the players' tree garlands (ToyModels.BuildGarland).
|
|
-- Runs only on the client (no network traffic). The time comes from the server clock, so every player sees the
|
|
-- same pattern at the same moment. Programs change every MODE_TIME seconds, like a garland controller:
|
|
-- alternate - two groups of bulbs take turns (odd / even)
|
|
-- chase - running lights (3 groups), direction flips each time
|
|
-- breathe - the two groups slowly fade in and out in turn
|
|
-- twinkle - random bulbs blink off for a moment
|
|
-- wave - a soft wave of light runs along the wire
|
|
-- Bulbs fade like incandescent lamps (not instant on/off). Far garlands (> FAR studs from the camera) are skipped.
|
|
-- Only the bulbs themselves blink: their PointLights (the glow on the needles) stay steady, otherwise the whole
|
|
-- tree would get lighter / darker with every pattern step (all lit bulbs are every 3rd = one chase group).
|
|
local RunService = game:GetService("RunService")
|
|
|
|
local MODES = { "alternate", "chase", "breathe", "twinkle", "wave" }
|
|
local MODE_TIME = 9
|
|
local FAR = 320
|
|
local DIM = 0.1 -- brightness of an "off" bulb (0..1)
|
|
local DARK = Color3.fromRGB(22, 22, 28)
|
|
|
|
local bulbs = {} -- [part] = { orig, index, cur }
|
|
local function add(p)
|
|
if bulbs[p] or not p:IsA("BasePart") or p.Name ~= "Bulb" then return end
|
|
local index = p:GetAttribute("Index")
|
|
if not index then return end
|
|
bulbs[p] = { orig = p.Color, index = index, cur = 1 }
|
|
end
|
|
for _, d in workspace:GetDescendants() do add(d) end
|
|
workspace.DescendantAdded:Connect(function(d)
|
|
if d.Name == "Bulb" then task.defer(add, d) end -- attributes are set right after creation
|
|
end)
|
|
workspace.DescendantRemoving:Connect(function(d) bulbs[d] = nil end)
|
|
|
|
local function hash(a, b) return (math.sin(a * 12.9898 + b * 78.233) * 43758.5453) % 1 end
|
|
|
|
-- target brightness 0..1 of bulb number i at time t for the given mode
|
|
local function target(mode, i, t, cycle)
|
|
if mode == "alternate" then
|
|
local phase = math.floor(t / 0.6) % 2
|
|
return (i % 2 == phase) and 1 or DIM
|
|
elseif mode == "chase" then
|
|
local step = math.floor(t / 0.2)
|
|
if cycle % 2 == 1 then step = -step end
|
|
return (i % 3 == step % 3) and 1 or DIM
|
|
elseif mode == "breathe" then
|
|
local s = 0.5 + 0.5 * math.sin(t * 1.8 + (i % 2) * math.pi)
|
|
return DIM + (1 - DIM) * s * s
|
|
elseif mode == "twinkle" then
|
|
local slot = math.floor(t / 0.35)
|
|
return hash(i, slot) < 0.22 and DIM or 1
|
|
else -- wave
|
|
local s = 0.5 + 0.5 * math.sin(t * 5 - i * 0.45)
|
|
return DIM + (1 - DIM) * s ^ 3
|
|
end
|
|
end
|
|
|
|
local acc = 0
|
|
RunService.Heartbeat:Connect(function(dt)
|
|
acc += dt
|
|
if acc < 1 / 30 then return end
|
|
local step = acc
|
|
acc = 0
|
|
local cam = workspace.CurrentCamera
|
|
local camPos = cam and cam.CFrame.Position or Vector3.zero
|
|
local t = workspace:GetServerTimeNow()
|
|
local cycle = math.floor(t / MODE_TIME)
|
|
local mode = MODES[cycle % #MODES + 1]
|
|
local intoMode = t - cycle * MODE_TIME
|
|
local k = math.min(1, step * 16) -- incandescent-like fade speed
|
|
for p, b in bulbs do
|
|
if (p.Position - camPos).Magnitude <= FAR then
|
|
local want = target(mode, b.index, t, cycle)
|
|
if intoMode < 0.5 then want = math.max(want, 1 - intoMode * 2) end -- short all-on flash between programs
|
|
local cur = b.cur + (want - b.cur) * k
|
|
if math.abs(cur - b.cur) > 0.004 then
|
|
b.cur = cur
|
|
p.Color = DARK:Lerp(b.orig, 0.25 + 0.75 * cur)
|
|
p.Material = cur > 0.3 and Enum.Material.Neon or Enum.Material.SmoothPlastic
|
|
end
|
|
end
|
|
end
|
|
end)
|