ParticleVector2Parameter.cs 7.0 KB

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