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

	-- 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
