// Copyright (c) Craftwork Games. All rights reserved. // Licensed under the MIT license. // See LICENSE file in the project root for full license information. using System; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using Microsoft.Xna.Framework; namespace MonoGame.Extended.Particles.Data; /// /// Represents an vector parameter for particle properties that can be either a constant value or a /// randomly generated value within a specified range. /// public struct ParticleVector2Parameter : IEquatable { /// /// The that determines whether this parameter uses a constant value or a randomly /// generated value. /// public ParticleValueKind Kind; /// /// The constant value when is . /// public Vector2 Constant; /// /// The minimum value of the range when is . /// public Vector2 RandomMin; /// /// The maximum value of the range when is . /// public Vector2 RandomMax; /// /// Gets the current value of this parameter based on its /// /// /// If is , returns . /// If is , returns a random value between /// and . /// public Vector2 Value { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if (Kind == ParticleValueKind.Constant) { return Constant; } Vector2 v; v.X = FastRandom.Shared.NextSingle(RandomMin.X, RandomMax.X); v.Y = FastRandom.Shared.NextSingle(RandomMin.Y, RandomMax.Y); return v; } } /// /// Initializes a new value with a constant value. /// /// The constant value for this parameter. public ParticleVector2Parameter(Vector2 value) { Kind = ParticleValueKind.Constant; Constant = value; RandomMin = default; RandomMax = default; } /// /// Initializes a new value with a random range. /// /// The minimum value of the random range. /// The maximum value of the random range. public ParticleVector2Parameter(Vector2 rangeStart, Vector2 rangeEnd) { Kind = ParticleValueKind.Random; Constant = default; RandomMin = rangeStart; RandomMax = rangeEnd; } /// /// Determines whether the specified object is equal to the current parameter. /// /// The object to compare with the current parameter. /// /// if the specified object is equal tot he current parameter; /// otherwise, . /// public override readonly bool Equals([NotNullWhen(true)] object obj) { return obj is ParticleVector2Parameter other && Equals(other); } /// /// Determines whether the specified parameter is equal to the current parameter. /// /// The parameter to compare with the current parameter. /// /// if the specified parameter is equal to the current parameter; /// otherwise, . /// /// /// When is only the values are /// compared. /// When is , both and /// values are compared. /// public readonly bool Equals(ParticleVector2Parameter other) { if (Kind == ParticleValueKind.Constant) { return Constant.Equals(other.Constant); } return RandomMin.Equals(other.RandomMin) && RandomMax.Equals(other.RandomMax); } /// /// Returns the hash code for this parameter. /// /// A 32-bit signed integer that is the hash code for this instance. /// /// When is , returns the hash of . /// When is returns the combined has of /// and . /// public override readonly int GetHashCode() { base.GetHashCode(); if (Kind == ParticleValueKind.Constant) { return Constant.GetHashCode(); } return HashCode.Combine(RandomMin, RandomMax); } /// /// Returns a string representation of this parameter. /// /// /// When is , returns the string representation of /// . /// When is , returns a string in the format /// "MinValue, MaxValue". /// public override readonly string ToString() { if (Kind == ParticleValueKind.Constant) { return Constant.ToString(); } return string.Format(NumberFormatInfo.InvariantInfo, "{0}{1}{2}", RandomMin, NumberFormatInfo.InvariantInfo.NumberGroupSeparator, RandomMax); } /// /// Determines whether two parameters are equal. /// /// The first parameter to compare. /// The second parameter to compare. /// /// if the parameters are equal; otherwise, . /// public static bool operator ==(ParticleVector2Parameter lhs, ParticleVector2Parameter rhs) { return lhs.Equals(rhs); } /// /// Determines whether two parameters are not equal. /// /// The first parameter to compare. /// The second parameter to compare. /// /// if the parameters are not equal; otherwise, . /// public static bool operator !=(ParticleVector2Parameter lhs, ParticleVector2Parameter rhs) { return !lhs.Equals(rhs); } }