123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace GFGGame
- {
- public class DropOutDataCache
- {
- private static Dictionary<int, List<DropOutData>> _probDic = new Dictionary<int, List<DropOutData>>();
- public static List<ItemData> GetDropItemDatas(int[] dropIds, bool doRandome)
- {
- List<ItemData> result = new List<ItemData>();
- foreach (int dropId in dropIds)
- {
- ItemData itemData = GetDropItemData(dropId, doRandome);
- if (itemData != null)
- {
- result.Add(itemData);
- }
- }
- return result;
- }
- public static List<ItemData> GetDropItemDatas(int dropId, int count)
- {
- List<ItemData> result = new List<ItemData>();
- int tryCount = 0;
- while (result.Count < count && tryCount < 1000)
- {
- ItemData itemData = GetDropItemData(dropId, true);
- if (itemData != null)
- {
- result.Add(itemData);
- }
- tryCount++;
- }
- return result;
- }
- public static ItemData GetDropItemData(int dropId, bool doRandome)
- {
- ItemData itemData = null;
- List<DropOutData> dropOutDatas = null;
- if (!_probDic.ContainsKey(dropId))
- {
- InitData(dropId);
- }
- dropOutDatas = _probDic[dropId];
- if (dropOutDatas != null)
- {
- float result = Random.Range(0, 1f);
- foreach (DropOutData dropOutData in dropOutDatas)
- {
- if (result <= dropOutData.WeightEnd || !doRandome)
- {
- return GetItemData(dropOutData.dropOutCfg, doRandome);
- }
- }
- }
- return itemData;
- }
- private static void InitData(int dropId)
- {
- List<DropOutData> dropOutDatas = new List<DropOutData>();
- _probDic[dropId] = dropOutDatas;
- var arr = DropOutCfgArray.Instance.GetCfgs(dropId);
- List<DropOutCfg> cfgs = new List<DropOutCfg>(arr);
- float totalWeight = 0;
- foreach (DropOutCfg cfg in cfgs)
- {
- totalWeight += cfg.weight;
- }
- float weightEnd = 0;
- foreach (DropOutCfg cfg in cfgs)
- {
- DropOutData dropOutData = new DropOutData();
- dropOutData.dropOutCfg = cfg;
- weightEnd += cfg.weight;
- dropOutData.WeightEnd = weightEnd / totalWeight;
- dropOutDatas.Add(dropOutData);
- }
- }
- private static ItemData GetItemData(DropOutCfg dropOutCfg, bool doRandomeNum)
- {
- if (dropOutCfg.item >= 10000000)//掉落id
- {
- return GetDropItemData(dropOutCfg.item, doRandomeNum);
- }
- else if (dropOutCfg.item > 0)
- {
- int num = dropOutCfg.maxNum;
- if (doRandomeNum)
- {
- num = Random.Range(dropOutCfg.minNum, dropOutCfg.maxNum + 1);
- }
- if (num > 0)
- {
- ItemData itemData = ItemDataPool.GetItemData(dropOutCfg.item);
- itemData.num = num;
- return itemData;
- }
- }
- return null;
- }
- public static List<ItemData> GetDropItemDatas1(int[] dropIds, bool doRandome)
- {
- List<ItemData> result = new List<ItemData>();
- foreach (int dropId in dropIds)
- {
- GetDropItemDatas(dropId, result);
- }
- return result;
- }
- public static List<ItemData> GetDropItemDatas(int dropId, List<ItemData> result)
- {
- List<DropOutCfg> dropOutCfgs = DropOutCfgArray.Instance.GetCfgs(dropId);
- for (int i = 0; i < dropOutCfgs.Count; i++)
- {
- List<DropOutCfg> _dropOutCfgs = DropOutCfgArray.Instance.GetCfgs(dropOutCfgs[i].item);
- if (_dropOutCfgs.Count > 0)
- {
- GetDropItemDatas(dropOutCfgs[i].item, result);
- }
- else
- {
- ItemData itemData = ItemUtil.createItemData(dropOutCfgs[i].item, dropOutCfgs[i].maxNum);// GetDropItemData(dropId, doRandome);
- if (dropOutCfgs[i].item > 0 && itemData != null)
- {
- result.Add(itemData);
- }
- }
- }
- return result;
- }
- }
- }
|