Advertisement
Froredion

SkillsController

Dec 27th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.67 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local UserInputService = game:GetService("UserInputService")
  3. local Knit = require(ReplicatedStorage.Packages.Knit)
  4. local Signal = require(ReplicatedStorage.Packages.Signal)
  5. local SkillModule = require(ReplicatedStorage.SharedSource.Externals.Utilities.SkillModule)
  6.  
  7. local SkillsController = Knit.CreateController {
  8.     Name = "SkillsController",
  9.     SkillSystems = {},
  10.    
  11.     IsSkillButtonPressed = {},
  12.     Toggle = false,
  13.    
  14.     SkillDisabled = false, -- if enabled, cannot skill for a while
  15.  
  16. }
  17.  
  18. local GameSettings = require(ReplicatedStorage.SharedSource.Datas.GameSettings)
  19.  
  20. local plr = game.Players.LocalPlayer
  21.  
  22. local clickSound = ReplicatedStorage.Assets.Sounds:WaitForChild("CastSkill")
  23.  
  24. ---- Services
  25. local SkillsService, ProfileService
  26.  
  27. ---- Controllers
  28. local DataController, MobileTargetSystemController, GamemodeController, HookController
  29.  
  30. local function changeSkillColorButton(castSkill)
  31.     if SkillsController.SkillDisabled then
  32.         castSkill.ImageColor3 = Color3.fromRGB(152, 0, 0)
  33.     else
  34.         castSkill.ImageColor3 = Color3.fromRGB(0, 0, 0)
  35.     end
  36. end
  37.  
  38. local disableSkillBackupThread
  39. local maximumSkillDisablingTime = 10  -- modify this to increase the allowed time to disable hook
  40. function SkillsController:DisableSkillToggle(toggle: boolean)  
  41.     local controlsGui = plr.PlayerGui:WaitForChild("ControlsGui")
  42.     local castSkill = controlsGui:WaitForChild("CastSkill")
  43.  
  44.     SkillsController.SkillDisabled = toggle
  45.     changeSkillColorButton(castSkill)
  46.  
  47.     if disableSkillBackupThread and coroutine.status(disableSkillBackupThread) ~= "dead" then
  48.         pcall(function()
  49.             task.cancel(disableSkillBackupThread)
  50.         end)
  51.     end
  52.     disableSkillBackupThread = task.delay(maximumSkillDisablingTime, function() -- timeout for disabling hook
  53.         SkillsController:DisableSkillToggle(false)
  54.     end)
  55. end
  56.  
  57. local MobileTargetSystemCF
  58. function SkillsController:InputBeganSkill(user, skillName, interactionStep, infos)
  59.     local skillSystem = SkillsController.SkillSystems[skillName]
  60.     if not skillSystem then
  61.         skillSystem = require(script:WaitForChild("Skills"):FindFirstChild(skillName) or script:WaitForChild("Skills"):FindFirstChild("Default"))
  62.     end
  63.    
  64.     local canProceed = true
  65.     if skillSystem.InputBeganSkill then
  66.         skillSystem.InputBeganSkill(user, skillName, interactionStep, infos)
  67.     end
  68.    
  69.     repeat
  70.         game["Run Service"].RenderStepped:Wait()
  71.         if skillSystem.OnUpdateBeganSkill then
  72.             canProceed = skillSystem.OnUpdateBeganSkill(user, skillName, interactionStep, {
  73.                 ["MobileTargetSystemCF"] = MobileTargetSystemCF
  74.             })
  75.         end
  76.     until not SkillsController.IsSkillButtonPressed[skillName]
  77.    
  78.     if skillSystem.InputEndedSkill then
  79.         skillSystem.InputEndedSkill(user, skillName, interactionStep, infos)
  80.     end
  81.    
  82.     return canProceed
  83. end
  84.  
  85. local cds = {}
  86. function SkillsController:InitiateSkill(user, skillName, interactionStep, infos)
  87.     if not SkillsController.SkillSystems[skillName] then
  88.         SkillsController.SkillSystems[skillName] = require(script:WaitForChild("Skills"):FindFirstChild(skillName) or script:WaitForChild("Skills"):FindFirstChild("Default"))
  89.     end
  90.    
  91.     local skillData = SkillModule.FindSkillData(skillName)
  92.     if interactionStep == 1 then
  93.         if cds[skillName] then
  94.             return
  95.         end
  96.        
  97.         local cooldownTime = skillData.SkillVariables.Cooldown
  98.         cds[skillName] = true
  99.         local controlsGui = plr.PlayerGui:WaitForChild("ControlsGui")
  100.         local castSkill = controlsGui.CastSkill
  101.         if GameSettings.NumberOfSkillsEquippable > 1 then
  102.             local profileData = DataController.Data
  103.             local found = table.find(profileData.SkillsEquipped, skillName)
  104.             if found then
  105.                 castSkill = controlsGui["CastSkill"..found]
  106.             end
  107.         end
  108.         if castSkill then
  109.             castSkill.Cooldown.Visible = true
  110.             castSkill.Cooldown.ImageRectSize = Vector2.new(256,256)
  111.             castSkill.Cooldown.Size = UDim2.new(1,0,1,0)
  112.             local Tween = game.TweenService:Create(castSkill.Cooldown,TweenInfo.new(cooldownTime,Enum.EasingStyle.Linear),{
  113.                 Size = UDim2.new(1,0,0,0),
  114.                 ImageRectSize = Vector2.new(256, 0)
  115.             })
  116.             Tween:Play()
  117.  
  118.             task.spawn(function()
  119.                 local loopAmountPerSecond = 10*cooldownTime
  120.                 castSkill.CooldownText.Visible = true
  121.                 for i=1,loopAmountPerSecond do
  122.                     local timeNow = math.round((cooldownTime - ((cooldownTime/loopAmountPerSecond)*i))*100)/100
  123.                     castSkill.CooldownText.Text = timeNow.."s"
  124.                     task.wait(cooldownTime/loopAmountPerSecond)
  125.                 end
  126.                 castSkill.CooldownText.Text = ""
  127.             end)
  128.             task.delay(cooldownTime, function()
  129.                 cds[skillName] = false
  130.                 castSkill.Cooldown.Visible = false
  131.             end)
  132.         end
  133.     end
  134.    
  135.     SkillsController.SkillSystems[skillName].Interactions[interactionStep](user, skillName, interactionStep, infos)
  136. end
  137.  
  138. function SkillsController:FireToServer(user, skillName, interactionStep, infos, bypassUserOnlyFilter: boolean)
  139.     local userPlayerIns = game.Players:GetPlayerFromCharacter(user)
  140.    
  141.     if (userPlayerIns and userPlayerIns ~= plr) and not bypassUserOnlyFilter then
  142.         if game["Run Service"]:IsStudio() then
  143.             print("Cannot fire, skill user ~= plr")
  144.         end
  145.     end
  146.     if ((userPlayerIns and userPlayerIns == plr) or bypassUserOnlyFilter) or (infos.PlayerNetworkOwner ~= nil and plr.Name == infos.PlayerNetworkOwner.Name) then
  147.         SkillsService.InitiateSkill:Fire(user, skillName, interactionStep, infos)
  148.     end
  149. end
  150.  
  151. -- confirms if self or NPC that is assigned to the player
  152. function SkillsController:IsSelfOrNPC(user, skillName, interactionStep, infos, bypassUserOnlyFilter: boolean)
  153.     return user == plr.Character or (infos.PlayerNetworkOwner ~= nil and plr.Name == infos.PlayerNetworkOwner.Name)
  154. end
  155.  
  156. function SkillsController:KnitStart()  
  157.     DataController:WaitUntilProfileLoaded()
  158.     local profileData = DataController.Data
  159.    
  160.     local controlsGui = plr.PlayerGui:WaitForChild("ControlsGui")
  161.     local function setSkill(i)
  162.         local castSkill = controlsGui:WaitForChild("CastSkill")
  163.         if GameSettings.NumberOfSkillsEquippable > 1 then
  164.             castSkill = controlsGui:WaitForChild("CastSkill"..i, 5)
  165.         end
  166.        
  167.         local skillData = SkillModule.FindSkillData(profileData.SkillsEquipped[i])
  168.        
  169.         if skillData then
  170.             castSkill.ImageLabel.Image = skillData.Image
  171.         else
  172.             castSkill.ImageLabel.Image = ""
  173.         end
  174.     end
  175.     for i=1,GameSettings.NumberOfSkillsEquippable do
  176.         setSkill(i)
  177.     end
  178.     ProfileService.UpdateSpecificData:Connect(function(Redirectories,newValue)
  179.         if Redirectories[1] == "SkillsEquipped" then
  180.             task.wait(.1)
  181.             for i=1,GameSettings.NumberOfSkillsEquippable do
  182.                 setSkill(i)
  183.             end
  184.         end
  185.     end)
  186.    
  187.     local function inputEndedSkill(plr, equipNum)
  188.         clickSound:Play()
  189.  
  190.         SkillsController:InitiateSkill(plr.Character, profileData.SkillsEquipped[equipNum], 1, {
  191.             ["MobileTargetSystemCF"] = MobileTargetSystemCF
  192.         })
  193.     end
  194.     local function inputBeganSkill(plr, equipNum)
  195.         local skillName = profileData.SkillsEquipped[equipNum]
  196.         if cds[skillName] then
  197.             return
  198.         end
  199.         if SkillsController.SkillDisabled or not GamemodeController.ControlsEnabled then
  200.             return
  201.         end
  202.        
  203.         local canProceed = SkillsController:InputBeganSkill(plr.Character, profileData.SkillsEquipped[equipNum], 1, {})
  204.         if canProceed then
  205.             inputEndedSkill(plr, equipNum)
  206.         end
  207.     end
  208.     UserInputService.InputBegan:Connect(function(Input, Other)
  209.         -- ButtonA is counted as jump so it doesn't detect it
  210.         if Input.KeyCode == Enum.KeyCode.X or Input.KeyCode == Enum.KeyCode.ButtonA then
  211.             if profileData.SkillsEquipped[2] then
  212.                 SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[2]] = true
  213.                 inputBeganSkill(plr, 2)
  214.             end
  215.         end
  216.        
  217.         if Other then
  218.             return
  219.         end
  220.        
  221.         if GameSettings.NumberOfSkillsEquippable == 1 then
  222.             if Input.KeyCode == Enum.KeyCode.Q or Input.KeyCode == Enum.KeyCode.ButtonX then
  223.                 SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[1]] = true
  224.                 inputBeganSkill(plr, 1)
  225.             end
  226.         else
  227.             -- 2+ skills
  228.             if Input.KeyCode == Enum.KeyCode.Z or Input.KeyCode == Enum.KeyCode.ButtonX then
  229.                 if profileData.SkillsEquipped[1] then
  230.                     SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[1]] = true
  231.                     inputBeganSkill(plr, 1)
  232.                 end
  233.             elseif Input.KeyCode == Enum.KeyCode.C or Input.KeyCode == Enum.KeyCode.ButtonB then
  234.                 if profileData.SkillsEquipped[3] then
  235.                     SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[3]] = true
  236.                     inputBeganSkill(plr, 3)
  237.                 end
  238.             end
  239.         end
  240.     end)
  241.     UserInputService.InputEnded:Connect(function(Input, Other)
  242.         if GameSettings.NumberOfSkillsEquippable == 1 then
  243.             if Input.KeyCode == Enum.KeyCode.Q or Input.KeyCode == Enum.KeyCode.ButtonX then
  244.                 SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[1]] = false
  245.             end
  246.         else
  247.             -- 2+ skills
  248.             if Input.KeyCode == Enum.KeyCode.Z or Input.KeyCode == Enum.KeyCode.ButtonX then
  249.                 if profileData.SkillsEquipped[1] then
  250.                     SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[1]] = false
  251.                 end
  252.             elseif Input.KeyCode == Enum.KeyCode.X or Input.KeyCode == Enum.KeyCode.ButtonA then
  253.                 if profileData.SkillsEquipped[2] then
  254.                     SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[2]] = false
  255.                 end
  256.             elseif Input.KeyCode == Enum.KeyCode.C or Input.KeyCode == Enum.KeyCode.ButtonB then
  257.                 if profileData.SkillsEquipped[3] then
  258.                     SkillsController.IsSkillButtonPressed[profileData.SkillsEquipped[3]] = false
  259.                 end
  260.             end
  261.         end
  262.     end)
  263.     local chainSystem = HookController.ChainSystem
  264.     local otherDatas = {
  265.         OnEnabledMobileTargetSystem = function()
  266.             local skillEquipped = profileData.SkillsEquipped[1]
  267.             SkillsController.IsSkillButtonPressed[skillEquipped] = true
  268.             task.spawn(function()
  269.                 inputBeganSkill(plr, 1)
  270.             end)
  271.         end,
  272.         OnInputChangedFunction = function(CFTarget)
  273.             local skillEquipped = profileData.SkillsEquipped[1]
  274.             SkillsController.IsSkillButtonPressed[skillEquipped] = true
  275.             MobileTargetSystemCF = CFTarget
  276.             chainSystem.CustomCFToFace = CFTarget
  277.         end,
  278.         Release = function()
  279.             local skillEquipped = profileData.SkillsEquipped[1]
  280.             SkillsController.IsSkillButtonPressed[skillEquipped] = false
  281.             chainSystem.CustomCFToFace = nil
  282.         end,
  283.         OnCancelledFunction = function()
  284.             local skillEquipped = profileData.SkillsEquipped[1]
  285.             SkillsController.IsSkillButtonPressed[skillEquipped] = false
  286.             chainSystem.CustomCFToFace = nil
  287.         end,
  288.     }
  289.     local castSkill = controlsGui:WaitForChild("CastSkill")
  290.     MobileTargetSystemController:SetupMobileTargetSystem(castSkill, otherDatas)
  291.    
  292.     for i=1,3 do
  293.         local castSkill = controlsGui:WaitForChild("CastSkill"..i)
  294.         local otherDatas = {
  295.             OnEnabledMobileTargetSystem = function()
  296.                 local skillEquipped = profileData.SkillsEquipped[i]
  297.                 SkillsController.IsSkillButtonPressed[skillEquipped] = true
  298.                 task.spawn(function()
  299.                     inputBeganSkill(plr, i)
  300.                 end)
  301.             end,
  302.             OnInputChangedFunction = function(CFTarget)
  303.                 local skillEquipped = profileData.SkillsEquipped[i]
  304.                 SkillsController.IsSkillButtonPressed[skillEquipped] = true
  305.                 MobileTargetSystemCF = CFTarget
  306.                 chainSystem.CustomCFToFace = CFTarget
  307.             end,
  308.             Release = function()
  309.                 local skillEquipped = profileData.SkillsEquipped[i]
  310.                 SkillsController.IsSkillButtonPressed[skillEquipped] = false
  311.                 chainSystem.CustomCFToFace = nil
  312.             end,
  313.             OnCancelledFunction = function()
  314.                 local skillEquipped = profileData.SkillsEquipped[i]
  315.                 SkillsController.IsSkillButtonPressed[skillEquipped] = false
  316.                 chainSystem.CustomCFToFace = nil
  317.             end,
  318.         }
  319.         MobileTargetSystemController:SetupMobileTargetSystem(castSkill, otherDatas)
  320.     end
  321.    
  322.     if GameSettings.NumberOfSkillsEquippable > 1 then
  323.         castSkill.Visible = false
  324.         for i=1,GameSettings.NumberOfSkillsEquippable do
  325.             local castSkill = controlsGui:WaitForChild("CastSkill"..i, 6)
  326.             castSkill.Visible = true
  327.         end
  328.     else
  329.        
  330.     end
  331.    
  332.     SkillsService.InitiateSkill:Connect(function(...)
  333.         SkillsController:InitiateSkill(...)
  334.     end)
  335.    
  336.     for i,v in pairs(script:WaitForChild("Skills"):GetChildren()) do
  337.         local succ, err = pcall(function()
  338.             local skillName = v.Name
  339.             if not SkillsController.SkillSystems[skillName] then
  340.                 SkillsController.SkillSystems[skillName] = require(script:WaitForChild("Skills"):FindFirstChild(skillName) or script:WaitForChild("Skills"):FindFirstChild("Default"))
  341.             end
  342.             SkillsController.SkillSystems[skillName].Initiate()
  343.         end)
  344.         if not succ then
  345.             warn(err)
  346.         end
  347.     end
  348. end
  349.  
  350. function SkillsController:KnitInit()   
  351.     SkillsService = Knit.GetService("SkillsService")
  352.     ProfileService = Knit.GetService("ProfileService")
  353.    
  354.     DataController = Knit.GetController("DataController")
  355.     MobileTargetSystemController = Knit.GetController("MobileTargetSystemController")
  356.     GamemodeController = Knit.GetController("GamemodeController")
  357.     HookController = Knit.GetController("HookController")
  358. end
  359.  
  360. return SkillsController
  361.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement