Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- imskyyc
- Cutscene Engine
- 12/12/2021
- ]]--
- -- Services --
- local TweenService = game:GetService("TweenService")
- -- Setup --
- local Engine = {}
- Engine.__index = Engine
- function Engine.new(): {[any]: any}
- --// Create a new master cutscene table with default values
- local Cutscene = {
- Steps = {},
- Loop = nil,
- ActiveTween = nil,
- RollTween = nil,
- OriginalState = nil,
- Playable = true
- }
- --// Allow the master table to index Engine functions
- setmetatable(Cutscene, Engine)
- return Cutscene
- end
- -- Append Functions --
- function Engine:AppendStep(Index: number, CutsceneData: {[any]: any})
- --// Set the specified step to the provided cutscene data
- self.Steps[Index] = CutsceneData
- end
- function Engine:PushStep(CutsceneData: {[any]: any})
- --// Add the step to the end of the Steps table
- table.insert(self.Steps, CutsceneData)
- end
- -- Run Functions --
- function Engine:Play(Loop, IgnoreState)
- --// Allow the tween loop to play, and set if it runs infinitely (Loop)
- self.Playable = true
- self.Loop = Loop
- local CurrentCamera = workspace.CurrentCamera
- --// If IgnoreState: boolean == nil then define the original state of the camera in the master table
- if not IgnoreState then
- self.OriginalState = {
- FieldOfView = CurrentCamera.FieldOfView,
- CFrame = CurrentCamera.CFrame,
- CameraSubject = CurrentCamera.CameraSubject,
- CameraType = CurrentCamera.CameraType,
- Focus = CurrentCamera.Focus
- }
- end
- --// Loop through each step in the steps table
- for Index, Step in ipairs(self.Steps) do
- --// Check if the cutscene is playable
- if self.Playable then
- --// Set the CameraType to scriptable so it can be tweened
- CurrentCamera.CameraType = Enum.CameraType.Scriptable
- --// Get the camera data, and prepare the master tween table
- local CameraData = self.Steps[Index]
- local TweenData = {
- CFrame = CameraData.CFrame.End
- }
- --// Check if FieldOfView settings are defined, and set the FOV to the starting value if provided
- if CameraData.FOV then
- CurrentCamera.FieldOfView = CameraData.FOV.Start
- TweenData.FieldOfView = CameraData.FOV.End
- else
- CurrentCamera.FieldOfView = 70
- end
- --// Check if the Camera has a Focus CFrame, and make the camera look at that CFrame while it moves
- if CameraData.Focus then
- local Focus = CameraData.Focus
- local StartPosition = CameraData.CFrame.Start.Position
- local EndPosition = CameraData.CFrame.End.Position
- CurrentCamera.CFrame = CFrame.lookAt(StartPosition, Focus)
- TweenData.CFrame = CFrame.lookAt(EndPosition, Focus)
- else
- CurrentCamera.CFrame = CameraData.CFrame.Start
- end
- --// Check if Roll settings are defined, and create a new Roll value to tween the roll of the camera.
- if CameraData.Roll then
- local Roll = Instance.new("NumberValue"); Roll.Value = CameraData.Roll.Start
- Roll:GetPropertyChangedSignal("Value"):Connect(function()
- CurrentCamera:SetRoll(Roll.Value)
- end)
- self.RollTween = TweenService:Create(Roll, TweenInfo.new(
- CameraData.Time,
- CameraData.EasingStyle,
- CameraData.EasingDirection), {Value = CameraData.Roll.End})
- self.RollTween:Play()
- end
- --// Define the active tween and wait for it to complete
- self.ActiveTween = TweenService:Create(CurrentCamera, TweenInfo.new(
- CameraData.Time,
- CameraData.EasingStyle,
- CameraData.EasingDirection), TweenData)
- self.ActiveTween:Play()
- self.ActiveTween.Completed:Wait()
- else --// break out of the loop if the cutscene has been paused
- break
- end
- end
- --// If Loop == true then restart the cutscene
- if Loop then
- self:Play(Loop, true)
- else --// Reset the camera
- self.Playable = false
- for Property, Value in ipairs(self.OriginalState) do
- CurrentCamera[Property] = Value
- end
- end
- end
- --// Pause the camera and roll tweens
- function Engine:Pause()
- if self.ActiveTween then
- self.ActiveTween:Pause()
- end
- if self.RollTween then
- self.RollTween:Pause()
- end
- end
- --// Resume the camera and roll tweens
- function Engine:Resume()
- if self.ActiveTween then
- self.ActiveTween:Play()
- end
- if self.RollTween then
- self.RollTween:Play()
- end
- end
- --// Cancel (reset) the camera and roll tweens
- function Engine:Cancel()
- if self.ActiveTween then
- self.ActiveTween:Cancel()
- end
- if self.RollTween then
- self.RollTween:Cancel()
- end
- end
- --// Destroy the camera and roll tweens, and reset the camera to its original state.
- function Engine:Destroy()
- local CurrentCamera = workspace.CurrentCamera
- if self.ActiveTween then
- self.ActiveTween:Destroy()
- end
- if self.RollTween then
- self.RollTween:Destroy()
- end
- for Property, Value in ipairs(self.OriginalState) do
- CurrentCamera[Property] = Value
- end
- self = nil
- end
- return Engine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement