ItemUtil.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  16. if (itemId == ConstItemID.GOLD)
  17. {
  18. AddGold(num, onSuccess);
  19. }
  20. else if (itemId == ConstItemID.POWER)
  21. {
  22. AddPower();
  23. }
  24. else
  25. {
  26. BuyItemConteoller.Show(itemId, currencyRatioCfg.costId, currencyRatioCfg.num, currencyRatioCfg.costNum, num, onSuccess, showTips, openSource, maxCount);
  27. BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
  28. }
  29. }
  30. public static ItemExchangeCfg GetCurrencyRatioCfgById(int itemId)
  31. {
  32. ItemExchangeCfg[] currencyRatioCfgs = ItemExchangeCfgArray.Instance.GetCfgs(itemId);
  33. if (currencyRatioCfgs.Length == 0)
  34. {
  35. UnityEngine.Debug.LogWarning(itemId + "在h货币换算.xlsx中没有配置");
  36. return null;
  37. }
  38. ItemExchangeCfg currencyRatioCfg;
  39. if (itemId == ConstItemID.POWER)
  40. {
  41. int powerBuyTimes = GameGlobal.myNumericComponent.GetAsInt(NumericType.PowerBuyTimes);
  42. //体力购买次数不同,对应消耗配置不同
  43. if (powerBuyTimes < currencyRatioCfgs.Length)
  44. {
  45. currencyRatioCfg = currencyRatioCfgs[powerBuyTimes];
  46. }
  47. else
  48. {
  49. currencyRatioCfg = currencyRatioCfgs[currencyRatioCfgs.Length - 1];
  50. }
  51. }
  52. else
  53. {
  54. currencyRatioCfg = currencyRatioCfgs[currencyRatioCfgs.Length - 1];
  55. }
  56. return currencyRatioCfg;
  57. }
  58. public static void AddPower(string prefix = "", Action onSuccess = null)
  59. {
  60. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.POWER);
  61. int count = GetCostItemCount(ConstItemID.POWER, currencyRatioCfg.num);
  62. int powerBuyTimes = GameGlobal.myNumericComponent.GetAsInt(NumericType.PowerBuyTimes);
  63. int lastBuyCount = currencyRatioCfg.maxLimit - powerBuyTimes;
  64. string showTxt = string.Format("每5分钟回复1点体力\n今日剩余购买{0}/{1}次", lastBuyCount, currencyRatioCfg.maxLimit);
  65. BuyConfirmController.Show(ConstItemID.POWER, currencyRatioCfg.num, currencyRatioCfg.costId, currencyRatioCfg.costNum, () =>
  66. {
  67. NumericHelper.RequestAddAttributePoint(GameGlobal.zoneScene , NumericType.PowerBuyTimes).Coroutine();
  68. if (onSuccess != null)
  69. {
  70. onSuccess();
  71. }
  72. }, lastBuyCount, powerBuyTimes, showTxt);
  73. }
  74. public static void AddGold(int value = 0, Action onSuccess = null)
  75. {
  76. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.GOLD);
  77. int count = value > 0 ? value : currencyRatioCfg.num;
  78. int costCount = GetCostItemCount(ConstItemID.GOLD, count);
  79. int lastBuyCount = currencyRatioCfg.maxLimit - GameGlobal.myNumericComponent.GetAsInt(NumericType.GoldBuyTimes);
  80. BuyConfirmController.Show(ConstItemID.GOLD, count, currencyRatioCfg.costId, costCount, () =>
  81. {
  82. NumericHelper.RequestAddAttributePoint(GameGlobal.zoneScene, NumericType.GoldBuyTimes).Coroutine();
  83. if (onSuccess != null)
  84. {
  85. onSuccess();
  86. }
  87. }, lastBuyCount, currencyRatioCfg.maxLimit);
  88. }
  89. public static void AddDiamondPurple()
  90. {
  91. ViewManager.Show(ViewName.RECHARGE_STORE_VIEW);
  92. }
  93. //红钻(鲛绡)
  94. public static void AddDiamondRed(int value = 0, Action onSuccess = null)
  95. {
  96. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  97. BuyItemConteoller.Show(currencyRatioCfg.id, currencyRatioCfg.costId, currencyRatioCfg.num, currencyRatioCfg.costNum, value > 0 ? value : currencyRatioCfg.num, onSuccess, true, true, GameConst.MAX_COUNT_TO_BUY_DIAMOND_RED);
  98. BuyItemConteoller.showTxtBuyTips = true;
  99. }
  100. /// <summary>
  101. /// 根据物品id和需求量获取兑换消耗品的消耗量
  102. /// </summary>
  103. public static int GetCostItemCount(int itemId, int count)
  104. {
  105. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  106. if (currencyRatioCfg != null)
  107. {
  108. return (int)Math.Ceiling((decimal)count / currencyRatioCfg.num * currencyRatioCfg.costNum);
  109. }
  110. else
  111. {
  112. return 0;
  113. }
  114. }
  115. /// <summary>
  116. /// 根据物品id和兑换消耗品的消耗量获取物品兑换量
  117. /// </summary>
  118. public static int GetItemExChangeCount(int itemId, int costCount)
  119. {
  120. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  121. if (currencyRatioCfg != null)
  122. {
  123. return (int)Math.Floor((decimal)costCount / currencyRatioCfg.costNum * currencyRatioCfg.num);
  124. }
  125. else
  126. {
  127. return 0;
  128. }
  129. }
  130. /// <summary>
  131. /// 添加物品,会消耗物品
  132. /// </summary>
  133. /// <param name="addId"></param>
  134. /// <param name="addNum"></param>
  135. /// <param name="costId"></param>
  136. /// <param name="costNum"></param>
  137. public static void AddItemUseCost(int addId, int addNum, int costId, int costNum)
  138. {
  139. ItemDataManager.Add(addId, addNum);
  140. ItemDataManager.Remove(costId, costNum);
  141. GetSuitItemController.TryShow(0);
  142. }
  143. public static string GetItemName(int itemId)
  144. {
  145. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  146. return itemCfg.name;
  147. }
  148. public static string GetSuitName(int suitId)
  149. {
  150. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  151. return suitCfg.name;
  152. }
  153. public static int GetItemRarity(int itemId)
  154. {
  155. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  156. return itemCfg.rarity;
  157. }
  158. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  159. {
  160. List<ItemData> itemList = new List<ItemData>();
  161. foreach (int[] item in bonus)
  162. {
  163. ItemData itemData = createItemData(item);
  164. itemList.Add(itemData);
  165. itemData.isOnceBonus = isOnceBonus;
  166. }
  167. return itemList;
  168. }
  169. public static ItemData createItemData(int[] bonus)
  170. {
  171. int itemID = bonus[0];
  172. int itemNum = 1;
  173. if (bonus.Length > 1)
  174. {
  175. itemNum = bonus[1];
  176. }
  177. return createItemData(itemID, itemNum);
  178. }
  179. public static ItemData createItemData(int itemId, int count)
  180. {
  181. ItemData itemData = ItemDataPool.GetItemData(itemId);
  182. itemData.num = count;
  183. return itemData;
  184. }
  185. public static Boolean CheckItemEnough(int itemId, int num)
  186. {
  187. int hasNum = ItemDataManager.GetItemNum(itemId);
  188. return hasNum >= num;
  189. }
  190. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  191. {
  192. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  193. if (needItemId > 0)
  194. {
  195. int subType = ItemUtilCS.GetItemSubType(needItemId);
  196. if (subType == cfg1.type)
  197. {
  198. return true;
  199. }
  200. else if (cfg1.subMenusArr.Length > 0)
  201. {
  202. foreach (int id2 in cfg1.subMenusArr)
  203. {
  204. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  205. if (cfg2.type == subType)
  206. {
  207. return true;
  208. }
  209. }
  210. }
  211. }
  212. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  213. {
  214. return true;
  215. }
  216. return false;
  217. }
  218. public static void SortItemIdsByOwned(int[] array)
  219. {
  220. Array.Sort(array, (int a, int b) =>
  221. {
  222. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  223. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  224. if (!ownedB && ownedA)
  225. {
  226. return 1;
  227. }
  228. else if (ownedB && !ownedA)
  229. {
  230. return -1;
  231. }
  232. return 0;
  233. });
  234. }
  235. public static string GetItemResExt(int itemType, int type)
  236. {
  237. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING)
  238. {
  239. return "jpg";
  240. }
  241. return "png";
  242. }
  243. }
  244. }