ItemUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class ItemUtil
  8. {
  9. /// <summary>
  10. /// 物品的大类型,对应ConstItemType
  11. /// </summary>
  12. /// <param name="itemId"></param>
  13. /// <returns></returns>
  14. public static int GetItemType(int itemId)
  15. {
  16. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  17. if (itemCfg != null)
  18. {
  19. return itemCfg.itemType;
  20. }
  21. return 0;
  22. }
  23. /// <summary>
  24. /// 物品子类型
  25. /// </summary>
  26. /// <param name="itemId"></param>
  27. /// <returns></returns>
  28. public static int GetItemSubType(int itemId)
  29. {
  30. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  31. int subType = 0;
  32. if (itemCfg != null)
  33. {
  34. subType = itemCfg.subType;
  35. }
  36. return subType;
  37. }
  38. /// <param name="showTips">是否弹购买成功飘字,默认是</param>
  39. /// <param name="openSource">是否打开来源界面。默认否</param>
  40. /// <param name="showTxtBuyTips">是否显示购买提示</param>
  41. 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 = "")
  42. {
  43. CurrencyRatioCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  44. if (itemId == ConstItemID.GOLD)
  45. {
  46. AddGold(num, onSuccess);
  47. }
  48. else if (itemId == ConstItemID.POWER)
  49. {
  50. AddPower();
  51. }
  52. else
  53. {
  54. BuyItemConteoller.Show(itemId, currencyRatioCfg.costId, currencyRatioCfg.num, currencyRatioCfg.costNum, num, onSuccess, showTips, openSource, maxCount);
  55. BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
  56. }
  57. }
  58. public static CurrencyRatioCfg GetCurrencyRatioCfgById(int itemId)
  59. {
  60. CurrencyRatioCfg[] currencyRatioCfgs = CurrencyRatioCfgArray.Instance.GetCfgs(itemId);
  61. if (currencyRatioCfgs.Length == 0)
  62. {
  63. UnityEngine.Debug.LogWarning(itemId + "在h货币换算.xlsx中没有配置");
  64. return null;
  65. }
  66. CurrencyRatioCfg currencyRatioCfg;
  67. if (itemId == ConstItemID.POWER)
  68. {
  69. //体力购买次数不同,对应消耗配置不同
  70. if (RoleDataManager.powerBuyTimes < currencyRatioCfgs.Length)
  71. {
  72. currencyRatioCfg = currencyRatioCfgs[RoleDataManager.powerBuyTimes];
  73. }
  74. else
  75. {
  76. currencyRatioCfg = currencyRatioCfgs[currencyRatioCfgs.Length - 1];
  77. }
  78. }
  79. else
  80. {
  81. currencyRatioCfg = currencyRatioCfgs[currencyRatioCfgs.Length - 1];
  82. }
  83. return currencyRatioCfg;
  84. }
  85. public static void AddPower(string prefix = "", Action onSuccess = null)
  86. {
  87. CurrencyRatioCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.POWER);
  88. int count = GetCostItemCount(ConstItemID.POWER, currencyRatioCfg.num);
  89. int lastBuyCount = currencyRatioCfg.maxLimit - RoleDataManager.powerBuyTimes;
  90. string showTxt = string.Format("每5分钟回复1点体力\n今日剩余购买{0}/{1}次", lastBuyCount, currencyRatioCfg.maxLimit);
  91. BuyConfirmController.Show(ConstItemID.POWER, currencyRatioCfg.num, currencyRatioCfg.costId, currencyRatioCfg.costNum, () =>
  92. {
  93. RoleDataManager.powerBuyTimes = RoleDataManager.powerBuyTimes + 1;
  94. if (onSuccess != null)
  95. {
  96. onSuccess();
  97. }
  98. }, lastBuyCount, RoleDataManager.powerBuyTimes, showTxt);
  99. }
  100. public static void AddGold(int value = 0, Action onSuccess = null)
  101. {
  102. CurrencyRatioCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.GOLD);
  103. int count = value > 0 ? value : currencyRatioCfg.num;
  104. int costCount = GetCostItemCount(ConstItemID.GOLD, count);
  105. int lastBuyCount = currencyRatioCfg.maxLimit - RoleDataManager.goldBuyTimes;
  106. BuyConfirmController.Show(ConstItemID.GOLD, count, currencyRatioCfg.costId, costCount, () =>
  107. {
  108. RoleDataManager.goldBuyTimes = RoleDataManager.goldBuyTimes + 1;
  109. if (onSuccess != null)
  110. {
  111. onSuccess();
  112. }
  113. }, lastBuyCount, currencyRatioCfg.maxLimit);
  114. }
  115. public static void AddDiamondPurple()
  116. {
  117. ViewManager.Show(ViewName.RECHARGE_STORE_VIEW);
  118. }
  119. public static void AddDiamondPurple(int value, int costNum)
  120. {
  121. if (!AntiAddictionController.CheckAntiAddictionRecharge(costNum))
  122. {
  123. RoleDataManager.diaP += value;
  124. GameProxy.ReqUpdateRecharge(costNum);
  125. PromptController.Instance.ShowFloatTextPrompt("虚拟充值成功", MessageType.SUCCESS);
  126. }
  127. }
  128. //红钻(鲛绡)
  129. public static void AddDiamondRed(int value = 0, Action onSuccess = null)
  130. {
  131. CurrencyRatioCfg currencyRatioCfg = GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  132. 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);
  133. BuyItemConteoller.showTxtBuyTips = true;
  134. }
  135. /// <summary>
  136. /// 根据物品id和需求量获取兑换消耗品的消耗量
  137. /// </summary>
  138. public static int GetCostItemCount(int itemId, int count)
  139. {
  140. CurrencyRatioCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  141. if (currencyRatioCfg != null)
  142. {
  143. return (int)Math.Ceiling((decimal)count / currencyRatioCfg.num * currencyRatioCfg.costNum);
  144. }
  145. else
  146. {
  147. return 0;
  148. }
  149. }
  150. /// <summary>
  151. /// 根据物品id和兑换消耗品的消耗量获取物品兑换量
  152. /// </summary>
  153. public static int GetItemExChangeCount(int itemId, int costCount)
  154. {
  155. CurrencyRatioCfg currencyRatioCfg = GetCurrencyRatioCfgById(itemId);
  156. if (currencyRatioCfg != null)
  157. {
  158. return (int)Math.Floor((decimal)costCount / currencyRatioCfg.costNum * currencyRatioCfg.num);
  159. }
  160. else
  161. {
  162. return 0;
  163. }
  164. }
  165. /// <summary>
  166. /// 添加物品,会消耗物品
  167. /// </summary>
  168. /// <param name="addId"></param>
  169. /// <param name="addNum"></param>
  170. /// <param name="costId"></param>
  171. /// <param name="costNum"></param>
  172. public static void AddItemUseCost(int addId, int addNum, int costId, int costNum)
  173. {
  174. ItemDataManager.Add(addId, addNum);
  175. ItemDataManager.Remove(costId, costNum);
  176. GetSuitItemController.TryShow(0);
  177. }
  178. public static string GetItemName(int itemId)
  179. {
  180. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  181. return itemCfg.name;
  182. }
  183. public static string GetSuitName(int suitId)
  184. {
  185. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  186. return suitCfg.name;
  187. }
  188. public static int GetItemRarity(int itemId)
  189. {
  190. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  191. return itemCfg.rarity;
  192. }
  193. public static List<ItemData> CreateItemDataList(string bonus, bool isOnceBonus = false)
  194. {
  195. List<ItemData> itemList = new List<ItemData>();
  196. string[] bonusList = bonus.Split(';');
  197. foreach (string i in bonusList)
  198. {
  199. if (i.Length > 0)
  200. {
  201. ItemData itemData = createItemData(i);
  202. itemList.Add(itemData);
  203. itemData.isOnceBonus = isOnceBonus;
  204. }
  205. }
  206. return itemList;
  207. }
  208. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  209. {
  210. List<ItemData> itemList = new List<ItemData>();
  211. foreach (int[] item in bonus)
  212. {
  213. ItemData itemData = createItemData(item);
  214. itemList.Add(itemData);
  215. itemData.isOnceBonus = isOnceBonus;
  216. }
  217. return itemList;
  218. }
  219. public static ItemData createItemData(string bonus)
  220. {
  221. string[] arr = bonus.Split('*');
  222. int itemID = int.Parse(arr[0]);
  223. int itemNum = 1;
  224. if (arr.Length > 1)
  225. {
  226. itemNum = int.Parse(arr[1]);
  227. }
  228. ItemData itemData = ItemDataPool.GetItemData(itemID);
  229. itemData.num = itemNum;
  230. return itemData;
  231. }
  232. public static ItemData createItemData(int[] bonus)
  233. {
  234. int itemID = bonus[0];
  235. int itemNum = 1;
  236. if (bonus.Length > 1)
  237. {
  238. itemNum = bonus[1];
  239. }
  240. ItemData itemData = ItemDataPool.GetItemData(itemID);
  241. itemData.num = itemNum;
  242. return itemData;
  243. }
  244. public static Boolean IsDressUpItem(int itemId)
  245. {
  246. int itemType = GetItemType(itemId);
  247. return itemType == ConstItemType.DRESS_UP;
  248. }
  249. public static Boolean CheckItemEnough(int itemId, int num)
  250. {
  251. int hasNum = ItemDataManager.GetItemNum(itemId);
  252. return hasNum >= num;
  253. }
  254. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  255. {
  256. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  257. if (needItemId > 0)
  258. {
  259. int subType = ItemUtil.GetItemSubType(needItemId);
  260. if (subType == cfg1.type)
  261. {
  262. return true;
  263. }
  264. else if (cfg1.subMenusArr.Length > 0)
  265. {
  266. foreach (int id2 in cfg1.subMenusArr)
  267. {
  268. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  269. if (cfg2.type == subType)
  270. {
  271. return true;
  272. }
  273. }
  274. }
  275. }
  276. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  277. {
  278. return true;
  279. }
  280. return false;
  281. }
  282. public static void SortItemIdsByOwned(int[] array)
  283. {
  284. Array.Sort(array, (int a, int b) =>
  285. {
  286. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  287. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  288. if (!ownedB && ownedA)
  289. {
  290. return 1;
  291. }
  292. else if (ownedB && !ownedA)
  293. {
  294. return -1;
  295. }
  296. return 0;
  297. });
  298. }
  299. public static string GetItemResExt(int itemType, int type)
  300. {
  301. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING)
  302. {
  303. return "jpg";
  304. }
  305. return "png";
  306. }
  307. }
  308. }