-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOTS Camera System.lua
372 lines (304 loc) · 11.2 KB
/
OTS Camera System.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
local CLASS = {}
--// SERVICES //--
local PLAYERS_SERVICE = game:GetService("Players")
local RUN_SERVICE = game:GetService("RunService")
local USER_INPUT_SERVICE = game:GetService("UserInputService")
--// CONSTANTS //--
local LOCAL_PLAYER = PLAYERS_SERVICE.LocalPlayer
local MOUSE = LOCAL_PLAYER:GetMouse()
local UPDATE_UNIQUE_KEY = "OTS_CAMERA_SYSTEM_UPDATE"
--// VARIABLES //--
--// CONSTRUCTOR //--
function CLASS.new()
--// Events //--
local activeCameraSettingsChangedEvent = Instance.new("BindableEvent")
local characterAlignmentChangedEvent = Instance.new("BindableEvent")
local mouseStepChangedEvent = Instance.new("BindableEvent")
local shoulderDirectionChangedEvent = Instance.new("BindableEvent")
local enabledEvent = Instance.new("BindableEvent")
local disabledEvent = Instance.new("BindableEvent")
----
local dataTable = setmetatable(
{
--// Properties //--
SavedCameraSettings = nil,
SavedMouseBehavior = nil,
ActiveCameraSettings = nil,
HorizontalAngle = 0,
VerticalAngle = 0,
ShoulderDirection = 1,
----
--// Flags //--
IsCharacterAligned = false,
IsMouseSteppedIn = false,
IsEnabled = false,
----
--// Events //--
ActiveCameraSettingsChangedEvent = activeCameraSettingsChangedEvent,
ActiveCameraSettingsChanged = activeCameraSettingsChangedEvent.Event,
CharacterAlignmentChangedEvent = characterAlignmentChangedEvent,
CharacterAlignmentChanged = characterAlignmentChangedEvent.Event,
MouseStepChangedEvent = mouseStepChangedEvent,
MouseStepChanged = mouseStepChangedEvent.Event,
ShoulderDirectionChangedEvent = shoulderDirectionChangedEvent,
ShoulderDirectionChanged = shoulderDirectionChangedEvent.Event,
EnabledEvent = enabledEvent,
Enabled = enabledEvent.Event,
DisabledEvent = disabledEvent,
Disabled = disabledEvent.Event,
----
--// Configurations //--
VerticalAngleLimits = NumberRange.new(-45, 45),
----
--// Camera Settings //--
CameraSettings = {
DefaultShoulder = {
FieldOfView = 70,
Offset = Vector3.new(2.5, 2.5, 8),
Sensitivity = 3,
LerpSpeed = 0.5
},
ZoomedShoulder = {
FieldOfView = 40,
Offset = Vector3.new(1.5, 1.5, 6),
Sensitivity = 1.5,
LerpSpeed = 0.5
}
}
----
},
CLASS
)
local proxyTable = setmetatable(
{
},
{
__index = function(self, index)
return dataTable[index]
end,
__newindex = function(self, index, newValue)
dataTable[index] = newValue
end
}
)
return proxyTable
end
--// FUNCTIONS //--
local function Lerp(x, y, a)
return x + (y - x) * a
end
--// METHODS //--
--// //--
function CLASS:SetActiveCameraSettings(cameraSettings)
assert(cameraSettings ~= nil, "OTS Camera System Argument Error: Argument 1 nil or missing")
assert(typeof(cameraSettings) == "string", "OTS Camera System Argument Error: string expected, got " .. typeof(cameraSettings))
assert(self.CameraSettings[cameraSettings] ~= nil, "OTS Camera System Argument Error: Attempt to set unrecognized camera settings " .. cameraSettings)
if (self.IsEnabled == false) then
warn("OTS Camera System Logic Warning: Attempt to change active camera settings without enabling OTS camera system")
return
end
self.ActiveCameraSettings = cameraSettings
self.ActiveCameraSettingsChangedEvent:Fire(cameraSettings)
end
function CLASS:SetCharacterAlignment(aligned)
assert(aligned ~= nil, "OTS Camera System Argument Error: Argument 1 nil or missing")
assert(typeof(aligned) == "boolean", "OTS Camera System Argument Error: boolean expected, got " .. typeof(aligned))
if (self.IsEnabled == false) then
warn("OTS Camera System Logic Warning: Attempt to change character alignment without enabling OTS camera system")
return
end
local character = LOCAL_PLAYER.Character
local humanoid = (character ~= nil) and (character:FindFirstChild("Humanoid"))
humanoid.AutoRotate = not aligned
self.IsCharacterAligned = aligned
self.CharacterAlignmentChangedEvent:Fire(aligned)
end
function CLASS:SetMouseStep(steppedIn)
assert(steppedIn ~= nil, "OTS Camera System Argument Error: Argument 1 nil or missing")
assert(typeof(steppedIn) == "boolean", "OTS Camera System Argument Error: boolean expected, got " .. typeof(steppedIn))
if (self.IsEnabled == false) then
warn("OTS Camera System Logic Warning: Attempt to change mouse step without enabling OTS camera system")
return
end
self.IsMouseSteppedIn = steppedIn
self.MouseStepChangedEvent:Fire(steppedIn)
if (steppedIn == true) then
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.LockCenter
else
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.Default
end
end
function CLASS:SetShoulderDirection(shoulderDirection)
assert(shoulderDirection ~= nil, "OTS Camera System Argument Error: Argument 1 nil or missing")
assert(typeof(shoulderDirection) == "number", "OTS Camera System Argument Error: number expected, got " .. typeof(shoulderDirection))
assert(math.abs(shoulderDirection) == 1, "OTS Camera System Argument Error: Attempt to set unrecognized shoulder direction " .. shoulderDirection)
if (self.IsEnabled == false) then
warn("OTS Camera System Logic Warning: Attempt to change shoulder direction without enabling OTS camera system")
return
end
self.ShoulderDirection = shoulderDirection
self.ShoulderDirectionChangedEvent:Fire(shoulderDirection)
end
----
--// //--
function CLASS:SaveCameraSettings()
local currentCamera = workspace.CurrentCamera
self.SavedCameraSettings = {
FieldOfView = currentCamera.FieldOfView,
CameraSubject = currentCamera.CameraSubject,
CameraType = currentCamera.CameraType
}
end
function CLASS:LoadCameraSettings()
local currentCamera = workspace.CurrentCamera
for setting, value in pairs(self.SavedCameraSettings) do
currentCamera[setting] = value
end
end
----
--// //--
function CLASS:Update()
local currentCamera = workspace.CurrentCamera
local activeCameraSettings = self.CameraSettings[self.ActiveCameraSettings]
--// Address mouse behavior and camera type //--
if (self.IsMouseSteppedIn == true) then
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.LockCenter
else
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.Default
end
currentCamera.CameraType = Enum.CameraType.Scriptable
---
--// Address mouse input //--
local mouseDelta = USER_INPUT_SERVICE:GetMouseDelta() * activeCameraSettings.Sensitivity
self.HorizontalAngle -= mouseDelta.X/currentCamera.ViewportSize.X
self.VerticalAngle -= mouseDelta.Y/currentCamera.ViewportSize.Y
self.VerticalAngle = math.rad(math.clamp(math.deg(self.VerticalAngle), self.VerticalAngleLimits.Min, self.VerticalAngleLimits.Max))
----
local character = LOCAL_PLAYER.Character
local humanoidRootPart = (character ~= nil) and (character:FindFirstChild("HumanoidRootPart"))
if (humanoidRootPart ~= nil) then
--// Lerp field of view //--
currentCamera.FieldOfView = Lerp(
currentCamera.FieldOfView,
activeCameraSettings.FieldOfView,
activeCameraSettings.LerpSpeed
)
----
--// Address shoulder direction //--
local offset = activeCameraSettings.Offset
offset = Vector3.new(offset.X * self.ShoulderDirection, offset.Y, offset.Z)
----
--// Calculate new camera cframe //--
local newCameraCFrame = CFrame.new(humanoidRootPart.Position) *
CFrame.Angles(0, self.HorizontalAngle, 0) *
CFrame.Angles(self.VerticalAngle, 0, 0) *
CFrame.new(offset)
newCameraCFrame = currentCamera.CFrame:Lerp(newCameraCFrame, activeCameraSettings.LerpSpeed)
----
--// Raycast for obstructions //--
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(
humanoidRootPart.Position,
newCameraCFrame.p - humanoidRootPart.Position,
raycastParams
)
----
--// Address obstructions if any //--
if (raycastResult ~= nil) then
local obstructionDisplacement = (raycastResult.Position - humanoidRootPart.Position)
local obstructionPosition = humanoidRootPart.Position + (obstructionDisplacement.Unit * (obstructionDisplacement.Magnitude - 0.1))
local x,y,z,r00,r01,r02,r10,r11,r12,r20,r21,r22 = newCameraCFrame:components()
newCameraCFrame = CFrame.new(obstructionPosition.x, obstructionPosition.y, obstructionPosition.z, r00, r01, r02, r10, r11, r12, r20, r21, r22)
end
----
--// Address character alignment //--
if (self.IsCharacterAligned == true) then
local newHumanoidRootPartCFrame = CFrame.new(humanoidRootPart.Position) *
CFrame.Angles(0, self.HorizontalAngle, 0)
humanoidRootPart.CFrame = humanoidRootPart.CFrame:Lerp(newHumanoidRootPartCFrame, activeCameraSettings.LerpSpeed/2)
end
----
currentCamera.CFrame = newCameraCFrame
else
self:Disable()
end
end
function CLASS:ConfigureStateForEnabled()
self:SaveCameraSettings()
self.SavedMouseBehavior = USER_INPUT_SERVICE.MouseBehavior
self:SetActiveCameraSettings("DefaultShoulder")
self:SetCharacterAlignment(false)
self:SetMouseStep(true)
self:SetShoulderDirection(1)
--// Calculate angles //--
local cameraCFrame = workspace.CurrentCamera.CFrame
local x, y, z = cameraCFrame:ToOrientation()
local horizontalAngle = y
local verticalAngle = x
----
self.HorizontalAngle = horizontalAngle
self.VerticalAngle = verticalAngle
end
function CLASS:ConfigureStateForDisabled()
self:LoadCameraSettings()
USER_INPUT_SERVICE.MouseBehavior = self.SavedMouseBehavior
self:SetActiveCameraSettings("DefaultShoulder")
self:SetCharacterAlignment(false)
self:SetMouseStep(false)
self:SetShoulderDirection(1)
self.HorizontalAngle = 0
self.VerticalAngle = 0
end
function CLASS:Enable()
assert(self.IsEnabled == false, "OTS Camera System Logic Error: Attempt to enable without disabling")
self.IsEnabled = true
self.EnabledEvent:Fire()
self:ConfigureStateForEnabled()
RUN_SERVICE:BindToRenderStep(
UPDATE_UNIQUE_KEY,
Enum.RenderPriority.Camera.Value - 10,
function()
if (self.IsEnabled == true) then
self:Update()
end
end
)
end
function CLASS:Disable()
assert(self.IsEnabled == true, "OTS Camera System Logic Error: Attempt to disable without enabling")
self:ConfigureStateForDisabled()
self.IsEnabled = false
self.DisabledEvent:Fire()
RUN_SERVICE:UnbindFromRenderStep(UPDATE_UNIQUE_KEY)
end
----
--// INSTRUCTIONS //--
CLASS.__index = CLASS
local singleton = CLASS.new()
USER_INPUT_SERVICE.InputBegan:Connect(function(inputObject, gameProcessedEvent)
if (gameProcessedEvent == false) and (singleton.IsEnabled == true) then
if (inputObject.KeyCode == Enum.KeyCode.Q) then
singleton:SetShoulderDirection(-1)
elseif (inputObject.KeyCode == Enum.KeyCode.E) then
singleton:SetShoulderDirection(1)
end
if (inputObject.UserInputType == Enum.UserInputType.MouseButton2) then
singleton:SetActiveCameraSettings("ZoomedShoulder")
end
if (inputObject.KeyCode == Enum.KeyCode.LeftControl) then
if (singleton.IsEnabled == true) then
singleton:SetMouseStep(not singleton.IsMouseSteppedIn)
end
end
end
end)
USER_INPUT_SERVICE.InputEnded:Connect(function(inputObject, gameProcessedEvent)
if (gameProcessedEvent == false) and (singleton.IsEnabled == true) then
if (inputObject.UserInputType == Enum.UserInputType.MouseButton2) then
singleton:SetActiveCameraSettings("DefaultShoulder")
end
end
end)
return singleton