DropOutDataCache.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(int[] dropIds, bool doRandome)
  9. {
  10. List<ItemData> result = new List<ItemData>();
  11. foreach (int dropId in dropIds)
  12. {
  13. ItemData itemData = GetDropItemData(dropId, doRandome);
  14. if (itemData != null)
  15. {
  16. result.Add(itemData);
  17. }
  18. }
  19. return result;
  20. }
  21. public static List<ItemData> GetDropItemDatas(int dropId, int count)
  22. {
  23. List<ItemData> result = new List<ItemData>();
  24. int tryCount = 0;
  25. while (result.Count < count && tryCount < 1000)
  26. {
  27. ItemData itemData = GetDropItemData(dropId, true);
  28. if (itemData != null)
  29. {
  30. result.Add(itemData);
  31. }
  32. tryCount++;
  33. }
  34. return result;
  35. }
  36. public static ItemData GetDropItemData(int dropId, bool doRandome)
  37. {
  38. ItemData itemData = null;
  39. List<DropOutData> dropOutDatas = null;
  40. if (!_probDic.ContainsKey(dropId))
  41. {
  42. InitData(dropId);
  43. }
  44. dropOutDatas = _probDic[dropId];
  45. if (dropOutDatas != null)
  46. {
  47. float result = Random.Range(0, 1f);
  48. foreach (DropOutData dropOutData in dropOutDatas)
  49. {
  50. if (result <= dropOutData.WeightEnd || !doRandome)
  51. {
  52. return GetItemData(dropOutData.dropOutCfg, doRandome);
  53. }
  54. }
  55. }
  56. return itemData;
  57. }
  58. private static void InitData(int dropId)
  59. {
  60. List<DropOutData> dropOutDatas = new List<DropOutData>();
  61. _probDic[dropId] = dropOutDatas;
  62. var arr = DropOutCfgArray.Instance.GetCfgs(dropId);
  63. List<DropOutCfg> cfgs = new List<DropOutCfg>(arr);
  64. float totalWeight = 0;
  65. foreach (DropOutCfg cfg in cfgs)
  66. {
  67. totalWeight += cfg.weight;
  68. }
  69. float weightEnd = 0;
  70. foreach (DropOutCfg cfg in cfgs)
  71. {
  72. DropOutData dropOutData = new DropOutData();
  73. dropOutData.dropOutCfg = cfg;
  74. weightEnd += cfg.weight;
  75. dropOutData.WeightEnd = weightEnd / totalWeight;
  76. dropOutDatas.Add(dropOutData);
  77. }
  78. }
  79. private static ItemData GetItemData(DropOutCfg dropOutCfg, bool doRandomeNum)
  80. {
  81. if (dropOutCfg.item >= 10000000)//掉落id
  82. {
  83. return GetDropItemData(dropOutCfg.item, doRandomeNum);
  84. }
  85. else if (dropOutCfg.item > 0)
  86. {
  87. int num = dropOutCfg.maxNum;
  88. if (doRandomeNum)
  89. {
  90. num = Random.Range(dropOutCfg.minNum, dropOutCfg.maxNum + 1);
  91. }
  92. if (num > 0)
  93. {
  94. ItemData itemData = ItemDataPool.GetItemData(dropOutCfg.item);
  95. itemData.num = num;
  96. return itemData;
  97. }
  98. }
  99. return null;
  100. }
  101. public static List<ItemData> GetDropItemDatas1(int[] dropIds, bool doRandome)
  102. {
  103. List<ItemData> result = new List<ItemData>();
  104. foreach (int dropId in dropIds)
  105. {
  106. GetDropItemDatas(dropId, result);
  107. }
  108. return result;
  109. }
  110. public static List<ItemData> GetDropItemDatas(int dropId, List<ItemData> result)
  111. {
  112. List<DropOutCfg> dropOutCfgs = DropOutCfgArray.Instance.GetCfgs(dropId);
  113. for (int i = 0; i < dropOutCfgs.Count; i++)
  114. {
  115. List<DropOutCfg> _dropOutCfgs = DropOutCfgArray.Instance.GetCfgs(dropOutCfgs[i].item);
  116. if (_dropOutCfgs.Count > 0)
  117. {
  118. GetDropItemDatas(dropOutCfgs[i].item, result);
  119. }
  120. else
  121. {
  122. ItemData itemData = ItemUtil.createItemData(dropOutCfgs[i].item, dropOutCfgs[i].maxNum);// GetDropItemData(dropId, doRandome);
  123. if (dropOutCfgs[i].item > 0 && itemData != null)
  124. {
  125. result.Add(itemData);
  126. }
  127. }
  128. }
  129. return result;
  130. }
  131. }
  132. }