// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Doozy.Runtime.Common
{
///
/// Data class used to get random float values from a given [min,max] interval
///
[Serializable]
public class RandomFloat
{
/// Minimum value for the interval
[SerializeField] private float MIN;
public float min
{
get => MIN;
set => MIN = value;
}
/// Maximum value for the interval
[SerializeField] private float MAX;
public float max
{
get => MAX;
set => MAX = value;
}
///
/// Current random value from the [MIN,MAX] interval
/// Value updated every time 'randomValue' is used
///
public float currentValue { get; private set; }
///
/// Previous random value
/// Used to make sure no two consecutive random values are used
///
public float previousValue { get; private set; }
///
/// Random number between MIN [inclusive] and MAX [inclusive] (Read Only)
/// Updates both the currentValue and the previousValue
///
public float randomValue
{
get
{
previousValue = currentValue;
currentValue = random;
int counter = 100; //fail-safe counter to avoid infinite loops (if min = max)
while (Mathf.Approximately(currentValue, previousValue) && counter > 0)
{
currentValue = random;
counter--;
}
return currentValue;
}
}
/// Random value from the [MIN,MAX] interval
private float random => Random.Range(MIN, MAX);
/// Construct a new RandomFloat using the [min,max] interval values from the other RandomFloat
/// Other RandomFloat
public RandomFloat(RandomFloat other) : this(other.min, other.max) {}
/// Construct a new RandomFloat with the default [min, max] interval of [0,1]
public RandomFloat() : this(0, 1) {}
/// Construct a new RandomFloat with the given min and max interval values
/// Min value
/// Max value
public RandomFloat(float minValue, float maxValue) =>
Reset(minValue, maxValue);
/// Reset the interval to the given min and max values
/// Min value
/// Max value
public void Reset(float minValue = 0, float maxValue = 1)
{
MIN = minValue;
MAX = maxValue;
previousValue = currentValue = minValue;
// previousValue = random; //set a random previous value
// currentValue = randomValue; //init a current random value
}
}
}