Ints.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 
  2. using UnityEngine;
  3. namespace FirstGearGames.Utilities.Maths
  4. {
  5. /// <summary>
  6. /// Various utility classes relating to floats.
  7. /// </summary>
  8. public static class Ints
  9. {
  10. private static System.Random _random = new System.Random();
  11. /// <summary>
  12. /// Pads an index a specified value. Preferred over typical padding so that pad values used with skins can be easily found in the code.
  13. /// </summary>
  14. /// <param name="value"></param>
  15. /// <param name="padding"></param>
  16. /// <returns></returns>
  17. public static string PadInt(int value, int padding)
  18. {
  19. return value.ToString().PadLeft(padding, '0');
  20. }
  21. /// <summary>
  22. /// Provides a random inclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max.
  23. /// </summary>
  24. /// <param name="minimum">Inclusive minimum value.</param>
  25. /// <param name="maximum">Inclusive maximum value.</param>
  26. /// <returns></returns>
  27. public static int RandomInclusiveRange(int minimum, int maximum)
  28. {
  29. return _random.Next(minimum, maximum + 1);
  30. }
  31. /// <summary>
  32. /// Provides a random exclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max.
  33. /// </summary>
  34. /// <param name="minimum">Inclusive minimum value.</param>
  35. /// <param name="maximum">Exclusive maximum value.</param>
  36. /// <returns></returns>
  37. public static int RandomExclusiveRange(int minimum, int maximum)
  38. {
  39. return _random.Next(minimum, maximum);
  40. }
  41. /// <summary>
  42. /// Returns a clamped int within a specified range.
  43. /// </summary>
  44. /// <param name="value">Value to clamp.</param>
  45. /// <param name="minimum">Minimum value.</param>
  46. /// <param name="maximum">Maximum value.</param>
  47. /// <returns></returns>
  48. public static int Clamp(int value, int minimum, int maximum)
  49. {
  50. if (value < minimum)
  51. value = minimum;
  52. else if (value > maximum)
  53. value = maximum;
  54. return value;
  55. }
  56. /// <summary>
  57. /// Determins if all values passed in are the same.
  58. /// </summary>
  59. /// <param name="values">Values to check.</param>
  60. /// <returns>True if all values are the same.</returns>
  61. public static bool ValuesMatch(params int[] values)
  62. {
  63. if (values.Length == 0)
  64. {
  65. Debug.Log("Ints -> ValuesMatch -> values array is empty.");
  66. return false;
  67. }
  68. //Assign first value as element in first array.
  69. int firstValue = values[0];
  70. //Check all values.
  71. for (int i = 1; i < values.Length; i++)
  72. {
  73. //If any value doesn't match first value return false.
  74. if (firstValue != values[i])
  75. return false;
  76. }
  77. //If this far all values match.
  78. return true;
  79. }
  80. }
  81. }