-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBeizerManager.lua
359 lines (298 loc) · 10.5 KB
/
BeizerManager.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
-- // Services
local Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
-- // Vars
local CurrentCamera = Workspace.CurrentCamera
local RenderStepped = RunService.RenderStepped
-- //
local function MouseOffsetFunction(self, X, Y)
local Offset = (typeof(self.Offset) == "function" and self.Offset() or self.Offset)
local NewPosition = Vector2.new(X, Y) + Offset
mousemoveabs(NewPosition.X, NewPosition.Y)
end
-- // Combine two tables
local function CombineTables(Base, ToAdd)
-- // Default
Base = Base or {}
ToAdd = ToAdd or {}
-- // Loop through data we want to add
for i, v in pairs(ToAdd) do
-- // Recursive
local BaseValue = Base[i] or false
if (typeof(v) == "table" and typeof(BaseValue) == "table") then
CombineTables(BaseValue, v)
continue
end
-- // Set
Base[i] = v
end
-- // Return
return Base
end
-- // Deep copying
local function DeepCopy(Original)
-- // Assert
assert(typeof(Original) == "table", "invalid type for Original (expected table)")
-- // Vars
local Copy = {}
-- // Loop through original
for i, v in pairs(Original) do
-- // Recursion if table
if (typeof(v) == "table") then
v = DeepCopy(v)
end
-- // Set
Copy[i] = v
end
-- // Return the copy
return Copy
end
-- //
local BeizerManager = {}
BeizerManager.__index = BeizerManager
do
-- // Default Data
BeizerManager.DefaultData = {
CurrentMode = "Mouse",
t = 0,
tThreshold = 0.99995,
StartPoint = Vector2.new(),
EndPoint = Vector2.new(),
CurvePoints = {
Vector2.new(1, 1),
Vector2.new(1, 1)
},
Active = false,
Smoothness = 0.0025,
DrawPath = false,
Function = MouseOffsetFunction,
Offset = function() return Vector2.new() end, -- // can be static too,
Started = false,
}
-- // Constructor
function BeizerManager.new(Data)
-- // Initialise object
local self = setmetatable({}, BeizerManager)
-- // Initialise defaults
Data = CombineTables(DeepCopy(BeizerManager.DefaultData), Data)
CombineTables(self, Data)
-- // Return Object
return self
end
-- // Aim to
function BeizerManager:ChangeData(Data)
-- // Vars
self.StartPoint = (self.GetStartPoint() or Data.StartPoint)
self.EndPoint = self.ModifyEndPoint(Data.TargetPosition)
self.Smoothness = Data.Smoothness or self.Smoothness
self.CurvePoints = Data.CurvePoints or self.CurvePoints
self.DrawPath = Data.DrawPath or self.DrawPath
-- // Set Active
self.t = 0
self.Active = true
end
-- // Do
function BeizerManager.CubicCurve(t, StartPoint, EndPoint, ControlPointA, ControlPointB)
local t1 = (1 - t)
local A = t1^3 * StartPoint
local B = 3 * t1^2 * t * ControlPointA
local C = 3 * t1 * t^2 * ControlPointB
local D = t^3 * EndPoint
return A + B + C + D
end
-- //
function BeizerManager.DoControlPoint(StartPoint, EndPoint, ControlPointA, ControlPointB)
-- //
local Change = (EndPoint - StartPoint)
-- // Calculate the control points - relative to the start and end points
local A = StartPoint + (Change * ControlPointA)
local B = StartPoint + (Change * ControlPointB)
-- //
return A, B
end
-- // Draw path
function BeizerManager.DrawPathFunc(CurvePosition, A, B)
local Path = Drawing.new("Circle")
Path.Radius = 2
Path.Color = Color3.fromRGB(255, 150, 150)
Path.Visible = true
Path.Position = CurvePosition
task.delay(1, function()
Path:Remove()
end)
local ControlPointA = Drawing.new("Circle")
ControlPointA.Radius = 5
ControlPointA.Color = Color3.fromRGB(225, 150, 255)
ControlPointA.Visible = true
ControlPointA.Position = A
task.delay(1, function()
ControlPointA:Remove()
end)
local ControlPointB = Drawing.new("Circle")
ControlPointB.Radius = 5
ControlPointB.Color = Color3.fromRGB(225, 150, 255)
ControlPointB.Visible = true
ControlPointB.Position = B
task.delay(1, function()
ControlPointB:Remove()
end)
end
-- //
function BeizerManager:DoIteration()
-- // Make sure is active
if (not self.Active) then
return
end
-- // Vars
local BeizerCurve = self.CubicCurve
local t = self.t
-- // I have to do it this way because a for loop stops before hand
while (t <= 1 and self.Active) do RenderStepped:Wait()
-- // Increment
t = t + self.Smoothness
-- // If past threshold, then do regular smoothing
if (t >= self.tThreshold) then
-- // Regular smoothing
local clampedT = math.clamp(t, 0, 1)
local New = self.StartPoint:Lerp(self.EndPoint, clampedT)
-- //
self:Function(New.X, New.Y)
else
-- // Work out X, Y based upon the curve
local A, B = self.DoControlPoint(self.StartPoint, self.EndPoint, unpack(self.CurvePoints))
local CurvePosition = BeizerCurve(t, self.StartPoint, self.EndPoint, A, B)
-- // Create Circle [Debugging]
if (self.DrawPath) then
BeizerManager.DrawPathFunc(CurvePosition, A, B)
end
-- //
self:Function(CurvePosition.X, CurvePosition.Y)
end
end
-- // Reset
self.Active = false
end
-- // Get Start Point
function BeizerManager.GetStartPoint()
return UserInputService:GetMouseLocation()
end
-- // Modify end point
function BeizerManager.ModifyEndPoint(EndPoint)
return EndPoint
end
-- // Start
function BeizerManager:Start()
self.Started = true
local thread = task.spawn(function()
while (self.Started) do RenderStepped:Wait()
self:DoIteration()
end
end)
return thread
end
-- // Stop
function BeizerManager:Stop()
self.Started = false
end
-- //
function BeizerManager:StopCurrent()
self.Active = false
self.t = 0
end
-- // Switch to "Camera" Mode
function BeizerManager:CameraMode()
-- // Ignore if already mode
if (self.Mode == "Camera") then
return
end
self.Mode = "Camera"
-- // Override get starting point
self.GetStartPoint = function()
local Pitch, Yaw, _ = CurrentCamera.CFrame:ToEulerAnglesYXZ()
local StartPoint = Vector2.new(Pitch, Yaw)
return StartPoint
end
-- // Override modifying the point so it now accepts a vector3
self.ModifyEndPoint = function(EndPoint)
local LookAtEndPoint = CFrame.lookAt(CurrentCamera.CFrame.Position, EndPoint)
local Pitch, Yaw, _ = LookAtEndPoint:ToEulerAnglesYXZ()
EndPoint = Vector2.new(Pitch, Yaw)
return EndPoint
end
-- // Override the camera move function
self.Function = function(self, Pitch, Yaw)
local RotationMatrix = CFrame.fromEulerAnglesYXZ(Pitch, Yaw, 0)
CurrentCamera.CFrame = CFrame.new(CurrentCamera.CFrame.Position) * RotationMatrix
end
-- // Override draw path func
self.DrawPathFunc = function()
-- // Too complicated to create in camera mode, would have to do what self.Function does then convert to ScreenPort or whatever then draw that. Well, it's not that complicated, I just rather not do it. Also, it might be resource intensive idk.
end
end
-- // Switch back to "Mouse" Mode
function BeizerManager.MouseMode(self)
-- // Ignore if already mode
if (self.Mode == "Mouse") then
return
end
self.Mode = "Mouse"
-- // Set
self.GetStartPoint = BeizerManager.GetStartPoint
self.ModifyEndPoint = BeizerManager.ModifyEndPoint
self.Function = MouseOffsetFunction
self.DrawPathFunc = BeizerManager.DrawPathFunc
end
-- // Aims to target position with mouse
function BeizerManager:AimToMouse(TargetPosition)
-- // Check if target position matches
if (typeof(TargetPosition) == "Vector3") then
TargetPosition = CurrentCamera:WorldToViewportPoint(TargetPosition)
TargetPosition = Vector2.new(TargetPosition.X, TargetPosition.Y)
end
-- // Set mode
self:MouseMode()
-- // Aim
return self:ChangeData({
TargetPosition = TargetPosition
})
end
-- // Aims to target position with camera
function BeizerManager:AimToCamera(TargetPosition)
-- // Check if target position matches
if (typeof(TargetPosition) == "Vector2") then
TargetPosition = CurrentCamera:ViewportPointToRay(TargetPosition.X, TargetPosition.Y, 0).Origin
end
-- // Set mode
self:CameraMode()
-- // Aim
return self:ChangeData({
TargetPosition = TargetPosition
})
end
-- // Dynamic Aim To
function BeizerManager:AimTo(TargetPosition, PreferMode, Data)
CombineTables(self, Data)
-- // Prefer mode
if (PreferMode) then
if (PreferMode == "Camera") then
return self:AimToCamera(TargetPosition)
elseif (PreferMode == "Mouse") then
return self:AimToMouse(TargetPosition)
end
end
-- // Vars
local IsZoomed = (CurrentCamera.CFrame.Position - CurrentCamera.Focus.Position).Magnitude < 0.7
local MouseLockedCenter = UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter
local HoldingRightClick = UserInputService.MouseBehavior == Enum.MouseBehavior.LockCurrentPosition
-- // Use mouse
if not (IsZoomed or MouseLockedCenter or HoldingRightClick) then
return self:AimToMouse(TargetPosition)
end
-- // Use camera
return self:AimToCamera(TargetPosition)
end
end
-- // Return
-- getgenv().BeizerManager = BeizerManager
return BeizerManager