41 lines
1.5 KiB
C#
41 lines
1.5 KiB
C#
using SepCore.Definition;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.CustomUtility
|
|
{
|
|
public static class RarityUtility
|
|
{
|
|
public static Rarity SelectRarityForLevel(int level, float luck = 0f)
|
|
{
|
|
int configId = level >= 1000 ? level : 1000 + level;
|
|
LevelDropConfig dropConfig = GameEntry.Luban.Get<LevelDropConfig>(configId);
|
|
return SelectRarity(dropConfig, luck);
|
|
}
|
|
|
|
public static Rarity SelectRarity(LevelDropConfig dropConfig, float luck = 0f)
|
|
{
|
|
float whiteWeight = dropConfig.WhiteWeight;
|
|
float luckMultiplier = Mathf.Max(0f, 1f + luck * 0.015f);
|
|
float blueWeight = dropConfig.BlueWeight * luckMultiplier;
|
|
float purpleWeight = dropConfig.PurpleWeight * luckMultiplier;
|
|
float redWeight = dropConfig.RedWeight * luckMultiplier;
|
|
|
|
float total = whiteWeight + blueWeight + purpleWeight + redWeight;
|
|
if (total <= 0f)
|
|
{
|
|
Log.Error("LevelDropConfig '{0}' has invalid total weight '{1}'.", dropConfig.Id, total);
|
|
return Rarity.White;
|
|
}
|
|
|
|
float roll = Random.Range(0f, total);
|
|
if (roll < whiteWeight) return Rarity.White;
|
|
roll -= whiteWeight;
|
|
if (roll < blueWeight) return Rarity.Blue;
|
|
roll -= blueWeight;
|
|
if (roll < purpleWeight) return Rarity.Purple;
|
|
return Rarity.Red;
|
|
}
|
|
}
|
|
}
|