Files
Roblox-Elevator-Game/src/client/Player/Character/Actions.lua

149 lines
4.3 KiB
Lua

--!optimize 2
--!native
--!strict
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
Actions.Sneaking = false
Actions.Crouching = false
Actions.Walk = 10
Actions.SneakingSpeed = 10/2 --10 is default
--Crouch static properties
Actions.Crouching = false
Actions.StandHeight = 2.1
Actions.CrouchHeight = .6
Actions.WalkSpeedMultiplier = 6
Actions.CrouchSpeed = .2
--Flashlight static properties
Actions.FlashlightEnabled = false
local CharacterShared = _G.include(script, "CharacterShared")
local FlashlightRemote: UDP = CharacterShared:WaitForChild("Flashlight")
local Storage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tween = require(Storage:WaitForChild("Tween"))
local Delta = require(Storage:WaitForChild("Delta"))
local CrouchTween = Tween.constructor()
function Actions.constructor(HumanoidSettings: inherented, CurrentCamera: CurrentCamera, ActionsTCP: TCP)
return setmetatable({
Humanoid = HumanoidSettings.Humanoid,
HumanoidSettings = HumanoidSettings,
CurrentCamera = CurrentCamera,
ActionsTCP = ActionsTCP
}, Actions)
end
function Actions:EnableSneak()
Actions.DoingAction = true
Actions.Sneaking = true
self.HumanoidSettings:SetWalkSpeed(Actions.SneakingSpeed)
end
function Actions:DisableSneak()
Actions.DoingAction = false
Actions.Sneaking = false
self.HumanoidSettings:SetWalkSpeed(Actions.Walk)
end
function Actions:EnableCrouch()
Actions.DoingAction = true
Actions.Crouching = true
local Easing = TweenInfo.new(Actions.CrouchSpeed, Enum.EasingStyle.Linear)
local WalkSpeed = self.Humanoid.WalkSpeed
CrouchTween:Start(self.Humanoid, {
HipHeight = Actions.CrouchHeight,
WalkSpeed = math.max(1, WalkSpeed-Actions.WalkSpeedMultiplier)
}, Easing)
end
function Actions:DisableCrouch()
Actions.DoingAction = false
Actions.Crouching = false
local Easing = TweenInfo.new(Actions.CrouchSpeed, Enum.EasingStyle.Linear)
local WalkSpeed = self.Humanoid.WalkSpeed
CrouchTween:Start(self.Humanoid, {
HipHeight = Actions.StandHeight,
WalkSpeed = math.max(1, WalkSpeed+Actions.WalkSpeedMultiplier)
}, Easing)
end
function Actions:EnableFlashlight(FlashlightKey: Enum.KeyCode)
Actions.FlashlightEnabled = true
task.spawn(function()
while Actions.FlashlightEnabled do
--Remotes cant transmit dictionaries
--Note: if dictionaries are more than 1 value, encode it into JSON
local c = table.pack(self.CurrentCamera.CFrame:ToEulerAnglesXYZ())
table.insert(c, c.n)
FlashlightRemote:FireServer(c)
Delta:time()
end
end)
self.ActionsTCP:FireServer(FlashlightKey)
end
function Actions:DisableFlashlight(FlashlightKey: Enum.KeyCode)
self.ActionsTCP:FireServer(FlashlightKey)
Actions.FlashlightEnabled = false
end
function Actions:ToggleFlashlight(FlashlightKey: Enum.KeyCode)
if Actions.FlashlightEnabled then
self:DisableFlashlight(FlashlightKey)
else
self:EnableFlashlight(FlashlightKey)
end
end
return Actions