LuckyBoxView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 GameObject _gameObject0;
  16. private GameObject _gameObject1;
  17. private GoWrapper _wrapper0;
  18. private GoWrapper _wrapper1;
  19. private DressUpObjUI _dressUpObjUIXiHe;
  20. private DressUpObjUI _dressUpObjUIChangXi;
  21. private bool isActiveBoxOpen = false;
  22. private int _activeBoxId = 0;
  23. private int _curIndex = 0;
  24. public override void Dispose()
  25. {
  26. if (_valueBarController != null)
  27. {
  28. _valueBarController.Dispose();
  29. _valueBarController = null;
  30. }
  31. foreach (int key in _lcukyBoxCtrl.Keys)
  32. {
  33. _lcukyBoxCtrl[key].Dispose();
  34. }
  35. _lcukyBoxCtrl.Clear();
  36. if (_dressUpObjUIXiHe != null)
  37. {
  38. _dressUpObjUIXiHe.Dispose();
  39. _dressUpObjUIXiHe = null;
  40. }
  41. if (_dressUpObjUIChangXi != null)
  42. {
  43. _dressUpObjUIChangXi.Dispose();
  44. _dressUpObjUIChangXi = null;
  45. }
  46. SceneController.DestroyObjectFromView(_gameObject0, _wrapper0);
  47. SceneController.DestroyObjectFromView(_gameObject1, _wrapper1);
  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, OnListBgScroll);
  80. EventAgent.AddEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  81. }
  82. protected override void OnShown()
  83. {
  84. base.OnShown();
  85. LuckyBoxDataManager.Instance.luckyBoxIds.Clear();
  86. LuckyBoxDataManager.Instance.luckyBoxIds.Add(LuckyBoxDataManager.BOX_ID_2);
  87. LuckyBoxDataManager.Instance.luckyBoxIds.Add(LuckyBoxDataManager.BOX_ID_3);
  88. _activeBoxId = 0;
  89. if (LuckyBoxDataManager.Instance.RotatingId > 0)
  90. {
  91. RotatingLuckyBoxCfg rotatingLuckyBox = RotatingLuckyBoxCfgArray.Instance.GetCfg(LuckyBoxDataManager.Instance.RotatingId);
  92. _activeBoxId = rotatingLuckyBox.luckyBoxId;
  93. LuckyBoxDataManager.Instance.endTime = TimeUtil.GetTimestamp(rotatingLuckyBox.endTime);
  94. LuckyBoxDataManager.Instance.luckyBoxIds.Insert(0, _activeBoxId);
  95. }
  96. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  97. if (this.viewData != null)
  98. {
  99. boxId = (int)this.viewData;
  100. }
  101. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0) boxId = LuckyBoxDataManager.BOX_ID_2;
  102. LuckyBoxDataManager.Instance.currentBoxId = boxId;
  103. if (_activeBoxId > 0) Timers.inst.Add(1, 0, CheckTime);
  104. _valueBarController.OnShown();
  105. _valueBarController.Controller(4);
  106. _curIndex = LuckyBoxDataManager.Instance.luckyBoxIds.IndexOf(boxId);
  107. _ui.m_listBg.numItems = LuckyBoxDataManager.Instance.luckyBoxIds.Count;
  108. _ui.m_listBg.ScrollToView(_curIndex);
  109. _ui.m_listBg.scrollPane.decelerationRate = 0.8f;
  110. LuckyBoxDataManager.Instance.luckyBoxIndex = _curIndex;
  111. OnListBgScroll();
  112. updateBoxEffect();
  113. Timers.inst.AddUpdate(CheckGuide);
  114. }
  115. // private string GetListItemResource(int index)
  116. // {
  117. // if (index == 0 && _activeBoxId > 0)
  118. // {
  119. // return "UI://LuckyBox/ComBox";
  120. // }
  121. // else
  122. // {
  123. // int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[index];
  124. // return string.Format("UI://LuckyBox/ComBox_{0}", boxId);
  125. // }
  126. // }
  127. private void RenderListBgItem(int index, GObject obj)
  128. {
  129. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[index];
  130. // LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  131. // LuckyBoxDataManager.Instance.InitData(boxId);
  132. // UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  133. // UI_ComBox1.ProxyEnd();
  134. }
  135. private void OnBtnLeftClick()
  136. {
  137. int index = _curIndex - 1;
  138. // _curIndex--;
  139. index = Mathf.Max(0, index);
  140. _ui.m_listBg.ScrollToView(index, true);
  141. // OnListBgScroll();
  142. }
  143. private void OnBtnRightClick()
  144. {
  145. int index = _curIndex + 1;
  146. // _curIndex++;
  147. index = Mathf.Min(_ui.m_listBg.numItems - 1, index);
  148. _ui.m_listBg.ScrollToView(index, true);
  149. // OnListBgScroll();
  150. }
  151. private void OnListBgScroll()
  152. {
  153. if (_lcukyBoxCtrl.ContainsKey(LuckyBoxDataManager.Instance.currentBoxId)) _lcukyBoxCtrl[LuckyBoxDataManager.Instance.currentBoxId].OnHide();
  154. _curIndex = _ui.m_listBg.GetFirstChildInView();
  155. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  156. if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_3)
  157. {
  158. _valueBarController.Controller(3);
  159. }
  160. else
  161. {
  162. _valueBarController.Controller(4);
  163. }
  164. _valueBarController.UpdateCJ();
  165. UpdateListItem();
  166. _ui.m_btnLeft.grayed = _curIndex <= 0;
  167. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  168. LuckyBoxDataManager.Instance.luckyBoxIndex = _curIndex;
  169. }
  170. private void UpdateListItem()
  171. {
  172. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  173. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  174. LuckyBoxDataManager.Instance.InitData(boxId);
  175. GObject obj = _ui.m_listBg.GetChildAt(_curIndex);
  176. UI_ComBox1 comBox = UI_ComBox1.Proxy(obj);
  177. if (!_lcukyBoxCtrl.ContainsKey(boxId))
  178. {
  179. _lcukyBoxCtrl.Add(boxId, new LuckyBoxController(comBox.m_comModel.target));
  180. }
  181. _lcukyBoxCtrl[LuckyBoxDataManager.Instance.currentBoxId].OnShown(LuckyBoxDataManager.Instance.currentBoxId);
  182. LuckyBoxDataManager.Instance.GetOwnedCount(boxId, out int count, out int totalCount);
  183. comBox.m_imgSpecial.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  184. GGraph holder = comBox.m_btnBuyTen.GetChild("holder").asGraph;
  185. holder.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  186. comBox.m_holder.visible = boxId != LuckyBoxDataManager.BOX_ID_3;
  187. string resPath0 = ResPathUtil.GetViewEffectPath("ui_LuckyBox", "Button_Glow");
  188. SceneController.AddObjectToView(_gameObject0, _wrapper0, holder, resPath0, out _gameObject0, out _wrapper0);
  189. string resPath1 = ResPathUtil.GetViewEffectPath("ui_LuckyBox", "but_text_dc");
  190. SceneController.AddObjectToView(_gameObject1, _wrapper1, comBox.m_holder, resPath1, out _gameObject1, out _wrapper1);
  191. comBox.m_txtOwned.SetVar("v1", "" + count).FlushVars();
  192. comBox.m_txtOwned.SetVar("v2", "" + totalCount).FlushVars();
  193. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(cfg.numericType);
  194. comBox.m_txtRemainTimes.text = string.Format("今日剩余次数:{0}", cfg.maxCount - boughtCount);
  195. comBox.m_comCostOne.m_txtCost.text = cfg.costNum.ToString();
  196. comBox.m_comCostOne.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  197. comBox.m_comCostTen.m_txtCost.text = cfg.costNumTen.ToString();
  198. comBox.m_comCostTen.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  199. if (comBox.m_btnBuyOne.data == null)
  200. {
  201. comBox.m_btnBuyOne.onClick.Add(OnClickBtnBuyOne);
  202. }
  203. comBox.m_btnBuyOne.data = boxId;
  204. if (comBox.m_btnBuyTen.data == null)
  205. {
  206. comBox.m_btnBuyTen.onClick.Add(OnClickBtnBuyTen);
  207. }
  208. comBox.m_btnBuyTen.data = boxId;
  209. if (comBox.m_btnPreview.data == null)
  210. {
  211. comBox.m_btnPreview.onClick.Add(OnClickBtnPreview);
  212. }
  213. comBox.m_btnPreview.data = boxId;
  214. obj.data = boxId;
  215. comBox.m_grpTime.visible = boxId == _activeBoxId;
  216. if (boxId == _activeBoxId)
  217. {
  218. long endTime = LuckyBoxDataManager.Instance.endTime;
  219. long curTime = TimeHelper.ServerNow();
  220. // TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  221. long time = endTime - curTime;
  222. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  223. comBox.m_txtTime.text = string.Format("活动时间:剩余{0}", strTime);
  224. comBox.m_loaTitle.url = "ui://LuckyBox/zx_title_1";
  225. }
  226. else
  227. {
  228. comBox.m_loaTitle.url = string.Format("ui://LuckyBox/zx_title_{0}", boxId);
  229. }
  230. UI_ComBox1.ProxyEnd();
  231. }
  232. private void UpdateEffect()
  233. {
  234. }
  235. private void CheckTime(object param = null)
  236. {
  237. if (LuckyBoxDataManager.Instance.currentBoxId != _activeBoxId) return;
  238. long endTime = LuckyBoxDataManager.Instance.endTime;
  239. long curTime = TimeHelper.ServerNow();
  240. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  241. GObject item = _ui.m_listBg.GetChildAt(0);
  242. if (item == null) return;
  243. GObject textField = item.asCom.GetChild("txtTime");
  244. if (textField == null) return;
  245. long time = endTime - curTime;
  246. string strTime = time > TimeUtil.SECOND_PER_DAY * 100 ? TimeUtil.FormattingTimeTo_DDHHmm(time) : TimeUtil.FormattingTimeTo_HHmmss(time);
  247. textField.asTextField.text = string.Format("活动时间:剩余{0}", strTime);
  248. }
  249. private void updateBoxEffect()
  250. {
  251. if (isActiveBoxOpen)
  252. {
  253. }
  254. }
  255. private void OnClickBtnPreview(EventContext context)
  256. {
  257. GObject obj = context.sender as GObject;
  258. int boxId = (int)obj.data;
  259. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW, boxId);
  260. }
  261. private void OnClickBtnBuyOne(EventContext context)
  262. {
  263. GObject obj = context.sender as GObject;
  264. int boxId = (int)obj.data;
  265. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  266. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  267. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  268. {
  269. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  270. return;
  271. }
  272. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  273. {
  274. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  275. if (result)
  276. {
  277. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  278. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  279. }
  280. });
  281. }
  282. private void OnClickBtnBuyTen(EventContext context)
  283. {
  284. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  285. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  286. {
  287. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  288. return;
  289. }
  290. GObject obj = context.sender as GObject;
  291. int boxId = (int)obj.data;
  292. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  293. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  294. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  295. {
  296. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  297. return;
  298. }
  299. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  300. {
  301. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  302. if (result)
  303. {
  304. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  305. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  306. }
  307. });
  308. }
  309. protected override void OnHide()
  310. {
  311. base.OnHide();
  312. _valueBarController.OnHide();
  313. foreach (int key in _lcukyBoxCtrl.Keys)
  314. {
  315. _lcukyBoxCtrl[key].OnHide();
  316. }
  317. Timers.inst.Remove(CheckTime);
  318. // Timers.inst.Remove(UpdateBg);
  319. Timers.inst.Remove(CheckGuide);
  320. // Timers.inst.Remove(UpdateTime);
  321. }
  322. protected override void RemoveEventListener()
  323. {
  324. base.RemoveEventListener();
  325. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, OnListBgScroll);
  326. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  327. }
  328. private void OnClickBtnBack()
  329. {
  330. Reset();
  331. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  332. }
  333. private void OnClickBtnHome()
  334. {
  335. GameController.GoBackToMainView();
  336. }
  337. private void Reset()
  338. {
  339. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  340. }
  341. private void CheckGuide(object param)
  342. {
  343. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  344. {
  345. UpdateToCheckGuide(null);
  346. }
  347. else
  348. {
  349. Timers.inst.Remove(CheckGuide);
  350. }
  351. }
  352. protected override void UpdateToCheckGuide(object param)
  353. {
  354. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  355. GObject gObject = _ui.m_listBg.GetChildAt(_curIndex);
  356. if (gObject == null) return;
  357. GButton btnBuyTen = gObject.asCom.GetChild("btnBuyTen").asButton;
  358. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  359. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  360. }
  361. protected override void TryCompleteGuide()
  362. {
  363. base.TryCompleteGuide();
  364. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  365. GuideController.TryCompleteGuideIndex(ConstGuideId.LUCKY_BOX, 2);
  366. }
  367. }
  368. }