Files
taptap2024_GJ_chidouren/Assets/Scripts/System/RandomPool/LevelRandom.cs
2024-10-16 00:03:41 +08:00

111 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.RandomPool;
using FJson;
using Framework.Save;
// using Framework.Save;
using Game.Data;
using UnityEngine;
namespace Game
{
/// <summary>
/// 稀有度随机
/// </summary>
public class LevelRandom
{
private LiteDictionary<int, int> _levelDic;
private Dictionary<int, int> _weightDic;
private int _weightSum;
public LevelRandom (Dictionary<int, int> levelDic, IList<int> commonRandomGroups)
{
this._weightDic = new Dictionary<int, int>();
this._levelDic = new LiteDictionary<int, int>();
foreach (var randomGroup in commonRandomGroups)
{
if (levelDic.TryGetValue (randomGroup , out var value))
{
this._levelDic[randomGroup] = value;
}
}
this.RefreshWeight();
}
/// <summary>
/// 刷新总权重
/// </summary>
private void RefreshWeight()
{
this._weightDic.Clear();
this._weightSum = 0;
foreach (var dicKey in this._levelDic.Keys)
{
this._weightSum += this._levelDic[dicKey];
this._weightDic[dicKey] = this._weightSum;
}
}
/// <summary>
/// 设置指定等级的中奖比重
/// </summary>
/// <param name="quality"></param>
/// <param name="weight"></param>
public void SetWeight(int quality, int weight)
{
this._levelDic[quality] = weight;
this.RefreshWeight();
}
public int RandomItemLevel(int seedId)
{
var d = RandomUtils.RandomFloat ();
var level = 0;
foreach (var dicKey in this._weightDic.Keys)
{
var value = this._weightDic[dicKey];
if (d <= value * 1f / this._weightSum)
{
level = dicKey;
break;
}
}
return level;
}
public int RandomItemLevel(int seedId , List<int> maskLevel)
{
var level = 0;
if (maskLevel == null || maskLevel.Count == 0)
return this.RandomItemLevel(seedId);
Dictionary<int, int> tempWeightList = new Dictionary<int, int>();
int tempWeightSum = 0;
foreach (var itemLevel in this._levelDic.Keys)
{
if (maskLevel.Contains(itemLevel))
continue;
tempWeightSum += this._levelDic[itemLevel];
tempWeightList[itemLevel] = tempWeightSum;
}
if (tempWeightSum != 0)
{
var d = RandomUtils.RandomFloat () * tempWeightSum;
foreach (var dicKey in tempWeightList.Keys)
{
var value = tempWeightList[dicKey];
if (d <= value)
{
level = dicKey;
break;
}
}
}
return level;
}
}
}