daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jan 11th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------
  2. -- Finite State Machine --
  3. --------------------------
  4. local Class = {}
  5.  
  6. function Class.new(Parent)
  7.     local FSM = {
  8.         Parent = Parent,
  9.         Stack = {},
  10.         States = {},
  11.     }
  12.     return setmetatable(FSM, Class)
  13. end
  14.  
  15. function Class:AddState(Index, Value)
  16.     self.States[Index] = {Name = Index, Func = Value}
  17. end
  18.  
  19. function Class:RemoveState(Index)
  20.     table.remove(self.States, Index)
  21. end
  22.  
  23. function Class:GetCurrentState()
  24.     local StateName = self.Stack[#self.Stack]
  25.     local State = self.States[StateName]
  26.     return State
  27. end
  28.  
  29. function Class:PopState()
  30.     --warn("Popping state")
  31.     local output = self.Stack[#self.Stack]
  32.     -- self.Stack[#self.Stack] = nil
  33.     table.remove(self.Stack, #self.Stack)
  34.     return output
  35. end
  36.  
  37. function Class:PushState(StateName)
  38.     local CurrentState = self:GetCurrentState()
  39.     if CurrentState == nil or CurrentState.Name ~= StateName then
  40.         --warn("Pushing state")
  41.         self.Stack[#self.Stack+1] = StateName
  42.     else
  43.         --warn("Cannot push State while in same State:", State)
  44.     end
  45. end
  46.  
  47. function Class:Update()
  48.     local CurrentState = self:GetCurrentState()
  49.     if CurrentState ~= nil then
  50.         CurrentState.Func(self.Parent)
  51.     else
  52.         error("Error: CurrentState is nil.")
  53.     end
  54. end
  55.  
  56. Class.__index = Class
  57.  
  58. return Class
  59.  
  60. -------------------------------------------------------------------------------------------------------------------------------
  61. -------------------------------------------------------------------------------------------------------------------------------
  62. -------------------------------------------------------------------------------------------------------------------------------
  63.  
  64. local Players = game:GetService("Players")
  65.  
  66.  
  67. return {
  68.     Idle = function(self)
  69.         self:StopMoving()
  70.        
  71.         local Goal = self:GetTarget()
  72.        
  73.         if Goal then
  74.             self.LastTargetUpdate = tick()
  75.             self.Target = Goal
  76.             self.StateMachine:PushState("Seek")
  77.         end
  78.     end,
  79.    
  80.     Seek = function(self)
  81.         --if tick() - self.LastTargetUpdate < self.TargetUpdateRate then
  82.         if tick() - self.LastTargetUpdate < 10 then
  83.             local DistanceFromTarget = self:MoveTo(self.Target[2])
  84.             if DistanceFromTarget < self.AttackRange then
  85.                 -- Target is within range
  86.                 self.LastAttack = tick() - (self.AttackRate * 0.75)
  87.                 self.StateMachine:PushState("Attack")
  88.             else
  89.                 -- Target is not in range
  90.                 -- self:LookAt(self.Target[2])
  91.                 if not self:HasPath() then
  92.                     warn("Getting path")
  93.                     self.StateMachine:PushState("FindPath")
  94.                 else
  95.                     warn("Following path")
  96.                     self:FollowPath()
  97.                 end
  98.             end
  99.         else
  100.             -- Time to change targets
  101.             print("Time to change targets")
  102.             self.StateMachine:PopState()
  103.         end
  104.     end,
  105.    
  106.     Attack = function(self)
  107.         if self:GetDistanceFromTarget(self.Target[2]) > self.AttackRange then
  108.             self.StateMachine:PopState()
  109.         else
  110.             self:Attack(self.Target[2])
  111.             self:LookAt(self.Target[2])
  112.             self:StopMoving()
  113.         end
  114.     end,
  115.    
  116.     FindPath = function(self)
  117.         self:StopMoving()
  118.         if self:FindPath(self.Target[2]) then
  119.             self.StateMachine:PopState()
  120.             --self.StateMachine:PushState("Seek")
  121.         end
  122.     end,
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top