ItemUtil.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. CurrencyRatioCfg 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 CurrencyRatioCfg GetCurrencyRatioCfgById(int itemId)
  31. {
  32. CurrencyRatioCfg[] currencyRatioCfgs = CurrencyRatioCfgArray.Instance.GetCfgs(itemId);
  33. if (currencyRatioCfgs.Length == 0)
  34. {
  35. UnityEngine.Debug.LogWarning(itemId + "在h货币换算.xlsx中没有配置");
  36. return null;
  37. }
  38. CurrencyRatioCfg 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. CurrencyRatioCfg 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. CurrencyRatioCfg 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. CurrencyRatioCfg 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. CurrencyRatioCfg 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. CurrencyRatioCfg 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(string bonus, bool isOnceBonus = false)
  168. {
  169. List<ItemData> itemList = new List<ItemData>();
  170. string[] bonusList = bonus.Split(';');
  171. foreach (string i in bonusList)
  172. {
  173. if (i.Length > 0)
  174. {
  175. ItemData itemData = createItemData(i);
  176. itemList.Add(itemData);
  177. itemData.isOnceBonus = isOnceBonus;
  178. }
  179. }
  180. return itemList;
  181. }
  182. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  183. {
  184. List<ItemData> itemList = new List<ItemData>();
  185. foreach (int[] item in bonus)
  186. {
  187. ItemData itemData = createItemData(item);
  188. itemList.Add(itemData);
  189. itemData.isOnceBonus = isOnceBonus;
  190. }
  191. return itemList;
  192. }
  193. public static ItemData createItemData(string bonus)
  194. {
  195. string[] arr = bonus.Split('*');
  196. int itemID = int.Parse(arr[0]);
  197. int itemNum = 1;
  198. if (arr.Length > 1)
  199. {
  200. itemNum = int.Parse(arr[1]);
  201. }
  202. ItemData itemData = ItemDataPool.GetItemData(itemID);
  203. itemData.num = itemNum;
  204. return itemData;
  205. }
  206. public static ItemData createItemData(int[] bonus)
  207. {
  208. int itemID = bonus[0];
  209. int itemNum = 1;
  210. if (bonus.Length > 1)
  211. {
  212. itemNum = bonus[1];
  213. }
  214. ItemData itemData = ItemDataPool.GetItemData(itemID);
  215. itemData.num = itemNum;
  216. return itemData;
  217. }
  218. public static Boolean CheckItemEnough(int itemId, int num)
  219. {
  220. int hasNum = ItemDataManager.GetItemNum(itemId);
  221. return hasNum >= num;
  222. }
  223. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  224. {
  225. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  226. if (needItemId > 0)
  227. {
  228. int subType = ItemUtilCS.GetItemSubType(needItemId);
  229. if (subType == cfg1.type)
  230. {
  231. return true;
  232. }
  233. else if (cfg1.subMenusArr.Length > 0)
  234. {
  235. foreach (int id2 in cfg1.subMenusArr)
  236. {
  237. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  238. if (cfg2.type == subType)
  239. {
  240. return true;
  241. }
  242. }
  243. }
  244. }
  245. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  246. {
  247. return true;
  248. }
  249. return false;
  250. }
  251. public static void SortItemIdsByOwned(int[] array)
  252. {
  253. Array.Sort(array, (int a, int b) =>
  254. {
  255. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  256. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  257. if (!ownedB && ownedA)
  258. {
  259. return 1;
  260. }
  261. else if (ownedB && !ownedA)
  262. {
  263. return -1;
  264. }
  265. return 0;
  266. });
  267. }
  268. public static string GetItemResExt(int itemType, int type)
  269. {
  270. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING)
  271. {
  272. return "jpg";
  273. }
  274. return "png";
  275. }
  276. }
  277. }