Advertisement
TaylorsRus

Untitled

Aug 29th, 2023
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. local StateClass = {}
  2. StateClass.__index = StateClass
  3.  
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local RunService = game:GetService("RunService")
  6. local Player = game:GetService("Players").LocalPlayer
  7.  
  8. local State = ReplicatedStorage:WaitForChild("State")
  9.  
  10. local AlreadyCounting = false
  11. local LastChange = nil
  12.  
  13. local COUNTDOWN_INTERVAL = .5
  14. local IDLE_REVERT_TIME = 5
  15.  
  16. function StateClass.new(StateMachine)
  17.     local self = setmetatable({}, StateClass)
  18.     self.Name = StateMachine
  19.     self.States = {}   
  20.     for _,State in ipairs(State[StateMachine]:GetChildren()) do
  21.         self.States[State.Name] = require(State)
  22.     end
  23.  
  24.     self.State = require(script.None)
  25.     self.ReturnState = self.States.Idle
  26.     return self
  27. end
  28.  
  29. function StateClass:InitStates()
  30.     for _,State in self.States do
  31.         if State.Init then
  32.             State.Init()
  33.         end
  34.     end
  35. end
  36.  
  37. function StateClass:ChangeState(State)
  38.     if self.State.Name == State then
  39.         return
  40.     end
  41.     local ValidState = false
  42.     local ErrorMessage = ""
  43.    
  44.     for StateName, Other in self.States do
  45.         ErrorMessage = "Attempted to change to invalid state enum."
  46.         if StateName == State then
  47.             ValidState = true
  48.             break
  49.         end
  50.     end
  51.  
  52.     local OldState = self.State
  53.     local NewState = self.States[State]
  54.    
  55.     if NewState.RequiredStates then
  56.         ValidState = false
  57.         ErrorMessage = "Attempted to forbidden state"
  58.        
  59.         for _,RequiredState in ipairs(NewState.RequiredStates) do
  60.             if RequiredState == OldState.Name then
  61.                 ValidState = true
  62.             end
  63.         end
  64.     end
  65.        
  66.     if not ValidState then
  67.         error(ErrorMessage..": "..NewState.Name)
  68.     end
  69.    
  70.    
  71.     if not NewState.IgnoreCallbacks then       
  72.         if OldState.Exit and not NewState.IgnoreExit then
  73.             OldState.Exit(NewState)
  74.         end
  75.         if NewState.Enter then
  76.             NewState.Enter(OldState)
  77.         end
  78.     end
  79.  
  80.     self.State = NewState  
  81.     print(self.Name.." State Change:",self.State.Name)
  82.     if NewState.Name == "Idle" then
  83.         return
  84.     end
  85.    
  86.     LastChange = os.clock()
  87.     if AlreadyCounting then return end
  88.     AlreadyCounting = true
  89.    
  90.     if NewState.Movement then
  91.         local Humanoid = Player.Character:WaitForChild("Humanoid")
  92.         repeat task.wait() until Humanoid.MoveDirection.Magnitude == 0
  93.     elseif not NewState.Movement and not NewState.Holdable then
  94.         while true do
  95.             task.wait(COUNTDOWN_INTERVAL)
  96.             local DeltaTime = os.clock() - LastChange
  97.             if DeltaTime >= IDLE_REVERT_TIME then
  98.                 break
  99.             end
  100.         end
  101.     end
  102.    
  103.     if not NewState.Holdable then
  104.         self:ChangeState(self.ReturnState)
  105.     end
  106.     AlreadyCounting = false
  107. end
  108.  
  109. function StateClass:ChangeReturnState(State)
  110.     if not self.States[State] then
  111.         error("Attempted to change return state to invalid state enum.")
  112.     end
  113.     self.ReturnState = self.States[State]
  114. end
  115.  
  116. function StateClass:FetchState(State)
  117.     return self.State
  118. end
  119.  
  120.  
  121. return StateClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement