ItemUtil.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ET;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace GFGGame
  7. {
  8. public class ItemUtil
  9. {
  10. /// <param name="showTips">是否弹购买成功飘字,默认是</param>
  11. /// <param name="openSource">是否打开来源界面。默认否</param>
  12. /// <param name="showTxtBuyTips">是否显示购买提示</param>
  13. public static void ExchangeItemById(int itemId, int num, bool showTips = true, Action onSuccess = null, bool openSource = false, int maxCount = 9990, bool showTxtBuyTips = false, string prefix = "")
  14. {
  15. if (itemId == ConstItemID.GOLD)
  16. {
  17. AddGold(num, onSuccess);
  18. }
  19. else if (itemId == ConstItemID.POWER)
  20. {
  21. AddPower();
  22. }
  23. else
  24. {
  25. BuyItemConteoller.Show(itemId, num, ConstBuyType.TYPE_ITEM, 0, onSuccess, showTips, openSource, maxCount);
  26. BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
  27. }
  28. }
  29. public static void AddPower(string prefix = "", Action onSuccess = null)
  30. {
  31. int maxLimit = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.POWER).maxLimit;
  32. int lastBuyCount = maxLimit - ItemDataManager.GetItemExchangeTimes(ConstItemID.POWER);
  33. string showTxt = string.Format("每5分钟回复1点体力\n今日剩余购买{0}/{1}次", lastBuyCount, maxLimit);
  34. BuyConfirmController.Show(ConstItemID.POWER, 1, () =>
  35. {
  36. if (onSuccess != null)
  37. {
  38. onSuccess();
  39. }
  40. }, showTxt);
  41. }
  42. public static void AddGold(int value = 0, Action onSuccess = null)
  43. {
  44. BuyConfirmController.Show(ConstItemID.GOLD, 1, () =>
  45. {
  46. if (onSuccess != null)
  47. {
  48. onSuccess();
  49. }
  50. });
  51. }
  52. public static void AddDiamondPurple()
  53. {
  54. ViewManager.Show(ViewName.RECHARGE_STORE_VIEW);
  55. }
  56. //红钻(鲛绡)
  57. public static void AddDiamondRed(int value = 0, Action onSuccess = null)
  58. {
  59. ItemExchangeCfg currencyRatioCfg = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.DIAMOND_RED);// GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  60. BuyItemConteoller.Show(currencyRatioCfg.id, value > 0 ? value : 1, ConstBuyType.TYPE_ITEM, 0, onSuccess, true, true);
  61. BuyItemConteoller.showTxtBuyTips = true;
  62. }
  63. /// <summary>
  64. /// 添加物品,会消耗物品
  65. /// </summary>
  66. /// <param name="addId"></param>
  67. /// <param name="addNum"></param>
  68. /// <param name="costId"></param>
  69. /// <param name="costNum"></param>
  70. public static void AddItemUseCost(int addId, int addNum, int costId, int costNum)
  71. {
  72. ItemDataManager.Add(addId, addNum);
  73. ItemDataManager.Remove(costId, costNum);
  74. GetSuitItemController.TryShow(0);
  75. }
  76. public static string GetItemName(int itemId)
  77. {
  78. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  79. return itemCfg.name;
  80. }
  81. public static string GetSuitName(int suitId)
  82. {
  83. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  84. return suitCfg.name;
  85. }
  86. public static int GetItemRarity(int itemId)
  87. {
  88. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  89. return itemCfg.rarity;
  90. }
  91. public static List<ItemData> CreateItemDataList(List<ItemInfoProto> items)
  92. {
  93. List<ItemData> itemList = new List<ItemData>();
  94. for (int i = 0; i < items.Count; i++)
  95. {
  96. ItemData itemData = createItemData(items[i].ConfigId, items[i].Count);
  97. itemList.Add(itemData);
  98. }
  99. return itemList;
  100. }
  101. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  102. {
  103. List<ItemData> itemList = new List<ItemData>();
  104. foreach (int[] item in bonus)
  105. {
  106. ItemData itemData = createItemData(item);
  107. itemList.Add(itemData);
  108. itemData.isOnceBonus = isOnceBonus;
  109. }
  110. return itemList;
  111. }
  112. public static ItemData createItemData(int[] bonus)
  113. {
  114. int itemID = bonus[0];
  115. int itemNum = 1;
  116. if (bonus.Length > 1)
  117. {
  118. itemNum = bonus[1];
  119. }
  120. return createItemData(itemID, itemNum);
  121. }
  122. public static ItemData createItemData(int itemId, int count)
  123. {
  124. ItemData itemData = ItemDataPool.GetItemData(itemId);
  125. itemData.num = count;
  126. return itemData;
  127. }
  128. public static Boolean CheckItemEnough(int itemId, int num)
  129. {
  130. int hasNum = ItemDataManager.GetItemNum(itemId);
  131. return hasNum >= num;
  132. }
  133. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  134. {
  135. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  136. if (needItemId > 0)
  137. {
  138. int subType = ItemUtilCS.GetItemSubType(needItemId);
  139. if (subType == cfg1.type)
  140. {
  141. return true;
  142. }
  143. else if (cfg1.subMenusArr.Length > 0)
  144. {
  145. foreach (int id2 in cfg1.subMenusArr)
  146. {
  147. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  148. if (cfg2.type == subType)
  149. {
  150. return true;
  151. }
  152. }
  153. }
  154. }
  155. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  156. {
  157. return true;
  158. }
  159. return false;
  160. }
  161. public static void SortItemIdsByOwned(int[] array)
  162. {
  163. Array.Sort(array, (int a, int b) =>
  164. {
  165. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  166. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  167. if (!ownedB && ownedA)
  168. {
  169. return 1;
  170. }
  171. else if (ownedB && !ownedA)
  172. {
  173. return -1;
  174. }
  175. return 0;
  176. });
  177. }
  178. public static string GetItemResExt(int itemType, int type)
  179. {
  180. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING)
  181. {
  182. return "jpg";
  183. }
  184. return "png";
  185. }
  186. }
  187. }