TreasureChestReduceTimePanelCtrl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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;
  9. using UnityEngine;
  10. using XGame.Database;
  11. using XGame.Framework.Time;
  12. using XGame.Framework.UI;
  13. namespace FL.FGUI
  14. {
  15. /// <summary>
  16. /// UI逻辑处理类
  17. /// </summary>
  18. /// <typeparam name=""></typeparam>
  19. public partial class TreasureChestReduceTimePanelCtrl : UIController<TreasureChestReduceTimePanelVM>
  20. {
  21. private int _useCount; // 当前要使用的加速卡数量
  22. private int _maxCount; // 拥有的加速卡的数量
  23. private int _needNum; // 完成升级时间需要的加速卡数量
  24. private long _needTime; // 完成升级需要的时间(豪秒)
  25. private int _reduceTime; // 加速卡减少时间(秒)
  26. private long _openUIServerTime; // 打开此界面时所处的当前的时间戳(用于加速类道具打开此界面停留时间比较久时)
  27. private ITimer _upgradeTimer; // 宝箱升级时间倒计时定时器
  28. private const int StayTime = 30000; // 停留时间超过30秒
  29. protected override void OnEnable(object intent)
  30. {
  31. AddUIListenres();
  32. InitUI((int)intent);
  33. _openUIServerTime = Context.Time.GetNowTime();
  34. }
  35. protected override void OnDisable()
  36. {
  37. RemoveUIListenres();
  38. ClearUpgradeTimer();
  39. }
  40. #region UI事件
  41. private void AddUIListenres()
  42. {
  43. VM.AddBtn.onClick.Add(OnClickAddBtn);
  44. VM.SubBtn.onClick.Add(OnClickSubBtn);
  45. VM.UseBtn.onClick.Add(OnClickUseBtn);
  46. }
  47. private void RemoveUIListenres()
  48. {
  49. VM.AddBtn.onClick.Remove(OnClickAddBtn);
  50. VM.SubBtn.onClick.Remove(OnClickSubBtn);
  51. VM.UseBtn.onClick.Remove(OnClickUseBtn);
  52. }
  53. private void OnClickAddBtn(EventContext context)
  54. {
  55. _useCount += 1;
  56. if (_useCount > _needNum) _useCount = _needNum;
  57. ShowUseCount();
  58. }
  59. private void OnClickSubBtn(EventContext context)
  60. {
  61. _useCount -= 1;
  62. if (_useCount < 0) _useCount = 0;
  63. ShowUseCount();
  64. }
  65. private void OnClickUseBtn(EventContext context)
  66. {
  67. var curServerTime = Context.Time.GetNowTime(); // 当前服务器时间戳
  68. if (curServerTime < DragonEggData.Instance.UpLvTime)
  69. {
  70. if (curServerTime - _openUIServerTime > StayTime)
  71. {
  72. ShowUseNum();
  73. }
  74. DragonEggService.Instance.SendToAcceleration(1,_useCount);
  75. }
  76. Context.ClosePanel();
  77. }
  78. #endregion
  79. private void InitUI(int itemId)
  80. {
  81. _maxCount = ItemData.Instance.GetItemNum(itemId); // 当前拥有的道具数量
  82. var itemInfo = ItemTableRepo.Get(itemId);
  83. if (itemInfo != null)
  84. {
  85. VM.ItemIcon.icon = itemInfo.Icon;
  86. _reduceTime = itemInfo.Effect[1];
  87. }
  88. ShowUI();
  89. }
  90. private void ShowUI()
  91. {
  92. ShowUseNum();
  93. ShowUseCount();
  94. ClearUpgradeTimer();
  95. if (_upgradeTimer == null)
  96. {
  97. _upgradeTimer = Context.Time.AddLooperTimer(500, (int dt) =>
  98. {
  99. ShowTimeUI();
  100. });
  101. }
  102. }
  103. private void ClearUpgradeTimer()
  104. {
  105. if (_upgradeTimer != null)
  106. {
  107. _upgradeTimer.Cancel();
  108. _upgradeTimer = null;
  109. }
  110. }
  111. private void ShowUseNum()
  112. {
  113. _needTime = DragonEggData.Instance.UpLvTime - Context.Time.GetNowTime();
  114. _needNum = (int)(_needTime + (_reduceTime - 1) * 1000) / (_reduceTime * 1000);
  115. if (_needNum > _maxCount) _needNum = _maxCount;
  116. _useCount = _needNum;
  117. }
  118. private void ShowBtnState()
  119. {
  120. VM.AddBtn.enabled = _useCount < _needNum;
  121. VM.SubBtn.enabled = _useCount > 0;
  122. VM.UseBtn.enabled = _useCount > 0;
  123. }
  124. private void ShowUseCount()
  125. {
  126. VM.NumLabel.text = $"{_useCount}/{_needNum}";
  127. if (_needNum > 0)ShowTimeUI();
  128. ShowBtnState();
  129. }
  130. private void ShowTimeUI()
  131. {
  132. _needTime = DragonEggData.Instance.UpLvTime - Context.Time.GetNowTime();
  133. if (_needTime < 0)
  134. {
  135. ClearUpgradeTimer();
  136. _useCount = 0;
  137. _needNum = 0;
  138. ShowUseCount();
  139. VM.DescLabel.text = string.Format(StringDefine.treasureChestUpTime, "00:00:00", "00:00:00");
  140. }
  141. else
  142. {
  143. TimeSpan timeData = TimeSpan.FromMilliseconds(_needTime);
  144. TimeSpan reduceTime = TimeSpan.FromSeconds(_useCount * _reduceTime);
  145. VM.DescLabel.text = string.Format(StringDefine.treasureChestUpTime, timeData.ToString(@"hh\:mm\:ss"), reduceTime.ToString("hh\\:mm\\:ss"));
  146. }
  147. }
  148. }
  149. }