From 04fc707bec85fd54db59b64ce4de618e6bbe2d85 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Fri, 8 Mar 2024 00:46:59 -0500 Subject: [PATCH] Type vacuuming and working classes types --- src/client/Character/Client/Actions.lua | 43 ++++++++++++-- .../Character/Client/Camera/Bobbing.lua | 57 ++++++++++++++----- src/client/Character/Client/Camera/init.lua | 30 ++++++---- src/client/Character/Client/Humanoid.lua | 23 +++++++- .../Character/Client/HumanoidRootPart.lua | 20 ++++++- .../Character/Client/SpineKinematics.lua | 26 ++++++--- src/client/Character/Server/Actions.lua | 30 ++++++++-- src/client/Character/Server/Flashlight.lua | 33 ++++++++--- src/client/Character/Server/Shadows.lua | 22 ++++++- .../Character/Server/SpineKinematics.lua | 56 ++++++++++-------- src/shared/types/class.lua | 11 ---- 11 files changed, 260 insertions(+), 91 deletions(-) delete mode 100644 src/shared/types/class.lua diff --git a/src/client/Character/Client/Actions.lua b/src/client/Character/Client/Actions.lua index 3c2a3d9..6f2b6e1 100644 --- a/src/client/Character/Client/Actions.lua +++ b/src/client/Character/Client/Actions.lua @@ -2,12 +2,47 @@ --!native --!strict -local Actions = {} -Actions.__index = Actions - type UDP = UnreliableRemoteEvent type TCP = RemoteEvent type inherented = any +type CurrentCamera = Camera + +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + EnableSneak: (self: ClassConstructor) -> (), + DisableSneak: (self: ClassConstructor) -> (), + EnableCrouch: (self: ClassConstructor) -> (), + DisableCrouch: (self: ClassConstructor) -> (), + EnableFlashlight: (self: ClassConstructor, FlashlightKey: Enum.KeyCode) -> (), + DisableFlashlight: (self: ClassConstructor, FlashlightKey: Enum.KeyCode) -> (), + ToggleFlashlight: (self: ClassConstructor, FlashlightKey: Enum.KeyCode) -> () +} & Impl_Static_Props + +type Constructor_Fun = (HumanoidSettings: inherented, CurrentCamera: CurrentCamera, ActionsTCP: TCP) -> ClassConstructor +type Impl_Static_Props = { + DoingAction: boolean, + Sneaking: boolean, + Crouching: boolean, + Walk: number, + SneakingSpeed: number, + StandHeight: number, + CrouchHeight: number, + WalkSpeedMultiplier: number, + CrouchSpeed: number, + FlashlightEnabled: boolean +} +type Constructor_Return_Props = { + Humanoid: Humanoid, + HumanoidSettings: any, + CurrentCamera: CurrentCamera, + ActionsTCP: TCP +} + +local Actions = {} :: Impl_Constructor +Actions.__index = Actions --Sneak static properties Actions.DoingAction = false @@ -34,7 +69,7 @@ local Delta = require(Storage:WaitForChild("Delta")) local CrouchTween = Tween.constructor() -function Actions.constructor(HumanoidSettings: inherented, CurrentCamera: Camera, ActionsTCP: TCP) +function Actions.constructor(HumanoidSettings: inherented, CurrentCamera: CurrentCamera, ActionsTCP: TCP) return setmetatable({ Humanoid = HumanoidSettings.Humanoid, HumanoidSettings = HumanoidSettings, diff --git a/src/client/Character/Client/Camera/Bobbing.lua b/src/client/Character/Client/Camera/Bobbing.lua index 178af0d..21ea102 100644 --- a/src/client/Character/Client/Camera/Bobbing.lua +++ b/src/client/Character/Client/Camera/Bobbing.lua @@ -2,7 +2,47 @@ --!native --!strict -local Bobbing = {} +type AnimationsMap = { + Idle: (tick: number, dt: number) -> Euler, + Walk: (tick: number, dt: number) -> Euler, + Stop: () -> Euler, + Falling: () -> Euler +} +type Euler = CFrame +type HumanoidRootPart = BasePart +type deltatime = number +type tick = number +type EulerValue = number +type CurrentCamera = Camera + +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + Frame: (self: ClassConstructor, dt: number) -> () +} & Impl_Static_Props + +type Constructor_Fun = (HumanoidRootPart: HumanoidRootPart, CurrentCamera: CurrentCamera, Humanoid: Humanoid) -> ClassConstructor +type Impl_Static_Props = { + TurnAlpha: number, + LeanMultiplier: number, + SwayMultiplier: number, + AnimationAlpha: number, + MaxGimbalLockY: number, + AnimationSpeed: number, + Tick: number, + ForceStop: boolean +} +type Constructor_Return_Props = { + HumanoidRootPart: HumanoidRootPart, + CurrentCamera: CurrentCamera, + Humanoid: Humanoid +} + +export type BobbingConstructor = ClassConstructor + +local Bobbing = {} :: Impl_Constructor Bobbing.__index = Bobbing Bobbing.TurnAlpha = 0.050 @@ -14,12 +54,7 @@ Bobbing.AnimationSpeed = 200 Bobbing.Tick = 0 Bobbing.ForceStop = false -type AnimationsMap = {[string]: (tick: number, dt: number) -> Euler} - -local Animations: AnimationsMap = {} - -type Euler = CFrame -type HumanoidRootPart = BasePart +local Animations = {} :: AnimationsMap local UIS = game:GetService("UserInputService") @@ -28,7 +63,7 @@ local ANG = CFrame.Angles local CameraLean = CN() local Animation = CN() -function Bobbing.constructor(HumanoidRootPart: HumanoidRootPart, CurrentCamera: Camera, Humanoid: Humanoid) +function Bobbing.constructor(HumanoidRootPart: HumanoidRootPart, CurrentCamera: CurrentCamera, Humanoid: Humanoid) return setmetatable({ HumanoidRootPart = HumanoidRootPart, CurrentCamera = CurrentCamera, @@ -36,10 +71,6 @@ function Bobbing.constructor(HumanoidRootPart: HumanoidRootPart, CurrentCamera: }, Bobbing) end -type deltatime = number -type tick = number -type EulerValue = number - function Animations.Idle(t: tick, dt: deltatime) return ANG( math.rad(math.cos(t/80)/(Bobbing.AnimationSpeed+50)), @@ -78,7 +109,7 @@ local function Camera_YArc(Camera: Camera): EulerValue --stop Euler gimbal lock return math.abs(math.deg(EulerY)) end -local function CameraAnimation(self, dt: deltatime) +local function CameraAnimation(self: ClassConstructor, dt: deltatime) --crying Bobbing.Tick += 1 diff --git a/src/client/Character/Client/Camera/init.lua b/src/client/Character/Client/Camera/init.lua index f19345b..26ac8dc 100644 --- a/src/client/Character/Client/Camera/init.lua +++ b/src/client/Character/Client/Camera/init.lua @@ -2,27 +2,37 @@ --!native --!strict -local Camera = {} -local FakeCamera = {} -Camera.__index = Camera -FakeCamera.__index = FakeCamera - -local RS = game:GetService("RunService") local Bobbing = require(script:WaitForChild("Bobbing")) type FakeCamera = BasePart type CurrentCamera = Camera type HumanoidRootPart = BasePart +type Module = any -type Camera_struct = { +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + EnableBobbing: (self: ClassConstructor) -> (), + DisableBobbing: (self: ClassConstructor) -> () +} + +type Constructor_Fun = (CurrentCamera: CurrentCamera, HumanoidRootPart: HumanoidRootPart, Humanoid: Humanoid) -> ClassConstructor +type Constructor_Return_Props = { CameraFPS: boolean, CurrentCamera: CurrentCamera, HumanoidRootPart: HumanoidRootPart, - BobbingCamera: any + BobbingCamera: Bobbing.BobbingConstructor } +local Camera = {} :: Impl_Constructor +Camera.__index = Camera + +local RS = game:GetService("RunService") + function Camera.constructor(CurrentCamera: CurrentCamera, HumanoidRootPart: HumanoidRootPart, Humanoid: Humanoid) - local self = {} :: Camera_struct + local self = {} self.CameraFPS = false self.CurrentCamera = CurrentCamera self.HumanoidRootPart = HumanoidRootPart @@ -48,7 +58,7 @@ function Camera:DisableBobbing() else print("Character Camera: DisableBobbing was called before EnableBobbing", debug.traceback()) end - (self.CurrentCamera :: Camera).CFrame *= CFrame.Angles(0,0,0) + self.CurrentCamera.CFrame *= CFrame.Angles(0,0,0) end return Camera \ No newline at end of file diff --git a/src/client/Character/Client/Humanoid.lua b/src/client/Character/Client/Humanoid.lua index 096d53f..55ef848 100644 --- a/src/client/Character/Client/Humanoid.lua +++ b/src/client/Character/Client/Humanoid.lua @@ -2,12 +2,29 @@ --!native --!strict -local HumanoidModule = {} -HumanoidModule.__index = HumanoidModule - type WalkSpeed = number? type JumpHeight = number? +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + SetWalkSpeed: (self: ClassConstructor, Speed: WalkSpeed) -> (), + SetJumpHeight: (self: ClassConstructor, Height: JumpHeight) -> () +} & Impl_Static_Props + +type Constructor_Fun = (Humanoid: Humanoid) -> ClassConstructor +type Impl_Static_Props = { + Running: boolean +} +type Constructor_Return_Props = { + Humanoid: Humanoid +} + +local HumanoidModule = {} :: Impl_Constructor +HumanoidModule.__index = HumanoidModule + function HumanoidModule.constructor(Humanoid: Humanoid) return setmetatable({ Humanoid = Humanoid diff --git a/src/client/Character/Client/HumanoidRootPart.lua b/src/client/Character/Client/HumanoidRootPart.lua index 8fb152d..ac0f9ff 100644 --- a/src/client/Character/Client/HumanoidRootPart.lua +++ b/src/client/Character/Client/HumanoidRootPart.lua @@ -2,10 +2,26 @@ --!native --!strict -local HumanoidRPSettings = {} +type HumanoidRootPart = BasePart + +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + DisableRobloxSounds: (self: ClassConstructor) -> (), + Velocity: (self: ClassConstructor) -> Vector3 +} + +type Constructor_Fun = (HumanoidRootPart: HumanoidRootPart) -> ClassConstructor +type Constructor_Return_Props = { + HumanoidRootPart: HumanoidRootPart +} + +local HumanoidRPSettings = {} :: Impl_Constructor HumanoidRPSettings.__index = HumanoidRPSettings -function HumanoidRPSettings.constructor(HumanoidRootPart: BasePart) +function HumanoidRPSettings.constructor(HumanoidRootPart: HumanoidRootPart) return setmetatable({ HumanoidRootPart = HumanoidRootPart }, HumanoidRPSettings) diff --git a/src/client/Character/Client/SpineKinematics.lua b/src/client/Character/Client/SpineKinematics.lua index 28164ac..5b876cb 100644 --- a/src/client/Character/Client/SpineKinematics.lua +++ b/src/client/Character/Client/SpineKinematics.lua @@ -2,19 +2,31 @@ --!native --!strict -local Spine = { - Running = false -} -Spine.__index = Spine - type UDP = UnreliableRemoteEvent type CurrentCamera = Camera -type struct_Spine = { +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + Enable: (self: ClassConstructor) -> (), + Disable: (self: ClassConstructor) -> () +} & Impl_Static_Props + +type Constructor_Fun = (CurrentCamera: CurrentCamera) -> ClassConstructor +type Impl_Static_Props = { + Running: boolean +} +type Constructor_Return_Props = { Remote: UDP, CurrentCamera: CurrentCamera } -type CharacterSharedFolder = Folder + +local Spine = {} :: Impl_Constructor +Spine.__index = Spine + +Spine.Running = false local Storage = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") diff --git a/src/client/Character/Server/Actions.lua b/src/client/Character/Server/Actions.lua index 51e0d82..ff9bef1 100644 --- a/src/client/Character/Server/Actions.lua +++ b/src/client/Character/Server/Actions.lua @@ -2,14 +2,34 @@ --!native --!strict -local Actions = {} -Actions.__index = Actions - type EventFunction = (KeyPressed: Enum.KeyCode) -> () type ActionsTCP = RBXScriptConnection type LocalPlayer = Player type TCP = RemoteEvent +type EventsDict = { + [Enum.KeyCode]: EventFunction +} + +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + Add: (self: ClassConstructor, Key: Enum.KeyCode, f: EventFunction) -> (), + Remove: (self: ClassConstructor, Key: Enum.KeyCode) -> () +} & Impl_Static_Props + +type Constructor_Fun = (LocalPlayer: LocalPlayer) -> ClassConstructor +type Impl_Static_Props = {} +type Constructor_Return_Props = { + Events: EventsDict, + CurrentActionsTCP_Event: ActionsTCP? +} + +local Actions = {} :: Impl_Constructor +Actions.__index = Actions + local CharacterShared = _G.include(script, "CharacterShared") local Remote = Instance.new("RemoteEvent") :: TCP @@ -19,9 +39,7 @@ Remote.Parent = CharacterShared local ActionsTCP_Event: ActionsTCP? function Actions.constructor(LocalPlayer: LocalPlayer) - local Events: { - [Enum.KeyCode]: EventFunction - } = {} + local Events: EventsDict = {} if ActionsTCP_Event then warn("[Server Actions]: TCP event was already created, duplicating...", debug.traceback()) diff --git a/src/client/Character/Server/Flashlight.lua b/src/client/Character/Server/Flashlight.lua index e725c66..6db91e8 100644 --- a/src/client/Character/Server/Flashlight.lua +++ b/src/client/Character/Server/Flashlight.lua @@ -8,9 +8,28 @@ type LocalPlayer = Player type HumanoidRootPart = BasePart type EulerXYZ_struct = {number} +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + On: (self: ClassConstructor) -> (), + Off: (self: ClassConstructor) -> (), + Toggle: (self: ClassConstructor) -> () +} & Impl_Static_Props +type Constructor_Fun = (LocalPlayer: LocalPlayer, HumanoidRootPart: HumanoidRootPart) -> ClassConstructor +type Impl_Static_Props = { + Enabled: boolean, + HeadHeight: number +} +type Constructor_Return_Props = { + FlashlightPart: Part, + SpotLight: SpotLight, + ToggleSound: Sound +} -local Flashlight = {} +local Flashlight = {} :: Impl_Constructor Flashlight.__index = Flashlight Flashlight.Enabled = false @@ -83,15 +102,15 @@ end function Flashlight:On() Flashlight.Enabled = true - (self.ToggleSound :: Sound):Play(); - (self.SpotLight :: SpotLight).Enabled = Flashlight.Enabled + self.ToggleSound:Play(); + self.SpotLight.Enabled = Flashlight.Enabled end function Flashlight:Off() Flashlight.Enabled = false - (self.ToggleSound :: Sound):Play(); - (self.SpotLight :: SpotLight).Enabled = Flashlight.Enabled + self.ToggleSound:Play(); + self.SpotLight.Enabled = Flashlight.Enabled end function Flashlight:Toggle() @@ -102,8 +121,4 @@ function Flashlight:Toggle() end end -function Flashlight:SetCFrame(CameraCFrame: CFrame) - -end - return Flashlight \ No newline at end of file diff --git a/src/client/Character/Server/Shadows.lua b/src/client/Character/Server/Shadows.lua index 4562b7e..4aeba76 100644 --- a/src/client/Character/Server/Shadows.lua +++ b/src/client/Character/Server/Shadows.lua @@ -4,7 +4,23 @@ --This really should be only client -local Shadows = {} +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + PartToggle: (self: ClassConstructor, Instance: BasePart, CastingShadow: boolean) -> (), + Remove: (self: ClassConstructor, Key: Enum.KeyCode) -> (), + on: (self: ClassConstructor) -> (), + off: (self: ClassConstructor) -> () +} + +type Constructor_Fun = (Character: Character) -> ClassConstructor +type Constructor_Return_Props = { + Character: Character +} + +local Shadows = {} :: Impl_Constructor Shadows.__index = Shadows type Character = Model @@ -21,10 +37,10 @@ function Shadows:PartToggle(Instance: BasePart, CastingShadow: boolean) end end -local function CharacterShadows(self, enabled: boolean) +local function CharacterShadows(self: ClassConstructor, enabled: boolean) local CharacterDescendants = self.Character:GetDescendants() for i = 1, #CharacterDescendants do - self:PartToggle(CharacterDescendants[i], enabled) + self:PartToggle(CharacterDescendants[i] :: BasePart, enabled) end end diff --git a/src/client/Character/Server/SpineKinematics.lua b/src/client/Character/Server/SpineKinematics.lua index a35912a..f6cca3b 100644 --- a/src/client/Character/Server/SpineKinematics.lua +++ b/src/client/Character/Server/SpineKinematics.lua @@ -2,23 +2,33 @@ --!native --!strict -local Spine = { - Enabled = true, - Multiplier = .5 -} -Spine.__index = Spine - type Head = BasePart type UpperTorso = BasePart type Neck = Motor6D type Waist = Motor6D - type NeckC0 = CFrame type WaistC0 = CFrame - type UDP = UnreliableRemoteEvent -type struct_Spine = { +type struct_SpineMovement = { + Neck: CFrame, + Waist: CFrame +} + +type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor)) +type Impl_Constructor = { + __index: Impl_Constructor, + constructor: Constructor_Fun, + --Class functions + Move: (self: ClassConstructor, CameraCFrame: CFrame, IsFirstPerson: boolean) -> () +} & Impl_Static_Props + +type Constructor_Fun = (Head: Head, UpperTorso: UpperTorso) -> ClassConstructor +type Impl_Static_Props = { + Enabled: boolean, + Multiplier: number +} +type Constructor_Return_Props = { Remote: UDP, Head: Head, UpperTorso: UpperTorso, @@ -28,29 +38,29 @@ type struct_Spine = { WaistC0: WaistC0 } -local Remote = Instance.new("UnreliableRemoteEvent") +local Spine = {} :: Impl_Constructor +Spine.__index = Spine + +Spine.Enabled = true +Spine.Multiplier = .5 + +local Remote = Instance.new("UnreliableRemoteEvent") :: UDP Remote.Name = "SpineStream" Remote.Parent = _G.include(script, "CharacterShared") function Spine.constructor(Head: Head, UpperTorso: UpperTorso) - local self = {} :: struct_Spine + local self = {} self.Head = Head self.UpperTorso = UpperTorso - self.Neck = Head:WaitForChild("Neck") :: Neck - self.Waist = UpperTorso:WaitForChild("Waist") :: Waist - self.Remote = Remote :: UDP - - self.NeckC0 = self.Neck.C0 - self.WaistC0 = self.Waist.C0 + self.Neck = Head:WaitForChild("Neck") :: Motor6D + self.Waist = UpperTorso:WaitForChild("Waist") :: Motor6D + self.Remote = Remote + self.NeckC0 = (self.Neck :: Motor6D).C0 + self.WaistC0 = (self.Neck :: Motor6D).C0 return setmetatable(self, Spine) end -type struct_SpineMovement = { - Neck: CFrame, - Waist: CFrame -} - -local function SpineMovement(self, CameraCFrame: CFrame, IsFirstPerson: boolean): struct_SpineMovement +local function SpineMovement(self: ClassConstructor, CameraCFrame: CFrame, IsFirstPerson: boolean): struct_SpineMovement local HeadCFrame: CFrame = self.Head.CFrame local TorsoPosition: Vector3 = self.UpperTorso.Position local TorsoLookVector: Vector3 = self.UpperTorso.CFrame.LookVector diff --git a/src/shared/types/class.lua b/src/shared/types/class.lua deleted file mode 100644 index 43c4e66..0000000 --- a/src/shared/types/class.lua +++ /dev/null @@ -1,11 +0,0 @@ --- type AccountImpl = { --- __index: AccountImpl, --- new: (name: string, balance: number) -> Account, --- deposit: (self: Account, credit: number) -> (), --- withdraw: (self: Account, debit: number) -> (), --- } - --- type Account = typeof(setmetatable({} :: { name: string, balance: number }, {} :: AccountImpl)) - -type hself = {__index: T} & U -export type constructor = typeof(setmetatable({} :: T & hself, {} :: U)) \ No newline at end of file