DragonEggUpgradeCtrl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 System.Collections.Generic;
  10. using UnityEngine;
  11. using XGame.Database;
  12. using XGame.Framework.Time;
  13. using XGame.Framework.UI;
  14. namespace FL.FGUI
  15. {
  16. /// <summary>
  17. /// UI逻辑处理类
  18. /// </summary>
  19. /// <typeparam name=""></typeparam>
  20. public partial class DragonEggUpgradeCtrl : UIController<DragonEggUpgradeVM>
  21. {
  22. public enum eState
  23. {
  24. fillEnergy = 0, // 填充能量状态
  25. fullEnergy = 1, // 满能量可升阶状态
  26. upgradeStep = 2, // 升阶倒计时状态
  27. maxStep = 3, // 已满级
  28. }
  29. private const int maxQualityLv = 11;// 总共11个品质等级
  30. private EnergyTable _curInfo; // 当前龙蛋等级信息
  31. private GLoader _costItemIcon;
  32. private GTextField _costNumLabel;
  33. private GTextField _watchAdNumLabel;
  34. private ITimer _stepTimer; // 升阶倒计时定时器
  35. protected override void OnEnable(object intent)
  36. {
  37. AddUIListenres();
  38. AddEventListeners();
  39. InitUI();
  40. ShowUI();
  41. }
  42. protected override void OnDisable()
  43. {
  44. if (_curInfo != null) _curInfo = null;
  45. RemoveUIListenres();
  46. RemoveEventListeners();
  47. ClearStepTimer();
  48. }
  49. #region UI事件
  50. private void AddUIListenres()
  51. {
  52. VM.GoldAddBtn.onClick.Add(OnClickGoldAddBtn);
  53. VM.PropAddBtn.onClick.Add(OnClickPropAddBtn);
  54. VM.UpgradeBtn.onClick.Add(OnClickUpgradeBtn);
  55. VM.BuyBtn.onClick.Add(OnClickBuyBtn);
  56. VM.AdBtn.onClick.Add(OnClickAdBtn);
  57. VM.SpeedBtn.onClick.Add(OnClickSpeedBtn);
  58. VM.CloseBtn.onClick.Add(OnClickCloseBtn);
  59. }
  60. private void RemoveUIListenres()
  61. {
  62. VM.GoldAddBtn.onClick.Remove(OnClickGoldAddBtn);
  63. VM.PropAddBtn.onClick.Remove(OnClickPropAddBtn);
  64. VM.UpgradeBtn.onClick.Remove(OnClickUpgradeBtn);
  65. VM.BuyBtn.onClick.Remove(OnClickBuyBtn);
  66. VM.AdBtn.onClick.Remove(OnClickAdBtn);
  67. VM.SpeedBtn.onClick.Remove(OnClickSpeedBtn);
  68. VM.CloseBtn.onClick.Remove(OnClickCloseBtn);
  69. }
  70. private void OnClickGoldAddBtn(EventContext context)
  71. {
  72. }
  73. private void OnClickPropAddBtn(EventContext context)
  74. {
  75. }
  76. private void OnClickUpgradeBtn(EventContext context)
  77. {
  78. DragonEggService.Instance.UpgradeDragonEgg(Context.Time.GetNowTime(), _curInfo.Time * 1000);
  79. }
  80. private void OnClickBuyBtn(EventContext context)
  81. {
  82. DragonEggService.Instance.FillDragonEggEnergy(_curInfo.Expend);
  83. }
  84. //观看广告减少升级时间按钮事件
  85. private void OnClickAdBtn(EventContext context)
  86. {
  87. DragonEggService.Instance.ReduceTimeByWatchAd(Context.Time.GetNowTime());
  88. }
  89. // 加速按钮,打开道具使用界面
  90. private void OnClickSpeedBtn(EventContext context)
  91. {
  92. var needTime = DragonEggData.Instance.UpgradeEndTime - Context.Time.GetNowTime();
  93. var args = new UseItemParam
  94. {
  95. id = DragonEggData.Instance.AccelerateId,
  96. showNum = DragonEggService.Instance.GetAccelerationCardNum(DragonEggData.Instance.AccelerateId, needTime, false),
  97. maxNum = DragonEggService.Instance.GetAccelerationCardNum(DragonEggData.Instance.AccelerateId, needTime),
  98. endTime = DragonEggData.Instance.UpgradeEndTime,
  99. onPromiseCallback = (int num) =>
  100. {
  101. XGame.Log.Warn($"使用加速卡道具后回调函数,使用数量num:{num}");
  102. },
  103. };
  104. Context.UI.OpenAsync(UIKeys.CommonUseItem, args);
  105. }
  106. private void OnClickCloseBtn(EventContext context)
  107. {
  108. Context.ClosePanel();
  109. }
  110. #endregion
  111. private void AddEventListeners()
  112. {
  113. EventSingle.Instance.AddListener(EventDefine.UpdataItemData, UpdataItemData);
  114. EventSingle.Instance.AddListener(EventDefine.FillEnergy, FillEnergy);
  115. EventSingle.Instance.AddListener(EventDefine.UpgradeDragonEgg, UpgradeDragonEgg);
  116. EventSingle.Instance.AddListener(EventDefine.ReduceDragonEggTime, ReduceDragonEggTime);
  117. EventSingle.Instance.AddListener(EventDefine.UpgradeDragonEggSucess, UpgradeDragonEggSucess);
  118. }
  119. private void RemoveEventListeners()
  120. {
  121. EventSingle.Instance.RemoveListener(EventDefine.UpdataItemData, UpdataItemData);
  122. EventSingle.Instance.RemoveListener(EventDefine.FillEnergy, FillEnergy);
  123. EventSingle.Instance.RemoveListener(EventDefine.UpgradeDragonEgg, UpgradeDragonEgg);
  124. EventSingle.Instance.RemoveListener(EventDefine.ReduceDragonEggTime, ReduceDragonEggTime);
  125. EventSingle.Instance.RemoveListener(EventDefine.UpgradeDragonEggSucess, UpgradeDragonEggSucess);
  126. }
  127. private void InitUI()
  128. {
  129. VM.EggQualityList.ListType = EGListType.None;
  130. if (_costItemIcon == null)
  131. {
  132. _costItemIcon = VM.BuyBtn.GetChild("ItemIcon").asLoader;
  133. }
  134. if (_costNumLabel == null)
  135. {
  136. _costNumLabel = VM.BuyBtn.GetChild("NumLabel").asTextField;
  137. }
  138. if (_watchAdNumLabel == null)
  139. {
  140. _watchAdNumLabel = VM.AdBtn.GetChild("NumLabel").asTextField;
  141. }
  142. }
  143. private void ShowUI()
  144. {
  145. _curInfo = EnergyTableRepo.Get(DragonEggData.Instance.EggLv);
  146. EnergyTable nextInfo = EnergyTableRepo.Get(DragonEggData.Instance.EggLv + 1);
  147. ShowEggQualityUI(nextInfo);
  148. ShowUpgradeUI(nextInfo);
  149. }
  150. /// <summary>
  151. /// 道具变化
  152. /// </summary>
  153. /// <param name="eventId"></param>
  154. /// <param name="args"></param>
  155. private void UpdataItemData(int eventId, object args)
  156. {
  157. }
  158. // 是否未解锁的品质
  159. private bool IsLockQuality(int quality)
  160. {
  161. int globalId = DragonEggData.Instance.GetUnlockQuality(quality);
  162. bool bLock = globalId > 0;
  163. if (bLock)
  164. {// 获取全局表的具体解锁条件来判断是否已解锁
  165. //已达解锁条件,移除
  166. if (!bLock)
  167. {
  168. DragonEggData.Instance.RemoveUnlockQualityDic(quality);
  169. }
  170. }
  171. return bLock;
  172. }
  173. /// <summary>
  174. /// 龙蛋品质概率UI
  175. /// </summary>
  176. private void ShowEggQualityUI(EnergyTable nextInfo)
  177. {
  178. bool bMax = nextInfo == null;
  179. int index = 0;
  180. var dataList = new List<EggQualityItemData>();
  181. for (int i = 0; i < maxQualityLv; i++)
  182. {
  183. index = (i + 1) * 2 - 1;
  184. dataList.Add(new EggQualityItemData
  185. {
  186. quality = i + 1,
  187. curVal = _curInfo.Pro_quality[index],
  188. nextVal = bMax ? 0 : nextInfo.Pro_quality[index],
  189. bMaxLv = bMax,
  190. bLock = IsLockQuality(i + 1),
  191. });
  192. }
  193. RefreshEggQualityList(dataList);
  194. }
  195. private void RefreshEggQualityList(List<EggQualityItemData> dataList)
  196. {
  197. VM.EggQualityList.BindDatas(dataList);
  198. }
  199. /// <summary>
  200. /// 升级龙蛋UI
  201. /// </summary>
  202. private void ShowUpgradeUI(EnergyTable nextInfo)
  203. {
  204. bool bMaxLv = nextInfo == null;
  205. if (bMaxLv)
  206. {
  207. VM.State.selectedIndex = (int)eState.maxStep;
  208. VM.MaxLv.text = string.Format(string.Format(StringDefine.dragonEggCurLv, $"{_curInfo.Id}(MAX)"));
  209. }
  210. else
  211. {
  212. ShowLvUI(_curInfo.Id, nextInfo.Id);
  213. if (DragonEggData.Instance.UpgradeEndTime > 0)
  214. {
  215. ShowUpgradeState(eState.upgradeStep);
  216. }
  217. else
  218. {
  219. VM.EggEnergyBar.max = _curInfo.Expend.Length / 2;
  220. ShowEggEnergyBar(DragonEggData.Instance.EggEnergy);
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// 填充龙蛋能量槽事件
  226. /// </summary>
  227. /// <param name="eventId"></param>
  228. /// <param name="args"></param>
  229. private void FillEnergy(int eventId, object args)
  230. {
  231. ShowEggEnergyBar(DragonEggData.Instance.EggEnergy);
  232. }
  233. private void ShowEggEnergyBar(int energy)
  234. {
  235. VM.EggEnergyBar.value = energy;
  236. ShowUpgradeState(energy == VM.EggEnergyBar.max ? eState.fullEnergy : eState.fillEnergy);
  237. }
  238. /// <summary>
  239. /// 龙蛋升级状态
  240. /// </summary>
  241. /// <param name="state"></param>
  242. private void ShowUpgradeState(eState state)
  243. {
  244. VM.State.selectedIndex = (int)state;
  245. if (state == eState.fillEnergy)
  246. {
  247. ShowEggEnergyCostUI();
  248. }
  249. else if (state == eState.upgradeStep)
  250. {
  251. ShowUpgradeStepUI();
  252. }
  253. }
  254. private void ShowLvUI(int curLv, int nextLv)
  255. {
  256. VM.CurLv.text = string.Format(StringDefine.dragonEggCurLv, curLv);
  257. VM.NextLv.text = string.Format(StringDefine.dragonEggCurLv, nextLv);
  258. }
  259. /// <summary>
  260. /// 填充能量消耗
  261. /// </summary>
  262. private void ShowEggEnergyCostUI()
  263. {
  264. if (_curInfo != null)
  265. {
  266. int index = DragonEggData.Instance.EggEnergy * 2;
  267. int itemId = _curInfo.Expend[index];
  268. var itemInfo = ItemTableRepo.Get(itemId);
  269. _costItemIcon.icon = itemInfo.Icon;
  270. long needCount = _curInfo.Expend[index + 1];
  271. long count = ItemData.Instance.GetItemNum(itemId);
  272. _costNumLabel.text = needCount.ToString();
  273. _costNumLabel.color = count < needCount ? Color.red : Color.white;
  274. }
  275. }
  276. /// <summary>
  277. /// 龙蛋升级中(倒计时状态)
  278. /// </summary>
  279. /// <param name="eventId"></param>
  280. /// <param name="args"></param>
  281. private void UpgradeDragonEgg(int eventId, object args)
  282. {
  283. ShowUpgradeState(eState.upgradeStep);
  284. }
  285. private void ShowUpgradeStepUI()
  286. {
  287. ShowAdBtnUI();
  288. ShowLeftStepTime();
  289. if (_stepTimer == null)
  290. {
  291. _stepTimer = Context.Time.AddLooperTimer(500, (int dt) =>
  292. {
  293. ShowLeftStepTime();
  294. });
  295. }
  296. }
  297. /// <summary>
  298. /// 龙蛋升阶倒计时
  299. /// </summary>
  300. private void ShowLeftStepTime()
  301. {
  302. long leftTime = DragonEggData.Instance.UpgradeEndTime - Context.Time.GetNowTime();
  303. if (leftTime > 0)
  304. {
  305. TimeSpan timeData = TimeSpan.FromMilliseconds(leftTime);
  306. VM.NeedTimeLabel.text = timeData.ToString(@"hh\:mm\:ss");
  307. }
  308. else
  309. {
  310. ClearStepTimer();
  311. }
  312. }
  313. private void ClearStepTimer()
  314. {
  315. if (_stepTimer != null)
  316. {
  317. _stepTimer.Cancel();
  318. _stepTimer = null;
  319. }
  320. }
  321. /// <summary>
  322. /// 观看广告按钮ui
  323. /// </summary>
  324. private void ShowAdBtnUI()
  325. {
  326. _watchAdNumLabel.text = $"{DragonEggData.Instance.WatchAdCount}/{DragonEggData.Instance.MaxWatchAdCount}";
  327. VM.AdBtn.enabled = DragonEggData.Instance.WatchAdCount > 0;
  328. }
  329. /// <summary>
  330. /// 减少龙蛋升级时间
  331. /// </summary>
  332. private void ReduceDragonEggTime(int eventId, object args)
  333. {
  334. ShowAdBtnUI();
  335. ShowLeftStepTime();
  336. }
  337. /// <summary>
  338. /// 龙蛋升级成功
  339. /// </summary>
  340. /// <param name="eventId"></param>
  341. /// <param name="args"></param>
  342. private void UpgradeDragonEggSucess(int eventId, object args)
  343. {
  344. ClearStepTimer();
  345. ShowUI();
  346. }
  347. }
  348. }