/
init.lua
1761 lines (1590 loc) · 48.6 KB
/
init.lua
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
LadyCelestia 9/23/2023
A complete overhaul of HitboxMaster.
Version: 1.1.1 (18/10/2023)
--]]
--[[
@define
Definitions
--]]
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")
local DebugMode = false
local HitboxSerial = 0
local ActiveHitboxes = {}
local ActiveZones = {}
local ZoneFolder = workspace:FindFirstChild("ZoneFolder") or Instance.new("Folder", workspace)
ZoneFolder.Name = "ZoneFolder"
--[[
@function
Universal functions
--]]
-- Debug mode for stack tracer
local function getStackLevel(): number
if DebugMode == true then
return 1
end
return 2
end
-- Concatenate string prefix for console output
local function concatPrint(String: string): string
return "[" .. script.Name .. "]: " .. String
end
-- Find an index/value or index-value pair in Table and all its children tables
local function DeepFind(Table: {any}, Row: {any}): {any}?
if #Row ~= 1 then
error(concatPrint("Row argument of function DeepFind may only have one key-value pair."), getStackLevel())
end
for i,v in pairs(Table) do
if typeof(v) ~= "table" then
if Row[i] ~= nil then
return {i = v}
else
for _,v2 in pairs(Row) do
if v == v2 then
return {i = v}
end
end
end
else
local RecursiveResult = DeepFind(v, Row)
if RecursiveResult ~= nil then
return RecursiveResult
end
end
end
return nil
end
-- table.remove en mass
local function CullTable(TableToCull: Pair<any, any>, CullingList: Pair<number, any>): Pair<any, any>
for i = 1, #TableToCull do
local target = CullingList[i]
if TableToCull[target] then
table.remove(TableToCull, target)
for i2, v in ipairs(CullingList) do
if v > target then
CullingList[i2] -= 1
end
end
end
end
return TableToCull
end
-- Find the highest numeral index in an any-pair table
local function FindHighestIndex(Table: {any}): number
local HighestIndex = 0
for i,_ in pairs(Table) do
if typeof(i) == "number" and i > HighestIndex then
HighestIndex = i
end
end
return HighestIndex
end
-- Get total entries of an any-pair table
local function GetTotalEntries(Table: {any}): number
local num = 0
for i,v in pairs(Table) do
if i ~= nil and v ~= nil then
num += 1
end
end
return num
end
-- Compare the values of two any-pair tables, return two ipair tables of exclusive values
local function CompareTableValues(Table1: {any}, Table2: {any}): {any} & {any}
local Table1Exclusives = {}
for _,v in pairs(Table1) do
local IsInTable = false
for _,v2 in pairs(Table2) do
if v == v2 then
IsInTable = true
break
end
end
if IsInTable == false then
table.insert(Table1Exclusives, v)
end
end
local Table2Exclusives = {}
for _,v in pairs(Table2) do
if table.find(Table1Exclusives, v) == nil then
local IsInTable = false
for _,v2 in pairs(Table1) do
if v == v2 then
IsInTable = true
break
end
end
if IsInTable == false then
table.insert(Table2Exclusives, v)
end
end
end
return Table1Exclusives, Table2Exclusives
end
-- Check if method/variable is private
local function IsPrivate(String: string): boolean
if string.match(String, "_%a+") ~= nil then
return true
end
return false
end
-- Factorized quadratic bezier equation
local function quadbez(a: LerpValue, b: LerpValue, c:LerpValue, t: number): LerpValue
return (1 - t)^2*a + 2*(1 - t)*t*b + t^2*c
end
-- Factorized cubic bezier equation
local function cubicbez(a: LerpValue, b: LerpValue, c: LerpValue, d: LerpValue, t: number): LerpValue
return (1 - t)^3*a + 3*(1 - t)^2*t*b + 3*(1 - t)*t^2*c + t^3+d
end
-- Return the combined length of segments
local function seglen(segments: {Vector3}): number
local length = 0
for i = 1, #segments do
local current, next = segments[i], segments[i + 1]
if not next then
continue
end
length += (current - next).Magnitude
end
return length
end
-- Return a rough estimated total length of a quadratic bezier curve
local function quadbezlen(startPoint: Vector3, controlPoint: Vector3, endPoint: Vector3, resolution: number?): number
resolution = resolution or 10
local segments = {}
for i = 1, resolution do
table.insert(segments, quadbez(startPoint, controlPoint, endPoint, i/resolution))
end
return seglen(segments)
end
-- Return a rough estimated total length of a cubic bezier curve
local function cubicbezlen(startPoint: Vector3, controlPoint1: Vector3, controlPoint2: Vector3, endPoint: Vector3, resolution: number?): number
resolution = resolution or 10
local segments = {}
for i = 1, resolution do
table.insert(segments, cubicbez(startPoint, controlPoint1, controlPoint2, endPoint, i/resolution))
end
return seglen(segments)
end
-- ZoneComponent character query validation
local function CharacterQuerySearch(CharacterList: {Pair<Model, {Pair<BasePart, boolean>}>}, CharacterResolution: string): {Model}
local ValidCharacterList = {}
for i,v in pairs(CharacterList) do
local valid = false
if CharacterResolution == "FullBody" then
valid = true
for _,v2 in pairs(v) do
if v2 == false then
valid = false
break
end
end
elseif CharacterResolution == "Root" then
for i2,v2 in pairs(v) do
if i2.Name == "HumanoidRootPart" and v2 == true then
valid = true
break
end
end
elseif CharacterResolution == "Head" then
for i2,v2 in pairs(v) do
if i2.Name == "Head" and v2 == true then
valid = true
break
end
end
else
for _,v2 in pairs(v) do
if v2 == true then
valid = true
break
end
end
end
if valid == true then
table.insert(ValidCharacterList, i)
end
end
return ValidCharacterList
end
-- Separated from HitReg for size compensation
local function HitCheck(self: HitboxType): ()
if self.Pierce > 0 then
local result: {BasePart} = {}
if self.Shape == "Sphere" then
if typeof(self.CopyCFrame) == "Instance" and self.CopyCFrame:IsA("BasePart") then
if self.CopyCFrameProperties["CFrame"] == true or self.CopyCFrameProperties["Position"] == true then
self.Position = self.CopyCFrame.Position
end
if self.CopyCFrameProperties["Size"] == true then
self.Size = self.CopyCFrame.Size
self.Radius = (self.CopyCFrame.Size.X + self.CopyCFrame.Size.Y + self.CopyCFrame.Size.Z) / 3
end
if self.CopyCFrameProperties["CFrame"] == true or self.CopyCFrameProperties["Orientation"] == true then
self.Orientation = self.CopyCFrame.Orientation
end
result = workspace:GetPartBoundsInRadius(self.Position, self.Radius, self.OverlapParams) or {}
elseif typeof(self.Orientation) == "Vector3" then
result = workspace:GetPartBoundsInRadius((CFrame.new(self.Position) * CFrame.Angles(math.rad(self.Orientation.X), math.rad(self.Orientation.Y), math.rad(self.Orientation.Z))).Position, self.Radius, self.OverlapParams) or {}
else
result = workspace:GetPartBoundsInRadius(self.Position, self.Radius, self.OverlapParams) or {}
end
elseif self.Shape == "Box" then
if typeof(self.CopyCFrame) == "Instance" and self.CopyCFrame:IsA("BasePart") then
if self.CopyCFrameProperties["CFrame"] == true or self.CopyCFrameProperties["Position"] == true then
self.Position = self.CopyCFrame.Position
end
if self.CopyCFrameProperties["Size"] == true then
self.Size = self.CopyCFrame.Size
self.Radius = (self.CopyCFrame.Size.X + self.CopyCFrame.Size.Y + self.CopyCFrame.Size.Z) / 3
end
if self.CopyCFrameProperties["CFrame"] == true or self.CopyCFrameProperties["Orientation"] == true then
self.Orientation = self.CopyCFrame.Orientation
end
result = workspace:GetPartBoundsInBox(CFrame.new(self.Position) * CFrame.Angles(math.rad(self.Orientation.X), math.rad(self.Orientation.Y), math.rad(self.Orientation.Z)), self.Size, self.OverlapParams) or {}
elseif typeof(self.Orientation) == "Vector3" then
result = workspace:GetPartBoundsInBox(CFrame.new(self.Position) * CFrame.Angles(math.rad(self.Orientation.X), math.rad(self.Orientation.Y), math.rad(self.Orientation.Z)), self.Size, self.OverlapParams) or {}
else
result = workspace:GetPartBoundsInBox(CFrame.new(self.Position), self.Size, self.OverlapParams) or {}
end
elseif self._CanWarn == true then
self._CanWarn = false
task.delay(5, function()
self._CanWarn = true
end)
warn(concatPrint("Hitbox " .. self.Serial .. " has an invalid shape."))
end
if #result > 0 then
local hitHumanoids: {humanoid: BasePart} = {}
local registeredHumanoids: {Humanoid} = {}
local appearedParts: {BasePart} = {}
for _,v in ipairs(result) do
table.insert(appearedParts, v)
local hum = v.Parent:FindFirstChildOfClass("Humanoid")
if hum ~= nil then
if v.Parent:FindFirstChild("HitboxSerial" .. self.Serial) == nil and hum:GetState() ~= Enum.HumanoidStateType.Dead then
table.insert(hitHumanoids, {hum, v})
end
end
end
local plcn = #self.PartLeft._ActiveRunner._Connections
local pecn = #self.PartEntered._ActiveRunner._Connections
if plcn > 0 or pecn > 0 then
local XLastPart, XCurrentPart = CompareTableValues(self._PartsInside, appearedParts)
if plcn > 0 then
for _,v in ipairs(XLastPart) do
self.PartLeft:Fire(v)
end
end
if pecn > 0 then
for _,v in ipairs(XCurrentPart) do
self.PartEntered:Fire(v)
end
end
end
self._PartsInside = appearedParts
for _,v in pairs(hitHumanoids) do
local canHit = true
for _,v2 in ipairs(registeredHumanoids) do
if v[1] == v2 then
canHit = false
break
end
end
if canHit == true then
if self.Debounce > 0 then
local newSerial: BoolValue = Instance.new("BoolValue")
Debris:AddItem(newSerial, self.Debounce)
newSerial.Name = "HitboxSerial" .. self.Serial
newSerial.Value = true
newSerial.Parent = v[2].Parent
end
local hcn = #self.Hit._ActiveRunner._Connections
if hcn > 0 then
---@diagnostic disable-next-line: redundant-parameter
self.Hit:Fire(v[1], v[2], { -- Humanoid, HitPart, HitboxDataBundle
["Serial"] = self.Serial,
["Position"] = self.Position,
["Radius"] = self.Radius or 0,
["Size"] = self.Size or Vector3.new(0, 0, 0)
})
end
table.insert(registeredHumanoids, v[1])
self.Pierce -= 1
if self.Pierce <= 0 then
break
end
end
end
end
end
end
-- Hitbox spatial query main
local function HitReg(self: HitboxType, deltaTime: number): {BasePart}
local HitboxMode = self:GetCurrentMode()
local Velocity = self:GetVelocity()
if HitboxMode == "Linear" then
self.Position += (self.Trajectory._DirectionalVector * (Velocity * deltaTime))
elseif HitboxMode == "Bezier" then
local interpolationGain = (Velocity * deltaTime) / self.Trajectory._Length
if interpolationGain > (1 - self.Trajectory._Completion) then
interpolationGain = (1 - self.Trajectory._Completion)
end
self.Trajectory._Completion += interpolationGain
self.BezierCompletion += interpolationGain
local BezierMode = self.Trajectory:_GetBezierMode()
if BezierMode == "Quadratic" then
self.Position = quadbez(self.StartPoint, self.ControlPoint1, self.EndPoint, self.Trajectory._Completion)
elseif BezierMode == "Cubic" then
self.Position = cubicbez(self.StartPoint, self.ControlPoint1, self.ControlPoint2, self.EndPoint, self.Trajectory._Completion)
end
elseif HitboxMode == "Attachment" then
self.Position = self._Attachment.WorldCFrame.Position
end
if self._CurrentFrame >= 5 and self._Visual ~= nil then
self._CurrentFrame = 0
if self.Shape == "Sphere" then
self._Visual.Shape = Enum.PartType.Ball
self._Visual.Size = Vector3.new(self.Radius * 2, self.Radius * 2, self.Radius * 2)
else
self._Visual.Shape = Enum.PartType.Block
self._Visual.Size = self.Size
end
if self.CopyCFrame ~= nil then
self._Visual.CFrame = self.CopyCFrame.CFrame
elseif self.Position ~= nil then
if self.Orientation ~= nil then
self._Visual.CFrame = CFrame.new(self.Position) * CFrame.Angles(math.rad(self.Orientation.X), math.rad(self.Orientation.Y), math.rad(self.Orientation.Z))
else
self._Visual.Position = self.Position
end
end
end
local distance = (self.Position - self._LastPosition).Magnitude
local savedPos = self.Position
if self.Shape == "Sphere" then
local factor = math.ceil(distance / self.Radius)
if factor > 1 then
for i = 1, factor do
self.Position = self._LastPosition:Lerp(savedPos, i / (factor + 1))
HitCheck(self)
end
end
end
self.Position = savedPos
HitCheck(self)
if ((self.LifeTime <= 0 and HitboxMode ~= "Bezier") or (HitboxMode == "Bezier" and self.Trajectory._Completion >= 1)) and self._Destroying == false then
self:Destroy()
end
end
-- Main runner
local function MainRunner(_, deltaTime: number): ()
for _,self: HitboxType in pairs(ActiveHitboxes) do
if self.Active == true then
self._CurrentFrame += 1
self.LifeTime -= deltaTime
if deltaTime < 1/30 then
HitReg(self, deltaTime)
else
local Factor = math.floor(deltaTime / (1/60))
---@diagnostic disable-next-line: unused-local
for i = 1, Factor do
HitReg(self, deltaTime / Factor)
end
end
end
end
for _,self: ZoneType in pairs(ActiveZones) do
if self.Active == true then
local Frames = 60 / self.Rate
self._CurrentFrame += 1
if self._CurrentFrame >= Frames then
self._CurrentFrame = 0
local CurrentPartList = self:GetPartsInside()
local CurrentCharacterList = self:GetCharactersInside(CurrentPartList)
local CurrentPlayerList = self:GetPlayersInside(CurrentPartList)
local plcn = #self.PartLeft._ActiveRunner._Connections
local pecn = #self.PartEntered._ActiveRunner._Connections
if plcn > 0 or pecn > 0 then
local XLastPart, XCurrentPart = CompareTableValues(self._PartsInside, CurrentPartList)
if plcn > 0 then
for _,v in ipairs(XLastPart) do
self.PartLeft:Fire(v)
end
end
if pecn > 0 and self._FirstQuery == false then
for _,v in ipairs(XCurrentPart) do
self.PartEntered:Fire(v)
end
end
end
local clcn = #self.CharacterLeft._ActiveRunner._Connections
local cecn = #self.CharacterEntered._ActiveRunner._Connections
if clcn > 0 or cecn > 0 then
local XLastCharacter, XCurrentCharacter = CompareTableValues(self._CharactersInside, CurrentCharacterList)
if clcn > 0 then
for _,v in ipairs(XLastCharacter) do
self.CharacterLeft:Fire(v)
end
end
if cecn > 0 and self._FirstQuery == false then
for _,v in ipairs(XCurrentCharacter) do
self.CharacterEntered:Fire(v)
end
end
end
local plrlcn = #self.PlayerLeft._ActiveRunner._Connections
local plrecn = #self.PlayerEntered._ActiveRunner._Connections
if plrlcn > 0 or plrecn > 0 then
local XLastPlayer, XCurrentPlayer = CompareTableValues(self._PlayersInside, CurrentPlayerList)
if plrlcn > 0 then
for _,v in ipairs(XLastPlayer) do
self.PlayerLeft:Fire(v)
end
end
if plrecn > 0 and self._FirstQuery == false then
for _,v in ipairs(XCurrentPlayer) do
self.PlayerEntered:Fire(v)
end
end
end
self._PartsInside = CurrentPartList
self._CharactersInside = CurrentCharacterList
self._PlayersInside = CurrentPlayerList
self._FirstQuery = false
end
end
end
end
local RunnerConnection = RunService.Stepped:Connect(MainRunner)
if RunService:IsServer() == true and RunService:IsStudio() == false then
game:BindToClose(function()
RunnerConnection:Disconnect()
for _,v in pairs(ActiveHitboxes) do
v:Destroy()
end
for _,v in pairs(ActiveZones) do
v:Destroy()
end
ActiveHitboxes = {}
ActiveZones = {}
end)
end
--[[
@class Enum
Enumeration implementation to prevent unexpected values
--]]
local enum = {}
local enumMetatable = {
__index = function(_, i): any
return enum[i] or error(concatPrint(i .. " is not a valid member of the Enum."), getStackLevel())
end,
__newindex = function(): ()
error(concatPrint("Enums are read-only."), getStackLevel())
end
}
enum._new = function(Name: string, Children: {string: any}): enumPair<any>
if enum[Name] ~= nil then
return enum[Name]
end
enum[Name] = Children
return setmetatable(Children, enumMetatable)
end
enum._get = function(Name: string): enumPair<any>
if enum[Name] then
return setmetatable(enum[Name], enumMetatable)
else
local Result = DeepFind(enum, {Name = nil})
if Result ~= nil then
return setmetatable(Result, enumMetatable)
end
end
end
-- Do not call _isEnum as a method as self is readonly
enum._isEnum = function(ValueToCheck: any): boolean
if IsPrivate(ValueToCheck) == false and DeepFind(enum, {ValueToCheck}) ~= nil then
return true
end
return false
end
enum._new("StateEnum", {Active = "Active", Paused = "Paused", Dead = "Dead"})
enum._new("ConstructionMode", {None = "None", Linear = "Linear", Bezier = "Bezier"})
enum._new("BezierMode", {Quadratic = "Quad", Cubic = "Cubic"})
enum._new("Shape", {Box = "Box", Sphere = "Sphere"})
enum._new("HitboxMode", {None = "None", Attachment = "Attachment", Linear = "Linear", Bezier = "Bezier", Orientation = "Orientation", Copying = "Copy"})
enum._new("CharacterResolution", {OnePart = "OnePart", Root = "Root", Head = "Head", FullBody = "FullBody"})
enum._new("QueryType", {Bounds = "Bounds", Full = "Full"})
--[[
@class ScriptConnection
Child of @ConnectionRunner
--]]
local Connection = {}
Connection.__index = Connection
Connection.StateEnum = enum._get("StateEnum")
Connection.new = function(signal: ScriptSignal, func: any, once: boolean?): ScriptConnection
local self = setmetatable({}, Connection)
self._Connected = true
self._Signal = signal
self._Function = func
self._Identifier = HttpService:GenerateGUID(false)
self._Once = false
if once ~= nil then
self._Once = once
end
self._State = enum.StateEnum.Paused
return self
end
function Connection:_Fire(...): ()
if self._Connected == true then
if self._Once == true then
self:Disconnect()
end
coroutine.wrap(self._Function)(...)
end
end
function Connection:GetIdentifier(): string
return self._Identifier
end
function Connection:Disconnect(): ()
self._Connected = false
self._Function = nil
self = {_State = enum.StateEnum.Dead}
end
--[[
@class ConnectionRunner
@parent ScriptConnection
Private child of @ScriptSignal, manages child ScriptConnections
--]]
local Runner = {}
Runner.__index = Runner
Runner.new = function(): ConnectionRunner
local self = setmetatable({}, Runner)
self._Connections = {}
self._State = enum.StateEnum.Active
return self
end
function Runner:_AddConnection(Cn: ScriptConnection): ()
Cn._State = enum.StateEnum.Active
table.insert(self._Connections, Cn)
end
function Runner:_CleanUp(): ()
local CullingList = {}
for i,v in ipairs(self._Connections) do
if v._Connected == false then
table.insert(CullingList, i)
end
end
CullTable(self._Connections, CullingList)
end
function Runner:_RemoveConnection(identifier: string): ()
for _,v in ipairs(self._Connections) do
if v._Identifier == identifier then
v:Disconnect()
end
end
self:_CleanUp()
end
function Runner:_GetConnections(): Pair<number, ScriptConnection>
return self._Connections
end
function Runner:_FireOne(identifier: string, args: {any}): ()
for _,v in ipairs(self._Connections) do
if v._Identifier == identifier then
v:Fire(table.unpack(args))
end
end
end
function Runner:_FireAll(...): ()
for _,v in ipairs(self._Connections) do
v:_Fire(...)
end
end
function Runner:Destroy(): ()
for _,v in ipairs(self._Connections) do
v:Disconnect()
end
self:_CleanUp()
self = {_State = enum}
end
--[[
@class ScriptSignal
@parent enum, ConnectionRunner
ScriptSignal implementation
--]]
local Signal = {}
Signal.__index = Signal
Signal.StateEnum = enum._get("StateEnum")
Signal.new = function(): ScriptSignal
local self = setmetatable({}, Signal)
self._ActiveRunner = Runner.new()
self._State = enum.StateEnum.Active
return self
end
function Signal:GetState(): string
return self._State
end
function Signal:FireOne(...): ()
self._ActiveRunner:_FireOne(...)
end
function Signal:Fire(...): ()
self._ActiveRunner:_FireAll(...)
end
function Signal:Connect(func: any, connectImmediately: boolean?): ScriptConnection
if connectImmediately == nil then
connectImmediately = true
end
local newConnection = Connection.new(self, func)
if connectImmediately == true then
self._ActiveRunner:_AddConnection(newConnection)
end
return newConnection
end
function Signal:DisconnectOne(identifier: string): ()
self._ActiveRunner:_RemoveConnection(identifier)
end
function Signal:DisconnectAll(): ()
for _,v in ipairs(self._ActiveRunner:_GetConnections()) do
v:Disconnect()
end
self._ActiveRunner:_CleanUp()
end
function Signal:Once(func: any): ScriptConnection
local newConnection = self:Connect(func, false)
newConnection._Once = true
self._ActiveRunner:_AddConnection(newConnection)
return newConnection
end
function Signal:Destroy(): ()
self:DisconnectAll()
self._ActiveRunner:Destroy()
self = {_State = enum.StateEnum.Dead}
end
--[[
@class Trajectory
Child of @Hitbox
Enables trajectory construction
--]]
local Trajectory = {}
Trajectory.__index = Trajectory
Trajectory.new = function(Fields: {Pair<string, any>}): TrajectoryType
Fields = Fields or {}
local self = setmetatable({}, Trajectory)
self._State = enum.StateEnum.Active
self.ConstructionMode = enum.ConstructionMode.None
-- Linear Construction
self._DirectionalVector = nil
-- Bezier Construction
self._Length = 0
self._Completion = 0
self._Points = {}
-- Universal
self.Velocity = Fields["Velocity"] or 10
return self
end
function Trajectory:_GetBezierMode(): string
if self._Points.Control2 == nil then
return enum.BezierMode.Quadratic
else
return enum.BezierMode.Cubic
end
end
function Trajectory:Construct(Mode: string, Fields: {Pair<string, any>}): boolean
Fields = Fields or {}
if Mode ~= enum.ConstructionMode.None and enum._isEnum(Mode) == true then
if Mode == enum.ConstructionMode.Linear then
self._DirectionalVector = Fields["DirectionalVector"] or error(concatPrint("Trajectory:Construct(Linear) is missing field DirectionalVector."), getStackLevel())
elseif Mode == enum.ConstructionMode.Bezier then
self._Points = Fields["BezierPoints"] or error(concatPrint("Trajectory:Construct(Bezier) is missing field BezierPoints."), getStackLevel())
if self:_GetBezierMode() == enum.BezierMode.Quadratic then
self._Length = quadbezlen(self._Points["Start"], self._Points["Control1"], self._Points["End"])
else
self._Length = cubicbezlen(self._Points["Start"], self._Points["Control1"], self._Points["Control2"], self._Points["End"])
end
end
self.Velocity = Fields["Velocity"] or self.Velocity
self.ConstructionMode = Mode
return true
end
return false
end
function Trajectory:Deconstruct(): ()
self.ConstructionMode = enum.ConstructionMode.None
self._DirectionalVector = nil
self._Length = 0
self._Completion = 0
self._Points = {}
end
function Trajectory:Destroy(): ()
self = {_State = enum.StateEnum.Dead}
end
--[[
@class Hitbox
@parent enum, ScriptSignal, Trajectory
Hitbox class
--]]
local Hitbox = {}
Hitbox.__index = Hitbox
Hitbox.ShapeEnum = enum._get("Shape")
Hitbox.ModeEnum = enum._get("HitboxMode")
Hitbox.StateEnum = enum._get("StateEnum")
Hitbox.ConstructionEnum = enum._get("ConstructionMode")
Hitbox.BezierEnum = enum._get("BezierMode")
Hitbox.new = function(Fields: {Pair<string, any>}): HitboxType
Fields = Fields or {}
HitboxSerial += 1
local self = setmetatable({}, Hitbox)
-- Private variables
self._Attachment = Fields["Attachment"]
self._CurrentFrame = 0
self._CanWarn = true
self._Visual = nil
self._Destroying = false
self._PartsInside = {}
self._LastPosition = Fields["Position"] or Vector3.new(0, 0, 0)
-- Public variables
self.Hit = Signal.new()
self.PartEntered = Signal.new()
self.PartLeft = Signal.new()
self.Trajectory = Trajectory.new()
self.Serial = HitboxSerial
self.Shape = Fields["Shape"] or enum.Shape.Box
self.Position = Fields["Position"] or Vector3.new(0, 0, 0)
self.Pierce = Fields["Pierce"] or 1
self.Debounce = Fields["Debounce"] or 5
self.LifeTime = Fields["LifeTime"] or 1
self.Orientation = Fields["Orientation"]
self.CopyCFrame = Fields["CopyCFrame"]
self.CopyCFrameProperties = Fields["CopyCFrameProperties"] or {CFrame = true, Size = true}
self.OverlapParams = OverlapParams.new()
self.OverlapParams.FilterType = Fields["FilterType"] or Enum.RaycastFilterType.Exclude
self.OverlapParams.FilterDescendantsInstances = Fields["FilterDescendantsInstances"] or {}
self.OverlapParams.RespectCanCollide = Fields["RespectCanCollide"] or false
self.OverlapParams.MaxParts = Fields["MaxParts"] or 0
self.Active = false
self.State = enum.StateEnum.Paused
self.Radius = Fields["Radius"] or 3 -- Used for Sphere shape
self.Size = Fields["Size"] or Vector3.new(3, 3, 3) -- Used for Box shape
ActiveHitboxes[self.Serial] = self
return self
end
function Hitbox:ChangePosition(newPos: Vector3): ()
if typeof(newPos) == "Vector3" then
self._LastPosition = newPos
self.Position = newPos
end
end
function Hitbox:Visualize(): BasePart?
if self._Visual ~= nil then
warn(concatPrint("Hitbox is already visualizing."))
else
self._Visual = Instance.new("Part")
self._Visual.Name = "HitboxVisualization" .. tostring(self.Serial)
self._Visual.Anchored = true
self._Visual.CanCollide = false
self._Visual.BrickColor = BrickColor.new("Really red")
self._Visual.Transparency = 0.75
self._Visual.Material = Enum.Material.SmoothPlastic
self._Visual.Position = self.Position or Vector3.new(0, 0, 0)
if self.Shape == enum.Shape.Sphere then
self._Visual.Shape = Enum.PartType.Ball
self._Visual.Size = Vector3.new(self.Radius * 2, self.Radius * 2, self.Radius * 2)
else
self._Visual.Shape = Enum.PartType.Block
self._Visual.Size = self.Size
end
self._Visual.Parent = workspace
return self._Visual
end
end
function Hitbox:Unvisualize(doNotWarn: boolean?): ()
if self._Visual == nil then
if doNotWarn ~= true then
--warn(concatPrint("Hitbox is not visualizing."))
end
return false
else
self._Visual:Destroy()
self._Visual = nil
return true
end
end
function Hitbox:Activate(): ()
self.Active = true
self.State = enum.StateEnum.Active
end
function Hitbox:Deactivate(): ()
self.Active = false
self.State = enum.StateEnum.Paused
end
function Hitbox:GetCurrentSerial(): number
return HitboxSerial
end
function Hitbox:GetConstructionMode(): string
return self.Trajectory.ConstructionMode
end
function Hitbox:GetCurrentMode(): string
if self._Attachment ~= nil then
return enum.HitboxMode.Attachment
elseif self.Trajectory.ConstructionMode == enum.ConstructionMode.Linear then
return enum.HitboxMode.Linear
elseif self.Trajectory.ConstructionMode == enum.ConstructionMode.Bezier then
return enum.HitboxMode.Bezier
elseif self.Orientation ~= nil and self.Orientation ~= Vector3.new(0, 0, 0) then
return enum.HitboxMode.Orientation
elseif self.CopyCFrame ~= nil then
return enum.HitboxMode.Copying
else
return enum.HitboxMode.None
end
end
function Hitbox:GetVelocity(): number
return self.Trajectory.Velocity
end
function Hitbox:SetVelocity(velocity: number): ()
self.Trajectory.Velocity = velocity
end
function Hitbox:AddIgnore(object: Instance): boolean
if typeof(object) == "Instance" then
if object:IsA("BasePart") or object:IsA("Model") then
local oldTable = self.OverlapParams.FilterDescendantsInstances or {}
table.insert(oldTable, object)
self.OverlapParams.FilterDescendantsInstances = oldTable
return true
end
end
return false
end
function Hitbox:RemoveIgnore(object: Instance): number
if typeof(object) == "Instance" then
if object:IsA("BasePart") or object:IsA("Model") then
local indexes = {}
for i,v in ipairs(self.OverlapParams.FilterDescendantsInstances) do
if v == object then
table.insert(indexes, i)
end
end
for i,v in ipairs(indexes) do
table.remove(self.OverlapParams.FilterDescendantsInstances, v)
for i2,v2 in ipairs(indexes) do
if i2 > i and v2 > v then
indexes[i2] -= 1
end
end
end
return #indexes
end
end
return 0
end
function Hitbox:IsHitboxBackstab(Part: BasePart, DataBundle: HitboxDataBundle, Margin: number?): boolean
if typeof(Margin) ~= "number" then
Margin = 0.32
end
Margin = math.abs(Margin)
if Margin > 0.5 then
Margin = 0.5
end
if DataBundle.Radius > 100 or DataBundle.Size.X > 50 or DataBundle.Size.Y > 50 or DataBundle.Size.Z > 50 then
warn(concatPrint("Hitbox is too large to support Hitbox:IsHitboxBackstab(). (Maximum 50 magnitude per-axis)"))
return false
elseif CFrame.new(DataBundle.Position, Part.Position + (Part.CFrame.LookVector * 5000)).LookVector:Dot(Part.CFrame.LookVector) >= math.abs(1 - Margin) and (Part.Position - DataBundle.Position).Magnitude > ((Part.Position - (Part.CFrame.LookVector * 2)) - DataBundle.Position).Magnitude then
return true