Advertisement
Guest User

VoHelp.cs

a guest
Aug 25th, 2015
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.81 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7.  
  8. public class VoHelp
  9. {
  10.  
  11.     public static int OneDee ( Vector2 coords, int width, int height = 0 ) { return OneDee( ( int )coords.x, ( int )coords.y, width, height ); }
  12.     public static int OneDee ( int x, int y, int width, int height = 0 )
  13.     {
  14.         return ( y * width ) + x;
  15.     }
  16.    
  17.     public static Vector2 TwoDee ( int index, int width, int height = 0 )
  18.     {
  19.         int x = index % width;
  20.         int y = index / width;
  21.         return new Vector2( x, y );
  22.     }
  23.    
  24.     public static bool CoinFlip ()
  25.     {
  26.         return UnityEngine.Random.value < 0.5f;
  27.     }
  28.  
  29.     public static float Sign ( float value )
  30.     {
  31.         if ( value == 0.0f ) return 0.0f;
  32.         return Mathf.Sign( value );
  33.     }
  34.  
  35.     public static float Decay ( float value, float inertia )
  36.     {
  37.         float sign = Mathf.Sign( value );
  38.         value -= value * ( 1.0f - inertia ) * Time.deltaTime;
  39.         if ( Mathf.Sign( value ) != sign ) value = 0.0f;
  40.         return value;
  41.     }
  42.  
  43.     public static float GetAngleDiff( Vector2 one, Vector2 two )
  44.     {
  45.         Vector2 diff = one - two;
  46.         return Mathf.Atan2( diff.y, diff.x ) * Mathf.Rad2Deg;  
  47.     }
  48.  
  49.     public static Vector2 Vector( float angle, float magnitude = 1.0f )
  50.     {
  51.         float rad = angle * Mathf.Deg2Rad;
  52.         return new Vector2( Mathf.Cos( rad ) * magnitude, Mathf.Sin( rad ) * magnitude );
  53.     }
  54.    
  55.     public static float GetAngle( Vector2 vec )
  56.     {
  57.             return Mathf.Atan2( vec.y, vec.x ) * Mathf.Rad2Deg;
  58.     }
  59.  
  60.     public static Vector2 MultX ( Vector2 vec, float mult )
  61.     {
  62.         vec.x *= mult;
  63.         return vec;
  64.     }
  65.  
  66.     public static Vector2 MultY ( Vector2 vec, float mult )
  67.     {
  68.         vec.x *= mult;
  69.         return vec;
  70.     }
  71.    
  72.   public static float Wrap ( float x, float min, float max )
  73.   {
  74.     Assert.IsTrue( min < max, "VoHelp.Wrap: MIN IS NOT LESS THAN MAX" );
  75.    
  76.     float diff = max - min;
  77.     while ( x < min ) { x += diff; }
  78.     while ( x > max ) { x -= diff; }
  79.     return x;
  80.   }
  81.  
  82.   public static int Wrap ( int x, int kLowerBound, int kUpperBound )
  83.   {
  84.     Assert.IsTrue( kLowerBound < kUpperBound, "VoHelp.Wrap: MIN IS NOT LESS THAN MAX" );
  85.    
  86.     int range_size = kUpperBound - kLowerBound + 1;
  87.  
  88.     if ( x < kLowerBound )
  89.         x += range_size * ( ( kLowerBound - x ) / range_size + 1 );
  90.  
  91.     return kLowerBound + ( x - kLowerBound ) % range_size;
  92.   }
  93.  
  94.   //returns 0.0 - 1.0
  95.   public static float SinWave ( float t )
  96.   {
  97.     return ( ( 1.0f + Mathf.Sin( t ) ) * 0.5f );
  98.   }
  99.  
  100.   public static int ClosestIndex ( float val, float[] vals )
  101.   {
  102.     int closest = -1;
  103.     float closestDist = float.MaxValue;
  104.    
  105.     for ( int i = 0; i < vals.Length; i++ )
  106.     {
  107.         float dist = Mathf.Abs( vals[ i ] - val );
  108.         if ( dist < closestDist )
  109.         {
  110.             closestDist = dist;
  111.             closest = i;
  112.         }
  113.     }
  114.     return closest;
  115.   }
  116.  
  117.     public static int ClosestIndex ( Vector2 val, Vector2[] vals )
  118.   {
  119.     Vector3 newVal = ( Vector3 )val;
  120.     Vector3[] newVals = new Vector3[ vals.Length ];
  121.     for ( int i = 0; i < newVals.Length; i++ ) { newVals[ i ] = vals[ i ]; }
  122.     return ClosestIndex( newVal, newVals );
  123.   }
  124.  
  125.     public static int ClosestIndex ( Vector3 val, Vector3[] vals )
  126.   {
  127.     int closest = -1;
  128.     float closestDist = float.MaxValue;
  129.    
  130.     for ( int i = 0; i < vals.Length; i++ )
  131.     {
  132.         float dist = ( val - vals[ i ] ).sqrMagnitude;
  133.         if ( dist < closestDist )
  134.         {
  135.             closestDist = dist;
  136.             closest = i;
  137.         }
  138.     }
  139.     return closest;
  140.   }
  141.    
  142.     public static bool Close( float f1, float f2, float epsilon = 0.0000001192093f )
  143.     {
  144.         return Mathf.Abs( f1 - f2 ) <= epsilon;
  145.     }
  146.    
  147.     public static float FreeLerp ( float a, float b, float t )
  148.     {
  149.         return ( 1.0f - t ) * a + t * b;
  150.     }
  151.  
  152.     public static Vector2 FreeLerp ( Vector2 a, Vector2 b, float t )
  153.     {
  154.         return new Vector2( FreeLerp( a.x, b.x, t ), FreeLerp( a.y, b.y, t ) );
  155.     }
  156.  
  157.     public static Vector3 FreeLerp ( Vector3 a, Vector3 b, float t )
  158.     {
  159.         return new Vector3( FreeLerp( a.x, b.x, t ), FreeLerp( a.y, b.y, t ), FreeLerp( a.z, b.z, t ) );
  160.     }
  161.  
  162.     public static Vector3 Delerp( Vector3 min, Vector3 max, Vector3 progress )
  163.     {
  164.         return new Vector3( Delerp( min.x, max.x, progress.x ), Delerp( min.y, max.y, progress.y ), Delerp( min.z, max.z, progress.z ) );
  165.     }  
  166.    
  167.     public static Vector2 Delerp( Vector3 min, Vector3 max, Vector2 progress )
  168.     {
  169.         return new Vector2( Delerp( min.x, max.x, progress.x ), Delerp( min.y, max.y, progress.y ) );
  170.     }  
  171.  
  172.     public static float Delerp( float min, float max, float progress )
  173.     {
  174.         return ( ( progress - min ) / ( max - min ) );
  175.     }  
  176.  
  177.     public static float SmoothOut ( float p )
  178.     {
  179.         return Mathf.Sin( p * Mathf.PI * 0.5f );
  180.     }
  181.  
  182.     public static float SmoothIn ( float p )
  183.     {
  184.         return 1.0f - Mathf.Cos( p * Mathf.PI * 0.5f );
  185.     }
  186.  
  187.     public static float Smooth ( float p, int repeat = 1 )
  188.     {
  189.         for ( int i = 0; i < repeat; i++ )
  190.         {
  191.             p = Mathf.SmoothStep( 0.0f, 1.0f, p );
  192.         }
  193.         return p;
  194.     }
  195.  
  196.     public static float ReturnLerp ( float minValue, float maxValue, float p )
  197.     {
  198.         p = Mathf.Sin( p * Mathf.PI );
  199.         return Mathf.Lerp( minValue, maxValue, p );
  200.     }  
  201.  
  202.     public static float MidLerp ( float minValue, float midValue, float maxValue, float p, float pTransition = 0.5f )
  203.     {
  204.         if ( p < 1.0f - pTransition )
  205.         {
  206.             p = Mathf.Clamp01( p / ( 1.0f - pTransition ) );
  207.             return Mathf.Lerp( minValue, midValue, SmoothOut( p ) );
  208.         }
  209.  
  210.         p = Mathf.Clamp01( ( p - ( 1.0f - pTransition ) ) / pTransition );
  211.         return Mathf.Lerp( midValue, maxValue, SmoothIn( p ) );
  212.     }
  213.  
  214.     public static float OverLerp ( float minValue, float maxValue, float currentP, float postTimeP = 0.5f, float postBounceP = 0.125f )
  215.     {
  216.         Assert.IsTrue( postTimeP <= 1.0f, "BLERP: Post values add up to be greater than one!" );
  217.  
  218.         float p = currentP;
  219.  
  220.         if ( p < 1.0f - postTimeP )
  221.         {          
  222.             p = Mathf.Clamp01( p / ( 1.0f - postTimeP ) );
  223.             return Mathf.Lerp( minValue, maxValue, p );
  224.         }
  225.         else if ( p < 1.0f )
  226.         {
  227.             float totalDist = maxValue - minValue;
  228.             p = Mathf.Clamp01( ( p - ( 1.0f - postTimeP ) ) / postTimeP );
  229.             p = Mathf.Sin( p * Mathf.PI );
  230.             return maxValue + Mathf.Lerp( 0.0f, totalDist * postBounceP, p );
  231.         }
  232.  
  233.         return maxValue;
  234.     }
  235.    
  236.     //Commas!
  237.     public static string AddCommas( int n )
  238.     {
  239.         return AddCommas(  n + ""  );
  240.     }
  241.    
  242.     public static string AddCommas( string n )
  243.     {
  244.         string display = "";
  245.         for ( int i = 0; i < n.Length; i++ ) {
  246.             if ( i > 0 && ( n.Length-i ) % 3 == 0 ) {
  247.                 display += ",";
  248.             }  
  249.             display += n[i];
  250.         }
  251.         return display;
  252.     }
  253.  
  254.     public static string AddSign ( int n )
  255.     {
  256.         if ( n > 0 ) return "+" + n;
  257.         return "-" + Mathf.Abs( n );
  258.     }
  259.  
  260.     public static string PrependZeroes ( int n, int numDigits )
  261.     {
  262.         string result = n + "";
  263.         while ( result.Length < numDigits ) result = "0" + result;
  264.         return result;
  265.     }
  266.  
  267.     public static string Capitalize( string s )
  268.     {
  269.         if ( string.IsNullOrEmpty( s ) ) return string.Empty;
  270.         return char.ToUpper( s[ 0 ] ) + s.Substring( 1 );
  271.     }
  272.  
  273.     public static T StringToEnum< T >( string str ) where T : struct
  274.     {  
  275.         try  
  276.         {  
  277.             T res = (T)Enum.Parse(typeof(T), str);  
  278.             if (!Enum.IsDefined(typeof(T), res)) return default(T);  
  279.             return res;  
  280.         }  
  281.         catch  
  282.         {  
  283.             UnityEngine.Debug.Log( str + " NOT FOUND IN ENUM " + typeof( T ).ToString() );
  284.             return default(T);  
  285.         }  
  286.     }
  287.    
  288.     //Bresenham's line algorithm
  289.     public static Vector2[] BreLineAlgorithm( int x0, int y0, int x1, int y1 ) {
  290.         ArrayList arr = new ArrayList();
  291.        
  292.         float dx = Mathf.Abs( x1 - x0 );
  293.         float dy = Mathf.Abs( y1 - y0 );
  294.        
  295.         int sx;
  296.         int sy;
  297.        
  298.         if ( x0 < x1 ) { sx = 1; } else { sx = -1; }
  299.         if ( y0 < y1 ) { sy = 1; } else { sy = -1; }
  300.        
  301.         int err = (int)dx-(int)dy;
  302.         while ( true ) {
  303.             arr.Add( new Vector2( x0, y0 ) );
  304.            
  305.             if ( x0 == x1 && y0 == y1 ) {
  306.                 Vector2[] result = new Vector2[ arr.Count ];
  307.                 arr.CopyTo( result );
  308.                 return result;
  309.             }
  310.            
  311.             float e2 = 5 * err;
  312.             if ( e2 > -dy ) {
  313.                 err -= (int)dy;
  314.                 x0 += sx;  
  315.             }
  316.             if ( e2 < dx ) {
  317.                 err += (int)dx;
  318.                 y0 += sy;  
  319.             }
  320.         }
  321.     }
  322.  
  323.   public static T[] Concat< T >( T[] arr1, T[] arr2 )
  324.   {
  325.     T[] arr3 = new T[ arr1.Length + arr2.Length ];
  326.     arr1.CopyTo( arr3, 0 );
  327.     arr2.CopyTo( arr3, arr1.Length );
  328.     return arr3;
  329.   }
  330.  
  331.   public static bool EitherEquals< T >( T thing1, T thing2, T thingEquals ) where T : System.IComparable<T>
  332.   {
  333.     if ( thing1.CompareTo( thingEquals ) == 0 ) { return true; }
  334.     if ( thing2.CompareTo( thingEquals ) == 0 ) { return true; }
  335.     return false;
  336.   }
  337.  
  338.   public static int Range ( IntRange r )
  339.   {
  340.     return ( int )UnityEngine.Random.Range( r.min, r.max );
  341.   }
  342.  
  343.   public static float Range ( FloatRange r )
  344.   {
  345.     return ( float )UnityEngine.Random.Range( r.min, r.max );
  346.   }
  347.  
  348.     public static T[,] Resize2D<T>(T[,] original, int x, int y)
  349.     {
  350.         T[,] newArray = new T[x, y];
  351.         int minX = Mathf.Min(original.GetLength(0), newArray.GetLength(0));
  352.         int minY = Mathf.Min(original.GetLength(1), newArray.GetLength(1));
  353.  
  354.         for (int i = 0; i < minY; ++i)
  355.             System.Array.Copy(original, i * original.GetLength(0), newArray, i * newArray.GetLength(0), minX);
  356.  
  357.         return newArray;
  358.     }
  359.  
  360.     public static T[] RemoveAt<T>( T[] source, int index )
  361.     {
  362.         T[] dest = new T[source.Length - 1];
  363.         if( index > 0 )
  364.             Array.Copy(source, 0, dest, 0, index);
  365.  
  366.         if( index < source.Length - 1 )
  367.             Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
  368.  
  369.         return dest;
  370.     }
  371.  
  372.     public static T PickRandom< T > ( T[] source )
  373.     {
  374.         return source[ UnityEngine.Random.Range( 0, source.Length ) ];
  375.     }
  376.  
  377.     public static T[] ShuffleList< T > ( T[] list )
  378.     {
  379.         int i = list.Length;
  380.         if ( i == 0 ) return list;
  381.  
  382.         while ( i > 0 ) {
  383.             i--;
  384.             int j = ( int )Mathf.Floor( UnityEngine.Random.value * ( i + 1 ) );
  385.             T tempi = list[ i ];
  386.             T tempj = list[ j ];
  387.             list[ i ] = tempj;
  388.             list[ j ] = tempi;
  389.         }
  390.  
  391.         return list;
  392.     }
  393.  
  394.     public static Color SetAlpha ( Color c, float a )
  395.     {
  396.         return new Color( c.r, c.g, c.b, a );
  397.     }
  398.  
  399.     public static Color RandomColor ()
  400.     {
  401.         Color result = new Color();
  402.         result.a = 1.0f;
  403.  
  404.         float total = 2.0f;
  405.         result.r = UnityEngine.Random.Range( 0.0f, 1.0f );
  406.         total -= result.r;
  407.         result.g = Mathf.Min( UnityEngine.Random.Range( 0.0f, total ), 1.0f );
  408.         total -= result.g;
  409.         result.b = total;
  410.        
  411.         return result;
  412.     }
  413.  
  414.     public static Color ParseColor ( string col )
  415.     {
  416.        //Takes strings formatted with numbers and no spaces before or after the commas:
  417.        // "RGBA(1.000, 1.000, 1.000, 1.000)""
  418.        col = col.Substring( 5, col.Length - 6 );
  419.        string[] strings = col.Split(","[0] );
  420.  
  421.        Color output = Color.white;
  422.        for ( int i = 0; i < 4; i++ ) {
  423.           output[ i ] = float.Parse( strings[ i ] );
  424.        }
  425.  
  426.        return output;
  427.     }
  428.  
  429.     public static Vector2 ParseVector2 ( string vec )
  430.     {
  431.         //"(0.0, 0.0)"
  432.        vec = vec.Substring( 1, vec.Length - 2 );
  433.        string[] strings = vec.Split(","[0] );
  434.  
  435.        Vector2 output = Vector2.zero;
  436.        for ( int i = 0; i < 2; i++ ) {
  437.           output[ i ] = float.Parse( strings[ i ] );
  438.        }
  439.  
  440.        return output;
  441.     }
  442.  
  443.     public static Vector3 ParseVector3 ( string vec )
  444.     {
  445.  
  446.         //"(0.0, 0.0, 0.0)"
  447.        vec = vec.Substring( 1, vec.Length - 2 );
  448.        string[] strings = vec.Split(","[0] );
  449.  
  450.        Vector3 output = Vector3.zero;
  451.        for ( int i = 0; i < 3; i++ ) {
  452.           output[ i ] = float.Parse( strings[ i ] );
  453.        }
  454.  
  455.        return output;
  456.     }
  457.  
  458.   public static string ReverseString( string s )
  459.   {
  460.      char[] charArray = s.ToCharArray();
  461.      System.Array.Reverse( charArray );
  462.      return new string( charArray );
  463.   }
  464.    
  465.   public static string RandomString ( int length )
  466.   {
  467.     string result = "";
  468.     for ( int i = 0; i < length; i++ ) result += VoHelp.RandomLetter( true );
  469.     return result;
  470.   }
  471.  
  472.     public static string ReadableTimespan ( TimeSpan t )
  473.     {
  474.         int days = ( int )t.TotalDays;
  475.         if ( days > 365 ) return ">1 year";
  476.         else if ( days > 0 ) return days + "d";
  477.  
  478.         int hours = ( int )t.TotalHours;
  479.         if ( hours > 0 ) return hours + "h";
  480.  
  481.         int minutes = ( int )t.TotalMinutes;
  482.         if ( minutes > 0 ) return minutes + "m";
  483.  
  484.         int seconds = ( int )t.TotalSeconds;
  485.         return seconds + "s";
  486.     }
  487.  
  488.  
  489.     public static string RandomLetter ( bool spaces = false )
  490.     {
  491.         string val = "";
  492.         int i = UnityEngine.Random.Range( 0, 26 + ( spaces ? 5 : 0 ) );
  493.         switch ( i ) {
  494.             case 0 :
  495.                 val = "A";
  496.                 break;
  497.             case 1 :
  498.                 val = "B";
  499.                 break;         
  500.             case 2 :
  501.                 val = "C";
  502.                 break;
  503.             case 3 :
  504.                 val = "D";
  505.                 break;
  506.             case 4 :
  507.                 val = "E";
  508.                 break;
  509.             case 5 :
  510.                 val = "F";
  511.                 break;
  512.             case 6 :
  513.                 val = "G";
  514.                 break;
  515.             case 7 :
  516.                 val = "H";
  517.                 break;
  518.             case 8 :
  519.                 val = "I";
  520.                 break;
  521.             case 9 :
  522.                 val = "J";
  523.                 break;
  524.             case 10 :
  525.                 val = "K";
  526.                 break;
  527.             case 11 :
  528.                 val = "L";
  529.                 break;
  530.             case 12 :
  531.                 val = "M";
  532.                 break;
  533.             case 13 :
  534.                 val = "N";
  535.                 break;
  536.             case 14 :
  537.                 val = "O";
  538.                 break;
  539.             case 15 :
  540.                 val = "P";
  541.                 break;
  542.             case 16 :
  543.                 val = "Q";
  544.                 break;
  545.             case 17 :
  546.                 val = "R";
  547.                 break;
  548.             case 18 :
  549.                 val = "S";
  550.                 break;
  551.             case 19 :
  552.                 val = "T";
  553.                 break;
  554.             case 20 :
  555.                 val = "U";
  556.                 break;
  557.             case 21 :
  558.                 val = "V";
  559.                 break;
  560.             case 22 :
  561.                 val = "W";
  562.                 break;
  563.             case 23 :
  564.                 val = "X";
  565.                 break;
  566.             case 24 :
  567.                 val = "Y";
  568.                 break;
  569.             case 25 :
  570.                 val = "Z";
  571.                 break;
  572.             default:
  573.                 val = " ";
  574.                 break;
  575.         }
  576.         return val;
  577.     }
  578.    
  579.     public static string GetTimestamp () { return GetTimestamp( DateTime.Now ); }
  580.     public static string GetTimestamp ( DateTime dateTime ) { return dateTime.ToString( "yyyyMMddHHmmssfff" ); }
  581.     public static DateTime ParseTimestamp ( string timeStamp ) { return DateTime.ParseExact( timeStamp, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.InvariantCulture ); }
  582.  
  583.  
  584.     public static void RunSh ( string path, string[] arguments = null )
  585.     {
  586.         string terminalPath = "sh";
  587.         string terminalCommand = path;
  588.         if ( arguments != null )
  589.         {
  590.         for ( int i = 0; i < arguments.Length; i++ )
  591.         {
  592.             terminalCommand += " " + arguments[ i ];
  593.         }
  594.         }
  595.         System.Diagnostics.Process.Start( terminalPath, terminalCommand );
  596.     }
  597. }
  598.  
  599.  
  600. [ System.Serializable ]
  601. public class FloatRange {
  602.     public FloatRange( float mi, float ma ) { min = mi; max = ma; }
  603.     public float min;
  604.     public float max;
  605.    
  606.     public float length
  607.     {
  608.         get { return max - min; }
  609.     }
  610. }
  611.  
  612. [ System.Serializable ]
  613. public class IntRange {
  614.     public IntRange( int mi, int ma ) { min = mi; max = ma; }
  615.     public int min;
  616.     public int max;
  617.    
  618.     public int length
  619.     {
  620.         get { return max - min; }
  621.     }
  622. }
  623.  
  624.  
  625. public static class VoExtensions
  626. {
  627.     public static string Yell (this String str)
  628.     {
  629.         return str.ToUpper().Replace( " ", "\n" );
  630.     }
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement