LuckyBoxView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using FairyGUI;
  2. using UI.LuckyBox;
  3. using UI.CommonGame;
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace GFGGame
  8. {
  9. public class LuckyBoxView : BaseWindow
  10. {
  11. private UI_LuckyBoxUI _ui;
  12. private ValueBarController _valueBarController;
  13. private GameObject _gameObject;
  14. private GoWrapper _wrapper;
  15. private GameObject _gameObject1;
  16. private GoWrapper _wrapper1;
  17. private GameObject _gameObject2;
  18. private GoWrapper _wrapper2;
  19. private GameObject _gameObject3;
  20. private GoWrapper _wrapper3;
  21. private bool isActiveBoxOpen = false;
  22. private int _selectedIndex = 0;
  23. public override void Dispose()
  24. {
  25. _valueBarController.Dispose();
  26. _valueBarController = null;
  27. base.Dispose();
  28. }
  29. protected override void OnInit()
  30. {
  31. base.OnInit();
  32. packageName = UI_LuckyBoxUI.PACKAGE_NAME;
  33. _ui = UI_LuckyBoxUI.Create();
  34. this.viewCom = _ui.target;
  35. isfullScreen = true;
  36. _ui.m_txtRemainTimes.visible = false;
  37. _valueBarController = new ValueBarController(_ui.m_valueBar);
  38. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  39. _ui.m_btnHome.onClick.Add(OnClickBtnHome);
  40. _ui.m_btnBuyOne.target.onClick.Add(OnClickBtnBuyOne);
  41. _ui.m_btnBuyTen.target.onClick.Add(OnClickBtnBuyTen);
  42. _ui.m_btnPreview.onClick.Add(OnClickBtnPreview);
  43. _ui.m_btnExchange.onClick.Add(OnClickBtnExChange);
  44. _ui.m_btnLeft.onClick.Add(() => { onClickChange(-1); });
  45. _ui.m_btnRight.onClick.Add(() => { onClickChange(1); });
  46. _ui.m_listBg.itemRenderer = RenderListBgItem;
  47. _ui.m_c1.onChanged.Add(OnListBgScroll);//分页控制器
  48. // Reset();//初始默认值
  49. }
  50. protected override void OnShown()
  51. {
  52. base.OnShown();
  53. EventAgent.AddEventListener(ConstMessage.ITEM_CHANGED, UpdateNormal);
  54. EventAgent.AddEventListener(ConstMessage.MONEY_CHANGE, UpdateNormal);
  55. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  56. if (this.viewData != null)
  57. {
  58. object[] datas = (this.viewData as object[]);
  59. if (datas != null && datas.Length > 1)
  60. {
  61. boxId = (int)datas[1];
  62. }
  63. else
  64. {
  65. boxId = (int)this.viewData;
  66. }
  67. }
  68. LuckyBoxBonusDataCache.currentBoxId = boxId;
  69. isActiveBoxOpen = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1) >= 0;
  70. _ui.m_listBg.numItems = LuckyBoxDataManager.Instance.luckyBoxIds.Length;
  71. _ui.m_listBg.ScrollToView(Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxBonusDataCache.currentBoxId));
  72. _ui.m_listBg.scrollPane.decelerationRate = 0.8f;
  73. Timers.inst.Add(1, 0, CheckTime);
  74. _valueBarController.OnShown();
  75. _valueBarController.Controller(4);
  76. onClickChange(0);
  77. updateActiveBox();
  78. }
  79. private void RenderListBgItem(int index, GObject obj)
  80. {
  81. UI_ComListBgItem item = UI_ComListBgItem.Proxy(obj);
  82. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxDataManager.Instance.luckyBoxIds[index]);
  83. item.m_loaBg.url = string.Format("ui://LuckyBox/{0}", cfg.res);
  84. }
  85. private void CheckTime(object param = null)
  86. {
  87. // int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  88. if (!isActiveBoxOpen)
  89. {
  90. //活动未开启
  91. LuckyBoxBonusDataCache.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  92. Timers.inst.Remove(CheckTime);
  93. return;
  94. }
  95. int endTime = LuckyBoxDataManager.Instance.endTime;
  96. int curTime = DateUtils.Instance.GetCurTime();
  97. UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(0)).m_txtTime.text = DateUtils.Instance.getFormatBySecond(endTime - curTime, 1, 1);
  98. }
  99. private void OnListBgScroll()
  100. {
  101. // if (_ui.m_c1.selectedIndex == -1 || _ui.m_listBg.numChildren <= 0) return;
  102. UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(_ui.m_c1.selectedIndex));
  103. LuckyBoxBonusDataCache.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_ui.m_c1.selectedIndex];
  104. item.m_txtTime.visible = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  105. item.m_imgTitle.visible = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  106. item.m_loaImg.visible = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_3;
  107. item.m_holder.visible = isActiveBoxOpen && LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  108. item.m_holder1.visible = isActiveBoxOpen && LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  109. onClickChange(0);
  110. UpdateNormal();
  111. }
  112. private void UpdateNormal()
  113. {
  114. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxBonusDataCache.currentBoxId);
  115. _valueBarController.UpdateCJ(LuckyBoxBonusDataCache.currentBoxId);
  116. _ui.m_comCostOne.m_txtCost.text = luckyBoxCfg.costNum.ToString();
  117. _ui.m_comCostTen.m_txtCost.text = luckyBoxCfg.costNumTen.ToString();
  118. _ui.m_comCostOne.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(luckyBoxCfg.costID).res);
  119. _ui.m_comCostTen.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(luckyBoxCfg.costID).res);
  120. Timers.inst.AddUpdate(UpdateToCheckGuide);
  121. int count = LuckyBoxBonusDataCache.GetOwnedCount();
  122. _ui.m_txtOwned.SetVar("v1", "" + count).FlushVars();
  123. }
  124. private void onClickChange(int count)
  125. {
  126. _ui.m_c1.selectedIndex = _ui.m_c1.selectedIndex + count;
  127. _ui.m_imgSpecial.visible = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  128. _ui.m_btnRight.visible = _ui.m_c1.selectedIndex != _ui.m_listBg.numItems - 1;
  129. _ui.m_btnLeft.visible = _ui.m_c1.selectedIndex != 0;
  130. _ui.m_btnBuyOne.m_holder.visible = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  131. _ui.m_btnBuyTen.m_holder.visible = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1;
  132. }
  133. private void updateActiveBox()
  134. {
  135. if (isActiveBoxOpen)
  136. {
  137. int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  138. UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(index));
  139. string resPath = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_bt");
  140. SceneController.AddObjectToView(_gameObject, _wrapper, item.m_holder, resPath, out _gameObject, out _wrapper);
  141. string resPath3 = ResPathUtil.GetDressUpAnimationPath("dz_jiyuet");
  142. SceneController.AddObjectToView(_gameObject3, _wrapper3, item.m_holder1, resPath3, out _gameObject3, out _wrapper3, 120);
  143. string resPath1 = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_sl");
  144. SceneController.AddObjectToView(_gameObject1, _wrapper1, _ui.m_btnBuyOne.m_holder, resPath1, out _gameObject1, out _wrapper1);
  145. SceneController.AddObjectToView(_gameObject2, _wrapper2, _ui.m_btnBuyTen.m_holder, resPath1, out _gameObject2, out _wrapper2);
  146. }
  147. }
  148. private void OnClickBtnExChange()
  149. {
  150. int storeId = LuckyBoxBonusDataCache.currentBoxId == LuckyBoxDataManager.BOX_ID_1 ? ConstStoreId.LUCKY_BOX_ACTIVITY_STORE_ID : ConstStoreId.LUCKY_BOX_STORE_ID;
  151. ViewManager.Show(ViewName.CLOTHING_SHOP_VIEW, new object[] { storeId }, new object[] { ViewName.LUCKY_BOX_VIEW, LuckyBoxBonusDataCache.currentBoxId });
  152. this.Hide();
  153. }
  154. private void OnClickBtnPreview()
  155. {
  156. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW);
  157. }
  158. private void OnClickBtnBuyOne()
  159. {
  160. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxBonusDataCache.currentBoxId);
  161. LuckyBoxBonusDataCache.CheckItemEnough(luckyBoxCfg.costID, luckyBoxCfg.costNum, 1, () =>
  162. {
  163. ItemDataManager.Remove(luckyBoxCfg.costID, luckyBoxCfg.costNum);
  164. LuckyBoxDataManager.Instance.RewardList = LuckyBoxBonusDataCache.GetBonusList(1);
  165. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, LuckyBoxBonusDataCache.currentBoxId, new object[] { ViewName.LUCKY_BOX_VIEW, LuckyBoxBonusDataCache.currentBoxId });
  166. });
  167. }
  168. private void OnClickBtnBuyTen()
  169. {
  170. // bool result = GuideController.TryCompleteGuide(ConstGuideId.MAIN_UI_BTN_ZHAI_XING);
  171. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxBonusDataCache.currentBoxId);
  172. if (GuideDataManager.GetGuideCount(ConstGuideId.MAIN_UI_BTN_ZHAI_XING) <= 0)
  173. {
  174. Timers.inst.AddUpdate(UpdateToCheckGuide);
  175. ItemDataManager.Remove(luckyBoxCfg.costID, luckyBoxCfg.costNumTen);
  176. LuckyBoxDataManager.Instance.RewardList = LuckyBoxBonusDataCache.GetBonusList(10, true);
  177. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, LuckyBoxBonusDataCache.currentBoxId, new object[] { ViewName.LUCKY_BOX_VIEW, LuckyBoxBonusDataCache.currentBoxId });
  178. }
  179. else
  180. {
  181. //判断消耗
  182. LuckyBoxBonusDataCache.CheckItemEnough(luckyBoxCfg.costID, luckyBoxCfg.costNumTen, 10, () =>
  183. {
  184. ItemDataManager.Remove(luckyBoxCfg.costID, luckyBoxCfg.costNumTen);
  185. LuckyBoxDataManager.Instance.RewardList = LuckyBoxBonusDataCache.GetBonusList(10, false);
  186. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, LuckyBoxBonusDataCache.currentBoxId, new object[] { ViewName.LUCKY_BOX_VIEW, LuckyBoxBonusDataCache.currentBoxId });
  187. });
  188. }
  189. }
  190. private void UpdateToCheckGuide(object param)
  191. {
  192. if (this.viewCom.parent != null)
  193. {
  194. if (GRoot.inst.GetTopWindow() == this.viewCom)
  195. {
  196. if (GuideController.TryGuideByGuideId(_ui.m_btnBuyTen.target, ConstGuideId.MAIN_UI_BTN_ZHAI_XING, 1, true, "点击摘取十次"))
  197. {
  198. Timers.inst.Remove(UpdateToCheckGuide);
  199. }
  200. else
  201. {
  202. if (GuideController.TryGuideByGuideId(_ui.m_btnBack, ConstGuideId.LUCKY_BOX_VIEW_BTN_BACK, 1, true, "获得一套完整的服装啦,马上去试穿一下", 0, ConstGuideId.MAIN_UI_BTN_ZHAI_XING))
  203. {
  204. Timers.inst.Remove(UpdateToCheckGuide);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. protected override void OnHide()
  211. {
  212. base.OnHide();
  213. _valueBarController.OnHide();
  214. Timers.inst.Remove(UpdateToCheckGuide);
  215. Timers.inst.Remove(CheckTime);
  216. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, UpdateNormal);
  217. EventAgent.RemoveEventListener(ConstMessage.MONEY_CHANGE, UpdateNormal);
  218. }
  219. private void OnClickBtnBack()
  220. {
  221. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX_VIEW_BTN_BACK);
  222. this.Hide();
  223. Reset();
  224. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  225. }
  226. private void OnClickBtnHome()
  227. {
  228. GameController.GoBackToMainView();
  229. // Reset();
  230. }
  231. private void Reset()
  232. {
  233. LuckyBoxBonusDataCache.currentBoxId = LuckyBoxDataManager.BOX_ID_1;
  234. }
  235. }
  236. }