ItemUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 (itemId == ConstItemID.GOLD)
  19. {
  20. AddGold(num, onSuccess);
  21. }
  22. else if (itemId == ConstItemID.POWER)
  23. {
  24. AddPower();
  25. }
  26. else
  27. {
  28. BuyItemConteoller.Show(itemId, num, ConstBuyType.TYPE_ITEM, onSuccess, showTips, openSource, maxCount);
  29. BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
  30. }
  31. }
  32. public static void AddPower(string prefix = "", Action onSuccess = null)
  33. {
  34. int maxLimit = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.POWER).maxLimit;
  35. int lastBuyCount = maxLimit - ItemDataManager.GetItemExchangeTimes(ConstItemID.POWER);
  36. string showTxt = string.Format("每5分钟回复1点体力\n今日剩余购买{0}/{1}次", lastBuyCount, maxLimit);
  37. EnduringGiftBoxController.Show(ConstItemID.POWER, 1, () =>
  38. {
  39. if (onSuccess != null)
  40. {
  41. onSuccess();
  42. }
  43. }, showTxt);
  44. }
  45. public static void AddGold(long value = 0, Action onSuccess = null)
  46. {
  47. EnduringGiftBoxController.Show(ConstItemID.GOLD, 1, () =>
  48. {
  49. if (onSuccess != null)
  50. {
  51. onSuccess();
  52. }
  53. });
  54. }
  55. public static void AddDiamondPurple()
  56. {
  57. // ViewManager.Show(ViewName.RECHARGE_STORE_VIEW);
  58. ViewManager.Show<StoreView>(new object[] { ConstStoreTabId.STORE_CHARGE, ConstStoreSubId.STORE_CHARGE });
  59. }
  60. //红钻(鲛绡)
  61. public static void AddDiamondRed(int value = 0, Action onSuccess = null)
  62. {
  63. ItemExchangeCfg currencyRatioCfg = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.DIAMOND_RED);// GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
  64. BuyItemConteoller.Show(currencyRatioCfg.id, value > 0 ? value : 1, ConstBuyType.TYPE_ITEM, onSuccess, true, true);
  65. BuyItemConteoller.showTxtBuyTips = true;
  66. }
  67. public static string GetItemName(int itemId)
  68. {
  69. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  70. return itemCfg.name;
  71. }
  72. public static string GetSuitName(int suitId)
  73. {
  74. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  75. return suitCfg.name;
  76. }
  77. public static int GetItemRarity(int itemId)
  78. {
  79. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  80. return itemCfg.rarity;
  81. }
  82. public static List<ItemData> CreateItemDataList(List<ItemInfoProto> items)
  83. {
  84. List<ItemData> itemList = new List<ItemData>();
  85. for (int i = 0; i < items.Count; i++)
  86. {
  87. ItemData itemData = createItemData(items[i].ConfigId, items[i].Count);
  88. itemList.Add(itemData);
  89. }
  90. return itemList;
  91. }
  92. public static List<ItemData> CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
  93. {
  94. List<ItemData> itemList = new List<ItemData>();
  95. foreach (int[] item in bonus)
  96. {
  97. ItemData itemData = createItemData(item);
  98. itemList.Add(itemData);
  99. itemData.isOnceBonus = isOnceBonus;
  100. }
  101. return itemList;
  102. }
  103. public static List<ItemData> CreateItemDataList(int[][] bonus, int num)
  104. {
  105. List<ItemData> itemList = new List<ItemData>();
  106. foreach (int[] item in bonus)
  107. {
  108. int count = 0;
  109. if (item.Length > 1)
  110. {
  111. count = item[1];
  112. }
  113. else
  114. {
  115. count = 1;
  116. }
  117. ItemData itemData = createItemData(item[0], count * num);
  118. itemList.Add(itemData);
  119. }
  120. return itemList;
  121. }
  122. public static List<ItemData> CreateItemDataList(int itemId, long count)
  123. {
  124. List<ItemData> itemList = new List<ItemData>();
  125. ItemData itemData = createItemData(itemId, count);
  126. itemList.Add(itemData);
  127. return itemList;
  128. }
  129. public static ItemData createItemData(int[] bonus)
  130. {
  131. int itemID = bonus[0];
  132. int itemNum = 1;
  133. if (bonus.Length > 1)
  134. {
  135. itemNum = bonus[1];
  136. }
  137. return createItemData(itemID, itemNum);
  138. }
  139. public static ItemData createItemData(int itemId, long count)
  140. {
  141. ItemData itemData = ItemDataPool.GetItemData(itemId);
  142. itemData.num = count;
  143. return itemData;
  144. }
  145. public static Boolean CheckItemEnough(int itemId, int num)
  146. {
  147. long hasNum = ItemDataManager.GetItemNum(itemId);
  148. return hasNum >= num;
  149. }
  150. public static Boolean CheckItemEnough(int[][] items, int count = 1)
  151. {
  152. return items.All(item => CheckItemEnough(item[0], item[1] * count));
  153. }
  154. public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
  155. {
  156. DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
  157. if (needItemId > 0)
  158. {
  159. int subType = ItemUtilCS.GetItemSubType(needItemId);
  160. if (subType == cfg1.type)
  161. {
  162. return true;
  163. }
  164. else if (cfg1.subMenusArr.Length > 0)
  165. {
  166. foreach (int id2 in cfg1.subMenusArr)
  167. {
  168. DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
  169. if (cfg2.type == subType)
  170. {
  171. return true;
  172. }
  173. }
  174. }
  175. }
  176. else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
  177. {
  178. return true;
  179. }
  180. return false;
  181. }
  182. public static void SortItemIdsByOwned(int[] array)
  183. {
  184. Array.Sort(array, (int a, int b) =>
  185. {
  186. bool ownedA = ItemDataManager.GetItemNum(a) > 0;
  187. bool ownedB = ItemDataManager.GetItemNum(b) > 0;
  188. if (!ownedB && ownedA)
  189. {
  190. return 1;
  191. }
  192. else if (ownedB && !ownedA)
  193. {
  194. return -1;
  195. }
  196. return 0;
  197. });
  198. }
  199. public static string GetItemResExt(int itemType, int type, bool isIcon = false)
  200. {
  201. if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING && !isIcon)
  202. {
  203. return "jpg";
  204. }
  205. return "png";
  206. }
  207. public static void UpdateItemNumAndNeedNum(GObject obj, int itemId, int needNum, bool ChangeColor = false)
  208. {
  209. UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
  210. long hasNum = ItemDataManager.GetItemNum(itemId);
  211. string needStrColor = ChangeColor ? "#B99F7B" : "#FDEED4";
  212. string strHasNum = StringUtil.GetColorText(hasNum.ToString(), hasNum < needNum ? "#C5645A" : needStrColor); hasNum.ToString();
  213. // com.m_txtNeed.text = needNum.ToString();
  214. com.m_txtCount.text = string.Format("{0}{1}{2}", strHasNum, StringUtil.GetColorText("/", needStrColor), StringUtil.GetColorText(needNum.ToString(), needStrColor));
  215. ItemCfg cfg1 = ItemCfgArray.Instance.GetCfg(itemId);
  216. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg1.res);
  217. }
  218. public static void UpdateItemNeedNum(GObject obj, int itemId, int needNum, bool checkNum = true, string color = "#716B59")
  219. {
  220. UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
  221. com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
  222. long hasNum = ItemDataManager.GetItemNum(itemId);
  223. string strNeedNum = !checkNum ? StringUtil.GetColorText(needNum.ToString(), color) : StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color); needNum.ToString();
  224. com.m_txtNeed.text = strNeedNum;
  225. ItemCfg cfg = ItemCfgArray.Instance.GetCfg(itemId);
  226. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg.res);
  227. UI_ComCostCurrency.ProxyEnd();
  228. }
  229. public static void UpdateItemNeedNum(GObject obj, int[] cost, bool checkNum = true, string color = "#716B59")
  230. {
  231. int itemId = cost[0];
  232. int needNum = cost[1];
  233. UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
  234. com.m_c1.selectedIndex = needNum == 0 ? 0 : 1;
  235. long hasNum = ItemDataManager.GetItemNum(itemId);
  236. string strNeedNum = !checkNum ? StringUtil.GetColorText(needNum.ToString(), color) : StringUtil.GetColorText(needNum.ToString(), hasNum < needNum ? "#D0A09B" : color); needNum.ToString();
  237. com.m_txtNeed.text = strNeedNum;
  238. ItemCfg cfg = ItemCfgArray.Instance.GetCfg(itemId);
  239. com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg.res);
  240. UI_ComCostCurrency.ProxyEnd();
  241. }
  242. public static void UpdateTag(GObject obj, string tag)
  243. {
  244. UI_ComTag item = UI_ComTag.Proxy(obj);
  245. int tagType = TagCfgArray.Instance.GetCfg(tag).type;
  246. item.m_txtTag.text = tag;
  247. item.m_loaTag.url = ResPathUtil.GetCommonGameResPath("fzd_bqbq_" + tagType);
  248. UI_ComTag.ProxyEnd();
  249. }
  250. /// <summary>
  251. /// 合并奖励配置,会将同一物品id的奖励合并
  252. /// </summary>
  253. public static int[][] MergeBonus(int[][] item1, int[][] item2)
  254. {
  255. Dictionary<int, int> dictionary = new Dictionary<int, int>();
  256. if (item1 != null)
  257. {
  258. foreach (int[] item in item1)
  259. {
  260. if (dictionary.ContainsKey(item[0]))
  261. {
  262. dictionary[item[0]] += item[1];
  263. }
  264. else
  265. {
  266. dictionary.Add(item[0], item[1]);
  267. }
  268. }
  269. }
  270. if (item2 != null)
  271. {
  272. foreach (int[] item in item2)
  273. {
  274. if (dictionary.ContainsKey(item[0]))
  275. {
  276. dictionary[item[0]] += item[1];
  277. }
  278. else
  279. {
  280. dictionary.Add(item[0], item[1]);
  281. }
  282. }
  283. }
  284. int[][] result = new int[dictionary.Count][];
  285. int i = 0;
  286. foreach (KeyValuePair<int, int> item in dictionary)
  287. {
  288. result[i++] = new[] { item.Key, item.Value };
  289. }
  290. return result;
  291. }
  292. }
  293. }