Advertisement
Guest User

Untitled

a guest
May 26th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. local PathfindingService = game:GetService("PathfindingService")
  2. local enemies = game:GetService("ServerStorage").Enemies:GetChildren()
  3.  
  4. local Enemy = {}
  5. Enemy.__index = Enemy
  6.  
  7. function Enemy.new(spawnPoint)
  8.     local self = setmetatable({}, Enemy)
  9.     local enemy = Enemy:Spawn(spawnPoint)
  10.     self.Enemy = enemy
  11.     self.Humanoid = enemy:WaitForChild("Humanoid")
  12.     self.Root = enemy:WaitForChild("HumanoidRootPart")
  13.     self.Root:SetNetworkOwner(enemy)
  14.     self.Health = enemy:GetAttribute("Health")
  15.     self.Humanoid.MaxHealth = self.Health
  16.     self.Damage = enemy:GetAttribute("Damage")
  17.     self.Target = nil
  18.    
  19.     return self
  20. end
  21.  
  22. function Enemy:Init()
  23.     while wait(0.1) do
  24.         if self.Humanoid.Health < 1 then
  25.             break
  26.         end
  27.         self:FindTarget()
  28.         if self.Target then
  29.             self.Humanoid.WalkSpeed = 16
  30.             self:FindPath()
  31.         else
  32.             self.Humanoid.WalkSpeed = 8
  33.             self:Wander()
  34.         end
  35.     end
  36. end
  37.  
  38. function Enemy:Spawn(spawnPoint)
  39.     local enemy = enemies[math.random(1, #enemies)]:Clone()
  40.     enemy.Parent = workspace
  41.     enemy:SetPrimaryPartCFrame(spawnPoint.CFrame)
  42.    
  43.     return enemy
  44. end
  45.  
  46. function Enemy:Wander()
  47.     local xRand = math.random(-50, 50)
  48.     local zRand = math.random(-50, 50)
  49.     local goal = self.Root.Position + Vector3.new(xRand, 0, zRand)
  50.    
  51.     local path = PathfindingService:CreatePath()
  52.     path:ComputeAsync(self.Root.Position, goal)
  53.     local waypoints = path:GetWaypoints()
  54.    
  55.     if path.Status == Enum.PathStatus.Success then
  56.         for _, waypoint in ipairs(waypoints) do
  57.             if waypoint.Action == Enum.PathWaypointAction.Jump  then
  58.                 self.Humanoid.Jump = true
  59.             end
  60.             self.Humanoid:MoveTo(waypoint.Position)
  61.             local timeOut = self.Humanoid.MoveToFinished:Wait(1)
  62.             if not timeOut then
  63.                 print("Enemy Stuck")
  64.                 self.Humanoid.Jump = true
  65.                 self:Wander()
  66.             end
  67.         end
  68.     else
  69.         print("Path failed")
  70.         self:Wander()
  71.     end
  72. end
  73.  
  74. function Enemy:CheckSight()
  75.     if self.Target then
  76.         local ray = Ray.new(self.Root.Position, (self.Target.Position - self.Root.Position).Unit * 40)
  77.         local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, {self.Enemy})
  78.         if hit then
  79.             if hit:IsDescendantOf(self.Target) and math.abs(hit.Position.Y - self.Root.Position.Y) < 3 then
  80.                 print("Enemy can see the target")
  81.                 return true
  82.             end
  83.         end
  84.         return false
  85.     else
  86.         return false
  87.     end
  88. end
  89.  
  90. function Enemy:FindTarget()
  91.     local distance = 200
  92.     local potentialTargets = {}
  93.     local seeTargets = {}
  94.     for i, v in ipairs(workspace:GetChildren()) do
  95.         local human = v:FindFirstChild("Humanoid")
  96.         local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
  97.  
  98.         if human and torso and v.Name ~= self.Enemy.Name then
  99.             if (self.Root.Position - torso.Position).magnitude < distance and human.Health > 0 then
  100.                 table.insert(potentialTargets, torso)
  101.             end
  102.         end
  103.     end
  104.     if #potentialTargets > 0 then
  105.         for i, v in ipairs(potentialTargets) do
  106.             if self:CheckSight() then
  107.                 table.insert(seeTargets, v)
  108.             elseif #seeTargets == 0 and (self.Root.Position - v.Position).magnitude < distance then
  109.                 self.Target = v
  110.                 distance = (self.Root.Position - v.Position).magnitude
  111.             end
  112.         end
  113.     end
  114.     if #seeTargets > 0 then
  115.         distance = 200
  116.         for i, v in ipairs(seeTargets) do
  117.             if (self.Root.Position - v.Position).magnitude < distance then
  118.                 self.Target = v
  119.                 distance = (self.Root.Position - v.Position).magnitude
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125. function Enemy:FindPath()
  126.     local path = PathfindingService:CreatePath()
  127.     path:ComputeAsync(self.Root.Position, self.Target.Position)
  128.     local waypoints = path:GetWaypoints()
  129.    
  130.     if path.Status == Enum.PathStatus.Success then
  131.         for _, waypoint in ipairs(waypoints) do
  132.             if waypoint.Action == Enum.PathWaypointAction.Jump then
  133.                 self.Humanoid.Jump = true
  134.             end
  135.             self.Humanoid:MoveTo(waypoint.Position)
  136.             local timeOut = self.Humanoid.MoveToFinished:Wait(1)
  137.             if not timeOut then
  138.                 self.Humanoid.Jump = true
  139.                 self:FindPath()
  140.                 break
  141.             end
  142.             if self:CheckSight() then
  143.                 repeat
  144.                     print("Moving to target")
  145.                     self.Humanoid:MoveTo(self.Target.Position)
  146.                     wait(0.1)
  147.                     if self.Target == nil then
  148.                         break
  149.                     elseif self.Target.Parent == nil then
  150.                         break
  151.                     end
  152.                 until self:CheckSight() == false or self.Humanoid.Health < 1 or self.Target.Parent.Humanoid.Health < 1
  153.                 break
  154.             end
  155.             if (self.Root.Position - waypoints[1].Position).magnitude > 20 then
  156.                 print("Target has moved, generating new path")
  157.                 self:FindPath()
  158.                 break
  159.             end
  160.         end
  161.     end
  162. end
  163.  
  164. return Enemy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement