182 lines
8.6 KiB
Luau
182 lines
8.6 KiB
Luau
-- IntroCamera (LocalScript, StarterPlayerScripts): the opening shot when the game starts (once per session).
|
|
-- 1. fade in from black, the camera circles the big Christmas tree in the middle of the map (workspace.CenterTree),
|
|
-- coming down and closer while it turns, always looking at the tree;
|
|
-- 2. then it flies in an arc to the player's spawn (the path in front of the own zone) and ends exactly where the
|
|
-- normal Roblox camera stands behind the character, so control is handed over without a jump.
|
|
-- The character can not walk during the shot. Any click / tap / key after the first second shortens it: the camera
|
|
-- leaves the tree right away and flies to the player faster.
|
|
local Players = game:GetService("Players")
|
|
local RunService = game:GetService("RunService")
|
|
local UIS = game:GetService("UserInputService")
|
|
|
|
local player = Players.LocalPlayer
|
|
local camera = workspace.CurrentCamera
|
|
|
|
local ORBIT_TIME = 4.5 -- seconds for the quarter turn around the tree
|
|
local FLY_TIME = 3.2 -- seconds from the tree to the player
|
|
local ZOOM = 12.5 -- Roblox default camera distance
|
|
local PITCH = math.rad(-15) -- final camera tilt: the one the Roblox camera settles on (measured), no jump
|
|
|
|
-- local attribute IntroDone: other client scripts (TutorialGuide) wait for the end of the shot
|
|
player:SetAttribute("IntroDone", false)
|
|
local tree = workspace:WaitForChild("CenterTree", 10)
|
|
local character = player.Character or player.CharacterAdded:Wait()
|
|
local hrp = character:WaitForChild("HumanoidRootPart", 10)
|
|
local humanoid = character:WaitForChild("Humanoid", 10)
|
|
if not tree or not hrp or not humanoid then player:SetAttribute("IntroDone", true) return end
|
|
|
|
local treeCf, treeSize = tree:GetBoundingBox()
|
|
-- the trunk goes into the ground, so the shot is measured from the ground next to the tree, not the model's bottom
|
|
local top = treeCf.Position.Y + treeSize.Y / 2
|
|
local params = RaycastParams.new()
|
|
params.FilterType = Enum.RaycastFilterType.Exclude
|
|
params.FilterDescendantsInstances = { tree, character }
|
|
local hit = workspace:Raycast(treeCf.Position + Vector3.new(treeSize.X, 200, 0), Vector3.new(0, -400, 0), params)
|
|
local groundY = hit and hit.Position.Y or (treeCf.Position.Y - treeSize.Y / 2)
|
|
local base = Vector3.new(treeCf.Position.X, groundY, treeCf.Position.Z)
|
|
local height = top - groundY -- visible tree height
|
|
local lookAt = base + Vector3.new(0, height * 0.55, 0) -- middle / upper part of the tree (the garland)
|
|
|
|
-- the HUD is hidden during the shot
|
|
local hidden = {}
|
|
local playerGui = player:WaitForChild("PlayerGui")
|
|
local function hide(g)
|
|
if g:IsA("ScreenGui") and g.Enabled and g.Name ~= "IntroFade" then
|
|
g.Enabled = false
|
|
table.insert(hidden, g)
|
|
end
|
|
end
|
|
for _, g in playerGui:GetChildren() do hide(g) end
|
|
local hideConn = playerGui.ChildAdded:Connect(hide) -- the HUD scripts may create their guis a bit later
|
|
|
|
-- freeze the character while the shot runs (the client owns its character's movement, so this is enough; the
|
|
-- PlayerModule is not always there to disable the controls)
|
|
local walkSpeed, jumpPower, jumpHeight = humanoid.WalkSpeed, humanoid.JumpPower, humanoid.JumpHeight
|
|
humanoid.WalkSpeed, humanoid.JumpPower, humanoid.JumpHeight = 0, 0, 0
|
|
|
|
camera.CameraType = Enum.CameraType.Scriptable
|
|
|
|
-- black screen that fades out at the start
|
|
local fadeGui = Instance.new("ScreenGui")
|
|
fadeGui.Name = "IntroFade"
|
|
fadeGui.IgnoreGuiInset = true
|
|
fadeGui.DisplayOrder = 100
|
|
fadeGui.ResetOnSpawn = false
|
|
local black = Instance.new("Frame")
|
|
black.Size = UDim2.fromScale(1, 1)
|
|
black.BackgroundColor3 = Color3.new(0, 0, 0)
|
|
black.BorderSizePixel = 0
|
|
black.Parent = fadeGui
|
|
fadeGui.Parent = player:WaitForChild("PlayerGui")
|
|
|
|
local function smooth(a) return a * a * (3 - 2 * a) end
|
|
local function easeInOut(a) return a < 0.5 and 4 * a * a * a or 1 - (-2 * a + 2) ^ 3 / 2 end
|
|
|
|
---------------------------------------------------------------- the route is planned once, before the camera moves
|
|
-- 1. wait (on the black screen) until the server has given the player a zone and moved the character to its
|
|
-- spawn point, so the end of the shot is known exactly
|
|
do
|
|
local t = os.clock()
|
|
while not player:GetAttribute("PlotIndex") and os.clock() - t < 15 do task.wait(0.1) end
|
|
local zone = player:GetAttribute("PlotIndex") and workspace:FindFirstChild("Zones")
|
|
and workspace.Zones:FindFirstChild("Zone_" .. player:GetAttribute("PlotIndex"))
|
|
local sp = zone and zone:FindFirstChild("SpawnPoint")
|
|
t = os.clock()
|
|
while sp and (hrp.Position - sp.Position).Magnitude > 6 and os.clock() - t < 3 do task.wait(0.1) end
|
|
task.wait(0.2)
|
|
end
|
|
|
|
-- 2. the end: where the normal camera will stand — behind the character, on its focus point, at the default zoom
|
|
local endLookFlat do
|
|
local look = hrp.CFrame.LookVector
|
|
endLookFlat = Vector3.new(look.X, 0, look.Z)
|
|
endLookFlat = endLookFlat.Magnitude > 0.01 and endLookFlat.Unit or Vector3.new(0, 0, -1)
|
|
end
|
|
local endFocus = hrp.Position + Vector3.new(0, 1.5, 0)
|
|
local endDir = (endLookFlat * math.cos(PITCH) + Vector3.new(0, math.sin(PITCH), 0)).Unit
|
|
local endCf = CFrame.lookAt(endFocus - endDir * ZOOM, endFocus)
|
|
|
|
-- 3. the orbit: a quarter turn that ends on the far side of the tree, looking over the tree towards the player's
|
|
-- zone — that is (almost) the same direction the end shot looks, so the camera never has to spin round
|
|
local playerAngle = math.atan2(endFocus.Z - base.Z, endFocus.X - base.X)
|
|
local orbitEndAngle = playerAngle + math.pi
|
|
local function orbitCFrame(a)
|
|
local angle = orbitEndAngle - (1 - a) * math.pi / 2
|
|
local radius = 58 - 22 * a
|
|
local h = height * (1.3 - 0.75 * a) -- from above the top down to about the middle
|
|
local pos = base + Vector3.new(math.cos(angle) * radius, h, math.sin(angle) * radius)
|
|
return CFrame.lookAt(pos, lookAt)
|
|
end
|
|
|
|
-- 4. the flight: a cubic Bezier from the end of the orbit up over the top of the tree and down onto the end
|
|
-- position, arriving along the final viewing direction
|
|
local P0 = orbitCFrame(1).Position
|
|
-- control points high enough that the curve clears the top of the tree with room to spare
|
|
local P1 = Vector3.new(base.X, top + 30, base.Z):Lerp(Vector3.new(P0.X, top + 30, P0.Z), 0.35)
|
|
local P2 = Vector3.new(endCf.Position.X, top + 4, endCf.Position.Z) - endLookFlat * 24
|
|
local P3 = endCf.Position
|
|
local function bezier(t)
|
|
local u = 1 - t
|
|
return P0 * (u * u * u) + P1 * (3 * u * u * t) + P2 * (3 * u * t * t) + P3 * (t * t * t)
|
|
end
|
|
-- the view turns by yaw / pitch only (no roll, no flips): shortest way round for the yaw
|
|
local function yawPitch(cf)
|
|
local l = cf.LookVector
|
|
return math.atan2(-l.X, -l.Z), math.asin(math.clamp(l.Y, -1, 1))
|
|
end
|
|
local yaw0, pitch0 = yawPitch(orbitCFrame(1))
|
|
local yaw1, pitch1 = yawPitch(endCf)
|
|
local dYaw = (yaw1 - yaw0 + math.pi) % (2 * math.pi) - math.pi
|
|
local function flightCFrame(t)
|
|
local e = easeInOut(t)
|
|
local yaw = yaw0 + dYaw * smooth(t)
|
|
local pitch = pitch0 + (pitch1 - pitch0) * smooth(t) - math.rad(22) * math.sin(math.pi * t) -- glance down at the tree while passing over it
|
|
return CFrame.new(bezier(e)) * CFrame.fromEulerAnglesYXZ(pitch, yaw, 0)
|
|
end
|
|
|
|
local skip = false
|
|
local t0 = os.clock()
|
|
local conn = UIS.InputBegan:Connect(function(input)
|
|
if os.clock() - t0 < 1 then return end
|
|
local t = input.UserInputType
|
|
if t == Enum.UserInputType.MouseButton1 or t == Enum.UserInputType.Touch or t == Enum.UserInputType.Keyboard
|
|
or t == Enum.UserInputType.Gamepad1 then
|
|
skip = true
|
|
end
|
|
end)
|
|
|
|
-- 1. around the tree
|
|
local fadeStart = os.clock()
|
|
local start = os.clock()
|
|
while true do
|
|
local a = math.clamp((os.clock() - start) / ORBIT_TIME, 0, 1)
|
|
black.BackgroundTransparency = math.clamp((os.clock() - fadeStart) / 1.2, 0, 1)
|
|
camera.CFrame = orbitCFrame(smooth(a))
|
|
if a >= 1 then break end
|
|
if skip then -- skipped: finish the turn quickly instead of jumping
|
|
start -= ORBIT_TIME * 0.06
|
|
end
|
|
RunService.RenderStepped:Wait()
|
|
end
|
|
black.BackgroundTransparency = 1
|
|
|
|
-- 2. fly to the player along the planned curve; the last frame is exactly the end position
|
|
local flyTime = skip and FLY_TIME * 0.7 or FLY_TIME
|
|
start = os.clock()
|
|
while true do
|
|
local a = math.clamp((os.clock() - start) / flyTime, 0, 1)
|
|
camera.CFrame = a >= 1 and endCf or flightCFrame(a)
|
|
if a >= 1 then break end
|
|
RunService.RenderStepped:Wait()
|
|
end
|
|
|
|
-- 3. hand over: the normal camera keeps this view, the character can move again
|
|
conn:Disconnect()
|
|
camera.CameraSubject = humanoid
|
|
camera.CameraType = Enum.CameraType.Custom
|
|
if humanoid.Parent then humanoid.WalkSpeed, humanoid.JumpPower, humanoid.JumpHeight = walkSpeed, jumpPower, jumpHeight end
|
|
fadeGui:Destroy()
|
|
hideConn:Disconnect()
|
|
for _, g in hidden do if g.Parent then g.Enabled = true end end
|
|
player:SetAttribute("IntroDone", true)
|