-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
GamepadRotateModel.lua
100 lines (77 loc) · 2.54 KB
/
GamepadRotateModel.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
--[=[
Rotation model for gamepad controls that uses Roblox's curve smoothing and other components.
@class GamepadRotateModel
]=]
local require = require(script.Parent.loader).load(script)
local AccelTween = require("AccelTween")
local BaseObject = require("BaseObject")
local CameraGamepadInputUtils = require("CameraGamepadInputUtils")
local GamepadRotateModel = setmetatable({}, BaseObject)
GamepadRotateModel.__index = GamepadRotateModel
GamepadRotateModel.ClassName = "GamepadRotateModel"
--[=[
Constructs a new GamepadRotateModel.
@return GamepadRotateModel
]=]
function GamepadRotateModel.new()
local self = setmetatable(BaseObject.new(), GamepadRotateModel)
self._rampVelocityX = AccelTween.new(25)
self._rampVelocityY = AccelTween.new(25)
self.IsRotating = Instance.new("BoolValue")
self.IsRotating.Value = false
self._maid:GiveTask(self.IsRotating)
return self
end
--[=[
Sets the acceleration for the game rotate model. The higher the acceleration
the more linear the gamepad rotate model feels.
:::tip
Set this to something high, like 2500, for an FPS. This makes control feel a lot better.
:::
@param acceleration number
]=]
function GamepadRotateModel:SetAcceleration(acceleration)
assert(type(acceleration) == "number", "Bad acceleration")
self._rampVelocityX.a = acceleration
self._rampVelocityY.a = acceleration
end
--[=[
Gets the delta for the thumbstick
@return Vector2
]=]
function GamepadRotateModel:GetThumbstickDeltaAngle()
if not self._lastInputObject then
return Vector2.zero
end
return Vector2.new(self._rampVelocityX.p, self._rampVelocityY.p)
end
--[=[
Stops rotation
]=]
function GamepadRotateModel:StopRotate()
self._lastInputObject = nil
self._rampVelocityX.t = 0
self._rampVelocityX.p = self._rampVelocityX.t
self._rampVelocityY.t = 0
self._rampVelocityY.p = self._rampVelocityY.t
self.IsRotating.Value = false
end
--[=[
Converts the thumbstick input into a smoothed delta based upon deadzone and other
components.
@param inputObject InputObject
]=]
function GamepadRotateModel:HandleThumbstickInput(inputObject)
if CameraGamepadInputUtils.outOfDeadZone(inputObject) then
self._lastInputObject = inputObject
local stickOffset = self._lastInputObject.Position
stickOffset = Vector2.new(-stickOffset.x, stickOffset.y) -- Invert axis!
local adjustedStickOffset = CameraGamepadInputUtils.gamepadLinearToCurve(stickOffset)
self._rampVelocityX.t = adjustedStickOffset.x
self._rampVelocityY.t = adjustedStickOffset.y
self.IsRotating.Value = true
else
self:StopRotate()
end
end
return GamepadRotateModel