LuckyBoxView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.costNumTen.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. if (boxId == 1)
  156. {
  157. long endTime = LuckyBoxDataManager.Instance.endTime;
  158. long curTime = TimeHelper.ServerNow();
  159. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  160. (obj as GComponent).GetChild("txtTime").asTextField.text = string.Format("剩余{0}{1}", num, str);
  161. }
  162. }
  163. // private void UpdateTime(object param)
  164. // {
  165. // GTextField txtTime = _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTime").asTextField;
  166. // txtTime.text = TimeUtil.FormattingTime(LuckyBoxDataManager.Instance.startTime, LuckyBoxDataManager.Instance.startTime);
  167. // }
  168. private void UpdateBg(object param)
  169. {
  170. string[] resArr = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxDataManager.Instance.currentBoxId).resArr;
  171. _bgIndex++;
  172. if (_bgIndex >= resArr.Length) _bgIndex = 0;
  173. GLoader loaBg = _ui.m_listBg.GetChildAt(0).asCom.GetChild("loaBg").asLoader;
  174. loaBg.url = ResPathUtil.GetBgImgPath(resArr[_bgIndex]);
  175. }
  176. private void OnBtnLeftClick()
  177. {
  178. _curIndex--;
  179. _curIndex = Mathf.Max(0, _curIndex);
  180. _ui.m_listBg.ScrollToView(_curIndex, true);
  181. OnListBgScroll();
  182. }
  183. private void OnBtnRightClick()
  184. {
  185. _curIndex++;
  186. _curIndex = Mathf.Min(_ui.m_listBg.numItems - 1, _curIndex);
  187. _ui.m_listBg.ScrollToView(_curIndex, true);
  188. OnListBgScroll();
  189. }
  190. private void OnListBgScroll()
  191. {
  192. _curIndex = _ui.m_listBg.ChildIndexToItemIndex(0);
  193. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  194. _bgIndex = 0;
  195. _valueBarController.UpdateCJ();
  196. _ui.m_btnLeft.grayed = _curIndex <= 0;
  197. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  198. if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_1)
  199. {
  200. Timers.inst.Add(1, 0, CheckTime);
  201. }
  202. else
  203. {
  204. Timers.inst.Remove(CheckTime);
  205. }
  206. }
  207. private void CheckTime(object param = null)
  208. {
  209. long endTime = LuckyBoxDataManager.Instance.endTime;
  210. long curTime = TimeHelper.ServerNow();
  211. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  212. _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTime").asTextField.text = string.Format("剩余{0}{1}", num, str);
  213. // _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTimeStr").asTextField.text = string.Format("{0}", str);
  214. }
  215. private void updateBoxEffect()
  216. {
  217. if (isActiveBoxOpen)
  218. {
  219. // int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  220. // UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(index));
  221. // string resPath = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_bt");
  222. // SceneController.AddObjectToView(_gameObject, _wrapper, item.m_holder, resPath, out _gameObject, out _wrapper);
  223. // string resPath3 = ResPathUtil.GetDressUpAnimationPath("dz_jiyuet");
  224. // SceneController.AddObjectToView(_gameObject3, _wrapper3, item.m_holder1, resPath3, out _gameObject3, out _wrapper3, 120);
  225. // string resPath1 = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_sl");
  226. // SceneController.AddObjectToView(_gameObject1, _wrapper1, _ui.m_btnBuyOne.m_holder, resPath1, out _gameObject1, out _wrapper1);
  227. // SceneController.AddObjectToView(_gameObject2, _wrapper2, _ui.m_btnBuyTen.m_holder, resPath1, out _gameObject2, out _wrapper2);
  228. // UI_ComListBgItem.ProxyEnd();
  229. }
  230. }
  231. private void OnClickBtnExChange(EventContext context)
  232. {
  233. GObject obj = context.sender as GObject;
  234. int boxId = (int)obj.data;
  235. int storeId = boxId == LuckyBoxDataManager.BOX_ID_1 ? ConstStoreId.LUCKY_BOX_ACTIVITY_STORE_ID : ConstStoreId.LUCKY_BOX_STORE_ID;
  236. ViewManager.Show(ViewName.CLOTHING_SHOP_VIEW, new object[] { storeId }, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  237. this.Hide();
  238. }
  239. private void OnClickBtnPreview(EventContext context)
  240. {
  241. GObject obj = context.sender as GObject;
  242. int boxId = (int)obj.data;
  243. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW, boxId);
  244. }
  245. private void OnClickBtnBuyOne(EventContext context)
  246. {
  247. GObject obj = context.sender as GObject;
  248. int boxId = (int)obj.data;
  249. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  250. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  251. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  252. {
  253. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  254. return;
  255. }
  256. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  257. {
  258. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  259. if (result)
  260. {
  261. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  262. LogServerHelper.SendNodeLog((int)PlayParticipationEnum.ZAI_XING, 2);
  263. }
  264. });
  265. }
  266. private void OnClickBtnBuyTen(EventContext context)
  267. {
  268. GObject obj = context.sender as GObject;
  269. int boxId = (int)obj.data;
  270. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  271. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  272. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  273. {
  274. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  275. return;
  276. }
  277. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  278. {
  279. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  280. if (result)
  281. {
  282. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  283. LogServerHelper.SendNodeLog((int)PlayParticipationEnum.ZAI_XING, 2);
  284. }
  285. });
  286. }
  287. protected override void OnHide()
  288. {
  289. base.OnHide();
  290. _valueBarController.OnHide();
  291. Timers.inst.Remove(CheckTime);
  292. Timers.inst.Remove(UpdateBg);
  293. Timers.inst.Remove(CheckGuide);
  294. // Timers.inst.Remove(UpdateTime);
  295. }
  296. protected override void RemoveEventListener()
  297. {
  298. base.RemoveEventListener();
  299. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, OnListBgScroll);
  300. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  301. }
  302. private void OnClickBtnBack()
  303. {
  304. this.Hide();
  305. Reset();
  306. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  307. }
  308. private void OnClickBtnHome()
  309. {
  310. GameController.GoBackToMainView();
  311. // Reset();
  312. }
  313. private void Reset()
  314. {
  315. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  316. }
  317. private void CheckGuide(object param)
  318. {
  319. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  320. {
  321. UpdateToCheckGuide(null);
  322. }
  323. else
  324. {
  325. Timers.inst.Remove(CheckGuide);
  326. }
  327. }
  328. protected override void UpdateToCheckGuide(object param)
  329. {
  330. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  331. GButton btnBuyTen = _ui.m_listBg.GetChildAt(0).asCom.GetChild("btnBuyTen").asButton;
  332. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  333. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  334. }
  335. }
  336. }