ItemUtil.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using ET;
  2. using FairyGUI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UI.CommonGame;
  8. using UnityEngine;
  9. namespace GFGGame
  10. {
  11. public class ItemUtil
  12. {
  13. /// <param name="showTips">是否弹购买成功飘字,默认是</param>
  14. /// <param name="openSource">是否打开来源界面。默认否</param>
  15. /// <param name="showTxtBuyTips">是否显示购买提示</param>
  16. public static void ExchangeItemById(int itemId, long num, bool showTips = true, Action onSuccess = null, bool openSource = false, int maxCount = 9990, bool showTxtBuyTips = false, string prefix = "")
  17. {
  18. if (!BuyCurrency(itemId, num, onSuccess))
  19. {
  20. BuyItemConteoller.Show(itemId, num, ConstBuyType.TYPE_ITEM, onSuccess, showTips, openSource, maxCount);
  21. BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
  22. }
  23. }
  24. public static bool BuyCurrency(int itemId, long num, Action onSuccess = null)
  25. {
  26. switch (itemId)
  27. {
  28. case ConstItemID.GOLD:
  29. AddGold(onSuccess);
  30. return true;
  31. case ConstItemID.POWER:
  32. AddPower();
  33. return true;
  34. case ConstItemID.DIAMOND_RED:
  35. AddDiamondRed(num);
  36. return true;
  37. case ConstItemID.DIAMOND_PURPLE:
  38. ItemUtil.AddDiamondPurple();
  39. return true;
  40. }
  41. return false;
  42. }
  43. public static void AddPower(Action onSuccess = null, int type = 0)
  44. {
  45. EnduringGiftBoxController.Show(ConstItemID.POWER, 1, () =>
  46. {
  47. if (onSuccess != null)
  48. {
  49. onSuccess();
  50. }
  51. }, "", type);
  52. }
  53. public static void AddGold(Action onSuccess = null, int type = 0)
  54. {
  55. EnduringGiftBoxController.Show(ConstItemID.GOLD, 1, () =>
  56. {
  57. if (onSuccess != null)
  58. {
  59. onSuccess();
  60. }
  61. }, "", type);
  62. }
  63. public static void AddDiamondPurple()
  64. {
  65. //ViewManager.Show<RechargeStoreView>();
  66. ViewManager.Show<StoreView>(new object[] { ConstStoreTabId.STORE_CHARGE, ConstStoreSubId.STORE_CHARGE });
  67. EventAgent.DispatchEvent(ConstMessage.CLOSE_SHOPPING_TIP);
  68. }
  69. //红钻(鲛绡)
  70. public static void AddDiamondRed(long value = 0, Action onSuccess = null)
  71. {
  72. ItemExchangeCfg currencyRatioCfg = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.DIAMOND_RED);// GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  73. BuyItemConteoller.Show(currencyRatioCfg.id, value > 0 ? value : 1, ConstBuyType.TYPE_ITEM, onSuccess, true, true);
  74. BuyItemConteoller.showTxtBuyTips = true;
  75. }
  76. public static string GetItemName(int itemId)
  77. {
  78. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  79. return itemCfg.name;
  80. }
  81. public static string GetSuitName(int suitId)
  82. {
  83. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  84. return suitCfg.name;
  85. }
  86. public static int GetItemRarity(int itemId)
  87. {
  88. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  89. return itemCfg.rarity;
  90. }
  91. public static List<ItemData> CreateItemDataList(List<ItemInfoProto> items)
  92. {
  93. List<ItemData> itemList = new List<ItemData>();
  94. for (int i = 0; i < items.Count; i++)
  95. {
  96. ItemData itemData = createItemData(items[i].ConfigId, items[i].Count);
  97. itemList.Add(itemData);
  98. }
  99. return itemList;
  100. }
  101. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  102. {
  103. List<ItemData> itemList = new List<ItemData>();
  104. foreach (int[] item in bonus)
  105. {
  106. ItemData itemData = createItemData(item);
  107. itemList.Add(itemData);
  108. itemData.isOnceBonus = isOnceBonus;
  109. }
  110. return itemList;
  111. }
  112. public static List<ItemData> CreateItemDataList(int[][] bonus, long num)
  113. {
  114. List<ItemData> itemList = new List<ItemData>();
  115. foreach (int[] item in bonus)
  116. {
  117. int count = 0;
  118. if (item.Length > 1)
  119. {
  120. count = item[1];
  121. }
  122. else
  123. {
  124. count = 1;
  125. }
  126. ItemData itemData = createItemData(item[0], count * num);
  127. itemList.Add(itemData);
  128. }
  129. return itemList;
  130. }
  131. public static List<ItemData> CreateItemDataList(int itemId, long count)
  132. {
  133. List<ItemData> itemList = new List<ItemData>();
  134. ItemData itemData = createItemData(itemId, count);
  135. itemList.Add(itemData);
  136. return itemList;
  137. }
  138. public static ItemData createItemData(int[] bonus)
  139. {
  140. int itemID = bonus[0];
  141. int itemNum = 1;
  142. if (bonus.Length > 1)
  143. {
  144. itemNum = bonus[1];
  145. }
  146. return createItemData(itemID, itemNum);
  147. }
  148. public static ItemData createItemData(int itemId, long count, long countRandomeMin = 0)
  149. {
  150. ItemData itemData = ItemDataPool.GetItemData(itemId);
  151. itemData.num = count;
  152. itemData.numRandomeMin = countRandomeMin;
  153. return itemData;
  154. }
  155. public static Boolean CheckItemEnough(int itemId, long num, bool showTips = false)
  156. {
  157. long hasNum = ItemDataManager.GetItemNum(itemId);
  158. if (hasNum < num && showTips)
  159. {
  160. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  161. PromptController.Instance.ShowFloatTextPrompt(string.Format("【{0}】 不足", itemCfg.name));
  162. }
  163. return hasNum >= num;
  164. }
  165. public static Boolean CheckItemEnough(int[][] items, int count = 1)
  166. {
  167. return items.All(item => CheckItemEnough(item[0], item[1] * count));
  168. }
  169. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  170. {
  171. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  172. if (needItemId > 0)
  173. {
  174. int subType = ItemUtilCS.GetItemSubType(needItemId);
  175. if (subType == cfg1.type)
  176. {
  177. return true;
  178. }
  179. else if (cfg1.subMenusArr.Length > 0)
  180. {
  181. foreach (int id2 in cfg1.subMenusArr)
  182. {
  183. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  184. if (cfg2.type == subType)
  185. {
  186. return true;
  187. }
  188. }
  189. }
  190. }
  191. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  192. {
  193. return true;
  194. }
  195. return false;
  196. }
  197. public static void SortItemIdsByOwned(int[] array)
  198. {
  199. Array.Sort(array, (int a, int b) =>
  200. {
  201. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  202. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  203. if (!ownedB && ownedA)
  204. {
  205. return 1;
  206. }
  207. else if (ownedB && !ownedA)
  208. {
  209. return -1;
  210. }
  211. return 0;
  212. });
  213. }
  214. public static string GetItemResExt(int itemType, int type, bool isIcon = false)
  215. {
  216. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING && !isIcon)
  217. {
  218. return "jpg";
  219. }
  220. return "png";
  221. }
  222. public static void UpdateItemNumAndNeedNum(GObject obj, int itemId, int needNum, bool ChangeColor = false)
  223. {
  224. UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
  225. long hasNum = ItemDataManager.GetItemNum(itemId);
  226. string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
  227. string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor); hasNum.ToString();
  228. // com.m_txtNeed.text = needNum.ToString();
  229. com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor), StringUtil.GetColorText(needNum.ToString(), needStrColor));
  230. ItemCfg cfg1 = ItemCfgArray.Instance.GetCfg(itemId);
  231. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg1.res);
  232. }
  233. public static void UpdateItemAndNeed(GObject obj, int itemId, int needNum, bool ChangeColor = false)
  234. {
  235. UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
  236. long hasNum = ItemDataManager.GetItemNum(itemId);
  237. string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
  238. string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor); hasNum.ToString();
  239. // com.m_txtNeed.text = needNum.ToString();
  240. com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor), StringUtil.GetColorText(needNum.ToString(), needStrColor));
  241. ItemCfg cfg1 = ItemCfgArray.Instance.GetCfg(itemId);
  242. com.m_loaIcon.url = ResPathUtil.GetIconPath(cfg1.res,"png");
  243. }
  244. public static void UpdateItemNeedNum(GObject obj, int itemId, int needNum, bool checkNum = true, string color = "#716B59")
  245. {
  246. UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
  247. com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
  248. long hasNum = ItemDataManager.GetItemNum(itemId);
  249. string strNeedNum = !checkNum ? StringUtil.GetColorText(needNum.ToString(), color) : StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color); needNum.ToString();
  250. com.m_txtNeed.text = strNeedNum;
  251. ItemCfg cfg = ItemCfgArray.Instance.GetCfg(itemId);
  252. com.m_loaIcon.url = ResPathUtil.GetIconPath(cfg);
  253. UI_ComCostCurrency.ProxyEnd();
  254. }
  255. public static void UpdateItemNeedNum(GObject obj, int[] cost, bool checkNum = true, string color = "#716B59")
  256. {
  257. int itemId = cost[0];
  258. int needNum = cost[1];
  259. UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
  260. com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
  261. long hasNum = ItemDataManager.GetItemNum(itemId);
  262. string strNeedNum = !checkNum ? StringUtil.GetColorText(needNum.ToString(), color) : StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color); needNum.ToString();
  263. com.m_txtNeed.text = strNeedNum;
  264. ItemCfg cfg = ItemCfgArray.Instance.GetCfg(itemId);
  265. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg.res);
  266. UI_ComCostCurrency.ProxyEnd();
  267. }
  268. public static void UpdateTag(GObject obj, string tag)
  269. {
  270. UI_ComTag item = UI_ComTag.Proxy(obj);
  271. int tagType = TagCfgArray.Instance.GetCfg(tag).type;
  272. item.m_txtTag.text = tag;
  273. item.m_loaTag.url = ResPathUtil.GetCommonGameResPath("fzd_bqbq_" + tagType);
  274. UI_ComTag.ProxyEnd();
  275. }
  276. /// <summary>
  277. /// 合并奖励配置,会将同一物品id的奖励合并
  278. /// </summary>
  279. public static int[][] MergeBonus(int[][] item1, int[][] item2)
  280. {
  281. Dictionary<int, int> dictionary = new Dictionary<int, int>();
  282. if (item1 != null)
  283. {
  284. foreach (int[] item in item1)
  285. {
  286. if (dictionary.ContainsKey(item[0]))
  287. {
  288. dictionary[item[0]] += item[1];
  289. }
  290. else
  291. {
  292. dictionary.Add(item[0], item[1]);
  293. }
  294. }
  295. }
  296. if (item2 != null)
  297. {
  298. foreach (int[] item in item2)
  299. {
  300. if (dictionary.ContainsKey(item[0]))
  301. {
  302. dictionary[item[0]] += item[1];
  303. }
  304. else
  305. {
  306. dictionary.Add(item[0], item[1]);
  307. }
  308. }
  309. }
  310. int[][] result = new int[dictionary.Count][];
  311. int i = 0;
  312. foreach (KeyValuePair<int, int> item in dictionary)
  313. {
  314. result[i++] = new[] { item.Key, item.Value };
  315. }
  316. return result;
  317. }
  318. public static void CreateItemView(int[] item, GComponent component)
  319. {
  320. var itemData = createItemData(item);
  321. component.data ??= new ItemView(component);
  322. (component.data as ItemView)?.SetData(itemData);
  323. (component.data as ItemView)?.ChangeTxtCountStyle();
  324. }
  325. }
  326. }