Advertisement
NeonMayhem

Isometric camera with show full map feature when gui pressed

Mar 29th, 2023 (edited)
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.11 KB | Source Code | 0 0
  1. -- Services
  2. local RenderStep = game:GetService("RunService").RenderStepped
  3. local ReplicatedStorage = game.ReplicatedStorage
  4. local UIS = game:GetService("UserInputService")
  5.  
  6. -- Player
  7. local LocalPlayer = game.Players.LocalPlayer
  8. local PlayerGui = LocalPlayer.PlayerGui
  9.  
  10. -- Remotes & Values
  11. local Remotes = ReplicatedStorage.Remotes
  12. local RequestTeamTable = Remotes.RequestTeamTable
  13. local MapLoaded = ReplicatedStorage.Values.MapLoaded
  14. local RequestCentrePosition = Remotes.RequestCentrePosition
  15.  
  16. -- Shorteners
  17. local Key = Enum.KeyCode
  18.  
  19. -- Instances
  20. local ZoomButton = PlayerGui:WaitForChild("GamePanel").MainFrame.ZoomButton
  21. local Camera = workspace.CurrentCamera
  22. local Mouse = LocalPlayer:GetMouse()
  23. local Tiles = workspace.Tiles
  24. local CameraParts = workspace.CameraParts
  25. local CameraPart = CameraParts.CameraPart
  26. local Pivot = CameraParts.Pivot
  27. local P0 = CameraParts.P0
  28. local P1 = CameraParts.P1
  29. local P2 = CameraParts.P2
  30.  
  31. -- Booleans
  32. local W,S,A,D,Q,E = false,false,false,false,false,false
  33. local ControlsEnabled, Zooming = true, false
  34.  
  35. -- Static numbers
  36. local Width = ReplicatedStorage.Values.Width -- 11 by default
  37. local X,Y,Z = 0,0,0
  38. local Scroll = 0
  39.  
  40. local MaxZoom = Width.Value * 51.96
  41. local MapViewMultiplier = 0.7
  42. local ZoomMultiplier = 0.5
  43. local MinZoom = math.min(50, MaxZoom * ZoomMultiplier)
  44. local Zoom = MinZoom
  45.  
  46. local MaxMoveSpeed = Width.Value
  47. local MinMoveSpeed = 1.3
  48.  
  49. local ZoomSpeed = Width.Value/2
  50. local ZoomReduction = ZoomSpeed/10
  51.  
  52. local Step = Width.Value * 2
  53. local StepReduction = Step/10
  54.  
  55. local MaxIndex = Width.Value
  56. local IndexAmount = 0.25
  57. local Index = 0
  58.  
  59. local Angle = 80
  60.  
  61. -- Data
  62. local TeamTable = RequestTeamTable:InvokeServer()
  63. local CentrePosition = RequestCentrePosition:InvokeServer()
  64.  
  65. -- Initial camera setup
  66. local TilePosition = nil
  67. local Rotation = nil
  68. local Player = nil
  69.  
  70. for i, TeamData in TeamTable do
  71.     Player = TeamData.Player
  72.     if Player == LocalPlayer then
  73.         TilePosition = TeamData.Tile.Position
  74.         Rotation = i * 60 + 180
  75.         break
  76.     end
  77. end
  78.  
  79. -- Increase/decrease camera zoom with scrollwheel
  80. Mouse.WheelForward:Connect(function()
  81.     if not ControlsEnabled then return end
  82.     Scroll = -Step
  83. end)
  84.  
  85. Mouse.WheelBackward:Connect(function()
  86.     if not ControlsEnabled then return end
  87.     Scroll = Step
  88. end)
  89.  
  90. -- Toggle map/camera view state
  91. ZoomButton.Activated:Connect(function()
  92.     if Zooming then return end
  93.     ZoomButton.AutoButtonColor = false
  94.     ControlsEnabled = false
  95.     Zooming = true
  96. end)
  97.  
  98. -- Make camera scriptable
  99. Camera.CameraType = Enum.CameraType.Scriptable
  100.  
  101. -- Position root camera part to player's starting tile position
  102. P0.Position = TilePosition + Vector3.new(0,1.5,0)
  103. -- Orient root to player's starting tile direction
  104. P0.Orientation += Vector3.new(0, Rotation, 0)
  105. -- Orient pivot point to player's starting tile direction
  106. Pivot.Orientation = Vector3.new(-Angle, Rotation, 0)
  107. P2.Position = Vector3.new(CentrePosition.X, MaxZoom * MapViewMultiplier, CentrePosition.Z)
  108.  
  109. -- Lerp function for changing camera move speed based on zoom
  110. local function Lerp(A,B,t)
  111.     return A + (B-A) * t
  112. end
  113.  
  114. -- Set movement variables based on key inputs
  115. local function SetMovement(MoveSpeed)
  116.  
  117.     -- Calculate movement reduction based on shift key down
  118.     local Multiplier = if UIS:IsKeyDown(Key.LeftShift) then 0.5 else 1
  119.  
  120.     -- Set WASD movement and keypressed variables
  121.     if UIS:IsKeyDown(Key.W) then
  122.         Z = MoveSpeed * Multiplier
  123.         W = true
  124.     else
  125.         W = false
  126.     end
  127.     if UIS:IsKeyDown(Key.S) then
  128.         Z = -MoveSpeed * Multiplier
  129.         S = true
  130.     else
  131.         S = false
  132.     end
  133.     if UIS:IsKeyDown(Key.A) then
  134.         X = MoveSpeed * Multiplier
  135.         A = true
  136.     else
  137.         A = false
  138.     end
  139.     if UIS:IsKeyDown(Key.D) then
  140.         X = -MoveSpeed * Multiplier
  141.         D = true
  142.     else
  143.         D = false
  144.     end
  145.  
  146.     -- Set QE zoom and keypressed variables
  147.     if UIS:IsKeyDown(Key.Q) then
  148.         Y = ZoomSpeed
  149.         Q = true
  150.     else
  151.         Q = false
  152.     end
  153.     if UIS:IsKeyDown(Key.E) then
  154.         Y = -ZoomSpeed
  155.         E = true
  156.     else
  157.         E = false
  158.     end
  159. end
  160.  
  161. -- Reduce camera movement until no movement based on keypressed values being false
  162. local function ReduceMovement(MoveReduction)
  163.  
  164.     -- WASD movement value reduction
  165.     if not W and Z > 0 then
  166.         Z -= MoveReduction
  167.         Z = math.max(Z, 0)
  168.     end
  169.     if not S and Z < 0 then
  170.         Z += MoveReduction
  171.         Z = math.min(Z, 0)
  172.     end
  173.  
  174.     if not A and X > 0 then
  175.         X -= MoveReduction
  176.         X = math.max(X, 0)
  177.     end
  178.     if not D and X < 0 then
  179.         X += MoveReduction
  180.         X = math.min(X, 0)
  181.     end
  182.  
  183.     -- QE zoom value reduction
  184.     if not Q and Y > 0 then
  185.         Y -= ZoomReduction
  186.         Y = math.max(Y, 0)
  187.     end
  188.     if not E and Y < 0 then
  189.         Y += ZoomReduction
  190.         Y = math.min(Y, 0)
  191.     end
  192.  
  193.     -- Scroll zoom value reduction
  194.     if Scroll > 0 then
  195.         Scroll -= StepReduction
  196.         Scroll = math.max(Scroll, 0)
  197.     elseif Scroll < 0 then
  198.         Scroll += StepReduction
  199.         Scroll = math.min(Scroll, 0)
  200.     end
  201.  
  202.     -- Set zoom value as a result of scrolling + QE press values
  203.     Zoom += (Scroll/10 + Y)
  204.     -- Constrain zoom values to min and max
  205.     Zoom = math.clamp(Zoom, MinZoom, MaxZoom * ZoomMultiplier)
  206. end
  207.  
  208. -- Every frame...
  209. RenderStep:Connect(function(dt)
  210.  
  211.     -- Calculate ratio needed to make camera move at a constant speed regardless of framerate
  212.     local Ratio = dt*60
  213.  
  214.     if ControlsEnabled then -- if player is allowed to move camera (during camera-view)
  215.  
  216.         -- Calulate movement speed and movement reduction (for key-release smoothing effect) based on current zoom.
  217.         -- Compensate for framerate above/below 60fps to
  218.         local MoveSpeed = Lerp(MinMoveSpeed, MaxMoveSpeed, Zoom/MaxZoom) * Ratio
  219.         local MoveReduction = MoveSpeed/10 * Ratio
  220.  
  221.         SetMovement(MoveSpeed)
  222.         ReduceMovement(MoveReduction)
  223.  
  224.         -- Move camera root part based on X and Z speed
  225.         P0.CFrame *= CFrame.new(X, 0, Z)
  226.         -- Move point at which camera rotates around with, relative to root part
  227.         Pivot.Position = P0.Position + Vector3.new(0,10,0)
  228.         -- Move camera part used for determining camera position to pivot point and offset by zoom
  229.         CameraPart.Position = Pivot.Position -Pivot.CFrame.LookVector * Zoom
  230.         CameraPart.CFrame = CFrame.lookAt(CameraPart.Position, Pivot.Position)
  231.         -- CFrame camera to camera part
  232.         Camera.CFrame = CameraPart.CFrame
  233.  
  234.     elseif Zooming then -- if transitioning to map-view or back to camera-view
  235.  
  236.         -- Place center control point of bezier curve between pivot point and top of the map. Account for current zoom.
  237.         P1.Position = CentrePosition:Lerp(P2.Position, 0.5) - Pivot.CFrame.LookVector * Zoom
  238.  
  239.         -- Set values for bezier curve
  240.         local t = Index/MaxIndex
  241.         local Lerp1 = CameraPart.Position:Lerp(P1.Position, t)
  242.         local Lerp2 = P1.Position:Lerp(P2.Position, t)
  243.         local Bezier = Lerp1:Lerp(Lerp2, t)
  244.  
  245.         if ZoomButton.Text == "Camera view" then -- If transitioning to map view:
  246.             if Index < MaxIndex then -- increase index used to determine progress for bezier until transition completed
  247.                 Index += IndexAmount * Ratio
  248.                 Index = math.min(Index, MaxIndex) -- set index to maxindex if index overshoots due to floating point error
  249.  
  250.             else -- If transition to map view finished:
  251.                 ZoomButton.AutoButtonColor = true -- make button appear clickable
  252.                 ZoomButton.Text = "Map view" -- set button text
  253.                 Zooming = false -- set zooming state
  254.             end
  255.  
  256.         else -- If transitioning to camera view:
  257.             if Index > 0 then -- decrease index used to determine progress for bezier until transition completed
  258.                 Index -= IndexAmount * Ratio
  259.                 Index = math.max(Index, 0) -- set index to 0 if index overshoots due to floating point error
  260.  
  261.             else -- If transition to camera view finished:
  262.                 ZoomButton.AutoButtonColor = true -- make button appear clickable
  263.                 ZoomButton.Text = "Camera view" -- set button text
  264.                 ControlsEnabled = true -- allow camera movement
  265.                 Zooming = false -- set zooming state
  266.             end
  267.         end
  268.  
  269.         -- Position pivot to bezier
  270.         Pivot.Position = Bezier
  271.  
  272.         -- Orient pivot to look down at map when in map view, or normal camera angle in camera view
  273.         Pivot.Orientation = Vector3.new(-Angle -(10 * Index/MaxIndex), Rotation, 0)
  274.         -- Camera view:                 -80    -(10 * 0) = -80 (default camera angle)
  275.         -- Map view:                    -80    -(10 * 1) = -90 (camera looking down)
  276.  
  277.         -- CFrame camera to pivot's cframe
  278.         Camera.CFrame = Pivot.CFrame
  279.     end
  280. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement