LuckyBoxView.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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.ServerNow();
  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_comCostOne.m_txtCost.text = cfg.costNum.ToString();
  197. comBox.m_comLuckBoxBtn.m_comCostOne.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  198. comBox.m_comLuckBoxBtn.m_comCostTen.m_txtCost.text = cfg.costNumTen.ToString();
  199. comBox.m_comLuckBoxBtn.m_comCostTen.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  200. if (comBox.m_comLuckBoxBtn.m_btnBuyOne.data == null)
  201. {
  202. comBox.m_comLuckBoxBtn.m_btnBuyOne.onClick.Add(OnClickBtnBuyOne);
  203. }
  204. comBox.m_comLuckBoxBtn.m_btnBuyOne.data = boxId;
  205. if (comBox.m_comLuckBoxBtn.m_btnBuyTen.data == null)
  206. {
  207. comBox.m_comLuckBoxBtn.m_btnBuyTen.onClick.Add(OnClickBtnBuyTen);
  208. }
  209. comBox.m_comLuckBoxBtn.m_btnBuyTen.data = boxId;
  210. if (comBox.m_btnPreview.data == null)
  211. {
  212. comBox.m_btnPreview.onClick.Add(OnClickBtnPreview);
  213. }
  214. comBox.m_btnPreview.data = boxId;
  215. obj.data = boxId;
  216. comBox.m_grpTime.visible = boxId == _activeBoxId;
  217. if (boxId == _activeBoxId)
  218. {
  219. long endTime = LuckyBoxDataManager.Instance.endTime;
  220. long curTime = TimeHelper.ServerNow();
  221. // TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  222. long time = endTime - curTime;
  223. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  224. comBox.m_txtTime.text = string.Format("活动时间:剩余{0}", strTime);
  225. comBox.m_loaTitle.url = "ui://LuckyBox/zx_title_1";
  226. }
  227. else
  228. {
  229. comBox.m_loaTitle.url = string.Format("ui://LuckyBox/zx_title_{0}", boxId);
  230. }
  231. UI_ComBox1.ProxyEnd();
  232. UpdateListItemData();
  233. UpdateFreeInfo();
  234. }
  235. private void UpdateListItemData()
  236. {
  237. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  238. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  239. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  240. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  241. LuckyBoxDataManager.Instance.GetOwnedCount(boxId, out int count, out int totalCount);
  242. comBox.m_txtOwned.SetVar("v1", "" + count).FlushVars();
  243. comBox.m_txtOwned.SetVar("v2", "" + totalCount).FlushVars();
  244. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(cfg.numericType);
  245. comBox.m_comLuckBoxBtn.m_txtRemainTimes.text = string.Format("今日剩余次数:{0}", cfg.maxCount - boughtCount);
  246. UI_ComBox1.ProxyEnd();
  247. }
  248. private void UpdateEffect()
  249. {
  250. }
  251. private void CheckTime(object param = null)
  252. {
  253. if (LuckyBoxDataManager.Instance.currentBoxId != _activeBoxId) return;
  254. long endTime = LuckyBoxDataManager.Instance.endTime;
  255. long curTime = TimeHelper.ServerNow();
  256. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  257. GObject item = _ui.m_listBg.GetChildAt(0);
  258. if (item == null) return;
  259. GObject textField = item.asCom.GetChild("txtTime");
  260. if (textField == null) return;
  261. long time = endTime - curTime;
  262. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  263. textField.asTextField.text = string.Format("活动时间:剩余{0}", strTime);
  264. }
  265. private void updateBoxEffect()
  266. {
  267. if (isActiveBoxOpen)
  268. {
  269. }
  270. }
  271. private void OnClickBtnPreview(EventContext context)
  272. {
  273. GObject obj = context.sender as GObject;
  274. int boxId = (int)obj.data;
  275. ViewManager.Show<LuckyBoxPreShowView>(boxId);
  276. }
  277. private void OnClickBtnBuyOne(EventContext context)
  278. {
  279. GObject obj = context.sender as GObject;
  280. int boxId = (int)obj.data;
  281. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  282. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  283. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  284. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  285. {
  286. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  287. return;
  288. }
  289. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  290. {
  291. bool result = false;
  292. if (freeTime > 0 && freeTime < TimeHelper.ServerNow())
  293. result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME, true);
  294. else
  295. result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  296. if (result)
  297. {
  298. ViewManager.Show<LuckyBoxStarView>(null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  299. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  300. }
  301. });
  302. }
  303. private void OnClickBtnBuyTen(EventContext context)
  304. {
  305. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  306. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  307. {
  308. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  309. return;
  310. }
  311. GObject obj = context.sender as GObject;
  312. int boxId = (int)obj.data;
  313. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  314. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  315. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  316. {
  317. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  318. return;
  319. }
  320. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  321. {
  322. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  323. if (result)
  324. {
  325. ViewManager.Show<LuckyBoxStarView>(null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  326. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  327. }
  328. });
  329. }
  330. protected override void OnHide()
  331. {
  332. base.OnHide();
  333. _valueBarController.OnHide();
  334. foreach (int key in _lcukyBoxCtrl.Keys)
  335. {
  336. _lcukyBoxCtrl[key].OnHide();
  337. }
  338. Timers.inst.Remove(OnTimerUpdate);
  339. Timers.inst.Remove(CheckTime);
  340. // Timers.inst.Remove(UpdateBg);
  341. Timers.inst.Remove(CheckGuide);
  342. // Timers.inst.Remove(UpdateTime);
  343. Debug.Log("OnHide: LuckyBoxView");
  344. }
  345. protected override void RemoveEventListener()
  346. {
  347. base.RemoveEventListener();
  348. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, UpdateListItemData);
  349. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, UpdateListItemData);
  350. EventAgent.RemoveEventListener(ConstMessage.LUCKY_BOX_FREE_TIME_CHANGED, UpdateFreeInfo);
  351. }
  352. private void OnClickBtnBack()
  353. {
  354. Reset();
  355. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  356. }
  357. private void OnClickBtnHome()
  358. {
  359. GameController.GoBackToMainView();
  360. }
  361. private void Reset()
  362. {
  363. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  364. }
  365. private void CheckGuide(object param)
  366. {
  367. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  368. {
  369. UpdateToCheckGuide(null);
  370. }
  371. else
  372. {
  373. Timers.inst.Remove(CheckGuide);
  374. }
  375. }
  376. protected override void UpdateToCheckGuide(object param)
  377. {
  378. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  379. GObject gObject = _ui.m_listBg.GetChildAt(_curIndex);
  380. if (gObject == null) return;
  381. GButton btnBuyTen = gObject.asCom.GetChild("btnBuyTen").asButton;
  382. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  383. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  384. }
  385. protected override void TryCompleteGuide()
  386. {
  387. base.TryCompleteGuide();
  388. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  389. GuideController.TryCompleteGuideIndex(ConstGuideId.LUCKY_BOX, 2);
  390. }
  391. private void UpdateFreeInfo()
  392. {
  393. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  394. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  395. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  396. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  397. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  398. if (freeTime > 0)
  399. {
  400. if (freeTime > TimeHelper.ServerNow())
  401. {
  402. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  403. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = true;
  404. }
  405. else
  406. {
  407. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 1;
  408. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  409. }
  410. }
  411. else
  412. {
  413. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  414. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  415. }
  416. }
  417. }
  418. }