local function y()local y=findPlayerHumanoid(Player)return y~=nil and y.Health>0 end local function P(y)if y~=nil then for y,P in pairs(y:GetChildren())do if P:IsA("\084\111\111\108")then return P end end end end local X=nil local C=nil local D=nil local U=nil local function m()if X then X:Cancel()X=nil end if D then D:Disconnect()D=nil end if U then U:Disconnect()U=nil end if C then C:Destroy()end end local function A(y,C,A,c,V)X=y y:Start(V)D=y.Finished.Event:Connect(function()m()if A then local y=P(c)if y then y:Activate()end end end)U=y.PathFailed.Event:Connect(function()m()if V==nil or V then local y=PlayFailureAnimation and not(X and X:IsActive())if y then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(C)end end)end local function c(y)if X and X:IsActive()then X:Cancel()end if PlayFailureAnimation then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(y)end function OnTap(X,C,D)local U=Workspace.CurrentCamera local V=Player.Character if not y()then return end if#X==1 or C then if U then local y=U:ScreenPointToRay(X[1].x,X[1].y)local P=Ray.new(y.Origin,y.Direction*1000)local q=findPlayerHumanoid(Player)local L,N,T=Utility.Raycast(P,true,getIgnoreList())local Z,O=Utility.FindCharacterAncestor(L)if D and(O and StarterGui:GetCore("\065\118\097\116\097\114\067\111\110\116\101\120\116\077\101\110\117\069\110\097\098\108\101\100"))then local y=Players:GetPlayerFromCharacter(O.Parent)if y then m()return end end if C then N=C Z=nil end if N and V then m()local y=Pather(N,T)if y:IsValidPath()then A(y,N,Z,V)else y:Cleanup()c(N)end end end elseif#X>=2 then if U then local y=P(V)if y then y:Activate()end end end end local function V(y)if y then y:Disconnect()end end


local function CheckAlive()
	local humanoid = findPlayerHumanoid(Player)
	return humanoid ~= nil and humanoid.Health > 0
end

local function GetEquippedTool(character)
	if character ~= nil then
		for _, child in pairs(character:GetChildren()) do
			if child:IsA('Tool') then
				return child
			end
		end
	end
end

local ExistingPather = nil
local ExistingIndicator = nil
local PathCompleteListener = nil
local PathFailedListener = nil

local function CleanupPath()
	if ExistingPather then
		ExistingPather:Cancel()
		ExistingPather = nil
	end
	if PathCompleteListener then
		PathCompleteListener:Disconnect()
		PathCompleteListener = nil
	end
	if PathFailedListener then
		PathFailedListener:Disconnect()
		PathFailedListener = nil
	end
	if ExistingIndicator then
		ExistingIndicator:Destroy()
	end
end

local function HandleMoveTo(thisPather, hitPt, hitChar, character, overrideShowPath)
	ExistingPather = thisPather
	thisPather:Start(overrideShowPath)

	PathCompleteListener = thisPather.Finished.Event:Connect(function()
		CleanupPath()
		if hitChar then
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end)
	PathFailedListener = thisPather.PathFailed.Event:Connect(function()
		CleanupPath()
		if overrideShowPath == nil or overrideShowPath then
			local shouldPlayFailureAnim = PlayFailureAnimation and not (ExistingPather and ExistingPather:IsActive())
			if shouldPlayFailureAnim then
				ClickToMoveDisplay.PlayFailureAnimation()
			end
			ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
		end
	end)
end

local function ShowPathFailedFeedback(hitPt)
	if ExistingPather and ExistingPather:IsActive() then
		ExistingPather:Cancel()
	end
	if PlayFailureAnimation then
		ClickToMoveDisplay.PlayFailureAnimation()
	end
	ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
end

function OnTap(tapPositions, goToPoint, wasTouchTap)
	-- Good to remember if this is the latest tap event
	local camera = Workspace.CurrentCamera
	local character = Player.Character

	if not CheckAlive() then return end

	-- This is a path tap position
	if #tapPositions == 1 or goToPoint then
		if camera then
			local unitRay = camera:ScreenPointToRay(tapPositions[1].x, tapPositions[1].y)
			local ray = Ray.new(unitRay.Origin, unitRay.Direction*1000)

			local myHumanoid = findPlayerHumanoid(Player)
			local hitPart, hitPt, hitNormal = Utility.Raycast(ray, true, getIgnoreList())

			local hitChar, hitHumanoid = Utility.FindCharacterAncestor(hitPart)
			if wasTouchTap and hitHumanoid and StarterGui:GetCore("AvatarContextMenuEnabled") then
				local clickedPlayer = Players:GetPlayerFromCharacter(hitHumanoid.Parent)
				if clickedPlayer then
					CleanupPath()
					return
				end
			end
			if goToPoint then
				hitPt = goToPoint
				hitChar = nil
			end
			if hitPt and character then
				-- Clean up current path
				CleanupPath()
				local thisPather = Pather(hitPt, hitNormal)
				if thisPather:IsValidPath() then
					HandleMoveTo(thisPather, hitPt, hitChar, character)
				else
					-- Clean up
					thisPather:Cleanup()
					-- Feedback here for when we don't have a good path
					ShowPathFailedFeedback(hitPt)
				end
			end
		end
	elseif #tapPositions >= 2 then
		if camera then
			-- Do shoot
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end
end

local function DisconnectEvent(event)
	if event then
		event:Disconnect()
	end
end
