Repository navigation
Expand file tree
/
Copy pathCaptionSystem.luau
More file actions
694 lines (599 loc) · 19.1 KB
/
Copy pathCaptionSystem.luau
File metadata and controls
694 lines (599 loc) · 19.1 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
local CaptionSystem = {}
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Text = require(ReplicatedStorage:WaitForChild("Text"))
local VCR_Font = require(ReplicatedStorage.Text.CustomFonts.VCR_OSD_MONO)
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local CAPTION_DURATION = 5
local FADE_TIME = 0.3
local EMPHASIS_SHAKE_INTENSITY = 1.5
local SHAKE_SPEED = 8
local JITTER_SPEED = 15
local WAVE_SPEED = 4
local scaryWords = {
"hopefully", "wrong", "terminated", "strikes", "need it", "everywhere", "before",
"trust me", "exactly", "pay attention", "five strikes", "one minute"
}
local function getShakeType(text)
local lowerText = string.lower(text)
for _, scaryWord in ipairs(scaryWords) do
if string.find(lowerText, string.lower(scaryWord), 1, true) then
return "jitter"
end
end
return "wave"
end
local captionGui = Instance.new("ScreenGui")
captionGui.Name = "CaptionSystem"
captionGui.ResetOnSpawn = false
captionGui.IgnoreGuiInset = true
captionGui.ZIndexBehavior = Enum.ZIndexBehavior.Global
captionGui.DisplayOrder = 600
captionGui.Parent = playerGui
local captionContainer = Instance.new("Frame")
captionContainer.Name = "CaptionContainer"
captionContainer.Size = UDim2.new(0.7, 0, 0, 80)
captionContainer.Position = UDim2.new(0.5, 0, 1.1, 0)
captionContainer.AnchorPoint = Vector2.new(0.5, 0.5)
captionContainer.BackgroundTransparency = 1
captionContainer.ZIndex = 999999999
captionContainer.Parent = captionGui
local BACKDROP_TRANSPARENCY = 0.5
local BACKDROP_STROKE_TRANSPARENCY = 0.65
local captionBackdrop = Instance.new("Frame")
captionBackdrop.Name = "Backdrop"
captionBackdrop.BackgroundColor3 = Color3.fromRGB(8, 10, 16)
captionBackdrop.BackgroundTransparency = 1
captionBackdrop.BorderSizePixel = 0
captionBackdrop.ZIndex = 0
captionBackdrop.Visible = false
captionBackdrop.Parent = captionContainer
local backdropCorner = Instance.new("UICorner")
backdropCorner.CornerRadius = UDim.new(0, 10)
backdropCorner.Parent = captionBackdrop
local backdropStroke = Instance.new("UIStroke")
backdropStroke.Color = Color3.fromRGB(0, 0, 0)
backdropStroke.Thickness = 1
backdropStroke.Transparency = 1
backdropStroke.Parent = captionBackdrop
local activeCharacters = {}
local shakeConnection = nil
local currentSound = nil
local currentCaptionId = 0
local skipCaptionActive = false
local globalShakeIntensity = 0
local renderScale = 1
local centerMode = false
local showTarget = UDim2.new(0.5, 0, 0.9, 0)
local hideTarget = UDim2.new(0.5, 0, 1.1, 0)
local function parseCaption(text)
local segments = {}
local currentPos = 1
while currentPos <= #text do
local emphasisStart = string.find(text, "<emphasis>", currentPos, true)
if emphasisStart then
if emphasisStart > currentPos then
table.insert(segments, {
text = string.sub(text, currentPos, emphasisStart - 1),
emphasized = false
})
end
local emphasisEnd = string.find(text, "</emphasis>", emphasisStart, true)
if emphasisEnd then
local emphasizedText = string.sub(text, emphasisStart + 10, emphasisEnd - 1)
table.insert(segments, {
text = emphasizedText,
emphasized = true,
shakeType = getShakeType(emphasizedText)
})
currentPos = emphasisEnd + 11
else
table.insert(segments, {
text = string.sub(text, currentPos),
emphasized = false
})
break
end
else
table.insert(segments, {
text = string.sub(text, currentPos),
emphasized = false
})
break
end
end
return segments
end
local function createVCRText(text, emphasized)
local textFrame = Instance.new("Frame")
textFrame.BackgroundTransparency = 1
textFrame.Parent = captionContainer
local color = emphasized and Color3.fromRGB(255, 220, 100) or Color3.fromRGB(255, 255, 255)
Text.Create(
textFrame,
text,
{
Font = VCR_Font,
Size = math.floor(16 * renderScale + 0.5),
Color = color,
Transparency = 1,
XAlignment = "Left",
YAlignment = "Top",
Dynamic = false
}
)
return textFrame
end
local function createCharacterLabels(segments)
for _, charData in pairs(activeCharacters) do
if charData.frame then
charData.frame:Destroy()
end
end
activeCharacters = {}
local charWidth = 12 * renderScale
local maxLineWidth = captionContainer.AbsoluteSize.X - 40
local lines = {{}}
local currentLine = 1
local currentX = 0
local currentY = 0
local lineHeight = 20 * renderScale
local allChars = {}
for _, segment in ipairs(segments) do
for i = 1, #segment.text do
local char = string.sub(segment.text, i, i)
table.insert(allChars, {
char = char,
emphasized = segment.emphasized,
shakeType = segment.shakeType or "wave"
})
end
end
local currentWordChars = {}
local currentWordWidth = 0
for charIndex, charData in ipairs(allChars) do
local char = charData.char
if char == " " then
if #currentWordChars > 0 then
if currentX + currentWordWidth > maxLineWidth and currentX > 0 then
currentX = 0
currentY = currentY + lineHeight
currentLine = currentLine + 1
lines[currentLine] = {}
end
for _, wordCharData in ipairs(currentWordChars) do
local charFrame = createVCRText(wordCharData.char, wordCharData.emphasized)
charFrame.Size = UDim2.new(0, charWidth + 2, 0, lineHeight)
charFrame.Position = UDim2.new(0, currentX, 0, currentY)
charFrame.ZIndex = 999999999
table.insert(activeCharacters, {
frame = charFrame,
char = wordCharData.char,
baseX = currentX,
baseY = currentY,
emphasized = wordCharData.emphasized,
shakeType = wordCharData.shakeType,
phase = (math.random() * math.pi * 2) + (#activeCharacters * 0.1),
line = currentLine,
charWidth = charWidth,
lineHeight = lineHeight
})
table.insert(lines[currentLine], {
frame = charFrame,
baseX = currentX,
index = #activeCharacters
})
currentX = currentX + charWidth + 2
end
currentWordChars = {}
currentWordWidth = 0
end
local spaceFrame = createVCRText(" ", false)
spaceFrame.Size = UDim2.new(0, charWidth + 2, 0, lineHeight)
spaceFrame.Position = UDim2.new(0, currentX, 0, currentY)
spaceFrame.ZIndex = 999999999
table.insert(activeCharacters, {
frame = spaceFrame,
char = " ",
baseX = currentX,
baseY = currentY,
emphasized = false,
phase = (math.random() * math.pi * 2) + (#activeCharacters * 0.1),
line = currentLine,
charWidth = charWidth,
lineHeight = lineHeight
})
table.insert(lines[currentLine], {
frame = spaceFrame,
baseX = currentX,
index = #activeCharacters
})
currentX = currentX + charWidth + 2
else
table.insert(currentWordChars, charData)
currentWordWidth = currentWordWidth + charWidth + 2
end
end
if #currentWordChars > 0 then
if currentX + currentWordWidth > maxLineWidth and currentX > 0 then
currentX = 0
currentY = currentY + lineHeight
currentLine = currentLine + 1
lines[currentLine] = {}
end
for _, wordCharData in ipairs(currentWordChars) do
local charFrame = createVCRText(wordCharData.char, wordCharData.emphasized)
charFrame.Size = UDim2.new(0, charWidth + 2, 0, lineHeight)
charFrame.Position = UDim2.new(0, currentX, 0, currentY)
charFrame.ZIndex = 999999999
table.insert(activeCharacters, {
frame = charFrame,
char = wordCharData.char,
baseX = currentX,
baseY = currentY,
emphasized = wordCharData.emphasized,
shakeType = wordCharData.shakeType,
phase = (math.random() * math.pi * 2) + (#activeCharacters * 0.1),
line = currentLine,
charWidth = charWidth,
lineHeight = lineHeight
})
table.insert(lines[currentLine], {
frame = charFrame,
baseX = currentX,
index = #activeCharacters
})
currentX = currentX + charWidth + 2
end
end
for lineNum, lineChars in ipairs(lines) do
if #lineChars > 0 then
local lineWidth = lineChars[#lineChars].baseX + charWidth
local offsetX = (captionContainer.AbsoluteSize.X - lineWidth) / 2
for _, charInfo in ipairs(lineChars) do
local charData = activeCharacters[charInfo.index]
if charData then
charData.baseX = charInfo.baseX + offsetX
charData.frame.Position = UDim2.new(0, charData.baseX, 0, charData.baseY)
end
end
end
end
if centerMode then
local totalHeight = #lines * lineHeight
local offsetY = (captionContainer.AbsoluteSize.Y - totalHeight) / 2
for _, charData in ipairs(activeCharacters) do
charData.baseY = charData.baseY + offsetY
charData.frame.Position = UDim2.new(0, charData.baseX, 0, charData.baseY)
end
end
local minX, maxX, minY, maxY = math.huge, -math.huge, math.huge, -math.huge
for _, charData in ipairs(activeCharacters) do
if charData.char ~= " " then
local x = charData.baseX
local y = charData.baseY
if x < minX then minX = x end
if x + charData.charWidth > maxX then maxX = x + charData.charWidth end
if y < minY then minY = y end
if y + charData.lineHeight > maxY then maxY = y + charData.lineHeight end
end
end
if minX == math.huge or centerMode then
captionBackdrop.Visible = false
else
local padX = 14 * renderScale
local padY = 8 * renderScale
captionBackdrop.Position = UDim2.new(0, minX - padX, 0, minY - padY)
captionBackdrop.Size = UDim2.new(0, (maxX - minX) + padX * 2, 0, (maxY - minY) + padY * 2)
captionBackdrop.Visible = true
end
end
local function updateShake(deltaTime)
local time = tick()
for i, charData in ipairs(activeCharacters) do
if charData.frame and charData.emphasized then
local speed = charData.shakeType == "jitter" and JITTER_SPEED or WAVE_SPEED
local offsetX, offsetY
if charData.shakeType == "jitter" then
offsetX = (math.random() - 0.5) * 2 * EMPHASIS_SHAKE_INTENSITY
offsetY = (math.random() - 0.5) * 2 * EMPHASIS_SHAKE_INTENSITY * 0.5
else
offsetX = math.sin(time * speed + charData.phase) * EMPHASIS_SHAKE_INTENSITY
offsetY = math.cos(time * speed * 1.3 + charData.phase + 1) * EMPHASIS_SHAKE_INTENSITY * 0.5
end
charData.frame.Position = UDim2.new(
0, charData.baseX + offsetX,
0, charData.baseY + offsetY
)
elseif charData.frame and globalShakeIntensity > 0 and charData.shaking and charData.char ~= " " then
local offsetX = (math.random() - 0.5) * 2 * globalShakeIntensity
local offsetY = (math.random() - 0.5) * 2 * globalShakeIntensity
charData.frame.Position = UDim2.new(
0, charData.baseX + offsetX,
0, charData.baseY + offsetY
)
elseif charData.frame then
charData.frame.Position = UDim2.new(
0, charData.baseX,
0, charData.baseY
)
end
end
end
local function hideCaption(captionId)
if captionId and captionId ~= currentCaptionId then
return
end
globalShakeIntensity = 0
if shakeConnection then
shakeConnection:Disconnect()
shakeConnection = nil
end
if currentSound then
currentSound:Stop()
currentSound:Destroy()
currentSound = nil
end
local slideOutTween = TweenService:Create(
captionContainer,
TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
{Position = hideTarget}
)
slideOutTween:Play()
TweenService:Create(captionBackdrop, TweenInfo.new(FADE_TIME, Enum.EasingStyle.Quad, Enum.EasingDirection.In), { BackgroundTransparency = 1 }):Play()
TweenService:Create(backdropStroke, TweenInfo.new(FADE_TIME, Enum.EasingStyle.Quad, Enum.EasingDirection.In), { Transparency = 1 }):Play()
for _, charData in ipairs(activeCharacters) do
if charData.frame then
for _, child in pairs(charData.frame:GetChildren()) do
if child:IsA("ImageLabel") then
local tween = TweenService:Create(
child,
TweenInfo.new(FADE_TIME, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
{ImageTransparency = 1}
)
tween:Play()
end
end
task.delay(FADE_TIME, function()
if charData.frame then
charData.frame:Destroy()
end
end)
end
end
activeCharacters = {}
end
local function playCaption(text, soundId, duration, isSkipCaption, captionId, speakerColor, glitch, shake)
currentCaptionId = captionId
globalShakeIntensity = shake and (type(shake) == "number" and shake or 4) or 0
local segments = parseCaption(text)
createCharacterLabels(segments)
if captionBackdrop.Visible then
TweenService:Create(captionBackdrop, TweenInfo.new(0.25, Enum.EasingStyle.Quad), { BackgroundTransparency = BACKDROP_TRANSPARENCY }):Play()
TweenService:Create(backdropStroke, TweenInfo.new(0.25, Enum.EasingStyle.Quad), { Transparency = BACKDROP_STROKE_TRANSPARENCY }):Play()
end
if shake then
local shakeStart, shakeEnd = 1, #activeCharacters
if type(shake) == "string" then
shakeStart, shakeEnd = string.find(text, shake, 1, true)
end
if shakeStart and shakeEnd then
for i = shakeStart, shakeEnd do
if activeCharacters[i] then activeCharacters[i].shaking = true end
end
end
end
local slideInTween = TweenService:Create(
captionContainer,
TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
{Position = showTarget}
)
slideInTween:Play()
if soundId then
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://" .. soundId
sound.Volume = 0.7
sound.RollOffMaxDistance = 0
sound.RollOffMinDistance = 0
sound.Parent = workspace
if sound.IsLoaded then
sound:Play()
else
sound.Loaded:Wait()
sound:Play()
end
currentSound = sound
if not duration then
duration = sound.TimeLength + 0.5
end
sound.Ended:Connect(function()
sound:Destroy()
currentSound = nil
end)
else
duration = duration or 10
end
local totalChars = #activeCharacters
local charsToShow = 0
for i, charData in ipairs(activeCharacters) do
if string.match(charData.char, "%S") then
charsToShow = charsToShow + 1
end
end
local typewriterDuration = (duration - FADE_TIME - 0.5) * 0.7
local delayPerChar = typewriterDuration / math.max(charsToShow, 1)
local keithPrefix = "Keith: "
local supervisorPrefix = "Supervisor: "
local prefixLength = 0
if string.sub(text, 1, #keithPrefix) == keithPrefix then
prefixLength = #keithPrefix
elseif string.sub(text, 1, #supervisorPrefix) == supervisorPrefix then
prefixLength = #supervisorPrefix
elseif speakerColor then
local colonPos = string.find(text, ": ", 1, true)
if colonPos then prefixLength = colonPos + 1 end
end
local lastCharDelay = 0
for i, charData in ipairs(activeCharacters) do
if captionId ~= currentCaptionId then
break
end
local isSupervisorPrefix = string.sub(text, 1, #supervisorPrefix) == supervisorPrefix and i <= prefixLength
local isKeithPrefix = string.sub(text, 1, #keithPrefix) == keithPrefix and i <= prefixLength
if i <= prefixLength then
for _, child in pairs(charData.frame:GetChildren()) do
if child:IsA("ImageLabel") then
child.ImageTransparency = 0
if isSupervisorPrefix then
child.ImageColor3 = Color3.fromRGB(200, 255, 200)
elseif isKeithPrefix then
child.ImageColor3 = Color3.fromRGB(200, 220, 255)
elseif speakerColor then
child.ImageColor3 = speakerColor
end
end
end
else
local charDelay = (i - prefixLength - 1) * delayPerChar
lastCharDelay = math.max(lastCharDelay, charDelay)
task.delay(charDelay, function()
if captionId ~= currentCaptionId then
return
end
if charData.frame then
for _, child in pairs(charData.frame:GetChildren()) do
if child:IsA("ImageLabel") then
child.ImageTransparency = 0
end
end
local popTween = TweenService:Create(
charData.frame,
TweenInfo.new(0.1, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
{Size = UDim2.new(0, (charData.charWidth + 2) * 1.2, 0, charData.lineHeight * 1.2)}
)
popTween:Play()
task.delay(0.1, function()
local returnTween = TweenService:Create(
charData.frame,
TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
{Size = UDim2.new(0, charData.charWidth + 2, 0, charData.lineHeight)}
)
returnTween:Play()
end)
end
end)
end
end
if shakeConnection then
shakeConnection:Disconnect()
end
shakeConnection = RunService.RenderStepped:Connect(updateShake)
if glitch and glitch.indices and ((glitch.set and #glitch.set > 0) or glitch.colors) then
local gset = glitch.set or ""
local colors = glitch.colors
local gcolor = speakerColor or Color3.fromRGB(255, 255, 255)
local originals = {}
for _, idx in ipairs(glitch.indices) do
local cd = activeCharacters[idx]
originals[idx] = cd and cd.char or " "
end
task.spawn(function()
local startDelay = 0
if glitch.indices[1] then
startDelay = math.max(0, (glitch.indices[1] - prefixLength - 1) * delayPerChar)
end
if startDelay > 0 then task.wait(startDelay) end
while captionId == currentCaptionId do
for _, idx in ipairs(glitch.indices) do
local cd = activeCharacters[idx]
if cd and cd.frame and cd.frame.Parent then
local ch
if colors and (#gset == 0 or math.random() < 0.45) then
ch = originals[idx]
else
local r = math.random(1, #gset)
ch = string.sub(gset, r, r)
end
local col = gcolor
if colors and #colors > 0 then
col = colors[math.random(1, #colors)]
end
for _, c in ipairs(cd.frame:GetChildren()) do
c:Destroy()
end
Text.Create(cd.frame, ch, {
Font = VCR_Font,
Size = math.floor(16 * renderScale + 0.5),
Color = col,
Transparency = 0,
XAlignment = "Left",
YAlignment = "Top",
Dynamic = false
})
cd.char = ch
end
end
task.wait(0.05)
end
end)
end
task.delay(duration - FADE_TIME, function()
if captionId == currentCaptionId then
hideCaption(captionId)
if isSkipCaption then
skipCaptionActive = false
end
end
end)
end
local function setLayout(mode)
if mode == "center" then
renderScale = 2.4
centerMode = true
captionContainer.Size = UDim2.new(0.9, 0, 0.55, 0)
captionContainer.Position = UDim2.new(0.5, 0, 0.5, 0)
showTarget = UDim2.new(0.5, 0, 0.5, 0)
hideTarget = UDim2.new(0.5, 0, 0.5, 0)
else
renderScale = 1
centerMode = false
captionContainer.Size = UDim2.new(0.7, 0, 0, 80)
showTarget = UDim2.new(0.5, 0, 0.9, 0)
hideTarget = UDim2.new(0.5, 0, 1.1, 0)
end
end
function CaptionSystem.ShowCenterCaption(text, soundId, duration, speakerColor)
skipCaptionActive = false
currentCaptionId = currentCaptionId + 1
local captionId = currentCaptionId
hideCaption()
setLayout("center")
task.wait(0.1)
playCaption(text, soundId, duration, false, captionId, speakerColor)
end
function CaptionSystem.ShowCaption(text, soundId, duration, isSkipCaption, speakerColor, glitch, shake)
if skipCaptionActive and not isSkipCaption then
return
end
if isSkipCaption then
skipCaptionActive = true
end
setLayout("bottom")
currentCaptionId = currentCaptionId + 1
local captionId = currentCaptionId
hideCaption()
task.wait(0.1)
playCaption(text, soundId, duration, isSkipCaption, captionId, speakerColor, glitch, shake)
end
function CaptionSystem.StopShake()
globalShakeIntensity = 0
end
function CaptionSystem.HideCaption()
skipCaptionActive = false
currentCaptionId = currentCaptionId + 1
hideCaption()
end
return CaptionSystem