LuckyBoxView.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. using FairyGUI;
  2. using UI.LuckyBox;
  3. using UI.CommonGame;
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using ET;
  8. namespace GFGGame
  9. {
  10. public class LuckyBoxView : BaseView
  11. {
  12. private UI_LuckyBoxUI _ui;
  13. private ValueBarController _valueBarController;
  14. private Dictionary<int, LuckyBoxController> _lcukyBoxCtrl = new Dictionary<int, LuckyBoxController>();
  15. private EffectUI _effectUI1;
  16. private EffectUI _effectUI2;
  17. private DressUpObjUI _dressUpObjUIXiHe;
  18. private DressUpObjUI _dressUpObjUIChangXi;
  19. private bool isActiveBoxOpen = false;
  20. private int _activeBoxId = 0;
  21. private int _curIndex = 0;
  22. public override void Dispose()
  23. {
  24. if (_valueBarController != null)
  25. {
  26. _valueBarController.Dispose();
  27. _valueBarController = null;
  28. }
  29. foreach (int key in _lcukyBoxCtrl.Keys)
  30. {
  31. _lcukyBoxCtrl[key].Dispose();
  32. }
  33. _lcukyBoxCtrl.Clear();
  34. if (_dressUpObjUIXiHe != null)
  35. {
  36. _dressUpObjUIXiHe.Dispose();
  37. _dressUpObjUIXiHe = null;
  38. }
  39. if (_dressUpObjUIChangXi != null)
  40. {
  41. _dressUpObjUIChangXi.Dispose();
  42. _dressUpObjUIChangXi = null;
  43. }
  44. EffectUIPool.Recycle(_effectUI1);
  45. _effectUI1 = null;
  46. EffectUIPool.Recycle(_effectUI2);
  47. _effectUI2 = null;
  48. if (_ui != null)
  49. {
  50. _ui.Dispose();
  51. _ui = null;
  52. }
  53. base.Dispose();
  54. }
  55. protected override void OnInit()
  56. {
  57. base.OnInit();
  58. packageName = UI_LuckyBoxUI.PACKAGE_NAME;
  59. _ui = UI_LuckyBoxUI.Create();
  60. this.viewCom = _ui.target;
  61. isfullScreen = true;
  62. _dressUpObjUIXiHe = new DressUpObjUI("SceneDressUp");
  63. _dressUpObjUIChangXi = new DressUpObjUI("SceneDressUp");
  64. // _ui.m_txtRemainTimes.visible = false;
  65. _valueBarController = new ValueBarController(_ui.m_valueBar);
  66. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  67. // _ui.m_btnHome.onClick.Add(OnClickBtnHome);
  68. _ui.m_btnLeft.onClick.Add(OnBtnLeftClick);
  69. _ui.m_btnRight.onClick.Add(OnBtnRightClick);
  70. // _ui.m_listBg.SetVirtual();
  71. _ui.m_listBg.itemRenderer = RenderListBgItem;
  72. // _ui.m_listBg.itemProvider = GetListItemResource;
  73. _ui.m_listBg.scrollPane.onScrollEnd.Add(OnListBgScroll);
  74. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("zx_bg");
  75. }
  76. protected override void AddEventListener()
  77. {
  78. base.AddEventListener();
  79. EventAgent.AddEventListener(ConstMessage.ITEM_CHANGED, UpdateListItemData);
  80. EventAgent.AddEventListener(ConstMessage.NUMERIC_CHANGE, UpdateListItemData);
  81. EventAgent.AddEventListener(ConstMessage.LUCKY_BOX_FREE_TIME_CHANGED, UpdateFreeInfo);
  82. }
  83. protected override void OnShown()
  84. {
  85. base.OnShown();
  86. Debug.Log("OnShown:LuckyBoxView");
  87. LuckyBoxDataManager.Instance.luckyBoxIds.Clear();
  88. LuckyBoxDataManager.Instance.luckyBoxIds.Add(LuckyBoxDataManager.BOX_ID_2);
  89. LuckyBoxDataManager.Instance.luckyBoxIds.Add(LuckyBoxDataManager.BOX_ID_3);
  90. _activeBoxId = 0;
  91. if (LuckyBoxDataManager.Instance.RotatingId > 0)
  92. {
  93. RotatingLuckyBoxCfg rotatingLuckyBox = RotatingLuckyBoxCfgArray.Instance.GetCfg(LuckyBoxDataManager.Instance.RotatingId);
  94. _activeBoxId = rotatingLuckyBox.luckyBoxId;
  95. LuckyBoxDataManager.Instance.endTime = TimeUtil.GetTimestamp(rotatingLuckyBox.endTime);
  96. LuckyBoxDataManager.Instance.luckyBoxIds.Insert(0, _activeBoxId);
  97. }
  98. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  99. if (this.viewData != null)
  100. {
  101. boxId = (int)this.viewData;
  102. }
  103. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0) boxId = LuckyBoxDataManager.BOX_ID_2;
  104. LuckyBoxDataManager.Instance.currentBoxId = boxId;
  105. if (_activeBoxId > 0) Timers.inst.Add(1, 0, CheckTime);
  106. _valueBarController.OnShown();
  107. _valueBarController.Controller(4);
  108. _curIndex = LuckyBoxDataManager.Instance.luckyBoxIds.IndexOf(boxId);
  109. _ui.m_listBg.numItems = LuckyBoxDataManager.Instance.luckyBoxIds.Count;
  110. _ui.m_listBg.ScrollToView(_curIndex);
  111. _ui.m_listBg.scrollPane.decelerationRate = 0.8f;
  112. LuckyBoxDataManager.Instance.luckyBoxIndex = _curIndex;
  113. OnListBgScroll();
  114. updateBoxEffect();
  115. Timers.inst.AddUpdate(CheckGuide);
  116. Timers.inst.Add(1f, 0, OnTimerUpdate, 1);
  117. }
  118. private void OnTimerUpdate(object param)
  119. {
  120. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  121. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  122. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  123. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  124. if (freeTime > 0)
  125. {
  126. long timeDifference = freeTime - TimeHelper.ClientNow();
  127. if (timeDifference> 0)
  128. {
  129. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  130. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = true;
  131. string strFreeTime = TimeUtil.FormattingTimeTo_HHmmss(timeDifference);
  132. comBox.m_comLuckBoxBtn.m_txtFreeTime.text = string.Format("{0}后免费", strFreeTime);
  133. }
  134. else
  135. {
  136. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 1;
  137. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  138. }
  139. }
  140. UI_ComBox1.ProxyEnd();
  141. }
  142. private void RenderListBgItem(int index, GObject obj)
  143. {
  144. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[index];
  145. }
  146. private void OnBtnLeftClick()
  147. {
  148. int index = _curIndex - 1;
  149. if (index < 0) return;
  150. _ui.m_listBg.ScrollToView(index, true);
  151. }
  152. private void OnBtnRightClick()
  153. {
  154. int index = _curIndex + 1;
  155. // if (index >= LuckyBoxDataManager.Instance.luckyBoxIds.Count) return;
  156. index = Mathf.Min(_ui.m_listBg.numItems - 1, index);
  157. _ui.m_listBg.ScrollToView(index, true);
  158. }
  159. private void OnListBgScroll()
  160. {
  161. if (_lcukyBoxCtrl.ContainsKey(LuckyBoxDataManager.Instance.currentBoxId)) _lcukyBoxCtrl[LuckyBoxDataManager.Instance.currentBoxId].OnHide();
  162. _curIndex = _ui.m_listBg.GetFirstChildInView();
  163. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  164. if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_3)
  165. {
  166. _valueBarController.Controller(3);
  167. }
  168. else
  169. {
  170. _valueBarController.Controller(4);
  171. }
  172. _valueBarController.UpdateCJ();
  173. UpdateListItem();
  174. _ui.m_btnLeft.grayed = _curIndex <= 0;
  175. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  176. LuckyBoxDataManager.Instance.luckyBoxIndex = _curIndex;
  177. }
  178. private void UpdateListItem()
  179. {
  180. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  181. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  182. LuckyBoxDataManager.Instance.InitData(boxId);
  183. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  184. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  185. if (!_lcukyBoxCtrl.ContainsKey(boxId))
  186. {
  187. _lcukyBoxCtrl.Add(boxId, new LuckyBoxController(comBox.m_comModel.target));
  188. }
  189. _lcukyBoxCtrl[LuckyBoxDataManager.Instance.currentBoxId].OnShown(LuckyBoxDataManager.Instance.currentBoxId);
  190. comBox.m_comLuckBoxBtn.m_imgSpecial.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  191. GGraph holder = comBox.m_comLuckBoxBtn.m_btnBuyTen.GetChild("holder").asGraph;
  192. holder.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  193. comBox.m_comLuckBoxBtn.m_holder.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  194. _effectUI1 = EffectUIPool.CreateEffectUI(holder, "ui_LuckyBox", "Button_Glow");
  195. _effectUI2 = EffectUIPool.CreateEffectUI(comBox.m_comLuckBoxBtn.m_holder, "ui_LuckyBox", "but_text_dc");
  196. comBox.m_comLuckBoxBtn.m_comCostTen.m_txtCost.text = cfg.costNumTen.ToString();
  197. comBox.m_comLuckBoxBtn.m_comCostTen.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  198. if (comBox.m_comLuckBoxBtn.m_btnBuyOne.data == null)
  199. {
  200. comBox.m_comLuckBoxBtn.m_btnBuyOne.onClick.Add(OnClickBtnBuyOne);
  201. }
  202. comBox.m_comLuckBoxBtn.m_btnBuyOne.data = boxId;
  203. if (comBox.m_comLuckBoxBtn.m_btnBuyTen.data == null)
  204. {
  205. comBox.m_comLuckBoxBtn.m_btnBuyTen.onClick.Add(OnClickBtnBuyTen);
  206. }
  207. comBox.m_comLuckBoxBtn.m_btnBuyTen.data = boxId;
  208. if (comBox.m_btnPreview.data == null)
  209. {
  210. comBox.m_btnPreview.onClick.Add(OnClickBtnPreview);
  211. }
  212. comBox.m_btnPreview.data = boxId;
  213. obj.data = boxId;
  214. comBox.m_grpTime.visible = boxId == _activeBoxId;
  215. if (boxId == _activeBoxId)
  216. {
  217. long endTime = LuckyBoxDataManager.Instance.endTime;
  218. long curTime = TimeHelper.ServerNow();
  219. // TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  220. long time = endTime - curTime;
  221. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  222. comBox.m_txtTime.text = string.Format("活动时间:剩余{0}", strTime);
  223. comBox.m_loaTitle.url = "ui://LuckyBox/zx_title_1";
  224. }
  225. else
  226. {
  227. comBox.m_loaTitle.url = string.Format("ui://LuckyBox/zx_title_{0}", boxId);
  228. }
  229. UI_ComBox1.ProxyEnd();
  230. UpdateListItemData();
  231. UpdateFreeInfo();
  232. }
  233. private void UpdateListItemData()
  234. {
  235. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  236. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  237. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  238. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  239. LuckyBoxDataManager.Instance.GetOwnedCount(boxId, out int count, out int totalCount);
  240. comBox.m_txtOwned.SetVar("v1", "" + count).FlushVars();
  241. comBox.m_txtOwned.SetVar("v2", "" + totalCount).FlushVars();
  242. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(cfg.numericType);
  243. comBox.m_comLuckBoxBtn.m_txtRemainTimes.text = string.Format("今日剩余次数:{0}", cfg.maxCount - boughtCount);
  244. UI_ComBox1.ProxyEnd();
  245. }
  246. private void UpdateEffect()
  247. {
  248. }
  249. private void CheckTime(object param = null)
  250. {
  251. if (LuckyBoxDataManager.Instance.currentBoxId != _activeBoxId) return;
  252. long endTime = LuckyBoxDataManager.Instance.endTime;
  253. long curTime = TimeHelper.ServerNow();
  254. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  255. GObject item = _ui.m_listBg.GetChildAt(0);
  256. if (item == null) return;
  257. GObject textField = item.asCom.GetChild("txtTime");
  258. if (textField == null) return;
  259. long time = endTime - curTime;
  260. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  261. textField.asTextField.text = string.Format("活动时间:剩余{0}", strTime);
  262. }
  263. private void updateBoxEffect()
  264. {
  265. if (isActiveBoxOpen)
  266. {
  267. }
  268. }
  269. private void OnClickBtnPreview(EventContext context)
  270. {
  271. GObject obj = context.sender as GObject;
  272. int boxId = (int)obj.data;
  273. ViewManager.Show<LuckyBoxPreShowView>(boxId);
  274. }
  275. private void OnClickBtnBuyOne(EventContext context)
  276. {
  277. GObject obj = context.sender as GObject;
  278. int boxId = (int)obj.data;
  279. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  280. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  281. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  282. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  283. {
  284. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  285. return;
  286. }
  287. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  288. {
  289. bool result = false;
  290. if (freeTime > 0 && freeTime < TimeHelper.ClientNow())
  291. result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME, true);
  292. else
  293. result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  294. if (result)
  295. {
  296. ViewManager.Show<LuckyBoxStarView>(null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  297. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  298. }
  299. });
  300. }
  301. private void OnClickBtnBuyTen(EventContext context)
  302. {
  303. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  304. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  305. {
  306. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  307. return;
  308. }
  309. GObject obj = context.sender as GObject;
  310. int boxId = (int)obj.data;
  311. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  312. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  313. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  314. {
  315. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  316. return;
  317. }
  318. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  319. {
  320. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  321. if (result)
  322. {
  323. ViewManager.Show<LuckyBoxStarView>(null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  324. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  325. }
  326. });
  327. }
  328. protected override void OnHide()
  329. {
  330. base.OnHide();
  331. _valueBarController.OnHide();
  332. foreach (int key in _lcukyBoxCtrl.Keys)
  333. {
  334. _lcukyBoxCtrl[key].OnHide();
  335. }
  336. Timers.inst.Remove(OnTimerUpdate);
  337. Timers.inst.Remove(CheckTime);
  338. // Timers.inst.Remove(UpdateBg);
  339. Timers.inst.Remove(CheckGuide);
  340. // Timers.inst.Remove(UpdateTime);
  341. Debug.Log("OnHide: LuckyBoxView");
  342. }
  343. protected override void RemoveEventListener()
  344. {
  345. base.RemoveEventListener();
  346. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, UpdateListItemData);
  347. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, UpdateListItemData);
  348. EventAgent.RemoveEventListener(ConstMessage.LUCKY_BOX_FREE_TIME_CHANGED, UpdateFreeInfo);
  349. }
  350. private void OnClickBtnBack()
  351. {
  352. Reset();
  353. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  354. }
  355. private void OnClickBtnHome()
  356. {
  357. GameController.GoBackToMainView();
  358. }
  359. private void Reset()
  360. {
  361. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  362. }
  363. private void CheckGuide(object param)
  364. {
  365. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  366. {
  367. UpdateToCheckGuide(null);
  368. }
  369. else
  370. {
  371. Timers.inst.Remove(CheckGuide);
  372. }
  373. }
  374. protected override void UpdateToCheckGuide(object param)
  375. {
  376. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  377. GObject gObject = _ui.m_listBg.GetChildAt(_curIndex);
  378. if (gObject == null) return;
  379. GButton btnBuyTen = gObject.asCom.GetChild("btnBuyTen").asButton;
  380. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  381. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  382. }
  383. protected override void TryCompleteGuide()
  384. {
  385. base.TryCompleteGuide();
  386. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  387. GuideController.TryCompleteGuideIndex(ConstGuideId.LUCKY_BOX, 2);
  388. }
  389. private void UpdateFreeInfo()
  390. {
  391. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  392. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  393. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  394. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  395. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  396. if (freeTime > 0)
  397. {
  398. if (freeTime > TimeHelper.ClientNow())
  399. {
  400. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  401. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = true;
  402. }
  403. else
  404. {
  405. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 1;
  406. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  407. }
  408. }
  409. else
  410. {
  411. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  412. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  413. comBox.m_comLuckBoxBtn.m_comCostOne.m_txtCost.text = cfg.costNum.ToString();
  414. comBox.m_comLuckBoxBtn.m_comCostOne.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  415. }
  416. }
  417. }
  418. }