using System; using System.Collections.Generic; using System.RandomPool; using FJson; using Framework.Save; // using Framework.Save; using Game.Data; using UnityEngine; namespace Game { /// /// 稀有度随机 /// public class LevelRandom { private LiteDictionary _levelDic; private Dictionary _weightDic; private int _weightSum; public LevelRandom (Dictionary levelDic, IList commonRandomGroups) { this._weightDic = new Dictionary(); this._levelDic = new LiteDictionary(); foreach (var randomGroup in commonRandomGroups) { if (levelDic.TryGetValue (randomGroup , out var value)) { this._levelDic[randomGroup] = value; } } this.RefreshWeight(); } /// /// 刷新总权重 /// 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; } } /// /// 设置指定等级的中奖比重 /// /// /// 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 maskLevel) { var level = 0; if (maskLevel == null || maskLevel.Count == 0) return this.RandomItemLevel(seedId); Dictionary tempWeightList = new Dictionary(); 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; } } }