local v=require(script.Parent:WaitForChild("\075\101\121\098\111\097\114\100"))local R=setmetatable({},v)R.__index=R function R.new(E)local e=setmetatable(v.new(E),R)e.fingerTouches={}e.numUnsunkTouches=0 e.mouse1Down=tick()e.mouse1DownPos=Vector2.new()e.mouse2DownTime=tick()e.mouse2DownPos=Vector2.new()e.mouse2UpTime=tick()e.keyboardMoveVector=ZERO_VECTOR3 e.tapConn=nil e.inputBeganConn=nil e.inputChangedConn=nil e.inputEndedConn=nil e.humanoidDiedConn=nil e.characterChildAddedConn=nil e.onCharacterAddedConn=nil e.characterChildRemovedConn=nil e.renderSteppedConn=nil e.menuOpenedConnection=nil e.running=false e.wasdEnabled=false return e end function R.DisconnectEvents(v)DisconnectEvent(v.tapConn)DisconnectEvent(v.inputBeganConn)DisconnectEvent(v.inputChangedConn)DisconnectEvent(v.inputEndedConn)DisconnectEvent(v.humanoidDiedConn)DisconnectEvent(v.characterChildAddedConn)DisconnectEvent(v.onCharacterAddedConn)DisconnectEvent(v.renderSteppedConn)DisconnectEvent(v.characterChildRemovedConn)DisconnectEvent(v.menuOpenedConnection)end function R.OnTouchBegan(E,v,R)if E.fingerTouches[v]==nil and not R then E.numUnsunkTouches=E.numUnsunkTouches+1 end E.fingerTouches[v]=R end function R.OnTouchChanged(E,v,R)if E.fingerTouches[v]==nil then E.fingerTouches[v]=R if not R then E.numUnsunkTouches=E.numUnsunkTouches+1 end end end function R.OnTouchEnded(E,v,R)if E.fingerTouches[v]~=nil and E.fingerTouches[v]==false then E.numUnsunkTouches=E.numUnsunkTouches-1 end E.fingerTouches[v]=nil end function R.OnCharacterAdded(R,v)R:DisconnectEvents()R.inputBeganConn=UserInputService.InputBegan:Connect(function(v,E)if v.UserInputType==Enum.UserInputType.Touch then R:OnTouchBegan(v,E)end if R.wasdEnabled and(E==false and(v.UserInputType==Enum.UserInputType.Keyboard and movementKeys[v.KeyCode]))then CleanupPath()ClickToMoveDisplay.CancelFailureAnimation()end if v.UserInputType==Enum.UserInputType.MouseButton1 then R.mouse1DownTime=tick()R.mouse1DownPos=v.Position end if v.UserInputType==Enum.UserInputType.MouseButton2 then R.mouse2DownTime=tick()R.mouse2DownPos=v.Position end end)R.inputChangedConn=UserInputService.InputChanged:Connect(function(v,E)if v.UserInputType==Enum.UserInputType.Touch then R:OnTouchChanged(v,E)end end)R.inputEndedConn=UserInputService.InputEnded:Connect(function(v,E)if v.UserInputType==Enum.UserInputType.Touch then R:OnTouchEnded(v,E)end if v.UserInputType==Enum.UserInputType.MouseButton2 then R.mouse2UpTime=tick()local E=v.Position local e=ExistingPather or R.keyboardMoveVector.Magnitude<=0 if R.mouse2UpTime-R.mouse2DownTime<.25 and((E-R.mouse2DownPos).magnitude<5 and e)then local v={E}OnTap(v)end end end)R.tapConn=UserInputService.TouchTap:Connect(function(v,R)if not R then OnTap(v,nil,true)end end)R.menuOpenedConnection=GuiService.MenuOpened:Connect(function()CleanupPath()end)local function E(v)if UserInputService.TouchEnabled then if v:IsA("\084\111\111\108")then v.ManualActivationOnly=true end end if v:IsA("\072\117\109\097\110\111\105\100")then DisconnectEvent(R.humanoidDiedConn)R.humanoidDiedConn=v.Died:Connect(function()if ExistingIndicator then DebrisService:AddItem(ExistingIndicator.Model,1)end end)end end R.characterChildAddedConn=v.ChildAdded:Connect(function(v)E(v)end)R.characterChildRemovedConn=v.ChildRemoved:Connect(function(v)if UserInputService.TouchEnabled then if v:IsA("\084\111\111\108")then v.ManualActivationOnly=false end end end)for v,R in pairs(v:GetChildren())do E(R)end end function R.Start(v)v:Enable(true)end function R.Stop(v)v:Enable(false)end function R.CleanupPath(v)CleanupPath()end function R.Enable(e,v,R,E)if v then if not e.running then if Player.Character then e:OnCharacterAdded(Player.Character)end e.onCharacterAddedConn=Player.CharacterAdded:Connect(function(v)e:OnCharacterAdded(v)end)e.running=true end e.touchJumpController=E if e.touchJumpController then e.touchJumpController:Enable(e.jumpEnabled)end else if e.running then e:DisconnectEvents()CleanupPath()if UserInputService.TouchEnabled then local v=Player.Character if v then for v,R in pairs(v:GetChildren())do if R:IsA("\084\111\111\108")then R.ManualActivationOnly=false end end end end e.running=false end if e.touchJumpController and not e.jumpEnabled then e.touchJumpController:Enable(true)end e.touchJumpController=nil end if UserInputService.KeyboardEnabled and v~=e.enabled then e.forwardValue=0 e.backwardValue=0 e.leftValue=0 e.rightValue=0 e.moveVector=ZERO_VECTOR3 if v then e:BindContextActions()e:ConnectFocusEventListeners()else e:UnbindContextActions()e:DisconnectFocusEventListeners()end end e.wasdEnabled=v and R or false e.enabled=v end function R.OnRenderStepped(R,v)R.isJumping=false if ExistingPather then ExistingPather:OnRenderStepped(v)if ExistingPather then R.moveVector=ExistingPather.NextActionMoveDirection R.moveVectorIsCameraRelative=false if ExistingPather.NextActionJump then R.isJumping=true end else R.moveVector=R.keyboardMoveVector R.moveVectorIsCameraRelative=true end else R.moveVector=R.keyboardMoveVector R.moveVectorIsCameraRelative=true end if R.jumpRequested then R.isJumping=true end end
--[[ The ClickToMove Controller Class ]]
--
local KeyboardController = require(script.Parent:WaitForChild("Keyboard"))
local ClickToMove = setmetatable({}, KeyboardController)
ClickToMove.__index = ClickToMove

function ClickToMove.new(CONTROL_ACTION_PRIORITY)
	local self = setmetatable(KeyboardController.new(CONTROL_ACTION_PRIORITY), ClickToMove)

	self.fingerTouches = {}
	self.numUnsunkTouches = 0
	-- PC simulation
	self.mouse1Down = tick()
	self.mouse1DownPos = Vector2.new()
	self.mouse2DownTime = tick()
	self.mouse2DownPos = Vector2.new()
	self.mouse2UpTime = tick()

	self.keyboardMoveVector = ZERO_VECTOR3

	self.tapConn = nil
	self.inputBeganConn = nil
	self.inputChangedConn = nil
	self.inputEndedConn = nil
	self.humanoidDiedConn = nil
	self.characterChildAddedConn = nil
	self.onCharacterAddedConn = nil
	self.characterChildRemovedConn = nil
	self.renderSteppedConn = nil
	self.menuOpenedConnection = nil

	self.running = false

	self.wasdEnabled = false

	return self
end

function ClickToMove:DisconnectEvents()
	DisconnectEvent(self.tapConn)
	DisconnectEvent(self.inputBeganConn)
	DisconnectEvent(self.inputChangedConn)
	DisconnectEvent(self.inputEndedConn)
	DisconnectEvent(self.humanoidDiedConn)
	DisconnectEvent(self.characterChildAddedConn)
	DisconnectEvent(self.onCharacterAddedConn)
	DisconnectEvent(self.renderSteppedConn)
	DisconnectEvent(self.characterChildRemovedConn)
	DisconnectEvent(self.menuOpenedConnection)
end

function ClickToMove:OnTouchBegan(input, processed)
	if self.fingerTouches[input] == nil and not processed then
		self.numUnsunkTouches = self.numUnsunkTouches + 1
	end
	self.fingerTouches[input] = processed
end

function ClickToMove:OnTouchChanged(input, processed)
	if self.fingerTouches[input] == nil then
		self.fingerTouches[input] = processed
		if not processed then
			self.numUnsunkTouches = self.numUnsunkTouches + 1
		end
	end
end

function ClickToMove:OnTouchEnded(input, processed)
	if self.fingerTouches[input] ~= nil and self.fingerTouches[input] == false then
		self.numUnsunkTouches = self.numUnsunkTouches - 1
	end
	self.fingerTouches[input] = nil
end

function ClickToMove:OnCharacterAdded(character)
	self:DisconnectEvents()

	self.inputBeganConn = UserInputService.InputBegan:Connect(function(input, processed)
		if input.UserInputType == Enum.UserInputType.Touch then
			self:OnTouchBegan(input, processed)
		end

		-- Cancel path when you use the keyboard controls if wasd is enabled.
		if
			self.wasdEnabled
			and processed == false
			and input.UserInputType == Enum.UserInputType.Keyboard
			and movementKeys[input.KeyCode]
		then
			CleanupPath()
			ClickToMoveDisplay.CancelFailureAnimation()
		end
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			self.mouse1DownTime = tick()
			self.mouse1DownPos = input.Position
		end
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			self.mouse2DownTime = tick()
			self.mouse2DownPos = input.Position
		end
	end)

	self.inputChangedConn = UserInputService.InputChanged:Connect(function(input, processed)
		if input.UserInputType == Enum.UserInputType.Touch then
			self:OnTouchChanged(input, processed)
		end
	end)

	self.inputEndedConn = UserInputService.InputEnded:Connect(function(input, processed)
		if input.UserInputType == Enum.UserInputType.Touch then
			self:OnTouchEnded(input, processed)
		end

		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			self.mouse2UpTime = tick()
			local currPos = input.Position
			-- We allow click to move during path following or if there is no keyboard movement
			local allowed = ExistingPather or self.keyboardMoveVector.Magnitude <= 0
			if
				self.mouse2UpTime - self.mouse2DownTime < 0.25
				and (currPos - self.mouse2DownPos).magnitude < 5
				and allowed
			then
				local positions = { currPos }
				OnTap(positions)
			end
		end
	end)

	self.tapConn = UserInputService.TouchTap:Connect(function(touchPositions, processed)
		if not processed then
			OnTap(touchPositions, nil, true)
		end
	end)

	self.menuOpenedConnection = GuiService.MenuOpened:Connect(function()
		CleanupPath()
	end)

	local function OnCharacterChildAdded(child)
		if UserInputService.TouchEnabled then
			if child:IsA("Tool") then
				child.ManualActivationOnly = true
			end
		end
		if child:IsA("Humanoid") then
			DisconnectEvent(self.humanoidDiedConn)
			self.humanoidDiedConn = child.Died:Connect(function()
				if ExistingIndicator then
					DebrisService:AddItem(ExistingIndicator.Model, 1)
				end
			end)
		end
	end

	self.characterChildAddedConn = character.ChildAdded:Connect(function(child)
		OnCharacterChildAdded(child)
	end)
	self.characterChildRemovedConn = character.ChildRemoved:Connect(function(child)
		if UserInputService.TouchEnabled then
			if child:IsA("Tool") then
				child.ManualActivationOnly = false
			end
		end
	end)
	for _, child in pairs(character:GetChildren()) do
		OnCharacterChildAdded(child)
	end
end

function ClickToMove:Start()
	self:Enable(true)
end

function ClickToMove:Stop()
	self:Enable(false)
end

function ClickToMove:CleanupPath()
	CleanupPath()
end

function ClickToMove:Enable(enable, enableWASD, touchJumpController)
	if enable then
		if not self.running then
			if Player.Character then -- retro-listen
				self:OnCharacterAdded(Player.Character)
			end
			self.onCharacterAddedConn = Player.CharacterAdded:Connect(function(char)
				self:OnCharacterAdded(char)
			end)
			self.running = true
		end
		self.touchJumpController = touchJumpController
		if self.touchJumpController then
			self.touchJumpController:Enable(self.jumpEnabled)
		end
	else
		if self.running then
			self:DisconnectEvents()
			CleanupPath()
			-- Restore tool activation on shutdown
			if UserInputService.TouchEnabled then
				local character = Player.Character
				if character then
					for _, child in pairs(character:GetChildren()) do
						if child:IsA("Tool") then
							child.ManualActivationOnly = false
						end
					end
				end
			end
			self.running = false
		end
		if self.touchJumpController and not self.jumpEnabled then
			self.touchJumpController:Enable(true)
		end
		self.touchJumpController = nil
	end

	-- Extension for initializing Keyboard input as this class now derives from Keyboard
	if UserInputService.KeyboardEnabled and enable ~= self.enabled then
		self.forwardValue = 0
		self.backwardValue = 0
		self.leftValue = 0
		self.rightValue = 0

		self.moveVector = ZERO_VECTOR3

		if enable then
			self:BindContextActions()
			self:ConnectFocusEventListeners()
		else
			self:UnbindContextActions()
			self:DisconnectFocusEventListeners()
		end
	end

	self.wasdEnabled = enable and enableWASD or false
	self.enabled = enable
end

function ClickToMove:OnRenderStepped(dt)
	-- Reset jump
	self.isJumping = false

	-- Handle Pather
	if ExistingPather then
		-- Let the Pather update
		ExistingPather:OnRenderStepped(dt)

		-- If we still have a Pather, set the resulting actions
		if ExistingPather then
			-- Setup move (NOT relative to camera)
			self.moveVector = ExistingPather.NextActionMoveDirection
			self.moveVectorIsCameraRelative = false

			-- Setup jump (but do NOT prevent the base Keayboard class from requesting jumps as well)
			if ExistingPather.NextActionJump then
				self.isJumping = true
			end
		else
			self.moveVector = self.keyboardMoveVector
			self.moveVectorIsCameraRelative = true
		end
	else
		self.moveVector = self.keyboardMoveVector
		self.moveVectorIsCameraRelative = true
	end

	-- Handle Keyboard's jump
	if self.jumpRequested then
		self.isJumping = true
	end
end
