Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Soldier, Names = {}, require(game.ReplicatedStorage.Names)
- local SoldierID = 0
- local SoldierActions = require(script.SoldierActions)
- local Hats = game.ServerStorage.Hats:GetChildren()
- Soldier.__index = Soldier
- function Soldier.new(Team)
- SoldierID += 1
- -- Create object.
- local NewSoldier = setmetatable({}, Soldier)
- NewSoldier.Name = Names[math.random(1, #Names)]
- -- Attributes
- NewSoldier.OnDuty = true
- NewSoldier.SoldierID = SoldierID
- -- Give character.
- local newCharacter = Team.TemplateCharacter:Clone()
- newCharacter.Name = NewSoldier.Name
- newCharacter.Humanoid.DisplayName = NewSoldier.Name
- newCharacter.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Viewer
- NewSoldier.Character = newCharacter
- NewSoldier.Character:SetAttribute("Break", false)
- -- Give random hat.
- local randHat = Hats[math.random(1, #Hats)]:Clone()
- randHat.Parent = newCharacter
- -- Character is ready.
- newCharacter.Parent = workspace
- -- SpawnPad
- if Team.SpawnPad then
- newCharacter:MoveTo(Team.SpawnPad.Position)
- end
- -- Add to team.
- if Team then
- Team:AddSoldier(NewSoldier)
- end
- coroutine.wrap(function()
- NewSoldier:HeartLoop(Team.AtWarWith)
- end)()
- return NewSoldier
- end
- function Soldier:Die()
- self.OnDuty = false
- wait(2)
- self.Character:Destroy()
- end
- function Soldier:DistanceTo(Part)
- local PrimaryPart = self.Character.PrimaryPart
- if PrimaryPart then
- local CurrentPosition = PrimaryPart.Position
- local EndPosition = Part.Position
- local Direction = EndPosition - CurrentPosition
- return Direction.Magnitude
- end
- end
- function Soldier:FindClosestEnemy(EnemyTeam)
- local ClosestStuds, ClosestEnemy = math.huge, false
- for _, EnemySoldier in pairs(EnemyTeam.Soldiers) do
- local Character = EnemySoldier.Character
- if Character and Character.PrimaryPart then
- local Distance = self:DistanceTo(Character.PrimaryPart)
- if Distance < ClosestStuds then
- if Character.Humanoid.Health > 1 then
- ClosestStuds = Distance
- ClosestEnemy = EnemySoldier
- end
- end
- end
- end
- return ClosestEnemy
- end
- local IdleRange = 5
- function Soldier:Idle()
- local Character, Position = self.Character, self.Character.PrimaryPart.Position
- if math.random(1,5) == 1 then
- local X = Position.X + math.random(-IdleRange, IdleRange)
- local Z = Position.Z + math.random(-IdleRange, IdleRange)
- Position = Vector3.new(X, Position.Y, Z)
- Character.Humanoid:MoveTo(Position)
- end
- end
- function Soldier:FindPath(Target)
- local Path = game:GetService("PathfindingService"):CreatePath({
- AgentCanJump = true
- })
- Path:ComputeAsync(self.Character.PrimaryPart.Position, Target.Character.PrimaryPart.Position)
- local Waypoints = Path:GetWaypoints()
- if Path.Status == Enum.PathStatus.Success then
- for _, v in ipairs(Waypoints) do
- if v.Action == Enum.PathWaypointAction.Jump then
- self.Character.Humanoid.Jump = true
- end
- self.Character.Humanoid:MoveTo(v.Position)
- local TimeOut = self.Character.Humanoid.MoveToFinished:Wait(1)
- -- TimeOut
- if not TimeOut then
- print("Path too long!")
- self:FindPath(Target)
- break
- end
- -- CheckSight
- if (not self.Character.PrimaryPart) or (not Target.Character.PrimaryPart) then
- return
- end
- if (self.Character.PrimaryPart.Position - Target.Character.PrimaryPart.Position).magnitude < 10 then
- Target.Character.Humanoid:TakeDamage(10)
- if Target.Character.Humanoid.Health < 1 then
- Target:Die()
- return
- end
- end
- -- Can the soldier currently make a kill?
- if (self.Character.PrimaryPart.Position - Waypoints[1].Position).magnitude > 20 then
- print("Target has moved.")
- self:FindPath(Target)
- break
- end
- end
- end
- end
- function Soldier:Heartbeat(EnemyTeam)
- local Target = self:FindClosestEnemy(EnemyTeam)
- if Target then
- self:FindPath(Target)
- print("Busy")
- else
- print("Bored")
- self:Idle()
- end
- end
- function Soldier:HeartLoop(EnemyTeam)
- while wait(0.1) do
- if not self.OnDuty then break end
- if self.Character.Humanoid.Health < 1 then
- break
- end
- self:Heartbeat(EnemyTeam)
- end
- end
- return Soldier
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement