MountAdvancedNestedCtrl.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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.Network;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using UnityEngine;
  12. using XGame.Database;
  13. using XGame.Framework.UI;
  14. namespace FL.FGUI
  15. {
  16. /// <summary>
  17. /// 高级坐骑升级预览
  18. /// </summary>
  19. public struct UpgradePreviewParam
  20. {
  21. public int mountId;
  22. public int level;
  23. public int skillId;
  24. }
  25. /// <summary>
  26. /// UI逻辑处理类
  27. /// </summary>
  28. /// <typeparam name=""></typeparam>
  29. public partial class MountAdvancedNestedCtrl : UIController<MountAdvancedNestedVM>
  30. {
  31. private bool _bInitData = true;
  32. private int _curAdvancedIndex; // 当前选择的高级坐骑索引
  33. private int[] _costData; // 解锁|升级消耗的道具id和数量
  34. private List<zuoqiBaseTable> _advancedMountsInfoList; // 高级坐骑信息列表数据
  35. private Dictionary<int, List<UpgradePreviewParam>> _upgradePreviewMap; // 坐骑升级预览数据
  36. protected override void OnEnable(object intent)
  37. {
  38. AddUIListenres();
  39. AddEventListener();
  40. }
  41. protected override void OnDisable()
  42. {
  43. RemoveUIListenres();
  44. RemoveEventListener();
  45. }
  46. #region UI事件
  47. private void AddUIListenres()
  48. {
  49. VM.RideBtn.onClick.Add(OnClickRideBtn);
  50. VM.UpgradePreviewBtn.onClick.Add(OnClickUpgradePreviewBtn);
  51. VM.EnableSkillBtn.onClick.Add(OnClickEnableSkillBtn);
  52. VM.UpgradeMountBtn.onClick.Add(OnClickUpgradeMountBtn);
  53. VM.UnlockMountBtn.onClick.Add(OnClickUnlockMountBtn);
  54. }
  55. private void RemoveUIListenres()
  56. {
  57. VM.RideBtn.onClick.Remove(OnClickRideBtn);
  58. VM.UpgradePreviewBtn.onClick.Remove(OnClickUpgradePreviewBtn);
  59. VM.EnableSkillBtn.onClick.Remove(OnClickEnableSkillBtn);
  60. VM.UpgradeMountBtn.onClick.Remove(OnClickUpgradeMountBtn);
  61. VM.UnlockMountBtn.onClick.Remove(OnClickUnlockMountBtn);
  62. }
  63. private void OnClickRideBtn(EventContext context)
  64. {
  65. int mountId = _advancedMountsInfoList[_curAdvancedIndex].Id;
  66. if (mountId == 0)
  67. {
  68. XGame.Framework.Assert.IsNotNull(mountId);
  69. }
  70. if (mountId == MountData.Instance.MountModelId)
  71. {
  72. MountService.Instance.SendToRideDown();
  73. }
  74. else
  75. MountService.Instance.SendToRideMount(mountId);
  76. }
  77. private void OnClickUpgradePreviewBtn(EventContext context)
  78. {
  79. int mountId = _advancedMountsInfoList[_curAdvancedIndex].Id;
  80. var dataList = _upgradePreviewMap.ContainsKey(mountId) ? _upgradePreviewMap[mountId] : null;
  81. Context.UI.OpenAsync(UIKeys.MountUpgradePreviewPanel, dataList);
  82. }
  83. private void OnClickEnableSkillBtn(EventContext context)
  84. {
  85. if (VM.SkillCtrl.selectedIndex == 0)
  86. MountService.Instance.SendToEnableAdvancedSkill(_advancedMountsInfoList[_curAdvancedIndex].Id);
  87. else
  88. MountService.Instance.SendToCancelAdvancedSkill();
  89. }
  90. private void OnClickUpgradeMountBtn(EventContext context)
  91. {
  92. MountService.Instance.SendToUpgradeStar(_advancedMountsInfoList[_curAdvancedIndex].Id);
  93. }
  94. private void OnClickUnlockMountBtn(EventContext context)
  95. {
  96. if (_advancedMountsInfoList[_curAdvancedIndex]?.Unlock.Length > 1)
  97. {
  98. int itemId = _advancedMountsInfoList[_curAdvancedIndex].Unlock[0];
  99. int needNum = _advancedMountsInfoList[_curAdvancedIndex].Unlock[1];
  100. if (ItemService.Instance.IsEnough(itemId, needNum, true))
  101. {
  102. MountService.Instance.SendToUnlockAdvancedMount(_advancedMountsInfoList[_curAdvancedIndex].Id);
  103. }
  104. }
  105. }
  106. #endregion
  107. private void AddEventListener()
  108. {
  109. Context.AddListener(EventDefine.ChangeRideState, ChangeRideState);
  110. Context.AddListener(EventDefine.UpdataItemData, OnUpdateItemNum);
  111. Context.AddListener(EventDefine.UnlockAdvancedMountSucessful, UnlockAdvancedMountSucessful);
  112. Context.AddListener(EventDefine.UseMountSkillSucessful, UseMountSkillSucessful);
  113. Context.AddListener(EventDefine.UpgradeAdvancedMountSucessful, UpgradeAdvancedMountSucessful);
  114. VM.AdvancedMountList.ItemClickEvent += ClickAdvancedMountItem;
  115. }
  116. private void RemoveEventListener()
  117. {
  118. Context.RemoveListener(EventDefine.ChangeRideState, ChangeRideState);
  119. Context.RemoveListener(EventDefine.UpdataItemData, OnUpdateItemNum);
  120. Context.RemoveListener(EventDefine.UnlockAdvancedMountSucessful, UnlockAdvancedMountSucessful);
  121. Context.RemoveListener(EventDefine.UseMountSkillSucessful, UseMountSkillSucessful);
  122. Context.RemoveListener(EventDefine.UpgradeAdvancedMountSucessful, UpgradeAdvancedMountSucessful);
  123. VM.AdvancedMountList.ItemClickEvent -= ClickAdvancedMountItem;
  124. }
  125. public void ClearData()
  126. {
  127. _advancedMountsInfoList?.Clear();
  128. _advancedMountsInfoList = null;
  129. _upgradePreviewMap?.Clear();
  130. _upgradePreviewMap = null;
  131. _bInitData = true;
  132. }
  133. /// <summary>
  134. /// 高级坐骑
  135. /// </summary>
  136. private void GetAdvanceMountData()
  137. {
  138. if (_advancedMountsInfoList == null) _advancedMountsInfoList = new List<zuoqiBaseTable>();
  139. var dataList = zuoqiBaseTableRepo.GetAll();
  140. foreach (var item in dataList)
  141. {
  142. if (item.Type == 2) // 高级坐骑
  143. {
  144. _advancedMountsInfoList.Add(item);
  145. }
  146. }
  147. }
  148. private void Init()
  149. {
  150. GetAdvanceMountData();
  151. InitUpgradePreviewData();
  152. }
  153. private void InitUpgradePreviewData()
  154. {
  155. if (_upgradePreviewMap == null)
  156. {
  157. _upgradePreviewMap = new Dictionary<int, List<UpgradePreviewParam>>();
  158. }
  159. var cfgDataList = zuoqiStarTableRepo.GetAll();
  160. UpgradePreviewParam upgradePreviewData;
  161. foreach (var item in cfgDataList)
  162. {
  163. if (item.Preview > 0)
  164. {
  165. upgradePreviewData = new UpgradePreviewParam()
  166. {
  167. mountId = item.Zqid,
  168. level = item.Level,
  169. skillId = item.StarSkill
  170. };
  171. if (_upgradePreviewMap.ContainsKey(item.Zqid))
  172. {
  173. _upgradePreviewMap[item.Zqid].Add(upgradePreviewData);
  174. }
  175. else
  176. {
  177. _upgradePreviewMap.Add(item.Zqid, new List<UpgradePreviewParam>()
  178. {
  179. upgradePreviewData
  180. });
  181. }
  182. }
  183. }
  184. }
  185. private void ShowAdvancedNestedUI(bool bShow, bool bHideList = true)
  186. {
  187. if (!bShow)
  188. {
  189. bHideList = !bHideList;
  190. }
  191. VM.AdvancedMountList.visible = bHideList;
  192. if (bShow)
  193. {
  194. VM.UnlockMountItem.Enable(null);
  195. }
  196. else
  197. {
  198. VM.UnlockMountItem.Disable();
  199. }
  200. }
  201. /// <summary>
  202. /// 显示高级坐骑界面UI
  203. /// </summary>
  204. public void ShowAdvancedMountUI(bool bSelectAdvancedIndex)
  205. {
  206. if (_bInitData)
  207. {
  208. Init();
  209. _bInitData = false;
  210. }
  211. _advancedMountsInfoList.Sort((a, b) =>
  212. {
  213. int aUnlcok = MountData.Instance.IsUnLock(a.Id) ? 1 : 0;
  214. int bUnlcok = MountData.Instance.IsUnLock(b.Id) ? 1 : 0;
  215. if (aUnlcok == bUnlcok)
  216. {
  217. int aLevel = MountData.Instance.GetAdvancedMountLv(a.Id);
  218. int bLevel = MountData.Instance.GetAdvancedMountLv(b.Id);
  219. if (aLevel == bLevel)
  220. {
  221. return a.Id - b.Id;
  222. }
  223. return bLevel - aLevel;
  224. }
  225. return bUnlcok - aUnlcok;
  226. });
  227. if (bSelectAdvancedIndex)
  228. {
  229. int index = _advancedMountsInfoList.FindIndex(item => item.Id == MountData.Instance.MountModelId);
  230. if (index != -1) _curAdvancedIndex = index;
  231. else _curAdvancedIndex = 0;
  232. }
  233. ShowMountsListUI();
  234. }
  235. private void ShowMountsListUI()
  236. {
  237. VM.AdvancedMountList.BindDatas(_advancedMountsInfoList);
  238. ClickAdvancedMountItem(_curAdvancedIndex);
  239. }
  240. /// <summary>
  241. /// 高级坐骑的升级预览和启用技能按钮状态
  242. /// </summary>
  243. /// <param name="bSHow"></param>
  244. private void ShowAdvancedMountBtn(bool bShow)
  245. {
  246. VM.UpgradePreviewBtn.visible = bShow;
  247. VM.EnableSkillBtn.visible = bShow;
  248. }
  249. /// <summary>
  250. /// 选择的高级坐骑
  251. /// </summary>
  252. /// <param name="selectIndex"></param>
  253. private void ShowSelectAdvancedMount(int selectIndex)
  254. {
  255. _curAdvancedIndex = selectIndex;
  256. var advancedMountInfo = _advancedMountsInfoList[selectIndex];
  257. if (advancedMountInfo != null)
  258. {
  259. VM.MountNameLabel.text = advancedMountInfo.Name;
  260. bool bUnlock = MountData.Instance.IsUnLock(advancedMountInfo.Id);
  261. int lv = MountData.Instance.GetAdvancedMountLv(advancedMountInfo.Id);
  262. ShowMountSpine(advancedMountInfo.Spine);
  263. ShowAttrUI(lv, advancedMountInfo.Id, bUnlock);
  264. ShowRideUI(bUnlock, advancedMountInfo.Id == MountData.Instance.MountModelId);
  265. ShowEnableSkillBtn(bUnlock, advancedMountInfo.Id == MountData.Instance.UseSkillMountId);
  266. VM.MountStar.visible = lv > 0;
  267. ShowAdvanceMountStarUI(lv);
  268. }
  269. }
  270. /// <summary>
  271. /// 坐骑的立绘
  272. /// </summary>
  273. /// <param name="mountPicture"></param>
  274. private void ShowMountSpine(string spineName)
  275. {
  276. VM.MountSpine.LoadSpine(spineName, "stand", true);
  277. }
  278. /// <summary>
  279. /// 高级坐骑的技能id
  280. /// </summary>
  281. /// <param name="skillId"></param>
  282. private void ShowMountSkillUI(int skillId)
  283. {
  284. var skillInfo = SkillTableRepo.Get(skillId);
  285. if (skillInfo != null)
  286. {
  287. VM.SkillIcon.icon = skillInfo.Icon;
  288. VM.SkillNameLabel.text = skillInfo.Name;
  289. VM.SkillDescLabel.text = skillInfo.Desc;
  290. }
  291. }
  292. /// <summary>
  293. ///
  294. /// </summary>
  295. /// <param name="bShow"></param>
  296. private void ShowEnableSkillBtn(bool bShow, bool bEnable = false)
  297. {
  298. VM.EnableSkillBtn.visible = bShow;
  299. if (bShow)
  300. {
  301. VM.SkillCtrl.selectedIndex = bEnable ? 1 : 0;
  302. }
  303. }
  304. /// <summary>
  305. /// 是否显示高级坐骑升级预览
  306. /// </summary>
  307. /// <param name="bShow"></param>
  308. private void ShowUpgradePreviewBtn(bool bShow)
  309. {
  310. VM.UpgradePreviewBtn.visible = bShow;
  311. }
  312. /// <summary>
  313. /// 高级坐骑拥有的效果
  314. /// </summary>
  315. /// <param name="curLv"></param>
  316. /// <param name="mountId"></param>
  317. private void ShowAttrUI(int curLv, int mountId, bool bUnlock)
  318. {
  319. if (curLv == 0)
  320. {
  321. var mountBaseInfo = zuoqiBaseTableRepo.Get(mountId);
  322. XGame.Framework.Assert.IsNotNull(mountBaseInfo, $"坐骑表zuoqiBase中不存在id:{mountId}");
  323. ShowCostUI(mountBaseInfo.Unlock);
  324. }
  325. //10000*坐骑id+等级
  326. var mountStarInfo = zuoqiStarTableRepo.Get(mountId * 10000 + curLv);
  327. if (mountStarInfo == null)
  328. {
  329. XGame.Log.Error($"坐骑表zuoqiStar中不存在id:{mountId * 10000 + curLv}");
  330. return;
  331. }
  332. if (bUnlock)
  333. ShowCostUI(mountStarInfo.StarUpCost);
  334. ShowMountSkillUI(mountStarInfo.StarSkill);
  335. int attrType = mountStarInfo.LevelOwnAttr[0];
  336. int attrVal = mountStarInfo.LevelOwnAttr.Length > 1 ? mountStarInfo.LevelOwnAttr[1] : 0;
  337. ShowCurAtrrDesc(attrType, attrVal);
  338. var nextMountStarInfo = zuoqiStarTableRepo.Get(mountId * 10000 + curLv + 1);
  339. if (nextMountStarInfo != null)
  340. {
  341. if (nextMountStarInfo?.LevelOwnAttr?.Length > 1)
  342. {
  343. VM.NextMountAttrDescLabel.text = nextMountStarInfo.LevelOwnAttr[1].ToRealFloatPercentage();
  344. }
  345. if (curLv == 0)
  346. {
  347. ShowMountSkillUI(nextMountStarInfo.StarSkill);
  348. }
  349. ShowAdvancedNestedUI(true);
  350. }
  351. else
  352. ShowAdvancedNestedUI(false, false);
  353. VM.AdvanceMountStateCtrl.selectedIndex = bUnlock ? (nextMountStarInfo == null ? 2 : 1) : 0;
  354. if (bUnlock) ShowUpgradePreviewBtn(nextMountStarInfo != null);
  355. }
  356. private void ShowCurAtrrDesc(int attrType, int attrValue)
  357. {
  358. var attributeInfo = AttrDescTableRepo.Get(attrType);
  359. if (attributeInfo != null)
  360. {
  361. VM.MountAttrDescLabel.text = attributeInfo.ShowName + attrValue.ToRealFloatPercentage();
  362. }
  363. }
  364. /// <summary>
  365. /// 高级坐骑解锁|升级消耗UI
  366. /// </summary>
  367. /// <param name="cost"></param>
  368. private void ShowCostUI(int[] costData)
  369. {
  370. _costData = costData;
  371. long num = ItemData.Instance.GetItemNum(costData[0]);
  372. var item = ItemService.Instance.GetItemBase(costData[0], num, 0);
  373. VM.UnlockMountItem.Ctrl.ShowUI(item);
  374. VM.UnlockMountItem.VM.CountLabel.visible = true;
  375. VM.UnlockMountItem.VM.CountLabel.text = $"{num.FormatNumber()}/{((long)costData[1]).FormatNumber()}";
  376. }
  377. /// <summary>
  378. /// 显示高级坐骑的星级
  379. /// </summary>
  380. /// <param name="starLevel"></param>
  381. private void ShowAdvanceMountStarUI(int starLevel)
  382. {
  383. int starNum = starLevel == 0 ? 0 : starLevel % MountData.MaxStarNum;
  384. if (starLevel > 0 && starNum == 0)
  385. starNum = MountData.MaxStarNum;
  386. // 获取星星的色阶
  387. int starStep = Mathf.FloorToInt((starLevel - 1) / MountData.MaxStarNum) + 1;
  388. int[] array = Enumerable.Repeat(starStep, starNum).ToArray();
  389. VM.MountStar.BindDatas(array);
  390. }
  391. /// <summary>
  392. /// 选择某个高级坐骑
  393. /// </summary>
  394. /// <param name="index"></param>
  395. private void ClickAdvancedMountItem(int index)
  396. {
  397. ShowSelectAdvancedMount(index);
  398. var mountItemList = VM.AdvancedMountList.GetChildren();
  399. for (int i = 0; i < mountItemList.Length; i++)
  400. {
  401. if (mountItemList[i] != null)
  402. {
  403. (mountItemList[i].asCom).GetController("SelectCtrl").selectedIndex = i == index ? 1 : 0;
  404. }
  405. }
  406. }
  407. /// <summary>
  408. /// 解锁某个高级坐骑成功
  409. /// </summary>
  410. /// <param name="eventId"></param>
  411. /// <param name="args"></param>
  412. private void UnlockAdvancedMountSucessful(int eventId, object args)
  413. {
  414. ShowAdvancedMountUI(false);
  415. }
  416. /// <summary>
  417. /// 成功启用某个高级坐骑技能
  418. /// </summary>
  419. /// <param name="eventId"></param>
  420. /// <param name="args"></param>
  421. private void UseMountSkillSucessful(int eventId, object args)
  422. {
  423. ShowEnableSkillBtn(true, _advancedMountsInfoList[_curAdvancedIndex].Id == MountData.Instance.UseSkillMountId);
  424. }
  425. /// <summary>
  426. /// 高级坐骑升级成功
  427. /// </summary>
  428. /// <param name="eventId"></param>
  429. /// <param name="args"></param>
  430. private void UpgradeAdvancedMountSucessful(int eventId, object args)
  431. {
  432. ShowSelectAdvancedMount(_curAdvancedIndex);
  433. Context.UI.OpenAsync(UIKeys.MountUpgradeSuccessfulPanel);
  434. }
  435. /// <summary>
  436. /// 骑乘状态UI
  437. /// </summary>
  438. /// <param name="bShow"></param>
  439. /// <param name="bRide"></param>
  440. private void ShowRideUI(bool bShow, bool bRide = false)
  441. {
  442. VM.RideBtn.visible = bShow;
  443. if (bShow)
  444. {
  445. VM.RideCtrl.selectedIndex = bRide ? 1 : 0;
  446. }
  447. }
  448. /// <summary>
  449. /// 坐骑骑乘状态变化
  450. /// </summary>
  451. /// <param name="eventId"></param>
  452. /// <param name="args"></param>
  453. private void ChangeRideState(int eventId, object args)
  454. {
  455. int mountId = _advancedMountsInfoList[_curAdvancedIndex].Id;
  456. ShowRideUI(MountData.Instance.IsUnLock(mountId), mountId == MountData.Instance.MountModelId);
  457. ShowMountsListUI();
  458. }
  459. /// <summary>
  460. /// 道具数据变化
  461. /// </summary>
  462. /// <param name="eventId"></param>
  463. /// <param name="args"></param>
  464. private void OnUpdateItemNum(int eventId, object args)
  465. {
  466. var itemDataList = args as List<ActItem>;
  467. if (itemDataList?.Count > 0)
  468. {
  469. for (int i = 0; i < itemDataList.Count; i++)
  470. {
  471. if (itemDataList[i].id == _costData[0])
  472. {
  473. ShowCostUI(_costData);
  474. }
  475. }
  476. }
  477. }
  478. }
  479. }