Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Services
- local RenderStep = game:GetService("RunService").RenderStepped
- local ReplicatedStorage = game.ReplicatedStorage
- local UIS = game:GetService("UserInputService")
- -- Player
- local LocalPlayer = game.Players.LocalPlayer
- local PlayerGui = LocalPlayer.PlayerGui
- -- Remotes & Values
- local Remotes = ReplicatedStorage.Remotes
- local RequestTeamTable = Remotes.RequestTeamTable
- local MapLoaded = ReplicatedStorage.Values.MapLoaded
- local RequestCentrePosition = Remotes.RequestCentrePosition
- -- Shorteners
- local Key = Enum.KeyCode
- -- Instances
- local ZoomButton = PlayerGui:WaitForChild("GamePanel").MainFrame.ZoomButton
- local Camera = workspace.CurrentCamera
- local Mouse = LocalPlayer:GetMouse()
- local Tiles = workspace.Tiles
- local CameraParts = workspace.CameraParts
- local CameraPart = CameraParts.CameraPart
- local Pivot = CameraParts.Pivot
- local P0 = CameraParts.P0
- local P1 = CameraParts.P1
- local P2 = CameraParts.P2
- -- Booleans
- local W,S,A,D,Q,E = false,false,false,false,false,false
- local ControlsEnabled, Zooming = true, false
- -- Static numbers
- local Width = ReplicatedStorage.Values.Width -- 11 by default
- local X,Y,Z = 0,0,0
- local Scroll = 0
- local MaxZoom = Width.Value * 51.96
- local MapViewMultiplier = 0.7
- local ZoomMultiplier = 0.5
- local MinZoom = math.min(50, MaxZoom * ZoomMultiplier)
- local Zoom = MinZoom
- local MaxMoveSpeed = Width.Value
- local MinMoveSpeed = 1.3
- local ZoomSpeed = Width.Value/2
- local ZoomReduction = ZoomSpeed/10
- local Step = Width.Value * 2
- local StepReduction = Step/10
- local MaxIndex = Width.Value
- local IndexAmount = 0.25
- local Index = 0
- local Angle = 80
- -- Data
- local TeamTable = RequestTeamTable:InvokeServer()
- local CentrePosition = RequestCentrePosition:InvokeServer()
- -- Initial camera setup
- local TilePosition = nil
- local Rotation = nil
- local Player = nil
- for i, TeamData in TeamTable do
- Player = TeamData.Player
- if Player == LocalPlayer then
- TilePosition = TeamData.Tile.Position
- Rotation = i * 60 + 180
- break
- end
- end
- -- Increase/decrease camera zoom with scrollwheel
- Mouse.WheelForward:Connect(function()
- if not ControlsEnabled then return end
- Scroll = -Step
- end)
- Mouse.WheelBackward:Connect(function()
- if not ControlsEnabled then return end
- Scroll = Step
- end)
- -- Toggle map/camera view state
- ZoomButton.Activated:Connect(function()
- if Zooming then return end
- ZoomButton.AutoButtonColor = false
- ControlsEnabled = false
- Zooming = true
- end)
- -- Make camera scriptable
- Camera.CameraType = Enum.CameraType.Scriptable
- -- Position root camera part to player's starting tile position
- P0.Position = TilePosition + Vector3.new(0,1.5,0)
- -- Orient root to player's starting tile direction
- P0.Orientation += Vector3.new(0, Rotation, 0)
- -- Orient pivot point to player's starting tile direction
- Pivot.Orientation = Vector3.new(-Angle, Rotation, 0)
- P2.Position = Vector3.new(CentrePosition.X, MaxZoom * MapViewMultiplier, CentrePosition.Z)
- -- Lerp function for changing camera move speed based on zoom
- local function Lerp(A,B,t)
- return A + (B-A) * t
- end
- -- Set movement variables based on key inputs
- local function SetMovement(MoveSpeed)
- -- Calculate movement reduction based on shift key down
- local Multiplier = if UIS:IsKeyDown(Key.LeftShift) then 0.5 else 1
- -- Set WASD movement and keypressed variables
- if UIS:IsKeyDown(Key.W) then
- Z = MoveSpeed * Multiplier
- W = true
- else
- W = false
- end
- if UIS:IsKeyDown(Key.S) then
- Z = -MoveSpeed * Multiplier
- S = true
- else
- S = false
- end
- if UIS:IsKeyDown(Key.A) then
- X = MoveSpeed * Multiplier
- A = true
- else
- A = false
- end
- if UIS:IsKeyDown(Key.D) then
- X = -MoveSpeed * Multiplier
- D = true
- else
- D = false
- end
- -- Set QE zoom and keypressed variables
- if UIS:IsKeyDown(Key.Q) then
- Y = ZoomSpeed
- Q = true
- else
- Q = false
- end
- if UIS:IsKeyDown(Key.E) then
- Y = -ZoomSpeed
- E = true
- else
- E = false
- end
- end
- -- Reduce camera movement until no movement based on keypressed values being false
- local function ReduceMovement(MoveReduction)
- -- WASD movement value reduction
- if not W and Z > 0 then
- Z -= MoveReduction
- Z = math.max(Z, 0)
- end
- if not S and Z < 0 then
- Z += MoveReduction
- Z = math.min(Z, 0)
- end
- if not A and X > 0 then
- X -= MoveReduction
- X = math.max(X, 0)
- end
- if not D and X < 0 then
- X += MoveReduction
- X = math.min(X, 0)
- end
- -- QE zoom value reduction
- if not Q and Y > 0 then
- Y -= ZoomReduction
- Y = math.max(Y, 0)
- end
- if not E and Y < 0 then
- Y += ZoomReduction
- Y = math.min(Y, 0)
- end
- -- Scroll zoom value reduction
- if Scroll > 0 then
- Scroll -= StepReduction
- Scroll = math.max(Scroll, 0)
- elseif Scroll < 0 then
- Scroll += StepReduction
- Scroll = math.min(Scroll, 0)
- end
- -- Set zoom value as a result of scrolling + QE press values
- Zoom += (Scroll/10 + Y)
- -- Constrain zoom values to min and max
- Zoom = math.clamp(Zoom, MinZoom, MaxZoom * ZoomMultiplier)
- end
- -- Every frame...
- RenderStep:Connect(function(dt)
- -- Calculate ratio needed to make camera move at a constant speed regardless of framerate
- local Ratio = dt*60
- if ControlsEnabled then -- if player is allowed to move camera (during camera-view)
- -- Calulate movement speed and movement reduction (for key-release smoothing effect) based on current zoom.
- -- Compensate for framerate above/below 60fps to
- local MoveSpeed = Lerp(MinMoveSpeed, MaxMoveSpeed, Zoom/MaxZoom) * Ratio
- local MoveReduction = MoveSpeed/10 * Ratio
- SetMovement(MoveSpeed)
- ReduceMovement(MoveReduction)
- -- Move camera root part based on X and Z speed
- P0.CFrame *= CFrame.new(X, 0, Z)
- -- Move point at which camera rotates around with, relative to root part
- Pivot.Position = P0.Position + Vector3.new(0,10,0)
- -- Move camera part used for determining camera position to pivot point and offset by zoom
- CameraPart.Position = Pivot.Position -Pivot.CFrame.LookVector * Zoom
- CameraPart.CFrame = CFrame.lookAt(CameraPart.Position, Pivot.Position)
- -- CFrame camera to camera part
- Camera.CFrame = CameraPart.CFrame
- elseif Zooming then -- if transitioning to map-view or back to camera-view
- -- Place center control point of bezier curve between pivot point and top of the map. Account for current zoom.
- P1.Position = CentrePosition:Lerp(P2.Position, 0.5) - Pivot.CFrame.LookVector * Zoom
- -- Set values for bezier curve
- local t = Index/MaxIndex
- local Lerp1 = CameraPart.Position:Lerp(P1.Position, t)
- local Lerp2 = P1.Position:Lerp(P2.Position, t)
- local Bezier = Lerp1:Lerp(Lerp2, t)
- if ZoomButton.Text == "Camera view" then -- If transitioning to map view:
- if Index < MaxIndex then -- increase index used to determine progress for bezier until transition completed
- Index += IndexAmount * Ratio
- Index = math.min(Index, MaxIndex) -- set index to maxindex if index overshoots due to floating point error
- else -- If transition to map view finished:
- ZoomButton.AutoButtonColor = true -- make button appear clickable
- ZoomButton.Text = "Map view" -- set button text
- Zooming = false -- set zooming state
- end
- else -- If transitioning to camera view:
- if Index > 0 then -- decrease index used to determine progress for bezier until transition completed
- Index -= IndexAmount * Ratio
- Index = math.max(Index, 0) -- set index to 0 if index overshoots due to floating point error
- else -- If transition to camera view finished:
- ZoomButton.AutoButtonColor = true -- make button appear clickable
- ZoomButton.Text = "Camera view" -- set button text
- ControlsEnabled = true -- allow camera movement
- Zooming = false -- set zooming state
- end
- end
- -- Position pivot to bezier
- Pivot.Position = Bezier
- -- Orient pivot to look down at map when in map view, or normal camera angle in camera view
- Pivot.Orientation = Vector3.new(-Angle -(10 * Index/MaxIndex), Rotation, 0)
- -- Camera view: -80 -(10 * 0) = -80 (default camera angle)
- -- Map view: -80 -(10 * 1) = -90 (camera looking down)
- -- CFrame camera to pivot's cframe
- Camera.CFrame = Pivot.CFrame
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement