Advertisement
SkyyC

Untitled

Dec 31st, 2021
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. --[[
  2.  
  3.     imskyyc
  4.     Cutscene Engine
  5.     12/12/2021
  6.    
  7. ]]--
  8.  
  9. -- Services --
  10. local TweenService = game:GetService("TweenService")
  11.  
  12. -- Setup --
  13. local Engine = {}
  14. Engine.__index = Engine
  15.  
  16. function Engine.new(): {[any]: any}
  17.     --// Create a new master cutscene table with default values
  18.     local Cutscene = {
  19.         Steps = {},
  20.         Loop = nil,
  21.         ActiveTween = nil,
  22.         RollTween = nil,
  23.         OriginalState = nil,
  24.         Playable = true
  25.     }
  26.    
  27.     --// Allow the master table to index Engine functions
  28.     setmetatable(Cutscene, Engine)
  29.    
  30.     return Cutscene
  31. end
  32.  
  33. -- Append Functions --
  34. function Engine:AppendStep(Index: number, CutsceneData: {[any]: any})
  35.     --// Set the specified step to the provided cutscene data
  36.     self.Steps[Index] = CutsceneData
  37. end
  38.  
  39. function Engine:PushStep(CutsceneData: {[any]: any})
  40.     --// Add the step to the end of the Steps table
  41.     table.insert(self.Steps, CutsceneData)
  42. end
  43.  
  44. -- Run Functions --
  45. function Engine:Play(Loop, IgnoreState)
  46.     --// Allow the tween loop to play, and set if it runs infinitely (Loop)
  47.     self.Playable = true
  48.     self.Loop = Loop
  49.    
  50.     local CurrentCamera = workspace.CurrentCamera
  51.    
  52.     --// If IgnoreState: boolean == nil then define the original state of the camera in the master table
  53.     if not IgnoreState then
  54.         self.OriginalState = {
  55.             FieldOfView = CurrentCamera.FieldOfView,
  56.             CFrame = CurrentCamera.CFrame,
  57.             CameraSubject = CurrentCamera.CameraSubject,
  58.             CameraType = CurrentCamera.CameraType,
  59.             Focus = CurrentCamera.Focus
  60.         }
  61.     end
  62.    
  63.     --// Loop through each step in the steps table
  64.     for Index, Step in ipairs(self.Steps) do
  65.         --// Check if the cutscene is playable
  66.         if self.Playable then
  67.             --// Set the CameraType to scriptable so it can be tweened
  68.             CurrentCamera.CameraType = Enum.CameraType.Scriptable
  69.            
  70.             --// Get the camera data, and prepare the master tween table
  71.             local CameraData = self.Steps[Index]
  72.             local TweenData = {
  73.                 CFrame = CameraData.CFrame.End
  74.             }
  75.            
  76.             --// Check if FieldOfView settings are defined, and set the FOV to the starting value if provided
  77.             if CameraData.FOV then
  78.                 CurrentCamera.FieldOfView = CameraData.FOV.Start
  79.                 TweenData.FieldOfView = CameraData.FOV.End
  80.             else
  81.                 CurrentCamera.FieldOfView = 70
  82.             end
  83.            
  84.             --// Check if the Camera has a Focus CFrame, and make the camera look at that CFrame while it moves
  85.             if CameraData.Focus then
  86.                 local Focus = CameraData.Focus
  87.                
  88.                 local StartPosition = CameraData.CFrame.Start.Position
  89.                 local EndPosition = CameraData.CFrame.End.Position
  90.                
  91.                 CurrentCamera.CFrame = CFrame.lookAt(StartPosition, Focus)
  92.                 TweenData.CFrame = CFrame.lookAt(EndPosition, Focus)
  93.             else
  94.                 CurrentCamera.CFrame = CameraData.CFrame.Start
  95.             end
  96.            
  97.             --// Check if Roll settings are defined, and create a new Roll value to tween the roll of the camera.
  98.             if CameraData.Roll then
  99.                 local Roll = Instance.new("NumberValue"); Roll.Value = CameraData.Roll.Start
  100.                 Roll:GetPropertyChangedSignal("Value"):Connect(function()
  101.                     CurrentCamera:SetRoll(Roll.Value)
  102.                 end)
  103.                
  104.                 self.RollTween = TweenService:Create(Roll, TweenInfo.new(
  105.                     CameraData.Time,
  106.                     CameraData.EasingStyle,
  107.                     CameraData.EasingDirection), {Value = CameraData.Roll.End})
  108.                
  109.                 self.RollTween:Play()
  110.             end
  111.            
  112.             --// Define the active tween and wait for it to complete
  113.             self.ActiveTween = TweenService:Create(CurrentCamera, TweenInfo.new(
  114.                 CameraData.Time,
  115.                 CameraData.EasingStyle,
  116.                 CameraData.EasingDirection), TweenData)
  117.  
  118.             self.ActiveTween:Play()
  119.            
  120.             self.ActiveTween.Completed:Wait()
  121.         else --// break out of the loop if the cutscene has been paused
  122.             break
  123.         end
  124.     end
  125.    
  126.     --// If Loop == true then restart the cutscene
  127.     if Loop then
  128.         self:Play(Loop, true)
  129.     else --// Reset the camera
  130.         self.Playable = false
  131.        
  132.         for Property, Value in ipairs(self.OriginalState) do
  133.             CurrentCamera[Property] = Value
  134.         end
  135.     end
  136. end
  137.  
  138. --// Pause the camera and roll tweens
  139. function Engine:Pause()
  140.     if self.ActiveTween then
  141.         self.ActiveTween:Pause()
  142.     end
  143.    
  144.     if self.RollTween then
  145.         self.RollTween:Pause()
  146.     end
  147. end
  148.  
  149. --// Resume the camera and roll tweens
  150. function Engine:Resume()
  151.     if self.ActiveTween then
  152.         self.ActiveTween:Play()
  153.     end
  154.    
  155.     if self.RollTween then
  156.         self.RollTween:Play()
  157.     end
  158. end
  159.  
  160. --// Cancel (reset) the camera and roll tweens
  161. function Engine:Cancel()
  162.     if self.ActiveTween then
  163.         self.ActiveTween:Cancel()
  164.     end
  165.    
  166.     if self.RollTween then
  167.         self.RollTween:Cancel()
  168.     end
  169. end
  170.  
  171. --// Destroy the camera and roll tweens, and reset the camera to its original state.
  172. function Engine:Destroy()
  173.     local CurrentCamera = workspace.CurrentCamera
  174.    
  175.     if self.ActiveTween then
  176.         self.ActiveTween:Destroy()
  177.     end
  178.    
  179.     if self.RollTween then
  180.         self.RollTween:Destroy()
  181.     end
  182.    
  183.     for Property, Value in ipairs(self.OriginalState) do
  184.         CurrentCamera[Property] = Value
  185.     end
  186.    
  187.     self = nil
  188. end
  189.  
  190. return Engine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement