// 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;
namespace MonoGame.Extended.Particles.Data;
///
/// Represents an integer parameter for particle properties that can be either a constant value or a randomly generated
/// value within a specified range.
///
public struct ParticleInt32Parameter : 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 int Constant;
///
/// The minimum value of the range when is .
///
public int RandomMin;
///
/// The maximum value of the range when is .
///
public int RandomMax;
///
/// Gets the current value of this parameter based on its
///
///
/// If is , returns .
/// If is , returns a random value between
/// and .
///
public int Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (Kind == ParticleValueKind.Constant)
{
return Constant;
}
return FastRandom.Shared.Next(RandomMin, RandomMax);
}
}
///
/// Initializes a new value with a constant value.
///
/// The constant value for this parameter.
public ParticleInt32Parameter(int 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 ParticleInt32Parameter(int rangeStart, int 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 ParticleInt32Parameter 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(ParticleInt32Parameter 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(CultureInfo.InvariantCulture);
}
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 ==(ParticleInt32Parameter lhs, ParticleInt32Parameter 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 !=(ParticleInt32Parameter lhs, ParticleInt32Parameter rhs)
{
return !lhs.Equals(rhs);
}
}