--Animation


local leftSlash = script.Parent:WaitForChild("LeftSlash")
local leftSlashAnimation = myHuman:LoadAnimation(leftSlash)
leftSlashAnimation.Priority = Enum.AnimationPriority.Action

local rightSlash = script.Parent:WaitForChild("RightSlash")
local rightSlashAnimation = myHuman:LoadAnimation(rightSlash)
rightSlashAnimation.Priority = Enum.AnimationPriority.Action

function getUnstuck()
	myHuman:Move(Vector3.new(math.random(-1,1),0,math.random(-1,1)))
	wait(0.3)
end

function wander()
	local goal = Vector3.new(myRoot.Position.X+math.random(-120,120),myRoot.Position.Y,myRoot.Position.Z+math.random(-120,120))
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position, goal)
	if path.Status == Enum.PathStatus.Success then
		pathFailCount = 0
		local waypoints = path:GetWaypoints()
		for i,v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(v.Position)
			if i % 5 == 0 then
				if findTarget() then
					break
				end
			end
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess then
				break
			end
		end
	else
		pathFailCount = pathFailCount + 1 
		if pathFailCount > 10 then
			pathFailCount = 0
			getUnstuck()
		end
	end
end

function findTarget()
	local dist = 200
	local target = nil 
	for i,v in ipairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		if human and torso and v.Name ~= script.Parent.Name then
			if (myRoot.Position - torso.Position).Magnitude < dist and human.Health > 0 then
				target = torso
				dist = (myRoot.Position - torso.Position).Magnitude
			end
		end
	end
	return target
end

function checkSight(target)
	local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 200)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
	if hit then
		if hit:IsDescendantOf(target.Parent) then
			return true
		end
	end
	return false
end

function checkHeight(target)
	if math.abs(myRoot.Position.Y - target.Position.Y) < 5 then
		return true
	end
	return false
end

function checkDirect(target)
	if checkSight(target) and checkHeight(target) and (myRoot.Position - target.Position).Magnitude < 30 then
		return true
	end
	return false
end

function attack(target)
	if attackCool == true then
		attackCool = false
		myHuman:MoveTo(target.Position)
		myRoot.Swing:Play()
		local attackAnim = math.random(1,2)
		if attackAnim == 1 then
			leftSlashAnimation:Play()
		else
			rightSlashAnimation:Play()
		end
		
		local human = target.Parent.Humanoid
		human:TakeDamage(math.random(100,300))
		myRoot.Hit:Play()
		if human.Health < 1 then
			wait(0.3)
		end
		
		spawn(function() wait(1) attackCool = true end)
	end
end

function pathToTarget(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position,target.Position)
	if path.Status == Enum.PathStatus.Success then
		pathFailCount = 0
		local waypoints = path:GetWaypoints()
		for i,v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			spawn(function()
				wait(0.5)
				if myRoot.Position.Y < myHuman.WalkToPoint.Y and chasing == false then
					myHuman.Jump = true
				end
			end)
			myHuman:MoveTo(v.Position)
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess then 
				break
			end
			if i % 5 == 0 then
				if checkDirect(target) then
					break
				end
				if target ~= findTarget() then
					break 
				end
			end
			if (waypoints[#waypoints].Position - target.Position).Magnitude > 30 then
				break
			end
		end
	else
		pathFailCount = pathFailCount + 1
		if pathFailCount > 10 then
			pathFailCount = 0
			getUnstuck()
		end
	end
end

function chase(target)
	chasing = true
	myHuman:MoveTo(target.Position)
end

function main()
	chasing = false 
	local target = findTarget()
	if target then
		local targetDistance = (myRoot.Position - target.Position).Magnitude
		if checkDirect(target) then
			if targetDistance > 3 then
				chase(target)
			end
			if targetDistance < 10 then
				attack(target)
			end
		elseif checkSight(target) and math.abs(myRoot.Position.X - target.Position.X) < 1 and 
			math.abs(myRoot.Position.Z - target.Position.Z) < 1 then
				getUnstuck()
		else
			pathToTarget(target)
		end
	else
		wander()
	end
end

while wait(0.1) do
	if myHuman.Health < 1 then
		break
	end
	main()
end