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