// 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.Runtime.CompilerServices;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Particles.Data;
///
/// Represents a color parameter for particle properties that can be either a constant color value
/// or a randomly generated color within a specified range.
///
///
/// This struct uses to represent color values, the HSL color space.
///
public struct ParticleColorParameter : IEquatable
{
///
/// The that determines whether this parameter uses a constant value or a randomly
/// generated value.
///
public ParticleValueKind Kind;
///
/// The constant color value when is set to .
///
public Vector3 Constant;
///
/// The minimum color values of the range when is set to .
///
public Vector3 RandomMin;
///
/// The maximum color values of the range when is set to .
///
public Vector3 RandomMax;
///
/// Gets the current color value of this parameter based on its .
///
///
/// If is , returns .
/// If is , returns a random color where each component
/// is a random value between the corresponding components of and .
/// The vector components represent HSL.
///
public Vector3 Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (Kind == ParticleValueKind.Constant)
{
return Constant;
}
else
{
Vector3 hsl;
hsl.X = FastRandom.Shared.NextSingle(RandomMin.X, RandomMax.X);
hsl.Y = FastRandom.Shared.NextSingle(RandomMin.Y, RandomMax.Y);
hsl.Z = FastRandom.Shared.NextSingle(RandomMin.Z, RandomMax.Z);
return hsl;
}
}
}
///
/// Initializes a new instance of the struct with a constant color value.
///
/// The constant color value for this parameter.
public ParticleColorParameter(Vector3 value)
{
Kind = ParticleValueKind.Constant;
Constant = value;
RandomMin = default;
RandomMax = default;
}
///
/// Initializes a new instance of the struct with a random color range.
///
/// The minimum color values of the random range.
/// The maximum color values of the random range.
public ParticleColorParameter(Vector3 rangeStart, Vector3 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 ParticleColorParameter 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(ParticleColorParameter 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()
{
if (Kind == ParticleValueKind.Constant)
{
return Constant.GetHashCode();
}
return HashCode.Combine(RandomMin, 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 ==(ParticleColorParameter lhs, ParticleColorParameter 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 !=(ParticleColorParameter lhs, ParticleColorParameter rhs)
{
return !lhs.Equals(rhs);
}
}