Advertisement
Alexplazz

Untitled

Jan 7th, 2024
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. local Soldier, Names = {}, require(game.ReplicatedStorage.Names)
  2. local SoldierID = 0
  3. local SoldierActions = require(script.SoldierActions)
  4. local Hats = game.ServerStorage.Hats:GetChildren()
  5. Soldier.__index = Soldier
  6.  
  7. function Soldier.new(Team)
  8.     SoldierID += 1
  9.     -- Create object.
  10.     local NewSoldier = setmetatable({}, Soldier)
  11.     NewSoldier.Name = Names[math.random(1, #Names)]
  12.    
  13.     -- Attributes
  14.     NewSoldier.OnDuty = true
  15.     NewSoldier.SoldierID = SoldierID
  16.    
  17.     -- Give character.
  18.     local newCharacter = Team.TemplateCharacter:Clone()
  19.     newCharacter.Name = NewSoldier.Name
  20.     newCharacter.Humanoid.DisplayName = NewSoldier.Name
  21.     newCharacter.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
  22.     NewSoldier.Character = newCharacter
  23.     NewSoldier.Character:SetAttribute("Break", false)
  24.    
  25.     -- Give random hat.
  26.     local randHat = Hats[math.random(1, #Hats)]:Clone()
  27.     randHat.Parent = newCharacter
  28.    
  29.     -- Character is ready.
  30.     newCharacter.Parent = workspace
  31.  
  32.     -- SpawnPad
  33.     if Team.SpawnPad then
  34.         newCharacter:MoveTo(Team.SpawnPad.Position)
  35.     end
  36.    
  37.     -- Add to team.
  38.     if Team then
  39.         Team:AddSoldier(NewSoldier)
  40.     end
  41.    
  42.     coroutine.wrap(function()
  43.         NewSoldier:HeartLoop(Team.AtWarWith)
  44.     end)()
  45.    
  46.     return NewSoldier
  47. end
  48.  
  49. function Soldier:Die()
  50.     self.OnDuty = false
  51.     wait(2)
  52.     self.Character:Destroy()
  53. end
  54.  
  55. function Soldier:DistanceTo(Part)
  56.     local PrimaryPart = self.Character.PrimaryPart
  57.     if PrimaryPart then
  58.         local CurrentPosition = PrimaryPart.Position
  59.         local EndPosition = Part.Position
  60.         local Direction = EndPosition - CurrentPosition
  61.         return Direction.Magnitude
  62.     end
  63. end
  64.  
  65. function Soldier:FindClosestEnemy(EnemyTeam)
  66.     local ClosestStuds, ClosestEnemy = math.huge, false
  67.     for _, EnemySoldier in pairs(EnemyTeam.Soldiers) do
  68.         local Character = EnemySoldier.Character
  69.         if Character and Character.PrimaryPart then
  70.             local Distance = self:DistanceTo(Character.PrimaryPart)
  71.             if Distance < ClosestStuds then
  72.                 if Character.Humanoid.Health > 1 then
  73.                     ClosestStuds = Distance
  74.                     ClosestEnemy = EnemySoldier
  75.                 end
  76.             end
  77.         end
  78.     end
  79.     return ClosestEnemy
  80. end
  81.  
  82. local IdleRange = 5
  83. function Soldier:Idle()
  84.     local Character, Position = self.Character, self.Character.PrimaryPart.Position
  85.     if math.random(1,5) == 1 then
  86.  
  87.         local X = Position.X + math.random(-IdleRange, IdleRange)
  88.         local Z = Position.Z + math.random(-IdleRange, IdleRange)
  89.  
  90.         Position = Vector3.new(X, Position.Y, Z)
  91.  
  92.         Character.Humanoid:MoveTo(Position)
  93.     end
  94. end
  95.  
  96. function Soldier:FindPath(Target)
  97.     local Path = game:GetService("PathfindingService"):CreatePath({
  98.         AgentCanJump = true
  99.     })
  100.  
  101.     Path:ComputeAsync(self.Character.PrimaryPart.Position, Target.Character.PrimaryPart.Position)
  102.     local Waypoints = Path:GetWaypoints()
  103.  
  104.     if Path.Status == Enum.PathStatus.Success then
  105.         for _, v in ipairs(Waypoints) do
  106.             if v.Action == Enum.PathWaypointAction.Jump then
  107.                 self.Character.Humanoid.Jump = true
  108.             end
  109.             self.Character.Humanoid:MoveTo(v.Position)
  110.             local TimeOut = self.Character.Humanoid.MoveToFinished:Wait(1)
  111.             -- TimeOut
  112.             if not TimeOut then
  113.                 print("Path too long!")
  114.                 self:FindPath(Target)
  115.                 break
  116.             end
  117.            
  118.             -- CheckSight
  119.             if (not self.Character.PrimaryPart) or (not Target.Character.PrimaryPart) then
  120.                 return 
  121.             end
  122.            
  123.             if (self.Character.PrimaryPart.Position - Target.Character.PrimaryPart.Position).magnitude < 10 then
  124.                 Target.Character.Humanoid:TakeDamage(10)
  125.                 if Target.Character.Humanoid.Health < 1 then
  126.                     Target:Die()
  127.                     return
  128.                 end
  129.             end
  130.            
  131.             -- Can the soldier currently make a kill?
  132.            
  133.            
  134.             if (self.Character.PrimaryPart.Position - Waypoints[1].Position).magnitude > 20 then
  135.                 print("Target has moved.")
  136.                 self:FindPath(Target)
  137.                 break
  138.             end
  139.         end
  140.     end
  141. end
  142.  
  143. function Soldier:Heartbeat(EnemyTeam)
  144.     local Target = self:FindClosestEnemy(EnemyTeam)
  145.     if Target then
  146.         self:FindPath(Target)
  147.         print("Busy")
  148.     else
  149.         print("Bored")
  150.         self:Idle()
  151.     end
  152. end
  153.  
  154. function Soldier:HeartLoop(EnemyTeam)
  155.     while wait(0.1) do
  156.         if not self.OnDuty then break end
  157.         if self.Character.Humanoid.Health < 1 then
  158.             break
  159.         end
  160.  
  161.         self:Heartbeat(EnemyTeam)
  162.     end
  163.  
  164. end
  165.  
  166. return Soldier
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement