ItemGetItemPanelCtrl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. XGame.Log.Info("WearEquipSucess成功穿戴|替换装备");
  220. var changEquipIdList = args as HashSet<long>;
  221. if (changEquipIdList != null)
  222. {
  223. foreach (var item in changEquipIdList)
  224. {
  225. _waitEquipIdList.Remove(item);
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 穿戴移除只会发生在身上装备栏空
  231. /// </summary>
  232. /// <param name="eventId"></param>
  233. /// <param name="args"></param>
  234. private void RemoveTemporaryEquip(int eventId, object args)
  235. {
  236. int[] removeIds = args as int[];
  237. if (removeIds.Length > 0)
  238. {
  239. foreach(var id in removeIds)
  240. {
  241. _waitEquipIdList.Remove(id);
  242. _dropItems = _dropItems.FindAll(item =>(item.count > 0 || item.id != id));
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// 成功穿戴|替换装备消息流程结束
  248. /// </summary>
  249. /// <param name="eventId"></param>
  250. /// <param name="args"></param>
  251. private void RefreshTemporaryEquip(int eventId, object args)
  252. {
  253. ShowEquipEquipPanel();
  254. }
  255. /// <summary>
  256. /// 监听装备穿戴|替换界面关闭事件
  257. /// </summary>
  258. /// <param name="eventId"></param>
  259. /// <param name="args"></param>
  260. private void OnUIClosed(int eventId, object args)
  261. {
  262. var uiKey = args as UIKey;
  263. if (uiKey != null && uiKey == UIKeys.EquipEquipPanel)
  264. {
  265. if (EquipData.Instance.BubblingEquipUid > 0)
  266. {
  267. _waitEquipIdList.Remove(EquipData.Instance.BubblingEquipUid);
  268. EquipData.Instance.BubblingEquipUid = 0;
  269. ShowEquipEquipPanel();
  270. }
  271. }
  272. }
  273. /// <summary>
  274. /// 一键出售,拥有高战力装备的二次确认框按钮监听事件
  275. /// </summary>
  276. /// <param name="eventId"></param>
  277. /// <param name="args"></param>
  278. private void ConfirmationToSell()
  279. {
  280. HashSet<int> sellIds = new HashSet<int>();
  281. foreach (var item in _dropItems)
  282. {
  283. if (item.count == 0)
  284. {
  285. sellIds.Add(item.id);
  286. }
  287. }
  288. DragonEggService.Instance.SellEquip(sellIds.ToArray());
  289. sellIds.Clear();
  290. OnClosePanel();
  291. }
  292. /// <summary>
  293. /// 一键出售临时装备
  294. /// </summary>
  295. private void SendToSellTemporaryEquip()
  296. {
  297. if (IsHighFighingPower())
  298. {
  299. //XGame.Log.Warn($"当前要出售的装备{_newEquip.FightingPower}战力高于身上穿戴的{_wearEquip.FightingPower}");
  300. ConfirmationBoxParam dialogParam = new ConfirmationBoxParam()
  301. {
  302. cnt = StringDefine.sellHightPowerEquip,
  303. //tipsKey = EDayTipsKey.SellEquip,
  304. onCancelCallback = OnClosePanel,
  305. onPromiseCallback = ConfirmationToSell
  306. };
  307. Context.UI.OpenAsync(UIKeys.CommonConfirmationBox, dialogParam);
  308. return;
  309. }
  310. ConfirmationToSell();
  311. OnClosePanel();
  312. }
  313. /// <summary>
  314. /// 是否有高战力装备未处理
  315. /// </summary>
  316. /// <returns></returns>
  317. private bool IsHighFighingPower()
  318. {
  319. for (int i = 0; i < _dropItems.Count; i++)
  320. {
  321. if (_dropItems[i]?.count == 0)
  322. {
  323. EquipItem equipData = EquipData.Instance.GetBagEquipItem(_dropItems[i].id);
  324. if (equipData != null)
  325. {
  326. EquipItem wearEquip = EquipData.Instance.GetWearEquipData(equipData.EquipType);
  327. if ( wearEquip == null || equipData.FightingPower > wearEquip.FightingPower)
  328. {
  329. return true;
  330. }
  331. }
  332. }
  333. }
  334. return false;
  335. }
  336. }
  337. }