ParticleInt32Parameter.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Globalization;
  7. using System.Runtime.CompilerServices;
  8. namespace MonoGame.Extended.Particles.Data;
  9. /// <summary>
  10. /// Represents an integer parameter for particle properties that can be either a constant value or a randomly generated
  11. /// value within a specified range.
  12. /// </summary>
  13. public struct ParticleInt32Parameter : IEquatable<ParticleInt32Parameter>
  14. {
  15. /// <summary>
  16. /// The <see cref="ParticleValueKind"/> that determines whether this parameter uses a constant value or a randomly
  17. /// generated value.
  18. /// </summary>
  19. public ParticleValueKind Kind;
  20. /// <summary>
  21. /// The constant value when <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>.
  22. /// </summary>
  23. public int Constant;
  24. /// <summary>
  25. /// The minimum value of the range when <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>.
  26. /// </summary>
  27. public int RandomMin;
  28. /// <summary>
  29. /// The maximum value of the range when <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>.
  30. /// </summary>
  31. public int RandomMax;
  32. /// <summary>
  33. /// Gets the current value of this parameter based on its <see cref="Kind"/>
  34. /// </summary>
  35. /// <remarks>
  36. /// If <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>, returns <see cref="Constant"/>.
  37. /// If <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, returns a random value between
  38. /// <see cref="RandomMin"/> and <see cref="RandomMax"/>.
  39. /// </remarks>
  40. public int Value
  41. {
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. get
  44. {
  45. if (Kind == ParticleValueKind.Constant)
  46. {
  47. return Constant;
  48. }
  49. return FastRandom.Shared.Next(RandomMin, RandomMax);
  50. }
  51. }
  52. /// <summary>
  53. /// Initializes a new <see cref="ParticleInt32Parameter"/> value with a constant value.
  54. /// </summary>
  55. /// <param name="value">The constant value for this parameter.</param>
  56. public ParticleInt32Parameter(int value)
  57. {
  58. Kind = ParticleValueKind.Constant;
  59. Constant = value;
  60. RandomMin = default;
  61. RandomMax = default;
  62. }
  63. /// <summary>
  64. /// Initializes a new <see cref="ParticleInt32Parameter"/> value with a random range.
  65. /// </summary>
  66. /// <param name="rangeStart">The minimum value of the random range.</param>
  67. /// <param name="rangeEnd">The maximum value of the random range.</param>
  68. public ParticleInt32Parameter(int rangeStart, int rangeEnd)
  69. {
  70. Kind = ParticleValueKind.Random;
  71. Constant = default;
  72. RandomMin = rangeStart;
  73. RandomMax = rangeEnd;
  74. }
  75. /// <summary>
  76. /// Determines whether the specified object is equal to the current parameter.
  77. /// </summary>
  78. /// <param name="obj">The object to compare with the current parameter.</param>
  79. /// <returns>
  80. /// <see langword="true"/> if the specified object is equal tot he current parameter;
  81. /// otherwise, <see langword="false"/>.
  82. /// </returns>
  83. public override readonly bool Equals([NotNullWhen(true)] object obj)
  84. {
  85. return obj is ParticleInt32Parameter other &&
  86. Equals(other);
  87. }
  88. /// <summary>
  89. /// Determines whether the specified parameter is equal to the current parameter.
  90. /// </summary>
  91. /// <param name="other">The parameter to compare with the current parameter.</param>
  92. /// <returns>
  93. /// <see langword="true"/> if the specified parameter is equal to the current parameter;
  94. /// otherwise, <see langword="false"/>.
  95. /// </returns>
  96. /// <remarks>
  97. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/> only the <see cref="Constant"/> values are
  98. /// compared.
  99. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, both <see cref="RandomMin"/> and
  100. /// <see cref="RandomMax"/> values are compared.
  101. /// </remarks>
  102. public readonly bool Equals(ParticleInt32Parameter other)
  103. {
  104. if (Kind == ParticleValueKind.Constant)
  105. {
  106. return Constant.Equals(other.Constant);
  107. }
  108. return RandomMin.Equals(other.RandomMin) &&
  109. RandomMax.Equals(other.RandomMax);
  110. }
  111. /// <summary>
  112. /// Returns the hash code for this parameter.
  113. /// </summary>
  114. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  115. /// <remarks>
  116. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>, returns the hash of <see cref="Constant"/>.
  117. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/> returns the combined has of
  118. /// <see cref="RandomMin"/> and <see cref="RandomMax"/>.
  119. /// </remarks>
  120. public override readonly int GetHashCode()
  121. {
  122. base.GetHashCode();
  123. if (Kind == ParticleValueKind.Constant)
  124. {
  125. return Constant.GetHashCode();
  126. }
  127. return HashCode.Combine(RandomMin, RandomMax);
  128. }
  129. /// <summary>
  130. /// Returns a string representation of this parameter.
  131. /// </summary>
  132. /// <returns>
  133. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>, returns the string representation of
  134. /// <see cref="Constant"/>.
  135. /// When <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, returns a string in the format
  136. /// "MinValue, MaxValue".
  137. /// </returns>
  138. public override readonly string ToString()
  139. {
  140. if (Kind == ParticleValueKind.Constant)
  141. {
  142. return Constant.ToString(CultureInfo.InvariantCulture);
  143. }
  144. return string.Format(NumberFormatInfo.InvariantInfo, "{0}{1}{2}", RandomMin, NumberFormatInfo.InvariantInfo.NumberGroupSeparator, RandomMax);
  145. }
  146. /// <summary>
  147. /// Determines whether two parameters are equal.
  148. /// </summary>
  149. /// <param name="lhs">The first parameter to compare.</param>
  150. /// <param name="rhs">The second parameter to compare.</param>
  151. /// <returns>
  152. /// <see langword="true"/> if the parameters are equal; otherwise, <see langword="false"/>.
  153. /// </returns>
  154. public static bool operator ==(ParticleInt32Parameter lhs, ParticleInt32Parameter rhs)
  155. {
  156. return lhs.Equals(rhs);
  157. }
  158. /// <summary>
  159. /// Determines whether two parameters are not equal.
  160. /// </summary>
  161. /// <param name="lhs">The first parameter to compare.</param>
  162. /// <param name="rhs">The second parameter to compare.</param>
  163. /// <returns>
  164. /// <see langword="true"/> if the parameters are not equal; otherwise, <see langword="false"/>.
  165. /// </returns>
  166. public static bool operator !=(ParticleInt32Parameter lhs, ParticleInt32Parameter rhs)
  167. {
  168. return !lhs.Equals(rhs);
  169. }
  170. }