ItemUtil.cs 9.5 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, ConstBuyType.TYPE_ITEM, 0, 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, 1, () =>
  66. {
  67. if (onSuccess != null)
  68. {
  69. onSuccess();
  70. }
  71. }, showTxt);
  72. }
  73. public static void AddGold(int value = 0, Action onSuccess = null)
  74. {
  75. // ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.GOLD);
  76. // int count = value > 0 ? value : currencyRatioCfg.num;
  77. // int costCount = GetCostItemCount(ConstItemID.GOLD, count);
  78. // int goldBuyTimes = GameGlobal.myNumericComponent.GetAsInt(NumericType.GoldBuyTimes);
  79. // int lastBuyCount = currencyRatioCfg.maxLimit - GameGlobal.myNumericComponent.GetAsInt(NumericType.GoldBuyTimes);
  80. BuyConfirmController.Show(ConstItemID.GOLD, 1, () =>
  81. {
  82. if (onSuccess != null)
  83. {
  84. onSuccess();
  85. }
  86. });
  87. }
  88. public static void AddDiamondPurple()
  89. {
  90. ViewManager.Show(ViewName.RECHARGE_STORE_VIEW);
  91. }
  92. //红钻(鲛绡)
  93. public static void AddDiamondRed(int value = 0, Action onSuccess = null)
  94. {
  95. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  96. BuyItemConteoller.Show(currencyRatioCfg.id, currencyRatioCfg.costId, currencyRatioCfg.num, currencyRatioCfg.costNum, value > 0 ? value : currencyRatioCfg.num, ConstBuyType.TYPE_ITEM, 0, onSuccess, true, true, GameConst.MAX_COUNT_TO_BUY_DIAMOND_RED);
  97. BuyItemConteoller.showTxtBuyTips = true;
  98. }
  99. /// <summary>
  100. /// 根据物品id和需求量获取兑换消耗品的消耗量
  101. /// </summary>
  102. public static int GetCostItemCount(int itemId, int count)
  103. {
  104. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  105. if (currencyRatioCfg != null)
  106. {
  107. return (int)Math.Ceiling((decimal)count / currencyRatioCfg.num * currencyRatioCfg.costNum);
  108. }
  109. else
  110. {
  111. return 0;
  112. }
  113. }
  114. /// <summary>
  115. /// 根据物品id和兑换消耗品的消耗量获取物品兑换量
  116. /// </summary>
  117. public static int GetItemExChangeCount(int itemId, int costCount)
  118. {
  119. ItemExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  120. if (currencyRatioCfg != null)
  121. {
  122. return (int)Math.Floor((decimal)costCount / currencyRatioCfg.costNum * currencyRatioCfg.num);
  123. }
  124. else
  125. {
  126. return 0;
  127. }
  128. }
  129. /// <summary>
  130. /// 添加物品,会消耗物品
  131. /// </summary>
  132. /// <param name="addId"></param>
  133. /// <param name="addNum"></param>
  134. /// <param name="costId"></param>
  135. /// <param name="costNum"></param>
  136. public static void AddItemUseCost(int addId, int addNum, int costId, int costNum)
  137. {
  138. ItemDataManager.Add(addId, addNum);
  139. ItemDataManager.Remove(costId, costNum);
  140. GetSuitItemController.TryShow(0);
  141. }
  142. public static string GetItemName(int itemId)
  143. {
  144. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  145. return itemCfg.name;
  146. }
  147. public static string GetSuitName(int suitId)
  148. {
  149. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  150. return suitCfg.name;
  151. }
  152. public static int GetItemRarity(int itemId)
  153. {
  154. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  155. return itemCfg.rarity;
  156. }
  157. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  158. {
  159. List<ItemData> itemList = new List<ItemData>();
  160. foreach (int[] item in bonus)
  161. {
  162. ItemData itemData = createItemData(item);
  163. itemList.Add(itemData);
  164. itemData.isOnceBonus = isOnceBonus;
  165. }
  166. return itemList;
  167. }
  168. public static ItemData createItemData(int[] bonus)
  169. {
  170. int itemID = bonus[0];
  171. int itemNum = 1;
  172. if (bonus.Length > 1)
  173. {
  174. itemNum = bonus[1];
  175. }
  176. return createItemData(itemID, itemNum);
  177. }
  178. public static ItemData createItemData(int itemId, int count)
  179. {
  180. ItemData itemData = ItemDataPool.GetItemData(itemId);
  181. itemData.num = count;
  182. return itemData;
  183. }
  184. public static Boolean CheckItemEnough(int itemId, int num)
  185. {
  186. int hasNum = ItemDataManager.GetItemNum(itemId);
  187. return hasNum >= num;
  188. }
  189. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  190. {
  191. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  192. if (needItemId > 0)
  193. {
  194. int subType = ItemUtilCS.GetItemSubType(needItemId);
  195. if (subType == cfg1.type)
  196. {
  197. return true;
  198. }
  199. else if (cfg1.subMenusArr.Length > 0)
  200. {
  201. foreach (int id2 in cfg1.subMenusArr)
  202. {
  203. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  204. if (cfg2.type == subType)
  205. {
  206. return true;
  207. }
  208. }
  209. }
  210. }
  211. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  212. {
  213. return true;
  214. }
  215. return false;
  216. }
  217. public static void SortItemIdsByOwned(int[] array)
  218. {
  219. Array.Sort(array, (int a, int b) =>
  220. {
  221. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  222. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  223. if (!ownedB && ownedA)
  224. {
  225. return 1;
  226. }
  227. else if (ownedB && !ownedA)
  228. {
  229. return -1;
  230. }
  231. return 0;
  232. });
  233. }
  234. public static string GetItemResExt(int itemType, int type)
  235. {
  236. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING)
  237. {
  238. return "jpg";
  239. }
  240. return "png";
  241. }
  242. }
  243. }