Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is just a snippet of the actual full code. The first comment is actually line 72 and the last "end" is actually line 278 of 1,597 lines.
- I just cut out the set up and additional methods to showcase how beautiful as heck this code is.
- --[[ The Module ]]--
- local BaseCamera = {}
- BaseCamera.__index = BaseCamera
- function BaseCamera.new()
- local self = setmetatable({}, BaseCamera)
- -- So that derived classes have access to this
- self.FIRST_PERSON_DISTANCE_THRESHOLD = FIRST_PERSON_DISTANCE_THRESHOLD
- self.cameraType = nil
- self.cameraMovementMode = nil
- local player = Players.LocalPlayer
- self.lastCameraTransform = nil
- self.rotateInput = ZERO_VECTOR2
- self.userPanningCamera = false
- self.lastUserPanCamera = tick()
- self.humanoidRootPart = nil
- self.humanoidCache = {}
- -- Subject and position on last update call
- self.lastSubject = nil
- self.lastSubjectPosition = Vector3.new(0,5,0)
- -- These subject distance members refer to the nominal camera-to-subject follow distance that the camera
- -- is trying to maintain, not the actual measured value.
- -- The default is updated when screen orientation or the min/max distances change,
- -- to be sure the default is always in range and appropriate for the orientation.
- self.defaultSubjectDistance = Util.Clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, DEFAULT_DISTANCE)
- self.currentSubjectDistance = Util.Clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, DEFAULT_DISTANCE)
- self.inFirstPerson = false
- self.inMouseLockedMode = false
- self.portraitMode = false
- self.isSmallTouchScreen = false
- -- Used by modules which want to reset the camera angle on respawn.
- self.resetCameraAngle = true
- self.enabled = false
- -- Input Event Connections
- self.inputBeganConn = nil
- self.inputChangedConn = nil
- self.inputEndedConn = nil
- self.startPos = nil
- self.lastPos = nil
- self.panBeginLook = nil
- self.panEnabled = true
- self.keyPanEnabled = true
- self.distanceChangeEnabled = true
- self.PlayerGui = nil
- self.cameraChangedConn = nil
- self.viewportSizeChangedConn = nil
- self.boundContextActions = {}
- -- VR Support
- self.shouldUseVRRotation = false
- self.VRRotationIntensityAvailable = false
- self.lastVRRotationIntensityCheckTime = 0
- self.lastVRRotationTime = 0
- self.vrRotateKeyCooldown = {}
- self.cameraTranslationConstraints = Vector3.new(1, 1, 1)
- self.humanoidJumpOrigin = nil
- self.trackingHumanoid = nil
- self.cameraFrozen = false
- self.subjectStateChangedConn = nil
- -- Gamepad support
- self.activeGamepad = nil
- self.gamepadPanningCamera = false
- self.lastThumbstickRotate = nil
- self.numOfSeconds = 0.7
- self.currentSpeed = 0
- self.maxSpeed = 6
- self.vrMaxSpeed = 4
- self.lastThumbstickPos = Vector2.new(0,0)
- self.ySensitivity = 0.65
- self.lastVelocity = nil
- self.gamepadConnectedConn = nil
- self.gamepadDisconnectedConn = nil
- self.currentZoomSpeed = 1.0
- self.L3ButtonDown = false
- self.dpadLeftDown = false
- self.dpadRightDown = false
- -- Touch input support
- self.isDynamicThumbstickEnabled = false
- self.fingerTouches = {}
- self.dynamicTouchInput = nil
- self.numUnsunkTouches = 0
- self.inputStartPositions = {}
- self.inputStartTimes = {}
- self.startingDiff = nil
- self.pinchBeginZoom = nil
- self.userPanningTheCamera = false
- self.touchActivateConn = nil
- -- Mouse locked formerly known as shift lock mode
- self.mouseLockOffset = ZERO_VECTOR3
- -- [[ NOTICE ]] --
- -- Initialization things used to always execute at game load time, but now these camera modules are instantiated
- -- when needed, so the code here may run well after the start of the game
- if player.Character then
- self:OnCharacterAdded(player.Character)
- end
- player.CharacterAdded:Connect(function(char)
- self:OnCharacterAdded(char)
- end)
- if self.cameraChangedConn then self.cameraChangedConn:Disconnect() end
- self.cameraChangedConn = workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
- self:OnCurrentCameraChanged()
- end)
- self:OnCurrentCameraChanged()
- if self.playerCameraModeChangeConn then self.playerCameraModeChangeConn:Disconnect() end
- self.playerCameraModeChangeConn = player:GetPropertyChangedSignal("CameraMode"):Connect(function()
- self:OnPlayerCameraPropertyChange()
- end)
- if self.minDistanceChangeConn then self.minDistanceChangeConn:Disconnect() end
- self.minDistanceChangeConn = player:GetPropertyChangedSignal("CameraMinZoomDistance"):Connect(function()
- self:OnPlayerCameraPropertyChange()
- end)
- if self.maxDistanceChangeConn then self.maxDistanceChangeConn:Disconnect() end
- self.maxDistanceChangeConn = player:GetPropertyChangedSignal("CameraMaxZoomDistance"):Connect(function()
- self:OnPlayerCameraPropertyChange()
- end)
- if self.playerDevTouchMoveModeChangeConn then self.playerDevTouchMoveModeChangeConn:Disconnect() end
- self.playerDevTouchMoveModeChangeConn = player:GetPropertyChangedSignal("DevTouchMovementMode"):Connect(function()
- self:OnDevTouchMovementModeChanged()
- end)
- self:OnDevTouchMovementModeChanged() -- Init
- if self.gameSettingsTouchMoveMoveChangeConn then self.gameSettingsTouchMoveMoveChangeConn:Disconnect() end
- self.gameSettingsTouchMoveMoveChangeConn = UserGameSettings:GetPropertyChangedSignal("TouchMovementMode"):Connect(function()
- self:OnGameSettingsTouchMovementModeChanged()
- end)
- self:OnGameSettingsTouchMovementModeChanged() -- Init
- UserGameSettings:SetCameraYInvertVisible()
- UserGameSettings:SetGamepadCameraSensitivityVisible()
- self.hasGameLoaded = game:IsLoaded()
- if not self.hasGameLoaded then
- self.gameLoadedConn = game.Loaded:Connect(function()
- self.hasGameLoaded = true
- self.gameLoadedConn:Disconnect()
- self.gameLoadedConn = nil
- end)
- end
- return self
- end
- function BaseCamera:GetModuleName()
- return "BaseCamera"
- end
- function BaseCamera:OnCharacterAdded(char)
- self.resetCameraAngle = self.resetCameraAngle or self:GetEnabled()
- self.humanoidRootPart = nil
- if UserInputService.TouchEnabled then
- self.PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
- for _, child in ipairs(char:GetChildren()) do
- if child:IsA("Tool") then
- self.isAToolEquipped = true
- end
- end
- char.ChildAdded:Connect(function(child)
- if child:IsA("Tool") then
- self.isAToolEquipped = true
- end
- end)
- char.ChildRemoved:Connect(function(child)
- if child:IsA("Tool") then
- self.isAToolEquipped = false
- end
- end)
- end
- end
- function BaseCamera:GetHumanoidRootPart()
- if not self.humanoidRootPart then
- local player = Players.LocalPlayer
- if player.Character then
- local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
- if humanoid then
- self.humanoidRootPart = humanoid.RootPart
- end
- end
- end
- return self.humanoidRootPart
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement