ItemUtil.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. ExchangeCfg 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 ExchangeCfg GetCurrencyRatioCfgById(int itemId)
  31. {
  32. ExchangeCfg[] currencyRatioCfgs = ExchangeCfgArray.Instance.GetCfgs(itemId);
  33. if (currencyRatioCfgs.Length == 0)
  34. {
  35. UnityEngine.Debug.LogWarning(itemId + "在h货币换算.xlsx中没有配置");
  36. return null;
  37. }
  38. ExchangeCfg 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. ExchangeCfg 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. ExchangeCfg 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. public static void AddDiamondPurple(int value, int costNum)
  94. {
  95. if (!AntiAddictionController.CheckAntiAddictionRecharge(costNum))
  96. {
  97. RoleDataManager.diaP += value;
  98. GameProxy.ReqUpdateRecharge(costNum);
  99. PromptController.Instance.ShowFloatTextPrompt("虚拟充值成功", MessageType.SUCCESS);
  100. }
  101. }
  102. //红钻(鲛绡)
  103. public static void AddDiamondRed(int value = 0, Action onSuccess = null)
  104. {
  105. ExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  106. 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);
  107. BuyItemConteoller.showTxtBuyTips = true;
  108. }
  109. /// <summary>
  110. /// 根据物品id和需求量获取兑换消耗品的消耗量
  111. /// </summary>
  112. public static int GetCostItemCount(int itemId, int count)
  113. {
  114. ExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  115. if (currencyRatioCfg != null)
  116. {
  117. return (int)Math.Ceiling((decimal)count / currencyRatioCfg.num * currencyRatioCfg.costNum);
  118. }
  119. else
  120. {
  121. return 0;
  122. }
  123. }
  124. /// <summary>
  125. /// 根据物品id和兑换消耗品的消耗量获取物品兑换量
  126. /// </summary>
  127. public static int GetItemExChangeCount(int itemId, int costCount)
  128. {
  129. ExchangeCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  130. if (currencyRatioCfg != null)
  131. {
  132. return (int)Math.Floor((decimal)costCount / currencyRatioCfg.costNum * currencyRatioCfg.num);
  133. }
  134. else
  135. {
  136. return 0;
  137. }
  138. }
  139. /// <summary>
  140. /// 添加物品,会消耗物品
  141. /// </summary>
  142. /// <param name="addId"></param>
  143. /// <param name="addNum"></param>
  144. /// <param name="costId"></param>
  145. /// <param name="costNum"></param>
  146. public static void AddItemUseCost(int addId, int addNum, int costId, int costNum)
  147. {
  148. ItemDataManager.Add(addId, addNum);
  149. ItemDataManager.Remove(costId, costNum);
  150. GetSuitItemController.TryShow(0);
  151. }
  152. public static string GetItemName(int itemId)
  153. {
  154. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  155. return itemCfg.name;
  156. }
  157. public static string GetSuitName(int suitId)
  158. {
  159. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  160. return suitCfg.name;
  161. }
  162. public static int GetItemRarity(int itemId)
  163. {
  164. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  165. return itemCfg.rarity;
  166. }
  167. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  168. {
  169. List<ItemData> itemList = new List<ItemData>();
  170. foreach (int[] item in bonus)
  171. {
  172. ItemData itemData = createItemData(item);
  173. itemList.Add(itemData);
  174. itemData.isOnceBonus = isOnceBonus;
  175. }
  176. return itemList;
  177. }
  178. public static ItemData createItemData(int[] bonus)
  179. {
  180. int itemID = bonus[0];
  181. int itemNum = 1;
  182. if (bonus.Length > 1)
  183. {
  184. itemNum = bonus[1];
  185. }
  186. return createItemData(itemID, itemNum);
  187. }
  188. public static ItemData createItemData(int itemId, int count)
  189. {
  190. ItemData itemData = ItemDataPool.GetItemData(itemId);
  191. itemData.num = count;
  192. return itemData;
  193. }
  194. public static Boolean CheckItemEnough(int itemId, int num)
  195. {
  196. int hasNum = ItemDataManager.GetItemNum(itemId);
  197. return hasNum >= num;
  198. }
  199. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  200. {
  201. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  202. if (needItemId > 0)
  203. {
  204. int subType = ItemUtilCS.GetItemSubType(needItemId);
  205. if (subType == cfg1.type)
  206. {
  207. return true;
  208. }
  209. else if (cfg1.subMenusArr.Length > 0)
  210. {
  211. foreach (int id2 in cfg1.subMenusArr)
  212. {
  213. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  214. if (cfg2.type == subType)
  215. {
  216. return true;
  217. }
  218. }
  219. }
  220. }
  221. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  222. {
  223. return true;
  224. }
  225. return false;
  226. }
  227. public static void SortItemIdsByOwned(int[] array)
  228. {
  229. Array.Sort(array, (int a, int b) =>
  230. {
  231. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  232. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  233. if (!ownedB && ownedA)
  234. {
  235. return 1;
  236. }
  237. else if (ownedB && !ownedA)
  238. {
  239. return -1;
  240. }
  241. return 0;
  242. });
  243. }
  244. public static string GetItemResExt(int itemType, int type)
  245. {
  246. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING)
  247. {
  248. return "jpg";
  249. }
  250. return "png";
  251. }
  252. }
  253. }