DropOutDataCache.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class DropOutDataCache
  6. {
  7. private static Dictionary<int, List<DropOutData>> _probDic = new Dictionary<int, List<DropOutData>>();
  8. public static List<ItemData> GetDropItemDatas(string dropIdsStr, bool doRandome)
  9. {
  10. List<ItemData> result = new List<ItemData>();
  11. string[] dropIdStrArr = dropIdsStr.Split(';');
  12. foreach(string value in dropIdStrArr)
  13. {
  14. int dropId = int.Parse(value);
  15. ItemData itemData = GetDropItemData(dropId, doRandome);
  16. if(itemData != null)
  17. {
  18. result.Add(itemData);
  19. }
  20. }
  21. return result;
  22. }
  23. public static List<ItemData> GetDropItemDatas(int[] dropIds, bool doRandome)
  24. {
  25. List<ItemData> result = new List<ItemData>();
  26. foreach (int dropId in dropIds)
  27. {
  28. ItemData itemData = GetDropItemData(dropId, doRandome);
  29. if (itemData != null)
  30. {
  31. result.Add(itemData);
  32. }
  33. }
  34. return result;
  35. }
  36. public static List<ItemData> GetDropItemDatas(int dropId, int count)
  37. {
  38. List<ItemData> result = new List<ItemData>();
  39. int tryCount = 0;
  40. while(result.Count < count && tryCount < 1000)
  41. {
  42. ItemData itemData = GetDropItemData(dropId, true);
  43. if(itemData != null)
  44. {
  45. result.Add(itemData);
  46. }
  47. tryCount++;
  48. }
  49. return result;
  50. }
  51. public static ItemData GetDropItemData(int dropId, bool doRandome)
  52. {
  53. ItemData itemData = null;
  54. List<DropOutData> dropOutDatas = null;
  55. if(!_probDic.ContainsKey(dropId))
  56. {
  57. InitData(dropId);
  58. }
  59. dropOutDatas = _probDic[dropId];
  60. if(dropOutDatas != null)
  61. {
  62. float result = Random.Range(0, 1f);
  63. foreach(DropOutData dropOutData in dropOutDatas)
  64. {
  65. if(result <= dropOutData.WeightEnd || !doRandome)
  66. {
  67. return GetItemData(dropOutData.dropOutCfg, doRandome);
  68. }
  69. }
  70. }
  71. return itemData;
  72. }
  73. private static void InitData(int dropId)
  74. {
  75. List<DropOutData> dropOutDatas = new List<DropOutData>();
  76. _probDic[dropId] = dropOutDatas;
  77. var arr = DropOutCfgArray.Instance.GetCfgs(dropId);
  78. List<DropOutCfg> cfgs = new List<DropOutCfg>(arr);
  79. float totalWeight = 0;
  80. foreach(DropOutCfg cfg in cfgs)
  81. {
  82. totalWeight += cfg.weight;
  83. }
  84. float weightEnd = 0;
  85. foreach(DropOutCfg cfg in cfgs)
  86. {
  87. DropOutData dropOutData = new DropOutData();
  88. dropOutData.dropOutCfg = cfg;
  89. weightEnd += cfg.weight;
  90. dropOutData.WeightEnd = weightEnd/totalWeight;
  91. dropOutDatas.Add(dropOutData);
  92. }
  93. }
  94. private static ItemData GetItemData(DropOutCfg dropOutCfg, bool doRandomeNum)
  95. {
  96. if(dropOutCfg.item >= 10000000)//掉落id
  97. {
  98. return GetDropItemData(dropOutCfg.item, doRandomeNum);
  99. }
  100. else if(dropOutCfg.item > 0)
  101. {
  102. int num = dropOutCfg.maxNum;
  103. if(doRandomeNum)
  104. {
  105. num = Random.Range(dropOutCfg.minNum, dropOutCfg.maxNum + 1);
  106. }
  107. if(num > 0)
  108. {
  109. ItemData itemData = ItemDataPool.GetItemData(dropOutCfg.item);
  110. itemData.num = num;
  111. return itemData;
  112. }
  113. }
  114. return null;
  115. }
  116. }
  117. }