LuckyBoxView.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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(0.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. if (freeTime > TimeHelper.ClientNow())
  127. {
  128. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  129. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = true;
  130. string strFreeTime = TimeUtil.FormattingTimeTo_HHmmss(freeTime - TimeHelper.ClientNow());
  131. comBox.m_comLuckBoxBtn.m_txtFreeTime.text = string.Format("{0}后免费", strFreeTime);
  132. }
  133. else
  134. {
  135. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 1;
  136. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  137. }
  138. }
  139. //else
  140. //{
  141. // comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  142. // comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  143. //}
  144. UI_ComBox1.ProxyEnd();
  145. }
  146. private void RenderListBgItem(int index, GObject obj)
  147. {
  148. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[index];
  149. }
  150. private void OnBtnLeftClick()
  151. {
  152. int index = _curIndex - 1;
  153. if (index < 0) return;
  154. _ui.m_listBg.ScrollToView(index, true);
  155. }
  156. private void OnBtnRightClick()
  157. {
  158. int index = _curIndex + 1;
  159. // if (index >= LuckyBoxDataManager.Instance.luckyBoxIds.Count) return;
  160. index = Mathf.Min(_ui.m_listBg.numItems - 1, index);
  161. _ui.m_listBg.ScrollToView(index, true);
  162. }
  163. private void OnListBgScroll()
  164. {
  165. if (_lcukyBoxCtrl.ContainsKey(LuckyBoxDataManager.Instance.currentBoxId)) _lcukyBoxCtrl[LuckyBoxDataManager.Instance.currentBoxId].OnHide();
  166. _curIndex = _ui.m_listBg.GetFirstChildInView();
  167. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  168. if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_3)
  169. {
  170. _valueBarController.Controller(3);
  171. }
  172. else
  173. {
  174. _valueBarController.Controller(4);
  175. }
  176. _valueBarController.UpdateCJ();
  177. UpdateListItem();
  178. _ui.m_btnLeft.grayed = _curIndex <= 0;
  179. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  180. LuckyBoxDataManager.Instance.luckyBoxIndex = _curIndex;
  181. }
  182. private void UpdateListItem()
  183. {
  184. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  185. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  186. LuckyBoxDataManager.Instance.InitData(boxId);
  187. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  188. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  189. if (!_lcukyBoxCtrl.ContainsKey(boxId))
  190. {
  191. _lcukyBoxCtrl.Add(boxId, new LuckyBoxController(comBox.m_comModel.target));
  192. }
  193. _lcukyBoxCtrl[LuckyBoxDataManager.Instance.currentBoxId].OnShown(LuckyBoxDataManager.Instance.currentBoxId);
  194. comBox.m_comLuckBoxBtn.m_imgSpecial.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  195. GGraph holder = comBox.m_comLuckBoxBtn.m_btnBuyTen.GetChild("holder").asGraph;
  196. holder.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  197. comBox.m_comLuckBoxBtn.m_holder.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  198. _effectUI1 = EffectUIPool.CreateEffectUI(holder, "ui_LuckyBox", "Button_Glow");
  199. _effectUI2 = EffectUIPool.CreateEffectUI(comBox.m_comLuckBoxBtn.m_holder, "ui_LuckyBox", "but_text_dc");
  200. comBox.m_comLuckBoxBtn.m_comCostTen.m_txtCost.text = cfg.costNumTen.ToString();
  201. comBox.m_comLuckBoxBtn.m_comCostTen.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  202. if (comBox.m_comLuckBoxBtn.m_btnBuyOne.data == null)
  203. {
  204. comBox.m_comLuckBoxBtn.m_btnBuyOne.onClick.Add(OnClickBtnBuyOne);
  205. }
  206. comBox.m_comLuckBoxBtn.m_btnBuyOne.data = boxId;
  207. if (comBox.m_comLuckBoxBtn.m_btnBuyTen.data == null)
  208. {
  209. comBox.m_comLuckBoxBtn.m_btnBuyTen.onClick.Add(OnClickBtnBuyTen);
  210. }
  211. comBox.m_comLuckBoxBtn.m_btnBuyTen.data = boxId;
  212. if (comBox.m_btnPreview.data == null)
  213. {
  214. comBox.m_btnPreview.onClick.Add(OnClickBtnPreview);
  215. }
  216. comBox.m_btnPreview.data = boxId;
  217. obj.data = boxId;
  218. comBox.m_grpTime.visible = boxId == _activeBoxId;
  219. if (boxId == _activeBoxId)
  220. {
  221. long endTime = LuckyBoxDataManager.Instance.endTime;
  222. long curTime = TimeHelper.ServerNow();
  223. // TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  224. long time = endTime - curTime;
  225. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  226. comBox.m_txtTime.text = string.Format("活动时间:剩余{0}", strTime);
  227. comBox.m_loaTitle.url = "ui://LuckyBox/zx_title_1";
  228. }
  229. else
  230. {
  231. comBox.m_loaTitle.url = string.Format("ui://LuckyBox/zx_title_{0}", boxId);
  232. }
  233. UI_ComBox1.ProxyEnd();
  234. UpdateListItemData();
  235. UpdateFreeInfo();
  236. }
  237. private void UpdateListItemData()
  238. {
  239. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  240. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  241. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  242. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  243. LuckyBoxDataManager.Instance.GetOwnedCount(boxId, out int count, out int totalCount);
  244. comBox.m_txtOwned.SetVar("v1", "" + count).FlushVars();
  245. comBox.m_txtOwned.SetVar("v2", "" + totalCount).FlushVars();
  246. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(cfg.numericType);
  247. comBox.m_comLuckBoxBtn.m_txtRemainTimes.text = string.Format("今日剩余次数:{0}", cfg.maxCount - boughtCount);
  248. UI_ComBox1.ProxyEnd();
  249. }
  250. private void UpdateEffect()
  251. {
  252. }
  253. private void CheckTime(object param = null)
  254. {
  255. if (LuckyBoxDataManager.Instance.currentBoxId != _activeBoxId) return;
  256. long endTime = LuckyBoxDataManager.Instance.endTime;
  257. long curTime = TimeHelper.ServerNow();
  258. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  259. GObject item = _ui.m_listBg.GetChildAt(0);
  260. if (item == null) return;
  261. GObject textField = item.asCom.GetChild("txtTime");
  262. if (textField == null) return;
  263. long time = endTime - curTime;
  264. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  265. textField.asTextField.text = string.Format("活动时间:剩余{0}", strTime);
  266. }
  267. private void updateBoxEffect()
  268. {
  269. if (isActiveBoxOpen)
  270. {
  271. }
  272. }
  273. private void OnClickBtnPreview(EventContext context)
  274. {
  275. GObject obj = context.sender as GObject;
  276. int boxId = (int)obj.data;
  277. ViewManager.Show<LuckyBoxPreShowView>(boxId);
  278. }
  279. private void OnClickBtnBuyOne(EventContext context)
  280. {
  281. GObject obj = context.sender as GObject;
  282. int boxId = (int)obj.data;
  283. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  284. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  285. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  286. {
  287. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  288. return;
  289. }
  290. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  291. {
  292. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  293. bool result = false;
  294. if (freeTime > 0 && freeTime > TimeHelper.ClientNow())
  295. result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME, true);
  296. else
  297. result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  298. if (result)
  299. {
  300. ViewManager.Show<LuckyBoxStarView>(null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  301. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  302. }
  303. });
  304. }
  305. private void OnClickBtnBuyTen(EventContext context)
  306. {
  307. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  308. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  309. {
  310. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  311. return;
  312. }
  313. GObject obj = context.sender as GObject;
  314. int boxId = (int)obj.data;
  315. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  316. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  317. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  318. {
  319. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  320. return;
  321. }
  322. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  323. {
  324. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  325. if (result)
  326. {
  327. ViewManager.Show<LuckyBoxStarView>(null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  328. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  329. }
  330. });
  331. }
  332. protected override void OnHide()
  333. {
  334. base.OnHide();
  335. _valueBarController.OnHide();
  336. foreach (int key in _lcukyBoxCtrl.Keys)
  337. {
  338. _lcukyBoxCtrl[key].OnHide();
  339. }
  340. Timers.inst.Remove(OnTimerUpdate);
  341. Timers.inst.Remove(CheckTime);
  342. // Timers.inst.Remove(UpdateBg);
  343. Timers.inst.Remove(CheckGuide);
  344. // Timers.inst.Remove(UpdateTime);
  345. Debug.Log("OnHide: LuckyBoxView");
  346. }
  347. protected override void RemoveEventListener()
  348. {
  349. base.RemoveEventListener();
  350. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, UpdateListItemData);
  351. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, UpdateListItemData);
  352. EventAgent.RemoveEventListener(ConstMessage.LUCKY_BOX_FREE_TIME_CHANGED, UpdateFreeInfo);
  353. }
  354. private void OnClickBtnBack()
  355. {
  356. Reset();
  357. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  358. }
  359. private void OnClickBtnHome()
  360. {
  361. GameController.GoBackToMainView();
  362. }
  363. private void Reset()
  364. {
  365. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  366. }
  367. private void CheckGuide(object param)
  368. {
  369. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  370. {
  371. UpdateToCheckGuide(null);
  372. }
  373. else
  374. {
  375. Timers.inst.Remove(CheckGuide);
  376. }
  377. }
  378. protected override void UpdateToCheckGuide(object param)
  379. {
  380. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  381. GObject gObject = _ui.m_listBg.GetChildAt(_curIndex);
  382. if (gObject == null) return;
  383. GButton btnBuyTen = gObject.asCom.GetChild("btnBuyTen").asButton;
  384. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  385. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  386. }
  387. protected override void TryCompleteGuide()
  388. {
  389. base.TryCompleteGuide();
  390. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  391. GuideController.TryCompleteGuideIndex(ConstGuideId.LUCKY_BOX, 2);
  392. }
  393. private void UpdateFreeInfo()
  394. {
  395. ET.Log.Debug("打印测试-------运行一次ddd---");
  396. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  397. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  398. long freeTime = LuckyBoxDataManager.Instance.GetFreeTime(boxId);
  399. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  400. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  401. if (freeTime > 0)
  402. {
  403. if (freeTime > TimeHelper.ClientNow())
  404. {
  405. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  406. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = true;
  407. }
  408. else
  409. {
  410. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 1;
  411. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  412. }
  413. }
  414. else
  415. {
  416. comBox.m_comLuckBoxBtn.m_comCostOne.m_c1.selectedIndex = 0;
  417. comBox.m_comLuckBoxBtn.m_txtFreeTime.visible = false;
  418. comBox.m_comLuckBoxBtn.m_comCostOne.m_txtCost.text = cfg.costNum.ToString();
  419. comBox.m_comLuckBoxBtn.m_comCostOne.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  420. }
  421. }
  422. }
  423. }