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(LuckyBoxDataManager.Instance.currentBoxId).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. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  182. _bgIndex = 0;
  183. _valueBarController.UpdateCJ();
  184. _ui.m_btnLeft.grayed = _curIndex <= 0;
  185. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  186. if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_1)
  187. {
  188. Timers.inst.Add(1, 0, CheckTime);
  189. }
  190. else
  191. {
  192. Timers.inst.Remove(CheckTime);
  193. }
  194. }
  195. private void CheckTime(object param = null)
  196. {
  197. long endTime = LuckyBoxDataManager.Instance.endTime;
  198. long curTime = TimeHelper.ServerNow();
  199. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  200. _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTime").asTextField.text = string.Format("{0}", num);
  201. _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTimeStr").asTextField.text = string.Format("{0}", str);
  202. }
  203. private void updateBoxEffect()
  204. {
  205. if (isActiveBoxOpen)
  206. {
  207. // int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  208. // UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(index));
  209. // string resPath = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_bt");
  210. // SceneController.AddObjectToView(_gameObject, _wrapper, item.m_holder, resPath, out _gameObject, out _wrapper);
  211. // string resPath3 = ResPathUtil.GetDressUpAnimationPath("dz_jiyuet");
  212. // SceneController.AddObjectToView(_gameObject3, _wrapper3, item.m_holder1, resPath3, out _gameObject3, out _wrapper3, 120);
  213. // string resPath1 = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_sl");
  214. // SceneController.AddObjectToView(_gameObject1, _wrapper1, _ui.m_btnBuyOne.m_holder, resPath1, out _gameObject1, out _wrapper1);
  215. // SceneController.AddObjectToView(_gameObject2, _wrapper2, _ui.m_btnBuyTen.m_holder, resPath1, out _gameObject2, out _wrapper2);
  216. // UI_ComListBgItem.ProxyEnd();
  217. }
  218. }
  219. private void OnClickBtnExChange(EventContext context)
  220. {
  221. GObject obj = context.sender as GObject;
  222. int boxId = (int)obj.data;
  223. int storeId = boxId == LuckyBoxDataManager.BOX_ID_1 ? ConstStoreId.LUCKY_BOX_ACTIVITY_STORE_ID : ConstStoreId.LUCKY_BOX_STORE_ID;
  224. ViewManager.Show(ViewName.CLOTHING_SHOP_VIEW, new object[] { storeId }, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  225. this.Hide();
  226. }
  227. private void OnClickBtnPreview(EventContext context)
  228. {
  229. GObject obj = context.sender as GObject;
  230. int boxId = (int)obj.data;
  231. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW, boxId);
  232. }
  233. private void OnClickBtnBuyOne(EventContext context)
  234. {
  235. GObject obj = context.sender as GObject;
  236. int boxId = (int)obj.data;
  237. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  238. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  239. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  240. {
  241. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  242. return;
  243. }
  244. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  245. {
  246. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  247. if (result)
  248. {
  249. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  250. LogServerHelper.SendNodeLog((int)PlayParticipationEnum.ZAI_XING, 2);
  251. }
  252. });
  253. }
  254. private void OnClickBtnBuyTen(EventContext context)
  255. {
  256. GObject obj = context.sender as GObject;
  257. int boxId = (int)obj.data;
  258. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  259. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  260. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  261. {
  262. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  263. return;
  264. }
  265. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  266. {
  267. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  268. if (result)
  269. {
  270. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  271. LogServerHelper.SendNodeLog((int)PlayParticipationEnum.ZAI_XING, 2);
  272. }
  273. });
  274. }
  275. protected override void OnHide()
  276. {
  277. base.OnHide();
  278. _valueBarController.OnHide();
  279. Timers.inst.Remove(CheckTime);
  280. Timers.inst.Remove(UpdateBg);
  281. Timers.inst.Remove(CheckGuide);
  282. }
  283. protected override void RemoveEventListener()
  284. {
  285. base.RemoveEventListener();
  286. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, OnListBgScroll);
  287. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  288. }
  289. private void OnClickBtnBack()
  290. {
  291. this.Hide();
  292. Reset();
  293. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  294. }
  295. private void OnClickBtnHome()
  296. {
  297. GameController.GoBackToMainView();
  298. // Reset();
  299. }
  300. private void Reset()
  301. {
  302. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  303. }
  304. private void CheckGuide(object param)
  305. {
  306. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0
  307. || GuideDataManager.IsGuideFinish(ConstGuideId.FREEDOM_DRESS) <= 0)
  308. {
  309. UpdateToCheckGuide(null);
  310. }
  311. else
  312. {
  313. Timers.inst.Remove(CheckGuide);
  314. }
  315. }
  316. protected override void UpdateToCheckGuide(object param)
  317. {
  318. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  319. GButton btnBuyTen = _ui.m_listBg.GetChildAt(0).asCom.GetChild("btnBuyTen").asButton;
  320. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 3, "点击摘取十次。");
  321. GuideController.TryGuide(_ui.m_btnBack, ConstGuideId.FREEDOM_DRESS, 1, "获得一套完整的服装啦,马上去试穿一下。");
  322. }
  323. }
  324. }