Camera zoom keybind and starting camera bobbing animations

This commit is contained in:
2023-12-20 17:44:46 -05:00
parent bfca13ed6e
commit b879e5ac85
6 changed files with 141 additions and 76 deletions

View File

@@ -1,6 +1,6 @@
local Character = script.Parent local Character = script.Parent
local Shadows = require(script:WaitForChild("Shadows")) local Shadows = require(script:WaitForChild("Shadows"))
local HumanoidModule = require(script:WaitForChild("Humanoid")) local HumanoidModule = require(script:WaitForChild("Humanoid"))
local Humanoid = Character:WaitForChild("Humanoid") local Humanoid = Character:WaitForChild("Humanoid")

View File

@@ -0,0 +1,3 @@
return function(dt)
end

View File

@@ -1,13 +1,26 @@
local Camera = { local Camera = {}
FOV = {
Default = 70,
Zoomed = 50
}
}
Camera.__index = Camera Camera.__index = Camera
function Camera.constructor(Player: Player) Camera.FOV = {
Default = 70,
Zoomed = 40,
}
Camera.FOV.Speed = {
In = 0.3,
Out = 0.4
}
local Storage = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")
local Tween = require(Storage:WaitForChild("Tween"))
-- local Bobbing = require(script:WaitForChild("Bobbing"))
local ZoomTween = Tween.constructor()
function Camera.constructor(CurrentCamera: Camera, Player: Player)
return setmetatable({ return setmetatable({
Camera = CurrentCamera,
Player = Player Player = Player
}, Camera) }, Camera)
end end
@@ -16,4 +29,28 @@ function Camera:LockFirstPerson()
self.Player.CameraMode = Enum.CameraMode.LockFirstPerson self.Player.CameraMode = Enum.CameraMode.LockFirstPerson
end end
function Camera:EnableBobbing()
self.CameraFrames = RS.RenderStepped:Connect(function(dt)
end)
end
function Camera:DisableBobbing()
if self.CameraFrames then
self.CameraFrames:Disconnect()
end
end
function Camera:ZoomIn()
ZoomTween:Start(self.Camera, {
FieldOfView = Camera.FOV.Zoomed
}, TweenInfo.new(Camera.FOV.Speed.In, Enum.EasingStyle.Quad))
end
function Camera:ZoomOut()
ZoomTween:Start(self.Camera, {
FieldOfView = Camera.FOV.Default
}, TweenInfo.new(Camera.FOV.Speed.Out, Enum.EasingStyle.Quad))
end
return Camera return Camera

View File

@@ -4,12 +4,36 @@ local CameraSettings = require(script:WaitForChild("Camera"))
local Crosshair = require(script:WaitForChild("Crosshair")) local Crosshair = require(script:WaitForChild("Crosshair"))
local Players = game:GetService("Players") local Players = game:GetService("Players")
local Player = Players.LocalPlayer local Storage = game:GetService("ReplicatedStorage")
local Camera = CameraSettings.constructor(Player) local ClientStorage = Storage:WaitForChild("Client")
local KeyBindsModule = require(ClientStorage:WaitForChild("KeyBinds"))
local Player = Players.LocalPlayer
local CurrentCamera
repeat
task.wait()
CurrentCamera = workspace.CurrentCamera
until CurrentCamera
local Camera = CameraSettings.constructor(CurrentCamera, Player)
local CrosshairObject = Crosshair.constructor(Player) local CrosshairObject = Crosshair.constructor(Player)
--Keybinds
local function CameraBinds()
local CameraBindMap = KeyBindsModule.constructor()
CameraBindMap:AddInputBegan(Enum.KeyCode.C, function()
Camera:ZoomIn()
end)
CameraBindMap:AddInputEnded(Enum.KeyCode.C, function()
Camera:ZoomOut()
end)
end
CoreGuis:off() CoreGuis:off()
Mouse:DisablePointer() Mouse:DisablePointer()
Camera:LockFirstPerson() Camera:LockFirstPerson()
CrosshairObject:Spawn() CrosshairObject:Spawn()
CameraBinds()

View File

@@ -1,7 +1,5 @@
local Binds = {} local BindLink = {}
local BindsMap = {} BindLink.__index = BindLink
Binds.__index = Binds
BindsMap.__index = BindsMap
local UIS = game:GetService("UserInputService") local UIS = game:GetService("UserInputService")
@@ -10,57 +8,64 @@ export type KeyBindMap = {
[Enum.KeyCode]: () -> () [Enum.KeyCode]: () -> ()
} }
} }
export type InputBegan = RBXScriptConnection
function Binds.constructor() --Allow multiple bindings of the same keys, no overwrites export type InputEnded = RBXScriptConnection
local RegisteredKeys: KeyBindMap = {
Began = {},
Ended = {}
}
return setmetatable({
RegisteredKeys = RegisteredKeys
}, BindsMap)
end
type CallbackFunction = () -> () type CallbackFunction = () -> ()
function Binds:Add(Key: Enum.KeyCode, Callback: CallbackFunction) function BindLink.constructor() --Allow multiple bindings of the same keys, no overwrites
local k = self.RegisteredKeys[Key] type BindConstructor = {
if k then BindMap: KeyBindMap,
warn(`Key "{Key.Name}" is already binded on this KeyBind map`) InputBegan: InputBegan,
InputEnded: InputEnded
}
local self: BindConstructor = {}
self.BindMap = {
Began = {},
Ended = {}
}
--Return these for convenience
self.InputBegan = UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
local switch = self.BindMap.Began[input.KeyCode]
if switch then
switch()
else
--switch.default()
end
end
end)
self.InputEnded = UIS.InputEnded:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
local switch = self.BindMap.Ended[input.KeyCode]
if switch then
switch()
else
--switch.default()
end
end
end)
return setmetatable(self, BindLink)
end
function BindLink:AddInputBegan(Key: Enum.KeyCode, Callback: CallbackFunction)
if self.BindMap.Began[Key] then
warn(`Key >began< "{Key.Name}" is already binded on this KeyBind map`, debug.traceback())
end end
self.RegisteredKeys[Key] = Callback self.BindMap.Began[Key] = Callback
end end
function BindsMap.NewBindState(RegisteredKeys: KeyBindMap) function BindLink:AddInputEnded(Key: Enum.KeyCode, Callback: CallbackFunction)
local InputBegan = UIS.InputBegan:Connect(function(input, gameProcessedEvent) if self.BindMap.Ended[Key] then
if not gameProcessedEvent then warn(`Key >ended< "{Key.Name}" is already binded on this KeyBind map`, debug.traceback())
local switch = RegisteredKeys.Began[input.KeyCode] end
if switch then self.BindMap.Ended[Key] = Callback
switch()
else
--switch.default()
end
end
end)
local InputEnded = UIS.InputEnded:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
local switch = RegisteredKeys.Ended[input.KeyCode]
if switch then
switch()
else
--switch.default()
end
end
end)
return setmetatable({
--Return these for convenience
InputBegan = InputBegan,
InputEnded = InputEnded
}, BindsMap)
end end
function BindLink:KeyHold(Key: Enum.KeyCode): boolean
return UIS:IsKeyDown(Key)
end
return BindLink
return Binds

View File

@@ -5,11 +5,7 @@ local TS = game:GetService("TweenService")
type TweenAnimation = {[string]: any} type TweenAnimation = {[string]: any}
function Tween.constructor( function Tween.constructor(TweenSettings: TweenInfo | nil, Object: Instance | nil, PreProperties: TweenAnimation | nil)
TweenSettings: TweenInfo,
Object: Instance | nil,
PreProperties: TweenAnimation | nil
)
return setmetatable({ return setmetatable({
TweenInfo = TweenSettings, TweenInfo = TweenSettings,
Instance = Object, Instance = Object,
@@ -17,30 +13,30 @@ function Tween.constructor(
}, Tween) }, Tween)
end end
function Tween:Start( function Tween:Start(PostInstance: Instance | nil, PostProperties: TweenAnimation | nil, PostTweenSettings: TweenInfo | nil): Tween
PostInstance: Instance | nil,
PostProperties: TweenAnimation | nil,
PostTweenSettings: TweenInfo | nil
): Tween
local Props = self.PreProperties local Props = self.PreProperties
local Object = self.Instance local Object = self.Instance
local TweenSettings = self.TweenInfo local TweenSettings = self.TweenInfo
if PostProperties and self.PreProperties then if PostProperties then
for tween_prop, tween_value in PostProperties do if self.PreProperties then
Props[tween_prop] = tween_value for tween_prop, tween_value in PostProperties do
Props[tween_prop] = tween_value
end
print("Tween library: Combining PostProperties and PreProperties together", debug.traceback())
else
Props = PostProperties
end end
print("Tween library: Combining PostProperties and PreProperties")
end end
if PostInstance then if PostInstance then
if Object then if Object then
print("Tween library: Overwriting an already defined animating object old=", Object, "new=", PostInstance) print("Tween library: Overwriting an already defined animating object old=", Object, "new=", PostInstance, debug.traceback())
end end
Object = PostInstance Object = PostInstance
end end
if PostTweenSettings then if PostTweenSettings then
if TweenSettings then if TweenSettings then
print("Tween library: Overwriting already defined Tween settings") print("Tween library: Overwriting already defined Tween settings", debug.traceback())
end end
TweenSettings = PostTweenSettings TweenSettings = PostTweenSettings
end end