LuckyBoxView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 GameObject _gameObject;
  15. private GoWrapper _wrapper;
  16. private GameObject _gameObject1;
  17. private GoWrapper _wrapper1;
  18. private GameObject _gameObject2;
  19. private GoWrapper _wrapper2;
  20. private GameObject _gameObject3;
  21. private GoWrapper _wrapper3;
  22. private GameObject _gameObject4;
  23. private GoWrapper _wrapper4;
  24. private GameObject _gameObject5;
  25. private GoWrapper _wrapper5;
  26. private bool isActiveBoxOpen = false;
  27. private int _bgIndex = 0;
  28. private int _curIndex = 0;
  29. public override void Dispose()
  30. {
  31. if (_valueBarController != null)
  32. {
  33. _valueBarController.Dispose();
  34. _valueBarController = null;
  35. }
  36. SceneController.DestroyObjectFromView(_gameObject, _wrapper);
  37. SceneController.DestroyObjectFromView(_gameObject1, _wrapper1);
  38. SceneController.DestroyObjectFromView(_gameObject2, _wrapper2);
  39. SceneController.DestroyObjectFromView(_gameObject3, _wrapper3);
  40. if (_ui != null)
  41. {
  42. _ui.Dispose();
  43. _ui = null;
  44. }
  45. base.Dispose();
  46. }
  47. protected override void OnInit()
  48. {
  49. base.OnInit();
  50. packageName = UI_LuckyBoxUI.PACKAGE_NAME;
  51. _ui = UI_LuckyBoxUI.Create();
  52. this.viewCom = _ui.target;
  53. isfullScreen = true;
  54. // _ui.m_txtRemainTimes.visible = false;
  55. _valueBarController = new ValueBarController(_ui.m_valueBar);
  56. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  57. _ui.m_btnHome.onClick.Add(OnClickBtnHome);
  58. _ui.m_btnLeft.onClick.Add(OnBtnLeftClick);
  59. _ui.m_btnRight.onClick.Add(OnBtnRightClick);
  60. _ui.m_listBg.SetVirtual();
  61. _ui.m_listBg.itemRenderer = RenderListBgItem;
  62. _ui.m_listBg.itemProvider = GetListItemResource;
  63. _ui.m_listBg.scrollPane.onScrollEnd.Add(OnListBgScroll);
  64. }
  65. protected override void AddEventListener()
  66. {
  67. base.AddEventListener();
  68. EventAgent.AddEventListener(ConstMessage.ITEM_CHANGED, OnListBgScroll);
  69. EventAgent.AddEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  70. }
  71. protected override void OnShown()
  72. {
  73. base.OnShown();
  74. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  75. if (this.viewData != null)
  76. {
  77. object[] datas = (this.viewData as object[]);
  78. if (datas != null && datas.Length > 1)
  79. {
  80. boxId = (int)datas[1];
  81. }
  82. else
  83. {
  84. boxId = (int)this.viewData;
  85. }
  86. }
  87. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0) boxId = LuckyBoxDataManager.BOX_ID_2;
  88. LuckyBoxDataManager.Instance.currentBoxId = boxId;
  89. isActiveBoxOpen = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1) >= 0;
  90. if (isActiveBoxOpen)
  91. {
  92. Timers.inst.Add(1, 0, CheckTime);
  93. }
  94. _valueBarController.OnShown();
  95. _valueBarController.Controller(4);
  96. _curIndex = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, boxId);
  97. _ui.m_listBg.numItems = LuckyBoxDataManager.Instance.luckyBoxIds.Length;
  98. _ui.m_listBg.ScrollToView(_curIndex);
  99. _ui.m_listBg.scrollPane.decelerationRate = 0.8f;
  100. OnListBgScroll();
  101. updateBoxEffect();
  102. Timers.inst.Add(8, 0, UpdateBg);
  103. Timers.inst.AddUpdate(CheckGuide);
  104. }
  105. private string GetListItemResource(int index)
  106. {
  107. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[index];
  108. return string.Format("UI://LuckyBox/ComBox_{0}", boxId);
  109. }
  110. private void RenderListBgItem(int index, GObject obj)
  111. {
  112. int boxId = LuckyBoxDataManager.Instance.luckyBoxIds[index];
  113. LuckyBoxCfg cfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  114. LuckyBoxDataManager.Instance.InitData(boxId);
  115. GLoader loaBg = (obj as GComponent).GetChild("loaBg").asLoader;
  116. loaBg.url = ResPathUtil.GetBgImgPath(cfg.resArr[0]);
  117. GButton btnPreview = (obj as GComponent).GetChild("btnPreview").asButton;
  118. btnPreview.GetController("c1").selectedIndex = boxId;
  119. GButton btnExchange = (obj as GComponent).GetChild("btnExchange").asButton;
  120. btnExchange.GetController("c1").selectedIndex = boxId;
  121. GTextField txtOwned = (obj as GComponent).GetChild("txtOwned").asTextField;
  122. int count = LuckyBoxDataManager.Instance.GetOwnedCount(boxId);
  123. txtOwned.SetVar("v1", "" + count).FlushVars();
  124. GTextField txtRemainTimes = (obj as GComponent).GetChild("txtRemainTimes").asTextField;
  125. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(cfg.numericType);
  126. txtRemainTimes.text = string.Format("今日剩余次数:{0}", cfg.maxCount - boughtCount);
  127. GTextField txtCost = (obj as GComponent).GetChild("comCostOne").asCom.GetChild("txtCost").asTextField;
  128. txtCost.text = cfg.costNum.ToString();
  129. GLoader loaCost = (obj as GComponent).GetChild("comCostOne").asCom.GetChild("loaCost").asLoader;
  130. loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  131. GTextField txtCostTen = (obj as GComponent).GetChild("comCostTen").asCom.GetChild("txtCost").asTextField;
  132. txtCostTen.text = cfg.costNumTen.ToString();
  133. GLoader loaCostTen = (obj as GComponent).GetChild("comCostTen").asCom.GetChild("loaCost").asLoader;
  134. loaCostTen.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  135. GButton btnBuyOne = (obj as GComponent).GetChild("btnBuyOne").asButton;
  136. if (btnBuyOne.data == null)
  137. {
  138. btnBuyOne.onClick.Add(OnClickBtnBuyOne);
  139. }
  140. btnBuyOne.data = boxId;
  141. GButton btnBuyTen = (obj as GComponent).GetChild("btnBuyTen").asButton;
  142. if (btnBuyTen.data == null)
  143. {
  144. btnBuyTen.onClick.Add(OnClickBtnBuyTen);
  145. }
  146. btnBuyTen.data = boxId;
  147. if (btnExchange.data == null)
  148. {
  149. btnExchange.onClick.Add(OnClickBtnExChange);
  150. }
  151. btnExchange.data = boxId;
  152. if (btnPreview.data == null)
  153. {
  154. btnPreview.onClick.Add(OnClickBtnPreview);
  155. }
  156. btnPreview.data = boxId;
  157. obj.data = boxId;
  158. if (boxId == 1)
  159. {
  160. long endTime = LuckyBoxDataManager.Instance.endTime;
  161. long curTime = TimeHelper.ServerNow();
  162. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  163. (obj as GComponent).GetChild("txtTime").asTextField.text = string.Format("剩余{0}{1}", num, str);
  164. GGraph holder = (obj as GComponent).GetChild("holder").asGraph;
  165. GGraph holder1 = (obj as GComponent).GetChild("holder1").asGraph;
  166. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(201013);//羲和
  167. string resPath1 = ResPathUtil.GetDressUpAnimationPath(suitCfg.picRes);
  168. SceneController.AddObjectToView(_gameObject4, _wrapper4, holder, resPath1, out _gameObject4, out _wrapper4);
  169. SuitCfg suitCfg1 = SuitCfgArray.Instance.GetCfg(201006);//常曦
  170. string resPath2 = ResPathUtil.GetDressUpAnimationPath(suitCfg1.picRes);
  171. SceneController.AddObjectToView(_gameObject5, _wrapper5, holder1, resPath2, out _gameObject5, out _wrapper5);
  172. }
  173. }
  174. // private void UpdateTime(object param)
  175. // {
  176. // GTextField txtTime = _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTime").asTextField;
  177. // txtTime.text = TimeUtil.FormattingTime(LuckyBoxDataManager.Instance.startTime, LuckyBoxDataManager.Instance.startTime);
  178. // }
  179. private void UpdateBg(object param)
  180. {
  181. string[] resArr = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxDataManager.Instance.currentBoxId).resArr;
  182. _bgIndex++;
  183. if (_bgIndex >= resArr.Length) _bgIndex = 0;
  184. GLoader loaBg = _ui.m_listBg.GetChildAt(0).asCom.GetChild("loaBg").asLoader;
  185. loaBg.url = ResPathUtil.GetBgImgPath(resArr[_bgIndex]);
  186. }
  187. private void OnBtnLeftClick()
  188. {
  189. _curIndex--;
  190. _curIndex = Mathf.Max(0, _curIndex);
  191. _ui.m_listBg.ScrollToView(_curIndex, true);
  192. OnListBgScroll();
  193. }
  194. private void OnBtnRightClick()
  195. {
  196. _curIndex++;
  197. _curIndex = Mathf.Min(_ui.m_listBg.numItems - 1, _curIndex);
  198. _ui.m_listBg.ScrollToView(_curIndex, true);
  199. OnListBgScroll();
  200. }
  201. private void OnListBgScroll()
  202. {
  203. _curIndex = _ui.m_listBg.ChildIndexToItemIndex(0);
  204. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  205. _bgIndex = 0;
  206. _valueBarController.UpdateCJ();
  207. _ui.m_btnLeft.grayed = _curIndex <= 0;
  208. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  209. if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_1)
  210. {
  211. Timers.inst.Add(1, 0, CheckTime);
  212. }
  213. else
  214. {
  215. Timers.inst.Remove(CheckTime);
  216. }
  217. }
  218. private void CheckTime(object param = null)
  219. {
  220. long endTime = LuckyBoxDataManager.Instance.endTime;
  221. long curTime = TimeHelper.ServerNow();
  222. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  223. _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTime").asTextField.text = string.Format("剩余{0}{1}", num, str);
  224. // _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTimeStr").asTextField.text = string.Format("{0}", str);
  225. }
  226. private void updateBoxEffect()
  227. {
  228. if (isActiveBoxOpen)
  229. {
  230. // int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  231. // UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(index));
  232. // string resPath = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_bt");
  233. // SceneController.AddObjectToView(_gameObject, _wrapper, item.m_holder, resPath, out _gameObject, out _wrapper);
  234. // string resPath3 = ResPathUtil.GetDressUpAnimationPath("dz_jiyuet");
  235. // SceneController.AddObjectToView(_gameObject3, _wrapper3, item.m_holder1, resPath3, out _gameObject3, out _wrapper3, 120);
  236. // string resPath1 = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_sl");
  237. // SceneController.AddObjectToView(_gameObject1, _wrapper1, _ui.m_btnBuyOne.m_holder, resPath1, out _gameObject1, out _wrapper1);
  238. // SceneController.AddObjectToView(_gameObject2, _wrapper2, _ui.m_btnBuyTen.m_holder, resPath1, out _gameObject2, out _wrapper2);
  239. // UI_ComListBgItem.ProxyEnd();
  240. }
  241. }
  242. private void OnClickBtnExChange(EventContext context)
  243. {
  244. GObject obj = context.sender as GObject;
  245. int boxId = (int)obj.data;
  246. int storeId = boxId == LuckyBoxDataManager.BOX_ID_1 ? ConstStoreId.LUCKY_BOX_ACTIVITY_STORE_ID : ConstStoreId.LUCKY_BOX_STORE_ID;
  247. ViewManager.Show(ViewName.CLOTHING_SHOP_VIEW, new object[] { storeId }, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  248. this.Hide();
  249. }
  250. private void OnClickBtnPreview(EventContext context)
  251. {
  252. GObject obj = context.sender as GObject;
  253. int boxId = (int)obj.data;
  254. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW, boxId);
  255. }
  256. private void OnClickBtnBuyOne(EventContext context)
  257. {
  258. GObject obj = context.sender as GObject;
  259. int boxId = (int)obj.data;
  260. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  261. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  262. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  263. {
  264. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  265. return;
  266. }
  267. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  268. {
  269. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  270. if (result)
  271. {
  272. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  273. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  274. }
  275. });
  276. }
  277. private void OnClickBtnBuyTen(EventContext context)
  278. {
  279. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  280. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  281. {
  282. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  283. return;
  284. }
  285. GObject obj = context.sender as GObject;
  286. int boxId = (int)obj.data;
  287. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  288. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  289. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  290. {
  291. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  292. return;
  293. }
  294. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  295. {
  296. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  297. if (result)
  298. {
  299. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  300. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  301. }
  302. });
  303. }
  304. protected override void OnHide()
  305. {
  306. base.OnHide();
  307. _valueBarController.OnHide();
  308. Timers.inst.Remove(CheckTime);
  309. Timers.inst.Remove(UpdateBg);
  310. Timers.inst.Remove(CheckGuide);
  311. // Timers.inst.Remove(UpdateTime);
  312. }
  313. protected override void RemoveEventListener()
  314. {
  315. base.RemoveEventListener();
  316. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, OnListBgScroll);
  317. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  318. }
  319. private void OnClickBtnBack()
  320. {
  321. Reset();
  322. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  323. }
  324. private void OnClickBtnHome()
  325. {
  326. GameController.GoBackToMainView();
  327. }
  328. private void Reset()
  329. {
  330. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  331. }
  332. private void CheckGuide(object param)
  333. {
  334. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  335. {
  336. UpdateToCheckGuide(null);
  337. GRoot.inst.touchable = true;
  338. }
  339. else
  340. {
  341. Timers.inst.Remove(CheckGuide);
  342. }
  343. }
  344. protected override void UpdateToCheckGuide(object param)
  345. {
  346. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  347. GButton btnBuyTen = _ui.m_listBg.GetChildAt(0).asCom.GetChild("btnBuyTen").asButton;
  348. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  349. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  350. }
  351. protected override void TryCompleteGuide()
  352. {
  353. base.TryCompleteGuide();
  354. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  355. GuideController.TryCompleteGuideIndex(cfg.id, 2);
  356. }
  357. }
  358. }