LuckyBoxView.cs 17 KB

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