LuckyBoxView.cs 12 KB

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