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