TreasureChestUpgradePanelCtrl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 System.Collections.Generic;
  9. using System;
  10. using XGame.Database;
  11. using XGame.Framework.Time;
  12. using XGame.Framework.UI;
  13. using UnityEngine;
  14. namespace FL.FGUI
  15. {
  16. /// <summary>
  17. /// UI逻辑处理类
  18. /// </summary>
  19. /// <typeparam name=""></typeparam>
  20. public partial class TreasureChestUpgradePanelCtrl : UIController<TreasureChestUpgradePanelVM>
  21. {
  22. private ITimer _upLvTimer; // 宝箱升级倒计时定时器
  23. private const int GoldId = 1; // 升级消耗金币(道具id)
  24. private const int AccelerationId = 1000; // 加速卡道具id
  25. private treasureTable _curTreasure;
  26. private treasureTable _nextTreasure;
  27. protected override void OnEnable(object intent)
  28. {
  29. AddUIListenres();
  30. AddEventLister();
  31. InitUI();
  32. ShowUI();
  33. }
  34. protected override void OnDisable()
  35. {
  36. RemoveUIListenres();
  37. RemoveEventLister();
  38. ClearStepTimer();
  39. VM.GoldResItem.Disable();
  40. VM.AccelerationResItem.Disable();
  41. VM.CostAccelerationResItem.Disable();
  42. }
  43. #region UI事件
  44. private void AddUIListenres()
  45. {
  46. VM.ProbabilityBtn.onClick.Add(OnClickProbabilityBtn);
  47. VM.IncomeBtn.onClick.Add(OnClickIncomeBtn);
  48. VM.UpgradeBtn.onClick.Add(OnClickUpgradeBtn);
  49. VM.AssistBtn.onClick.Add(OnClickAssistBtn);
  50. VM.ReduceTimeBtn.onClick.Add(OnClickReduceTimeBtn);
  51. VM.AccelerationTimeBtn.onClick.Add(OnClickAccelerationTimeBtn);
  52. }
  53. private void RemoveUIListenres()
  54. {
  55. VM.ProbabilityBtn.onClick.Remove(OnClickProbabilityBtn);
  56. VM.IncomeBtn.onClick.Remove(OnClickIncomeBtn);
  57. VM.UpgradeBtn.onClick.Remove(OnClickUpgradeBtn);
  58. VM.AssistBtn.onClick.Remove(OnClickAssistBtn);
  59. VM.ReduceTimeBtn.onClick.Remove(OnClickReduceTimeBtn);
  60. VM.AccelerationTimeBtn.onClick.Remove(OnClickAccelerationTimeBtn);
  61. }
  62. private void OnClickProbabilityBtn(EventContext context)
  63. {
  64. ShowPageUI(0);
  65. }
  66. private void OnClickIncomeBtn(EventContext context)
  67. {
  68. ShowPageUI(1);
  69. }
  70. /// <summary>
  71. /// 升级按钮
  72. /// </summary>
  73. /// <param name="context"></param>
  74. private void OnClickUpgradeBtn(EventContext context)
  75. {
  76. DragonEggService.Instance.SendToUpgrade();
  77. }
  78. //协助按钮
  79. private void OnClickAssistBtn(EventContext context)
  80. {
  81. Context.ShowTips("发送援助到公会聊天频道");
  82. }
  83. /// <summary>
  84. /// 免费观看广告获取减少时间
  85. /// </summary>
  86. /// <param name="context"></param>
  87. private void OnClickReduceTimeBtn(EventContext context)
  88. {
  89. DragonEggService.Instance.SendToAcceleration(2, 0);
  90. }
  91. /// <summary>
  92. /// 加速按钮
  93. /// </summary>
  94. /// <param name="context"></param>
  95. private void OnClickAccelerationTimeBtn(EventContext context)
  96. {
  97. Context.UI.OpenAsync(UIKeys.TreasureChestReduceTimePanel,AccelerationId);
  98. }
  99. #endregion
  100. private void AddEventLister()
  101. {
  102. EventSingle.Instance.AddListener(EventDefine.RefreshUpgradeTime, RefreshUpgradeTime);
  103. EventSingle.Instance.AddListener(EventDefine.ShowEquipQualityProbability, ShowEquipQualityProbability);
  104. EventSingle.Instance.AddListener(EventDefine.RefreshWatchAd, RefreshWatchAd);
  105. EventSingle.Instance.AddListener(EventDefine.RefreshTreasureChestUI, RefreshTreasureChestUI);
  106. }
  107. private void RemoveEventLister()
  108. {
  109. EventSingle.Instance.RemoveListener(EventDefine.RefreshUpgradeTime, RefreshUpgradeTime);
  110. EventSingle.Instance.RemoveListener(EventDefine.ShowEquipQualityProbability, ShowEquipQualityProbability);
  111. EventSingle.Instance.RemoveListener(EventDefine.RefreshWatchAd, RefreshWatchAd);
  112. EventSingle.Instance.RemoveListener(EventDefine.RefreshTreasureChestUI, RefreshTreasureChestUI);
  113. }
  114. private void ShowUI()
  115. {
  116. _curTreasure = treasureTableRepo.Get(DragonEggData.Instance.Level);
  117. _nextTreasure = treasureTableRepo.Get(DragonEggData.Instance.Level + 1);
  118. bool bMaxLv = _nextTreasure == null;
  119. ShowBoxLv(DragonEggData.Instance.Level, bMaxLv);
  120. ShowExpUI(_curTreasure, bMaxLv);
  121. ShowPageUI(VM.PageCtrl.selectedIndex);
  122. }
  123. private void ShowPageUI(int index)
  124. {
  125. VM.PageCtrl.selectedIndex = index;
  126. if (index == 0)
  127. {
  128. ShowEquipProbability(_curTreasure, _nextTreasure);
  129. }
  130. else
  131. {
  132. ShowIncome(_curTreasure, _nextTreasure);
  133. }
  134. }
  135. private void ShowBoxLv(int curLv, bool bMaxLv)
  136. {
  137. VM.MaxLvCtrl.selectedIndex = bMaxLv ? 1 : 0;
  138. if (!bMaxLv)
  139. {
  140. VM.NextLvLabel.text = (curLv + 1).ToString();
  141. }
  142. VM.CurLvLabel.text = curLv.ToString();
  143. }
  144. private void ShowExpUI(treasureTable curTreasure, bool bMaxLv)
  145. {
  146. if (!bMaxLv && curTreasure?.Expend != null)
  147. {
  148. if (DragonEggData.Instance.UpLvTime > Context.Time.GetNowTime())
  149. {
  150. ShowUpgradeTimeUI();
  151. return;
  152. }
  153. VM.BoxLvExpBar.max = curTreasure.Expend;
  154. VM.BoxLvExpBar.value = DragonEggData.Instance.Exp;
  155. var needExp = curTreasure.Expend > DragonEggData.Instance.Exp ? curTreasure.Expend - DragonEggData.Instance.Exp : 0;
  156. VM.NeedExpLabel.text = string.Format(StringDefine.upgradeDragonEggBoxExp, needExp);
  157. VM.UpgradeBtn.enabled = DragonEggData.Instance.Exp >= curTreasure.Expend;
  158. }
  159. }
  160. /// <summary>
  161. /// 升级倒计时状态中
  162. /// </summary>
  163. private void ShowUpgradeTimeUI()
  164. {
  165. ShowUpgradeState(1);
  166. ClearStepTimer();
  167. if (_upLvTimer == null)
  168. {
  169. _upLvTimer = Context.Time.AddLooperTimer(500, (int dt) =>
  170. {
  171. ShowLeftStepTime();
  172. });
  173. }
  174. ShowLeftStepTime();
  175. }
  176. /// <summary>
  177. /// 龙蛋升阶倒计时
  178. /// </summary>
  179. private void ShowLeftStepTime()
  180. {
  181. long leftTime = DragonEggData.Instance.UpLvTime - Context.Time.GetNowTime();
  182. if (leftTime > 0)
  183. {
  184. TimeSpan timeData = TimeSpan.FromMilliseconds(leftTime);
  185. VM.EndTimeLabel.text = timeData.ToString(@"hh\:mm\:ss");
  186. }
  187. else
  188. {
  189. ClearStepTimer();
  190. ShowUpgradeState(0);
  191. }
  192. }
  193. private void ShowUpgradeState(int index)
  194. {
  195. VM.UpgradeCtrl.selectedIndex = index;
  196. if (index == 1)
  197. {
  198. ShowWatchAdUI();
  199. }
  200. }
  201. private void RefreshWatchAd(int eventId, object args)
  202. {
  203. if (VM.UpgradeCtrl.selectedIndex == 1)
  204. {
  205. ShowWatchAdUI();
  206. }
  207. }
  208. private void ShowWatchAdUI()
  209. {
  210. // 处于观看广告按钮CD状态中
  211. VM.CdTimeCtrl.selectedIndex = DragonEggData.Instance.WatchADCount < KeyValue.boxUpBillNum ? 0 : 1;
  212. VM.AdNumLabel.text = string.Format(StringDefine.treasureChestWatchADCount, DragonEggData.Instance.WatchADCount, KeyValue.boxUpBillNum);
  213. }
  214. private void ClearStepTimer()
  215. {
  216. if (_upLvTimer != null)
  217. {
  218. _upLvTimer.Cancel();
  219. _upLvTimer = null;
  220. }
  221. }
  222. /// <summary>
  223. /// 刷新宝箱等级界面UI
  224. /// </summary>
  225. /// <param name="eventId"></param>
  226. /// <param name="args"></param>
  227. private void RefreshTreasureChestUI(int eventId, object args)
  228. {
  229. ShowUI();
  230. }
  231. /// <summary>
  232. /// 刷新升级倒计时
  233. /// </summary>
  234. /// <param name="eventId"></param>
  235. /// <param name="args"></param>
  236. private void RefreshUpgradeTime(int eventId, object args)
  237. {
  238. ShowUpgradeTimeUI();
  239. }
  240. /// <summary>
  241. /// 收益页签查看装备品质产出几率按钮事件监听
  242. /// </summary>
  243. /// <param name="eventId"></param>
  244. /// <param name="args"></param>
  245. private void ShowEquipQualityProbability(int eventId, object args)
  246. {
  247. ShowPageUI(0);
  248. }
  249. private void InitUI()
  250. {
  251. VM.GoldResItem.Ctrl.ShowUI(GoldId, ItemData.Instance.GetItemNum(GoldId));
  252. VM.AccelerationResItem.Ctrl.ShowUI(AccelerationId, ItemData.Instance.GetItemNum(AccelerationId));
  253. VM.CostAccelerationResItem.Ctrl.ShowUI(AccelerationId, 1);
  254. VM.CdTimeLabel.text = string.Format(StringDefine.treasureChestADTime, Mathf.FloorToInt(KeyValue.boxUpBillTime / 60));
  255. }
  256. /// <summary>
  257. /// 装备概率UI
  258. /// </summary>
  259. private void ShowEquipProbability(treasureTable curTreasure, treasureTable nextTreasure)
  260. {
  261. var probabilityDataList = new List<ProbabilityParam>();
  262. bool bMaxLv = nextTreasure == null;
  263. for (int i = 0; i < curTreasure.EquipmentProbability.Length; i += 2)
  264. {
  265. probabilityDataList.Add(new ProbabilityParam()
  266. {
  267. quality = curTreasure.EquipmentProbability[i],
  268. curValue = curTreasure.EquipmentProbability[i + 1],
  269. nextValue = bMaxLv ? 0 : nextTreasure.EquipmentProbability[i + 1],
  270. bMaxLv = bMaxLv,
  271. });
  272. }
  273. VM.ProbabilityList.BindDatas(probabilityDataList);
  274. }
  275. /// <summary>
  276. /// 宝箱收益UI
  277. /// </summary>
  278. private void ShowIncome(treasureTable curTreasure, treasureTable nextTreasure)
  279. {
  280. var dropDataList = new List<TreasureChestIncome>()
  281. {
  282. new TreasureChestIncome(){dropId=curTreasure.WoodenChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.WoodenChestDrop[0] },
  283. new TreasureChestIncome(){dropId=curTreasure.BlackIronChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.BlackIronChestDrop[0] },
  284. new TreasureChestIncome(){dropId=curTreasure.PlatinumChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.PlatinumChestDrop[0] },
  285. new TreasureChestIncome(){dropId=curTreasure.ChemistryChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.ChemistryChestDrop[0] },
  286. new TreasureChestIncome(){dropId=curTreasure.PearlsChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.PearlsChestDrop[0] },
  287. };
  288. VM.IncomeList.BindDatas(dropDataList);
  289. }
  290. }
  291. }