local R=require(script.Parent:WaitForChild("\066\097\115\101\067\104\097\114\097\099\116\101\114\067\111\110\116\114\111\108\108\101\114"))local w=setmetatable({},R)w.__index=w function w.new()local e=setmetatable(R.new(),w)e.parentUIFrame=nil e.jumpButton=nil e.characterAddedConn=nil e.humanoidStateEnabledChangedConn=nil e.humanoidJumpPowerConn=nil e.humanoidParentConn=nil e.externallyEnabled=false e.jumpPower=0 e.jumpStateEnabled=true e.isJumping=false e.humanoid=nil return e end function w.EnableButton(w,R)if R then if not w.jumpButton then w:Create()end local R=Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if R and w.externallyEnabled then if w.externallyEnabled then if R.JumpPower>0 then w.jumpButton.Visible=true end end end else w.jumpButton.Visible=false w.isJumping=false w.jumpButton.ImageRectOffset=Vector2.new(1,146)end end function w.UpdateEnabled(R)if R.jumpPower>0 and R.jumpStateEnabled then R:EnableButton(true)else R:EnableButton(false)end end function w.HumanoidChanged(w,R)local e=Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if e then if R=="\074\117\109\112\080\111\119\101\114"then w.jumpPower=e.JumpPower w:UpdateEnabled()elseif R=="\080\097\114\101\110\116"then if not e.Parent then w.humanoidChangeConn:Disconnect()end end end end function w.HumanoidStateEnabledChanged(e,R,w)if R==Enum.HumanoidStateType.Jumping then e.jumpStateEnabled=w e:UpdateEnabled()end end function w.CharacterAdded(w,R)if w.humanoidChangeConn then w.humanoidChangeConn:Disconnect()w.humanoidChangeConn=nil end w.humanoid=R:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")while not w.humanoid do R.ChildAdded:wait()w.humanoid=R:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")end w.humanoidJumpPowerConn=(w.humanoid:GetPropertyChangedSignal("\074\117\109\112\080\111\119\101\114")):Connect(function()w.jumpPower=w.humanoid.JumpPower w:UpdateEnabled()end)w.humanoidParentConn=(w.humanoid:GetPropertyChangedSignal("\080\097\114\101\110\116")):Connect(function()if not w.humanoid.Parent then w.humanoidJumpPowerConn:Disconnect()w.humanoidJumpPowerConn=nil w.humanoidParentConn:Disconnect()w.humanoidParentConn=nil end end)w.humanoidStateEnabledChangedConn=w.humanoid.StateEnabledChanged:Connect(function(R,e)w:HumanoidStateEnabledChanged(R,e)end)w.jumpPower=w.humanoid.JumpPower w.jumpStateEnabled=w.humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)w:UpdateEnabled()end function w.SetupCharacterAddedFunction(R)R.characterAddedConn=Players.LocalPlayer.CharacterAdded:Connect(function(w)R:CharacterAdded(w)end)if Players.LocalPlayer.Character then R:CharacterAdded(Players.LocalPlayer.Character)end end function w.Enable(e,R,w)if w then e.parentUIFrame=w end e.externallyEnabled=R e:EnableButton(R)end function w.Create(R)if not R.parentUIFrame then return end if R.jumpButton then R.jumpButton:Destroy()R.jumpButton=nil end local w=math.min(R.parentUIFrame.AbsoluteSize.x,R.parentUIFrame.AbsoluteSize.y)local e=w<=500 local Q=e and 70 or 120 R.jumpButton=Instance.new("\073\109\097\103\101\066\117\116\116\111\110")R.jumpButton.Name="\074\117\109\112\066\117\116\116\111\110"R.jumpButton.Visible=false R.jumpButton.BackgroundTransparency=1 R.jumpButton.Image=TOUCH_CONTROL_SHEET R.jumpButton.ImageRectOffset=Vector2.new(1,146)R.jumpButton.ImageRectSize=Vector2.new(144,144)R.jumpButton.Size=UDim2.new(0,Q,0,Q)R.jumpButton.Position=e and UDim2.new(1,-(Q*1.5-10),1,-Q-20)or UDim2.new(1,-(Q*1.5-10),1,-Q*1.75)local S=nil R.jumpButton.InputBegan:connect(function(w)if S or w.UserInputType~=Enum.UserInputType.Touch or w.UserInputState~=Enum.UserInputState.Begin then return end S=w R.jumpButton.ImageRectOffset=Vector2.new(146,146)R.isJumping=true end)local D=function()S=nil R.isJumping=false R.jumpButton.ImageRectOffset=Vector2.new(1,146)end R.jumpButton.InputEnded:connect(function(R)if R==S then D()end end)GuiService.MenuOpened:connect(function()if S then D()end end)if not R.characterAddedConn then R:SetupCharacterAddedFunction()end R.jumpButton.Parent=R.parentUIFrame end return w
--
local BaseCharacterController = require(script.Parent:WaitForChild("BaseCharacterController"))
local TouchJump = setmetatable({}, BaseCharacterController)
TouchJump.__index = TouchJump

function TouchJump.new()
	local self = setmetatable(BaseCharacterController.new(), TouchJump)

	self.parentUIFrame = nil
	self.jumpButton = nil
	self.characterAddedConn = nil
	self.humanoidStateEnabledChangedConn = nil
	self.humanoidJumpPowerConn = nil
	self.humanoidParentConn = nil
	self.externallyEnabled = false
	self.jumpPower = 0
	self.jumpStateEnabled = true
	self.isJumping = false
	self.humanoid = nil -- saved reference because property change connections are made using it

	return self
end

function TouchJump:EnableButton(enable)
	if enable then
		if not self.jumpButton then
			self:Create()
		end
		local humanoid = Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
		if humanoid and self.externallyEnabled then
			if self.externallyEnabled then
				if humanoid.JumpPower > 0 then
					self.jumpButton.Visible = true
				end
			end
		end
	else
		self.jumpButton.Visible = false
		self.isJumping = false
		self.jumpButton.ImageRectOffset = Vector2.new(1, 146)
	end
end

function TouchJump:UpdateEnabled()
	if self.jumpPower > 0 and self.jumpStateEnabled then
		self:EnableButton(true)
	else
		self:EnableButton(false)
	end
end

function TouchJump:HumanoidChanged(prop)
	local humanoid = Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		if prop == "JumpPower" then
			self.jumpPower =  humanoid.JumpPower
			self:UpdateEnabled()
		elseif prop == "Parent" then
			if not humanoid.Parent then
				self.humanoidChangeConn:Disconnect()
			end
		end
	end
end

function TouchJump:HumanoidStateEnabledChanged(state, isEnabled)
	if state == Enum.HumanoidStateType.Jumping then
		self.jumpStateEnabled = isEnabled
		self:UpdateEnabled()
	end
end

function TouchJump:CharacterAdded(char)
	if self.humanoidChangeConn then
		self.humanoidChangeConn:Disconnect()
		self.humanoidChangeConn = nil
	end

	self.humanoid = char:FindFirstChildOfClass("Humanoid")
	while not self.humanoid do
		char.ChildAdded:wait()
		self.humanoid = char:FindFirstChildOfClass("Humanoid")
	end

	self.humanoidJumpPowerConn = self.humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function()
		self.jumpPower =  self.humanoid.JumpPower
		self:UpdateEnabled()
	end)

	self.humanoidParentConn = self.humanoid:GetPropertyChangedSignal("Parent"):Connect(function()
		if not self.humanoid.Parent then
			self.humanoidJumpPowerConn:Disconnect()
			self.humanoidJumpPowerConn = nil
			self.humanoidParentConn:Disconnect()
			self.humanoidParentConn = nil
		end
	end)

	self.humanoidStateEnabledChangedConn = self.humanoid.StateEnabledChanged:Connect(function(state, enabled)
		self:HumanoidStateEnabledChanged(state, enabled)
	end)

	self.jumpPower = self.humanoid.JumpPower
	self.jumpStateEnabled = self.humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)
	self:UpdateEnabled()
end

function TouchJump:SetupCharacterAddedFunction()
	self.characterAddedConn = Players.LocalPlayer.CharacterAdded:Connect(function(char)
		self:CharacterAdded(char)
	end)
	if Players.LocalPlayer.Character then
		self:CharacterAdded(Players.LocalPlayer.Character)
	end
end

function TouchJump:Enable(enable, parentFrame)
	if parentFrame then
		self.parentUIFrame = parentFrame
	end
	self.externallyEnabled = enable
	self:EnableButton(enable)
end

function TouchJump:Create()
	if not self.parentUIFrame then
		return
	end

	if self.jumpButton then
		self.jumpButton:Destroy()
		self.jumpButton = nil
	end

	local minAxis = math.min(self.parentUIFrame.AbsoluteSize.x, self.parentUIFrame.AbsoluteSize.y)
	local isSmallScreen = minAxis <= 500
	local jumpButtonSize = isSmallScreen and 70 or 120

	self.jumpButton = Instance.new("ImageButton")
	self.jumpButton.Name = "JumpButton"
	self.jumpButton.Visible = false
	self.jumpButton.BackgroundTransparency = 1
	self.jumpButton.Image = TOUCH_CONTROL_SHEET
	self.jumpButton.ImageRectOffset = Vector2.new(1, 146)
	self.jumpButton.ImageRectSize = Vector2.new(144, 144)
	self.jumpButton.Size = UDim2.new(0, jumpButtonSize, 0, jumpButtonSize)

    self.jumpButton.Position = isSmallScreen and UDim2.new(1, -(jumpButtonSize*1.5-10), 1, -jumpButtonSize - 20) or
        UDim2.new(1, -(jumpButtonSize*1.5-10), 1, -jumpButtonSize * 1.75)

	local touchObject = nil
	self.jumpButton.InputBegan:connect(function(inputObject)
		--A touch that starts elsewhere on the screen will be sent to a frame's InputBegan event
		--if it moves over the frame. So we check that this is actually a new touch (inputObject.UserInputState ~= Enum.UserInputState.Begin)
		if touchObject or inputObject.UserInputType ~= Enum.UserInputType.Touch
			or inputObject.UserInputState ~= Enum.UserInputState.Begin then
			return
		end

		touchObject = inputObject
		self.jumpButton.ImageRectOffset = Vector2.new(146, 146)
		self.isJumping = true
	end)

	local OnInputEnded = function()
		touchObject = nil
		self.isJumping = false
		self.jumpButton.ImageRectOffset = Vector2.new(1, 146)
	end

	self.jumpButton.InputEnded:connect(function(inputObject)
		if inputObject == touchObject then
			OnInputEnded()
		end
	end)

	GuiService.MenuOpened:connect(function()
		if touchObject then
			OnInputEnded()
		end
	end)

	if not self.characterAddedConn then
		self:SetupCharacterAddedFunction()
	end

	self.jumpButton.Parent = self.parentUIFrame
end

return TouchJump
