2025-09-27 22:33:47 +01:00

46 lines
1.4 KiB
C#

using UnityEngine;
public static class MathHelpers
{
public static bool RollChanceNormalized(float chance)
{
if (chance >= 1f) return true; // 100% always succeeds
if (chance <= 0f) return false; // 0% always fails
return UnityEngine.Random.value < chance;
}
public static bool RollChancePercent(float chancePercent)
{
if (chancePercent >= 100f) return true; // 100% always succeeds
if (chancePercent <= 0f) return false; // 0% always fails
return UnityEngine.Random.value < (chancePercent / 100f);
}
/// <summary>
/// 0.05 or 5 will always return from 0 to 100 ==> 5%
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static float NormalizePercentage(float value)
{
// If value is <= 1, assume it's already in decimal format (0.05 = 5%)
// If value is > 1, assume it's in percentage format (5 = 5%)
return value <= 1f ? value * 100f : value;
}
/// <summary>
/// 0.05 or 5 will always return from 0 to 1 ==> 0.05
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static float NormalizePercentageDecimal(float value)
{
return value <= 1f ? value : value / 100f;
}
public static float PercentAsDecimal(float value)
{
return value / 100f;
}
}