Advertisement
Guest User

RoCurve Source Code

a guest
May 21st, 2021
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.77 KB | None | 0 0
  1. local ChangeHistoryService = game:GetService("ChangeHistoryService")
  2.  
  3. local toolbar = plugin:CreateToolbar("RoCurves")
  4.  
  5. local newScriptButton = toolbar:CreateButton("RoCurves", "Generate Smooth Curves.", "rbxassetid://6840838833")
  6. local Opened = false
  7.  
  8.  
  9. local RoCurvesInfo = DockWidgetPluginGuiInfo.new(
  10.     Enum.InitialDockState.Left,
  11.     false,   -- Widget will be initially enabled
  12.     false,  -- Don't override the previous enabled state
  13.     300,    -- Default width of the floating window
  14.     300,    -- Default height of the floating window
  15.     300,    -- Minimum width of the floating window (optional)
  16.     400     -- Minimum height of the floating window (optional)
  17. )
  18.  
  19. local PluginWidget = plugin:CreateDockWidgetPluginGui("RoCurves", RoCurvesInfo)
  20. PluginWidget.Title = "Curve Settings"
  21. local RoCurveGui = script.Parent.RoCurveGui
  22. RoCurveGui.Parent = PluginWidget
  23.  
  24. local SelectOne = RoCurveGui.SelectOne
  25. local SelectTwo = RoCurveGui.SelectTwo
  26. local SelectThree = RoCurveGui.SelectThree
  27. local PartNumber = RoCurveGui.PartNumber
  28. local Generate = RoCurveGui.Generate
  29. local Preview = RoCurveGui.Preview
  30.  
  31. --Variables
  32. local CurvePart1 = nil
  33. local CurvePart2 = nil
  34. local CurvePart3 = nil
  35. local Button1Active = false
  36. local Button2Active = false
  37. local Button3Active = false
  38.  
  39. local function MouseSelector(Target)
  40.     --if Opened then
  41.     if Button1Active == true then
  42.        
  43.         Button1Active = false
  44.         Button2Active = false
  45.         Button3Active = false
  46.  
  47.         CurvePart1 = Target
  48.     elseif Button2Active == true then
  49.        
  50.         Button1Active = false
  51.         Button2Active = false
  52.         Button3Active = false
  53.  
  54.         CurvePart2 = Target
  55.     elseif Button3Active == true then
  56.         Button1Active = false
  57.         Button2Active = false
  58.         Button3Active = false
  59.  
  60.         CurvePart3 = Target
  61.     end
  62. end
  63.  
  64. --Handle Opening/Closing the Plugin.
  65. newScriptButton.Click:Connect(function()
  66.     if Opened then
  67.         PluginWidget.Enabled = false
  68.         Opened = false
  69.     else
  70.         --Handle the Mouse.
  71.         plugin:Activate(false) -- gain non exclusive access to the mouse
  72.         local mouse = plugin:GetMouse() -- get the PluginMouse
  73.  
  74.         mouse.Button1Down:Connect(function()
  75.             MouseSelector(mouse.Target)
  76.         end)
  77.         PluginWidget.Enabled = true
  78.         Opened = true
  79.     end
  80. end)
  81.  
  82.  
  83.  
  84. --Handle the ButtonPlugins
  85. SelectOne.MouseButton1Click:Connect(function()
  86.     Button1Active = true
  87.     Button2Active = false
  88.     Button3Active = false
  89.     plugin:Activate(false) -- gain non exclusive access to the mouse
  90.     local mouse = plugin:GetMouse() -- get the PluginMouse
  91.  
  92.     mouse.Button1Down:Connect(function()
  93.         MouseSelector(mouse.Target)
  94.     end)
  95. end)
  96.  
  97. SelectTwo.MouseButton1Click:Connect(function()
  98.     Button1Active = false
  99.     Button2Active = true
  100.     Button3Active = false
  101.     plugin:Activate(false) -- gain non exclusive access to the mouse
  102.     local mouse = plugin:GetMouse() -- get the PluginMouse
  103.  
  104.     mouse.Button1Down:Connect(function()
  105.         MouseSelector(mouse.Target)
  106.     end)
  107. end)
  108.  
  109. SelectThree.MouseButton1Click:Connect(function()
  110.     print("Middle Part Selected!")
  111.     Button1Active = false
  112.     Button2Active = false
  113.     Button3Active = true
  114.     plugin:Activate(false) -- gain non exclusive access to the mouse
  115.     local mouse = plugin:GetMouse() -- get the PluginMouse
  116.  
  117.     mouse.Button1Down:Connect(function()
  118.         MouseSelector(mouse.Target)
  119.     end)
  120. end)
  121.  
  122.  
  123. --
  124.  
  125.  
  126. PartNumber:GetPropertyChangedSignal("Text"):Connect(function()
  127.     PartNumber.Text = PartNumber.Text:gsub('%D+', '');
  128. end)
  129.  
  130. local function RunCode(ActionName)
  131.     if ActionName == "EndPreview" then
  132.         if  game.Workspace:FindFirstChild("PreviewCurve") then
  133.             game.Workspace:FindFirstChild("PreviewCurve"):Destroy()
  134.         end
  135.         Preview.BackgroundColor3 = Color3.fromRGB(253, 245, 9)
  136.         Preview.Text = "PREVIEW"
  137.     else
  138.         local PartMinimum = 3 --Default
  139.         local PartNum = PartNumber.Text
  140.         local PartCount = 0
  141.  
  142.         if CurvePart1 and CurvePart2 and CurvePart3 then
  143.             local Button1Active = false
  144.             local Button2Active = false
  145.             local Button3Active = false
  146.             if PartNumber.Text ~= " " and PartNumber.Text ~= "" then
  147.                 if tonumber(PartNumber.Text) > PartMinimum then
  148.                     PartCount = tonumber(PartNum)
  149.                 else
  150.                     PartCount = PartMinimum
  151.                 end
  152.             else
  153.                 PartCount = PartMinimum
  154.             end
  155.  
  156.             ChangeHistoryService:SetWaypoint("Waypoint Before")
  157.  
  158.             local NewFolder = Instance.new("Folder")
  159.             local FinalFolder = Instance.new("Folder")
  160.  
  161.             if ActionName == "Generate" then
  162.                 NewFolder.Name = "PartHolder"
  163.                 FinalFolder.Name = "Curve"
  164.             else
  165.                 NewFolder.Name = "PreviewPartHolder"
  166.                 FinalFolder.Name = "PreviewCurve"
  167.             end
  168.  
  169.             NewFolder.Parent = game.Workspace
  170.             FinalFolder.Parent = game.Workspace
  171.  
  172.             local function QuadraticBezier(t,p0,p1,p2)
  173.                 return (1-t)^2*p0+2*(1-t)*t*p1+t^2*p2;
  174.             end;
  175.  
  176.             local Part0 = CurvePart1 --<Start point
  177.             local Part1 = CurvePart3 --<'Curve' Point
  178.             local Part2 = CurvePart2 --<End Point
  179.  
  180.             local Start1Orientation = CurvePart1.Orientation
  181.             local FinishOrientation = CurvePart2.Orientation
  182.  
  183.             local Increment = 1/PartCount
  184.  
  185.             for t = 0,1,Increment do
  186.  
  187.                 local TargetPosition = QuadraticBezier(t , Part0.Position, Part1.Position, Part2.Position);
  188.  
  189.                 local Part = Instance.new("Part")
  190.                 Part.Name = t
  191.                 Part.Size = Vector3.new(.05,.05,.05)
  192.                 if ActionName == "Generate" then
  193.                     Part.Material = Part0.Material
  194.                     Part.Color = Part0.Color
  195.                 else
  196.                     Part.Material = Enum.Material.SmoothPlastic
  197.                     Part.Color = Color3.new(0.164706, 0.65098, 0.027451)
  198.                     Part.Transparency = .2
  199.                 end
  200.  
  201.                 Part.Anchored = true
  202.  
  203.                 Part.CFrame = CFrame.new(TargetPosition,  QuadraticBezier(t + 0.01 , Part0.Position, Part1.Position, Part2.Position))
  204.  
  205.                 Part.Parent = NewFolder
  206.             end
  207.  
  208.            
  209.             local Start = Increment
  210.             local JustCreated = nil
  211.  
  212.             local function StretchPart()
  213.                 local pos1 = nil
  214.                 local pos2 = nil
  215.                 if JustCreated == nil then
  216.                     pos1 = NewFolder:FindFirstChild(tostring(0)).Position
  217.                     Start = 0 + Increment
  218.                 else
  219.                     pos1 = NewFolder:FindFirstChild(tostring(Start)).Position
  220.                     --pos1 = JustCreated.Position
  221.                     Start = Start + Increment
  222.                 end
  223.  
  224.  
  225.                 if  Start < 1 then
  226.                     pos2 = NewFolder:FindFirstChild(tostring(Start)).Position
  227.                     local newpart = Instance.new("Part")
  228.                     --Part front at pos1 and back and pos2
  229.  
  230.  
  231.                     local v = (pos1 - pos2)
  232.                     newpart.Anchored = true
  233.                     newpart.CanCollide = false
  234.                     newpart.Name = NewFolder:FindFirstChild(tostring(Start)).Name
  235.                     newpart.Size = Vector3.new(1,CurvePart1.Size.Y,v.Magnitude)
  236.                     newpart.CFrame = CFrame.new(pos2 + .5*v, pos1) * CFrame.new(-.5,0,0)
  237.                    
  238.                     if ActionName == "Generate" then
  239.                         newpart.Material = NewFolder:FindFirstChild(tostring(Start)).Material
  240.                         newpart.Color = NewFolder:FindFirstChild(tostring(Start)).Color
  241.                     else
  242.                         newpart.Material = Enum.Material.SmoothPlastic
  243.                         newpart.Color = Color3.new(0.164706, 0.65098, 0.027451)
  244.                         newpart.Transparency = .2
  245.                     end
  246.  
  247.                     newpart.Parent = FinalFolder
  248.                     JustCreated = newpart
  249.  
  250.                     wait()
  251.                     StretchPart()
  252.                 end
  253.             end
  254.  
  255.             StretchPart()
  256.             Start = Start - Increment
  257.            
  258.             local pos1 = nil
  259.             --Now for the final part
  260.             local Mark = 0
  261.             for i, v in pairs(FinalFolder:GetChildren()) do
  262.                 if tonumber(v.Name) > Mark then
  263.                     Mark = tonumber(v.Name)
  264.                     pos1 = NewFolder:FindFirstChild(tostring(Mark)).Position
  265.                    
  266.                 end
  267.             end
  268.            
  269.            
  270.             local pos2 = CurvePart2.Position
  271.             local newpart = Instance.new("Part")
  272.  
  273.             local v = (pos1 - pos2)
  274.             newpart.Anchored = true
  275.             newpart.CanCollide = false
  276.             newpart.Size = Vector3.new(1 ,CurvePart1.Size.Y,v.Magnitude)
  277.             newpart.CFrame = CFrame.new(pos2 + .5*v, pos1) * CFrame.new(-.5,0,0)
  278.  
  279.             if ActionName == "Generate" then
  280.                 newpart.Material = NewFolder:FindFirstChild(tostring(Start)).Material
  281.                 newpart.Color = NewFolder:FindFirstChild(tostring(Start)).Color
  282.             else
  283.                 newpart.Material = Enum.Material.SmoothPlastic
  284.                 newpart.Color = Color3.new(0.164706, 0.65098, 0.027451)
  285.                 newpart.Transparency = .2
  286.             end
  287.             newpart.Parent = FinalFolder
  288.             JustCreated = newpart
  289.  
  290.             --
  291.             NewFolder:Destroy()
  292.            
  293.             --for i, v in pairs(FinalFolder:GetChildren()) do
  294.                 --local lookVector = v.CFrame.lookVector.X
  295.                
  296.             --end
  297.            
  298.             ChangeHistoryService:SetWaypoint("Waypoint After")
  299.             if ActionName == "Generate" then
  300.                 CurvePart1 = nil
  301.                 CurvePart2 = nil
  302.                 CurvePart3 = nil
  303.             end
  304.         end
  305.     end
  306. end
  307.  
  308. local ActivePreview = false
  309.  
  310. Generate.MouseButton1Click:Connect(function()
  311.     if ActivePreview == true then
  312.         ActivePreview = false
  313.        
  314.         RunCode("EndPreview")
  315.     end
  316.     wait()
  317.     RunCode("Generate")
  318. end)
  319.  
  320.  
  321. Preview.MouseButton1Click:Connect(function()
  322.     if ActivePreview == false then
  323.         ActivePreview = true
  324.         Preview.BackgroundColor3 = Color3.fromRGB(255, 1, 1)
  325.         Preview.Text = "STOP PREVIEW"
  326.         RunCode("Preview")
  327.     else
  328.         ActivePreview = false
  329.        
  330.         RunCode("EndPreview")
  331.     end
  332. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement