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