// 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 int values from a given [min,max] interval
///
[Serializable]
public class RandomInt
{
/// Minimum value for the interval
[SerializeField] private int MIN;
public int min
{
get => MIN;
set => MIN = value;
}
/// Maximum value for the interval
[SerializeField] private int MAX;
public int max
{
get => MAX;
set => MAX = value;
}
///
/// Current random value from the [MIN,MAX] interval
/// Value updated every time 'randomValue' is used
///
public int currentValue { get; private set; }
///
/// Previous random value
/// Used to make sure no two consecutive random values are used
///
public int previousValue { get; private set; }
///
/// Random number between MIN [inclusive] and MAX [inclusive] (Read Only)
/// Updates both the currentValue and the previousValue
///
public int randomValue
{
get
{
previousValue = currentValue;
currentValue = random;
int counter = 100; //fail-safe counter to avoid infinite loops (if min = max)
while (currentValue == previousValue && counter > 0)
{
currentValue = random;
counter--;
}
return currentValue;
}
}
/// Random value from the [MIN,MAX] interval
private int random => Random.Range(MIN, MAX + 1);
/// Construct a new RandomInt using the [min,max] interval values from the other RandomInt
/// Other RandomInt
public RandomInt(RandomInt other) : this(other.min, other.max) {}
/// Construct a new RandomInt with the default [min, max] interval of [0,1]
public RandomInt() : this(0, 1) {}
/// Construct a new RandomInt with the given min and max interval values
/// Min value
/// Max value
public RandomInt(int minValue, int maxValue) =>
Reset(minValue, maxValue);
/// Reset the interval to the given min and max values
/// Min value
/// Max value
public void Reset(int minValue = 0, int maxValue = 1)
{
MIN = minValue;
MAX = maxValue;
previousValue = currentValue = minValue;
// previousValue = random; //set a random previous value
// currentValue = randomValue; //init a current random value
}
}
}