DragonEggBoxCtrl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 Spine.Unity;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using XGame.Database;
  12. using XGame.Framework.FGUI;
  13. using XGame.Framework.Time;
  14. using XGame.Framework.UI;
  15. namespace FL.FGUI
  16. {
  17. /// <summary>
  18. /// UI逻辑处理类
  19. /// </summary>
  20. /// <typeparam name=""></typeparam>
  21. public partial class DragonEggBoxCtrl : UIController<DragonEggBoxVM>
  22. {
  23. private int _addSocre;// 选择的品质龙蛋增加的积分;
  24. private ITimer _autoTimer;// 自动定时器
  25. private ITimer _openBoxTimer;
  26. private Vector3 _startPos;
  27. private Vector3 _endPos;
  28. private Vector3 _scoreEndPos;
  29. private HashSet<DragonEggFlyIconView> _flyingList;
  30. private List<GButton> _dragonBtnList;
  31. private Dictionary<EAttributeType, string> _attributeDescMap;
  32. private const int _costDragonEggNum = 10; // 每次消耗的龙蛋数量
  33. protected override void OnEnable(object intent)
  34. {
  35. AddUIListenres();
  36. VM.MenusBtnCtrl.onChanged.Add(OnMenusBtnCtrlChange);
  37. EventSingle.Instance.AddListener(EventDefine.AttributeChange, OnChangeAttribute); // 角色属性变化
  38. Init();
  39. }
  40. protected override void OnDisable()
  41. {
  42. RemoveUIListenres();
  43. ClearAutoTimer();
  44. ClearOpenTimer();
  45. EventSingle.Instance.RemoveListener(EventDefine.AttributeChange, OnChangeAttribute); // 角色属性变化
  46. VM.MenusBtnCtrl.onChanged.Remove(OnMenusBtnCtrlChange);
  47. if (_flyingList != null)
  48. {
  49. foreach (var flying in _flyingList)
  50. {
  51. Context.Asset.Recycle(flying);
  52. }
  53. _flyingList.Clear();
  54. _flyingList = null;
  55. }
  56. if (_attributeDescMap != null)
  57. {
  58. _attributeDescMap.Clear();
  59. _attributeDescMap = null;
  60. }
  61. if (_dragonBtnList != null)
  62. {
  63. _dragonBtnList.Clear();
  64. _dragonBtnList = null;
  65. }
  66. VM.DragonSpine.url = string.Empty;
  67. }
  68. #region UI事件
  69. private void AddUIListenres()
  70. {
  71. VM.AutoBtn.onClick.Add(OnClickAutoBtn);
  72. VM.NextBtn.onClick.Add(OnClickNextBtn);
  73. VM.LastBtn.onClick.Add(OnClickLastBtn);
  74. VM.BagBtn.onClick.Add(OnClickBagBtn);
  75. VM.BoxAwardBtn.onClick.Add(OnClickBoxAwardBtn);
  76. VM.DragonSpineBtn.onClick.Add(OnclickDragonSpineBtn);
  77. }
  78. private void RemoveUIListenres()
  79. {
  80. VM.AutoBtn.onClick.Remove(OnClickAutoBtn);
  81. VM.NextBtn.onClick.Remove(OnClickNextBtn);
  82. VM.LastBtn.onClick.Remove(OnClickLastBtn);
  83. VM.BagBtn.onClick.Remove(OnClickBagBtn);
  84. VM.BoxAwardBtn.onClick.Remove(OnClickBoxAwardBtn);
  85. VM.DragonSpineBtn.onClick.Remove(OnclickDragonSpineBtn);
  86. }
  87. private void OnClickAutoBtn(EventContext context)
  88. {
  89. if (_autoTimer == null)
  90. {
  91. if (IsEmptyBox())
  92. {
  93. return;
  94. }
  95. OpenDragonEggBoxAni(true);
  96. _autoTimer = Context.Time.AddLooperTimer(500, (int dt) =>
  97. {
  98. if (IsEmptyBox(false))
  99. {
  100. ClearAutoTimer(true);
  101. return;
  102. }
  103. PlayDropAni();
  104. });
  105. }
  106. else
  107. {
  108. ClearAutoTimer(true);
  109. }
  110. }
  111. private void OnClickNextBtn(EventContext context)
  112. {
  113. OnSelectDragon(VM.MenusBtnCtrl.selectedIndex + 1);
  114. }
  115. private void OnClickLastBtn(EventContext context)
  116. {
  117. OnSelectDragon(VM.MenusBtnCtrl.selectedIndex - 1);
  118. }
  119. /// <summary>
  120. /// 点击背包按钮
  121. /// </summary>
  122. /// <param name="context"></param>
  123. private void OnClickBagBtn(EventContext context)
  124. {
  125. XGame.Log.Debug("打开背包");
  126. // 测试装备掉落
  127. DragonEggService.Instance.MakeEquip();
  128. }
  129. /// <summary>
  130. /// 积分进度条满值可领取对应的宝箱奖励
  131. /// </summary>
  132. /// <param name="context"></param>
  133. private void OnClickBoxAwardBtn(EventContext context)
  134. {
  135. OnChangeDrangonEggScore();
  136. }
  137. private void OnclickDragonSpineBtn(EventContext context)
  138. {
  139. ClearAutoTimer(true);
  140. if (IsEmptyBox()) return;
  141. OpenDragonEggBoxAni(false);
  142. PlayDropAni();
  143. }
  144. #endregion
  145. private void Init()
  146. {
  147. _startPos = VM.DragonSpineBtn.displayObject.LocalToWorld(new Vector3(VM.DragonSpineBtn.width / 3, VM.DragonSpineBtn.height / 2, 0));
  148. _endPos = VM.BagBtn.displayObject.LocalToWorld(new Vector3(VM.BagBtn.width / 10, VM.BagBtn.height / 8, 0));
  149. _scoreEndPos = VM.EggPointBar.displayObject.LocalToWorld(new Vector3(VM.EggPointBar.width / 2, VM.EggPointBar.height / 2, 0));
  150. InitDrangonBoxUI();
  151. InitAttributeDescMap();
  152. ShowAttributeUI();
  153. ShowDragonEggScoreUI(0);
  154. }
  155. /// <summary>
  156. /// 加载龙的spine
  157. /// </summary>
  158. /// <param name="spineName"></param>
  159. private void LoadDragonSpine(string spineName)
  160. {
  161. VM.DragonSpine.LoadSpine(spineName, "box_idle", true);
  162. }
  163. /// <summary>
  164. /// 龙蛋积分变化
  165. /// </summary>
  166. /// <param name="addScore"></param>
  167. /// <param name="bTweenValue"></param>
  168. private void ShowDragonEggScoreUI(int addScore, bool bTweenValue = false)
  169. {
  170. if (addScore != 0) DragonEggData.Instance.DragonEggSocre += addScore;
  171. VM.EggPointBar.max = DragonEggData.Instance.MaxDragonEggSocre;
  172. if (bTweenValue)
  173. VM.EggPointBar.TweenValue(DragonEggData.Instance.DragonEggSocre, 0.5f);
  174. else
  175. VM.EggPointBar.value = DragonEggData.Instance.DragonEggSocre;
  176. OnchangeAwardState(DragonEggData.Instance.DragonEggSocre >= DragonEggData.Instance.MaxDragonEggSocre);
  177. }
  178. private void InitDrangonBoxUI()
  179. {
  180. if (_dragonBtnList == null) _dragonBtnList = new List<GButton>() { VM.DragonBtn1, VM.DragonBtn2, VM.DragonBtn3, VM.DragonBtn4, VM.DragonBtn5 };
  181. int index = -1;
  182. for (int i = 0; i < 5; i++)
  183. {
  184. int num = DragonEggData.Instance.GetDragonEgg((EQualityLevel)i);
  185. ShowDragonBoxNum(i, num);
  186. if (num > 0 && index == -1) index = i;
  187. }
  188. OnSelectDragon(index == -1 ? 0 : index);
  189. }
  190. /// <summary>
  191. /// 判断龙裔宝卵数量是否
  192. /// </summary>
  193. /// <returns></returns>
  194. private bool IsEmptyBox(bool bShowTip = true)
  195. {
  196. var quality = (EQualityLevel)VM.MenusBtnCtrl.selectedIndex;
  197. var boxNum = DragonEggData.Instance.GetDragonEgg(quality);
  198. if (boxNum == 0)
  199. {
  200. if (bShowTip)
  201. {
  202. EventSingle.Instance.Notify(EventDefine.ShowTips, StringDefine.dragonEggsEmpty);
  203. }
  204. return true;
  205. }
  206. return false;
  207. }
  208. /// <summary>
  209. /// 龙蛋品质宝箱数量
  210. /// </summary>
  211. /// <param name="index"></param>
  212. /// <param name="num"></param>
  213. private void ShowDragonBoxNum(int index, int num)
  214. {
  215. if (_dragonBtnList[index]?.title != null) _dragonBtnList[index].title = $"X{num}";
  216. }
  217. private void ShowFlyIconView(int i, bool bScore)
  218. {
  219. var flyParam = new FlyingIconParam()
  220. {
  221. iconName = i < 6 ? $"itemicon_{i + 1}" : $"itemicon_{99}",
  222. startPos = _startPos,
  223. bScore = bScore,
  224. endPos = _endPos,
  225. };
  226. if (_flyingList == null) _flyingList = new HashSet<DragonEggFlyIconView>();
  227. var loadAsync = Context.Asset.LoadFguiNested(UINestedKeys.DragonEggFlyIcon, VM.DragonSpineBtn);
  228. loadAsync.On(_ =>
  229. {
  230. var flyView = loadAsync.Result as DragonEggFlyIconView;
  231. if (flyView != null)
  232. {
  233. flyParam.callback = (bScore) =>
  234. {
  235. FlyingCallback(flyView, bScore);
  236. };
  237. flyView.Ctrl.ShowUI(flyParam);
  238. _flyingList.Add(flyView);
  239. }
  240. });
  241. }
  242. private void FlyingCallback(DragonEggFlyIconView flyView, bool bScore)
  243. {
  244. _flyingList.Remove(flyView);
  245. Context.Asset.Recycle(flyView);
  246. if (_flyingList.Count == 0) OnchangBagState(false);
  247. if (bScore)
  248. {
  249. ShowDragonEggScoreUI(_addSocre, true);
  250. }
  251. }
  252. private void OnchangBagState(bool bOpen)
  253. {
  254. VM.BagBtn.GetController("State").selectedIndex = bOpen ? 1 : 0;
  255. }
  256. /// <summary>
  257. /// 开龙蛋宝箱的掉落动画
  258. /// </summary>
  259. private void PlayDropAni()
  260. {
  261. var quality = (EQualityLevel)VM.MenusBtnCtrl.selectedIndex;
  262. var boxNum = DragonEggData.Instance.GetDragonEgg(quality);
  263. int num = boxNum < _costDragonEggNum ? boxNum : _costDragonEggNum;
  264. DragonEggService.Instance.OnchangeDragonEgg(quality, -num);
  265. ShowDragonBoxNum((int)quality, DragonEggData.Instance.GetDragonEgg(quality));
  266. _addSocre = (VM.MenusBtnCtrl.selectedIndex + 1) * num;
  267. OnchangBagState(true);
  268. //ShowFlyIconView(0, true);
  269. for (int i = 0; i < num; i++)
  270. {
  271. ShowFlyIconView(i, i == num - 1);
  272. }
  273. }
  274. /// <summary>
  275. /// 宝箱按钮领取状态
  276. /// </summary>
  277. /// <param name="bGray"></param>
  278. private void OnchangeAwardState(bool bAward)
  279. {
  280. VM.AwardState.selectedIndex = bAward ? 1 : 0;
  281. }
  282. private void OnSelectDragon(int index)
  283. {
  284. VM.MenusBtnCtrl.selectedIndex = index;
  285. ShowSelectDragonUI();
  286. }
  287. /// <summary>
  288. /// 当前选中的龙蛋
  289. /// </summary>
  290. private void ShowSelectDragonUI()
  291. {
  292. ClearAutoTimer(true);
  293. int selectedIndex = VM.MenusBtnCtrl.selectedIndex;
  294. ShowDragonName(selectedIndex);
  295. // 获取龙的spine
  296. LoadDragonSpine("box_skeletondata");
  297. }
  298. /// <summary>
  299. /// 龙蛋按钮菜单栏变化
  300. /// </summary>
  301. /// <param name="context"></param>
  302. private void OnMenusBtnCtrlChange(EventContext context)
  303. {
  304. ShowSelectDragonUI();
  305. }
  306. private void ShowDragonName(int selectedIndex)
  307. {
  308. VM.LastBtn.visible = selectedIndex != 0;
  309. VM.NextBtn.visible = selectedIndex != 4;
  310. VM.DragonLabel.text = $"品质{selectedIndex + 1}钻石龙";
  311. VM.DragonLabel.color = ColorDefine.ToEquipNameColor((EQualityLevel)(selectedIndex + 1));
  312. }
  313. /// <summary>
  314. /// 领取龙蛋积分宝箱后龙蛋进度条积分变化
  315. /// </summary>
  316. private void OnChangeDrangonEggScore()
  317. {
  318. int addSocre = -DragonEggData.Instance.MaxDragonEggSocre;
  319. DragonEggData.Instance.MaxDragonEggSocre += 10;
  320. ShowDragonEggScoreUI(addSocre);
  321. }
  322. private void InitAttributeDescMap()
  323. {
  324. if (_attributeDescMap == null)
  325. {
  326. _attributeDescMap = new Dictionary<EAttributeType, string>()
  327. {
  328. {EAttributeType.Atk, GetAttributeDesc(EAttributeType.Atk)},
  329. {EAttributeType.Hp, GetAttributeDesc(EAttributeType.Hp)},
  330. {EAttributeType.Def, GetAttributeDesc(EAttributeType.Def)}
  331. };
  332. }
  333. }
  334. private string GetAttributeDesc(EAttributeType attrType)
  335. {
  336. var attrInfo = AttrDescTableRepo.Get((int)attrType);
  337. return attrInfo?.ShowName ?? string.Empty;
  338. }
  339. /// <summary>
  340. /// 显示角色属性UI(攻击|生命|防御)
  341. /// </summary>
  342. private void ShowAttributeUI()
  343. {
  344. ShowAtrributeVal(VM.AttackLabel, EAttributeType.Atk);
  345. ShowAtrributeVal(VM.HpLabel, EAttributeType.Hp);
  346. ShowAtrributeVal(VM.DefenseLabel, EAttributeType.Def);
  347. }
  348. private void ShowAtrributeVal(GTextField attrLabel, EAttributeType attrType)
  349. {
  350. attrLabel.text = $"{_attributeDescMap[attrType]}:{PlayerData.Instance.Attr.GetValue(attrType)}";
  351. }
  352. /// <summary>
  353. /// 角色属性变化
  354. /// </summary>
  355. /// <param name="eventId"></param>
  356. /// <param name="args"></param>
  357. private void OnChangeAttribute(int eventId, object args)
  358. {
  359. EAttributeType attrType = (EAttributeType)args;
  360. if (attrType == EAttributeType.Atk)
  361. {
  362. ShowAtrributeVal(VM.AttackLabel, EAttributeType.Atk);
  363. }
  364. else if (attrType == EAttributeType.Hp)
  365. {
  366. ShowAtrributeVal(VM.HpLabel, EAttributeType.Hp);
  367. }
  368. else if (attrType == EAttributeType.Def)
  369. {
  370. ShowAtrributeVal(VM.DefenseLabel, EAttributeType.Def);
  371. }
  372. }
  373. /// <summary>
  374. /// 龙蛋开箱动画
  375. /// </summary>
  376. private void OpenDragonEggBoxAni(bool bLoop)
  377. {
  378. if (VM.DragonSpine.Play("box_open", bLoop))
  379. {
  380. if (!bLoop)
  381. {
  382. ClearOpenTimer();
  383. _openBoxTimer = Context.Time.AddDelayTimer(VM.DragonSpine.spineAnimation.GetDurationMS(), () =>
  384. {
  385. VM.DragonSpine.Play("box_idle", true);
  386. });
  387. }
  388. }
  389. }
  390. private void ClearOpenTimer()
  391. {
  392. if (_openBoxTimer != null)
  393. {
  394. _openBoxTimer.Cancel();
  395. _openBoxTimer = null;
  396. }
  397. }
  398. private void ClearAutoTimer(bool bResetIdle = false)
  399. {
  400. if (_autoTimer != null)
  401. {
  402. if (bResetIdle)
  403. {
  404. VM.DragonSpine.Play("box_idle", true);
  405. }
  406. _autoTimer.Cancel();
  407. _autoTimer = null;
  408. }
  409. }
  410. }
  411. }