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