--------------------[ JUMPING ANIMATION ]---------------------------------------------


function onFall(initialVelocity)
	spawn(function()
		local velocityAlpha = math.max(math.min(initialVelocity / Humanoid.JumpPower, 1), 0)
		local startJumpPos = jumpAnim.Pos
		local startJumpRot = jumpAnim.Rot
		local endJumpPos = 0.04 * S.fallSettings.fallMultiplier * velocityAlpha
		local endJumpRot = RAD(4) * S.fallSettings.fallMultiplier * velocityAlpha
		local t0 = tick()
		while true do
			RS.Heartbeat:wait()
			local Alpha = math.min((tick() - t0) / 0.15, 1) * 90
			if onGround then break end
			jumpAnim.Pos = numLerp(startJumpPos, endJumpPos, Sine(Alpha))
			jumpAnim.Rot = numLerp(startJumpRot, endJumpRot, Sine(Alpha))
			if Alpha == 90 then break end
		end
		startJumpPos = endJumpPos
		startJumpRot = endJumpRot
		endJumpPos = -0.08 * S.fallSettings.fallMultiplier
		endJumpRot = -RAD(8) * S.fallSettings.fallMultiplier
		local X = 1
		while true do
			local dt = RS.Heartbeat:wait()
			X = X + (dt * 60) / X
			local Alpha = (X - 1) / 15
			if onGround then break end
			jumpAnim.Pos = numLerp(startJumpPos, endJumpPos, Alpha)
			jumpAnim.Rot = numLerp(startJumpRot, endJumpRot, Alpha)
		end
	end)
end

function onLand(fallDist)
	spawn(function()
		local animAlpha = math.min(fallDist, S.fallSettings.maxDist) * (2 / 3)
		local startJumpPos = jumpAnim.Pos
		local startJumpRot = jumpAnim.Rot
		local endJumpPos = animAlpha / 100 * S.fallSettings.landMultiplier * (runReady and 1 or 2)
		local endJumpRot = RAD(animAlpha) * S.fallSettings.landMultiplier * (runReady and 1 or 2)
		local t0 = tick()
		while true do
			RS.Heartbeat:wait()
			local Alpha = math.min((tick() - t0) / 0.2, 1)
			if (not onGround) then break end
			jumpAnim.Pos = numLerp(startJumpPos, endJumpPos, Alpha)
			jumpAnim.Rot = numLerp(startJumpRot, endJumpRot, Alpha)
			if Alpha == 1 then break end
		end
		t0 = tick()
		while true do
			RS.Heartbeat:wait()
			local Alpha = math.min((tick() - t0) / 0.3, 1) * 90
			if (not onGround) then break end
			jumpAnim.Pos = numLerp(endJumpPos, 0, Sine(Alpha))
			jumpAnim.Rot = numLerp(endJumpRot, 0, Sine(Alpha))
			if Alpha == 90 then break end
		end
	end)
end

function onHumanoidStateChanged(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		onGround = false
		if S.fallAnimation then
			onFall(HRP.Velocity.Y)
			while HRP.Velocity.Y > 0 do RS.RenderStepped:wait() end
			startFallHeight = HRP.Position.Y
		end
	elseif oldState == Enum.HumanoidStateType.Freefall then
		onGround = true
		if S.fallAnimation then
			local fallDist = startFallHeight - HRP.Position.Y
			onLand(fallDist)
		end
	end
end