ItemUtil.cs 9.8 KB

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