Advertisement
Guest User

Lua Module Example

a guest
Aug 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.73 KB | None | 0 0
  1. 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.
  2. I just cut out the set up and additional methods to showcase how beautiful as heck this code is.
  3.  
  4. --[[ The Module ]]--
  5. local BaseCamera = {}
  6. BaseCamera.__index = BaseCamera
  7.  
  8. function BaseCamera.new()
  9.     local self = setmetatable({}, BaseCamera)
  10.  
  11.     -- So that derived classes have access to this
  12.     self.FIRST_PERSON_DISTANCE_THRESHOLD = FIRST_PERSON_DISTANCE_THRESHOLD
  13.  
  14.     self.cameraType = nil
  15.     self.cameraMovementMode = nil
  16.  
  17.     local player = Players.LocalPlayer
  18.     self.lastCameraTransform = nil
  19.     self.rotateInput = ZERO_VECTOR2
  20.     self.userPanningCamera = false
  21.     self.lastUserPanCamera = tick()
  22.  
  23.     self.humanoidRootPart = nil
  24.     self.humanoidCache = {}
  25.  
  26.     -- Subject and position on last update call
  27.     self.lastSubject = nil
  28.     self.lastSubjectPosition = Vector3.new(0,5,0)
  29.  
  30.     -- These subject distance members refer to the nominal camera-to-subject follow distance that the camera
  31.     -- is trying to maintain, not the actual measured value.
  32.     -- The default is updated when screen orientation or the min/max distances change,
  33.     -- to be sure the default is always in range and appropriate for the orientation.
  34.     self.defaultSubjectDistance = Util.Clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, DEFAULT_DISTANCE)
  35.     self.currentSubjectDistance = Util.Clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, DEFAULT_DISTANCE)
  36.  
  37.     self.inFirstPerson = false
  38.     self.inMouseLockedMode = false
  39.     self.portraitMode = false
  40.     self.isSmallTouchScreen = false
  41.  
  42.     -- Used by modules which want to reset the camera angle on respawn.
  43.     self.resetCameraAngle = true
  44.  
  45.     self.enabled = false
  46.  
  47.     -- Input Event Connections
  48.     self.inputBeganConn = nil
  49.     self.inputChangedConn = nil
  50.     self.inputEndedConn = nil
  51.  
  52.     self.startPos = nil
  53.     self.lastPos = nil
  54.     self.panBeginLook = nil
  55.  
  56.     self.panEnabled = true
  57.     self.keyPanEnabled = true
  58.     self.distanceChangeEnabled = true
  59.  
  60.     self.PlayerGui = nil
  61.  
  62.     self.cameraChangedConn = nil
  63.     self.viewportSizeChangedConn = nil
  64.  
  65.     self.boundContextActions = {}
  66.  
  67.     -- VR Support
  68.     self.shouldUseVRRotation = false
  69.     self.VRRotationIntensityAvailable = false
  70.     self.lastVRRotationIntensityCheckTime = 0
  71.     self.lastVRRotationTime = 0
  72.     self.vrRotateKeyCooldown = {}
  73.     self.cameraTranslationConstraints = Vector3.new(1, 1, 1)
  74.     self.humanoidJumpOrigin = nil
  75.     self.trackingHumanoid = nil
  76.     self.cameraFrozen = false
  77.     self.subjectStateChangedConn = nil
  78.  
  79.     -- Gamepad support
  80.     self.activeGamepad = nil
  81.     self.gamepadPanningCamera = false
  82.     self.lastThumbstickRotate = nil
  83.     self.numOfSeconds = 0.7
  84.     self.currentSpeed = 0
  85.     self.maxSpeed = 6
  86.     self.vrMaxSpeed = 4
  87.     self.lastThumbstickPos = Vector2.new(0,0)
  88.     self.ySensitivity = 0.65
  89.     self.lastVelocity = nil
  90.     self.gamepadConnectedConn = nil
  91.     self.gamepadDisconnectedConn = nil
  92.     self.currentZoomSpeed = 1.0
  93.     self.L3ButtonDown = false
  94.     self.dpadLeftDown = false
  95.     self.dpadRightDown = false
  96.  
  97.     -- Touch input support
  98.     self.isDynamicThumbstickEnabled = false
  99.     self.fingerTouches = {}
  100.     self.dynamicTouchInput = nil
  101.     self.numUnsunkTouches = 0
  102.     self.inputStartPositions = {}
  103.     self.inputStartTimes = {}
  104.     self.startingDiff = nil
  105.     self.pinchBeginZoom = nil
  106.     self.userPanningTheCamera = false
  107.     self.touchActivateConn = nil
  108.  
  109.     -- Mouse locked formerly known as shift lock mode
  110.     self.mouseLockOffset = ZERO_VECTOR3
  111.  
  112.     -- [[ NOTICE ]] --
  113.     -- Initialization things used to always execute at game load time, but now these camera modules are instantiated
  114.     -- when needed, so the code here may run well after the start of the game
  115.  
  116.     if player.Character then
  117.         self:OnCharacterAdded(player.Character)
  118.     end
  119.  
  120.     player.CharacterAdded:Connect(function(char)
  121.         self:OnCharacterAdded(char)
  122.     end)
  123.  
  124.     if self.cameraChangedConn then self.cameraChangedConn:Disconnect() end
  125.     self.cameraChangedConn = workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
  126.         self:OnCurrentCameraChanged()
  127.     end)
  128.     self:OnCurrentCameraChanged()
  129.  
  130.     if self.playerCameraModeChangeConn then self.playerCameraModeChangeConn:Disconnect() end
  131.     self.playerCameraModeChangeConn = player:GetPropertyChangedSignal("CameraMode"):Connect(function()
  132.         self:OnPlayerCameraPropertyChange()
  133.     end)
  134.  
  135.     if self.minDistanceChangeConn then self.minDistanceChangeConn:Disconnect() end
  136.     self.minDistanceChangeConn = player:GetPropertyChangedSignal("CameraMinZoomDistance"):Connect(function()
  137.         self:OnPlayerCameraPropertyChange()
  138.     end)
  139.  
  140.     if self.maxDistanceChangeConn then self.maxDistanceChangeConn:Disconnect() end
  141.     self.maxDistanceChangeConn = player:GetPropertyChangedSignal("CameraMaxZoomDistance"):Connect(function()
  142.         self:OnPlayerCameraPropertyChange()
  143.     end)
  144.  
  145.     if self.playerDevTouchMoveModeChangeConn then self.playerDevTouchMoveModeChangeConn:Disconnect() end
  146.     self.playerDevTouchMoveModeChangeConn = player:GetPropertyChangedSignal("DevTouchMovementMode"):Connect(function()
  147.         self:OnDevTouchMovementModeChanged()
  148.     end)
  149.     self:OnDevTouchMovementModeChanged() -- Init
  150.  
  151.     if self.gameSettingsTouchMoveMoveChangeConn then self.gameSettingsTouchMoveMoveChangeConn:Disconnect() end
  152.     self.gameSettingsTouchMoveMoveChangeConn = UserGameSettings:GetPropertyChangedSignal("TouchMovementMode"):Connect(function()
  153.         self:OnGameSettingsTouchMovementModeChanged()
  154.     end)
  155.     self:OnGameSettingsTouchMovementModeChanged() -- Init
  156.  
  157.     UserGameSettings:SetCameraYInvertVisible()
  158.     UserGameSettings:SetGamepadCameraSensitivityVisible()
  159.  
  160.     self.hasGameLoaded = game:IsLoaded()
  161.     if not self.hasGameLoaded then
  162.         self.gameLoadedConn = game.Loaded:Connect(function()
  163.             self.hasGameLoaded = true
  164.             self.gameLoadedConn:Disconnect()
  165.             self.gameLoadedConn = nil
  166.         end)
  167.     end
  168.  
  169.     return self
  170. end
  171.  
  172. function BaseCamera:GetModuleName()
  173.     return "BaseCamera"
  174. end
  175.  
  176. function BaseCamera:OnCharacterAdded(char)
  177.     self.resetCameraAngle = self.resetCameraAngle or self:GetEnabled()
  178.     self.humanoidRootPart = nil
  179.     if UserInputService.TouchEnabled then
  180.         self.PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
  181.         for _, child in ipairs(char:GetChildren()) do
  182.             if child:IsA("Tool") then
  183.                 self.isAToolEquipped = true
  184.             end
  185.         end
  186.         char.ChildAdded:Connect(function(child)
  187.             if child:IsA("Tool") then
  188.                 self.isAToolEquipped = true
  189.             end
  190.         end)
  191.         char.ChildRemoved:Connect(function(child)
  192.             if child:IsA("Tool") then
  193.                 self.isAToolEquipped = false
  194.             end
  195.         end)
  196.     end
  197. end
  198.  
  199. function BaseCamera:GetHumanoidRootPart()
  200.     if not self.humanoidRootPart then
  201.         local player = Players.LocalPlayer
  202.         if player.Character then
  203.             local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
  204.             if humanoid then
  205.                 self.humanoidRootPart = humanoid.RootPart
  206.             end
  207.         end
  208.     end
  209.     return self.humanoidRootPart
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement