LuckyBoxView.cs 17 KB

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