ItemGetItemPanelCtrl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using FL.Data.Items;
  9. using FL.Network;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using XGame.Database;
  13. using XGame.Framework;
  14. using XGame.Framework.Time;
  15. using XGame.Framework.UI;
  16. namespace FL.FGUI
  17. {
  18. public class ObtainItemsParam
  19. {
  20. public List<KindItem> dropItems;
  21. public bool bShowBtn;
  22. }
  23. /// <summary>
  24. /// UI逻辑处理类
  25. /// </summary>
  26. /// <typeparam name=""></typeparam>
  27. public partial class ItemGetItemPanelCtrl : UIController<ItemGetItemPanelVM>
  28. {
  29. private ITimer _delayTimer; // 延迟展示高战力装备
  30. private List<long> _waitEquipIdList; // 穿戴|替换等待服务器消息返回的装备id列表
  31. private List<KindItem> _dropItems;
  32. protected override void OnEnable(object intent)
  33. {
  34. AddUIListenres();
  35. AddEventLisenter();
  36. ShowUI(intent as ObtainItemsParam);
  37. }
  38. protected override void OnDisable()
  39. {
  40. RemoveUIListenres();
  41. RemoveEventLisenter();
  42. ClearTimer();
  43. if (_waitEquipIdList != null ) _waitEquipIdList.Clear();
  44. if (_dropItems != null ) _dropItems.Clear();
  45. EquipData.Instance.ClearTemporaryEquipList();
  46. }
  47. private void ClearTimer()
  48. {
  49. _delayTimer?.Cancel();
  50. _delayTimer = null;
  51. }
  52. #region UI事件
  53. private void AddUIListenres()
  54. {
  55. VM.OpenBagBtn.onClick.Add(OnClickOpenBagBtn);
  56. }
  57. private void RemoveUIListenres()
  58. {
  59. VM.OpenBagBtn.onClick.Remove(OnClickOpenBagBtn);
  60. }
  61. /// <summary>
  62. /// 一键处理
  63. /// </summary>
  64. /// <param name="context"></param>
  65. private void OnClickOpenBagBtn(EventContext context)
  66. {
  67. SendToSellTemporaryEquip();
  68. }
  69. #endregion
  70. private void AddEventLisenter()
  71. {
  72. EventSingle.Instance.AddListener(EventDefine.WearEquipSucess, WearEquipSucess);
  73. EventSingle.Instance.AddListener(EventDefine.RemoveTemporaryEquip, RemoveTemporaryEquip);
  74. EventSingle.Instance.AddListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
  75. FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  76. }
  77. private void RemoveEventLisenter()
  78. {
  79. EventSingle.Instance.RemoveListener(EventDefine.WearEquipSucess, WearEquipSucess);
  80. EventSingle.Instance.RemoveListener(EventDefine.RemoveTemporaryEquip, RemoveTemporaryEquip);
  81. EventSingle.Instance.RemoveListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
  82. FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
  83. }
  84. private void OnClosePanel()
  85. {
  86. Context.ClosePanel();
  87. }
  88. private void ShowUI(ObtainItemsParam ObtainData)
  89. {
  90. ShowBtn(false);
  91. if (_dropItems == null)
  92. {
  93. _dropItems = new List<KindItem>();
  94. }
  95. if (ObtainData?.dropItems != null)
  96. {
  97. _dropItems.AddRange(ObtainData.dropItems);
  98. }
  99. ShowDropUI();
  100. if(ObtainData != null && ObtainData.bShowBtn)
  101. {
  102. ShowEquipUI();
  103. }
  104. }
  105. private void ShowDropUI()
  106. {
  107. //XGame.Log.Info($"道具的数量:{_dropItems.Count}");
  108. bool bLess = _dropItems.Count < 6; // 单行
  109. VM.ItemList.visible = !bLess;
  110. VM.LessItemList.visible = bLess;
  111. if (bLess)
  112. {
  113. VM.LessItemList.BindDatas(_dropItems);
  114. }
  115. else
  116. {
  117. VM.ItemList.BindDatas(_dropItems);
  118. }
  119. VM.EmptyComCtrl.selectedIndex = bLess ? 0 : (_dropItems.Count > 15 ? 2 : 1);
  120. VM.ItemList.scrollPane.bouncebackEffect = _dropItems.Count > 15 ? true : false;
  121. }
  122. private void ShowBtn(bool bShow)
  123. {
  124. VM.OpenBagBtn.visible = bShow;
  125. }
  126. private void ShowEquipUI()
  127. {
  128. List<KindItem> equipItemList = _dropItems.FindAll(item => item.count == 0);
  129. var highEquipMap = new Dictionary<EEquipType, EquipItem>();
  130. foreach (var item in equipItemList)
  131. {
  132. var equipItem = EquipData.Instance.GetTemporaryEquipItem(item.id);
  133. if (equipItem != null && IsHighEquip(equipItem))
  134. {
  135. if (highEquipMap.ContainsKey(equipItem.EquipType))
  136. {
  137. if (highEquipMap[equipItem.EquipType].FightingPower < equipItem.FightingPower)
  138. {
  139. highEquipMap[equipItem.EquipType] = equipItem;
  140. }
  141. }
  142. else
  143. {
  144. highEquipMap.Add(equipItem.EquipType, equipItem);
  145. }
  146. }
  147. }
  148. if (highEquipMap.Count > 0)
  149. {
  150. DelayToShowHighEquip(highEquipMap);
  151. }
  152. else
  153. ShowBtn(true);
  154. }
  155. /// <summary>
  156. /// 等待掉落道具显示后再冒泡展示高战力装备替换界面
  157. /// </summary>
  158. /// <param name="highEquipMap"></param>
  159. private void DelayToShowHighEquip(Dictionary<EEquipType, EquipItem> highEquipMap)
  160. {
  161. ClearTimer();
  162. _delayTimer = Context.Time.AddDelayTimer(1000, () =>
  163. {
  164. ShowHighEquipUI(highEquipMap);
  165. });
  166. }
  167. /// <summary>
  168. /// 是否高战力装备
  169. /// </summary>
  170. /// <param name="equipItem"></param>
  171. /// <returns></returns>
  172. private bool IsHighEquip(EquipItem equipItem)
  173. {
  174. EquipItem wearEquip = EquipData.Instance.GetWearEquipData(equipItem.Table.Part);
  175. if (wearEquip == null) return true;
  176. return equipItem.FightingPower > wearEquip.FightingPower;
  177. }
  178. private void ShowHighEquipUI(Dictionary<EEquipType, EquipItem> highEquipMap)
  179. {
  180. if (highEquipMap?.Count == 0)
  181. {
  182. ShowBtn(true);
  183. return;
  184. }
  185. if (_waitEquipIdList?.Count > 0)
  186. _waitEquipIdList.Clear();
  187. else
  188. _waitEquipIdList = new List<long>();
  189. foreach (var item in highEquipMap)
  190. {
  191. _waitEquipIdList.Add(item.Value.Id);
  192. }
  193. highEquipMap.Clear();
  194. ShowEquipEquipPanel();
  195. }
  196. /// <summary>
  197. /// 显示装备穿戴|替换界面
  198. /// </summary>
  199. private void ShowEquipEquipPanel()
  200. {
  201. if (_waitEquipIdList.Count > 0)
  202. {
  203. EquipData.Instance.BubblingEquipUid = _waitEquipIdList[0];
  204. Context.UI.OpenAsync(UIKeys.EquipEquipPanel, EquipData.Instance.GetTemporaryEquipItem(_waitEquipIdList[0]));
  205. }
  206. else
  207. {
  208. ShowDropUI();
  209. ShowBtn(true);
  210. }
  211. }
  212. /// <summary>
  213. /// 成功穿戴|替换装备
  214. /// </summary>
  215. /// <param name="eventId"></param>
  216. /// <param name="args"></param>
  217. private void WearEquipSucess(int eventId, object args)
  218. {
  219. var changEquipIdList = args as HashSet<long>;
  220. if (changEquipIdList != null)
  221. {
  222. foreach (var item in changEquipIdList)
  223. {
  224. _waitEquipIdList.Remove(item);
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// 穿戴移除只会发生在身上装备栏空
  230. /// </summary>
  231. /// <param name="eventId"></param>
  232. /// <param name="args"></param>
  233. private void RemoveTemporaryEquip(int eventId, object args)
  234. {
  235. int[] removeIds = args as int[];
  236. if (removeIds.Length > 0)
  237. {
  238. foreach(var id in removeIds)
  239. {
  240. _waitEquipIdList.Remove(id);
  241. _dropItems = _dropItems.FindAll(item =>(item.count > 0 || item.id != id));
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// 成功穿戴|替换装备消息流程结束
  247. /// </summary>
  248. /// <param name="eventId"></param>
  249. /// <param name="args"></param>
  250. private void RefreshTemporaryEquip(int eventId, object args)
  251. {
  252. ShowEquipEquipPanel();
  253. }
  254. /// <summary>
  255. /// 监听装备穿戴|替换界面关闭事件
  256. /// </summary>
  257. /// <param name="eventId"></param>
  258. /// <param name="args"></param>
  259. private void OnUIClosed(int eventId, object args)
  260. {
  261. var uiKey = args as UIKey;
  262. if (uiKey != null && uiKey == UIKeys.EquipEquipPanel)
  263. {
  264. if (EquipData.Instance.BubblingEquipUid > 0)
  265. {
  266. _waitEquipIdList.Remove(EquipData.Instance.BubblingEquipUid);
  267. EquipData.Instance.BubblingEquipUid = 0;
  268. ShowEquipEquipPanel();
  269. }
  270. }
  271. }
  272. /// <summary>
  273. /// 一键出售,拥有高战力装备的二次确认框按钮监听事件
  274. /// </summary>
  275. /// <param name="eventId"></param>
  276. /// <param name="args"></param>
  277. private void ConfirmationToSell()
  278. {
  279. HashSet<int> sellIds = new HashSet<int>();
  280. foreach (var item in _dropItems)
  281. {
  282. if (item.count == 0)
  283. {
  284. sellIds.Add(item.id);
  285. }
  286. }
  287. DragonEggService.Instance.SellEquip(sellIds.ToArray());
  288. sellIds.Clear();
  289. OnClosePanel();
  290. }
  291. /// <summary>
  292. /// 一键出售临时装备
  293. /// </summary>
  294. private void SendToSellTemporaryEquip()
  295. {
  296. if (IsHighFighingPower())
  297. {
  298. //XGame.Log.Warn($"当前要出售的装备{_newEquip.FightingPower}战力高于身上穿戴的{_wearEquip.FightingPower}");
  299. ConfirmationBoxParam dialogParam = new ConfirmationBoxParam()
  300. {
  301. cnt = StringDefine.sellHightPowerEquip,
  302. //tipsKey = EDayTipsKey.SellEquip,
  303. onCancelCallback = OnClosePanel,
  304. onPromiseCallback = ConfirmationToSell
  305. };
  306. Context.UI.OpenAsync(UIKeys.CommonConfirmationBox, dialogParam);
  307. return;
  308. }
  309. ConfirmationToSell();
  310. OnClosePanel();
  311. }
  312. /// <summary>
  313. /// 是否有高战力装备未处理
  314. /// </summary>
  315. /// <returns></returns>
  316. private bool IsHighFighingPower()
  317. {
  318. for (int i = 0; i < _dropItems.Count; i++)
  319. {
  320. if (_dropItems[i]?.count == 0)
  321. {
  322. EquipItem equipData = EquipData.Instance.GetBagEquipItem(_dropItems[i].id);
  323. if (equipData != null)
  324. {
  325. EquipItem wearEquip = EquipData.Instance.GetWearEquipData(equipData.EquipType);
  326. if ( wearEquip == null || equipData.FightingPower > wearEquip.FightingPower)
  327. {
  328. return true;
  329. }
  330. }
  331. }
  332. }
  333. return false;
  334. }
  335. }
  336. }