LuckyBoxView.cs 15 KB

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