LuckyBoxView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. comBox.m_comModel.m_loaBg.url = ResPathUtil.GetBgImgPath(cfg.resArr[_bgIndex]);
  154. if (_lcukyBoxCtrl.Count <= index)
  155. {
  156. _lcukyBoxCtrl.Add(new LuckyBoxController(comBox.m_comModel.target));
  157. if (_ui.m_listBg.ChildIndexToItemIndex(0) == index)
  158. {
  159. _lcukyBoxCtrl[index].OnShown(boxId);
  160. }
  161. }
  162. comBox.m_btnPreview.m_c1.selectedIndex = boxId;
  163. LuckyBoxDataManager.Instance.GetOwnedCount(boxId, out int count, out int totalCount);
  164. comBox.m_txtOwned.SetVar("v1", "" + count).FlushVars();
  165. comBox.m_txtOwned.SetVar("v2", "" + totalCount).FlushVars();
  166. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(cfg.numericType);
  167. comBox.m_txtRemainTimes.text = string.Format("今日剩余次数:{0}", cfg.maxCount - boughtCount);
  168. comBox.m_comCostOne.m_txtCost.text = cfg.costNum.ToString();
  169. comBox.m_comCostOne.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  170. comBox.m_comCostTen.m_txtCost.text = cfg.costNumTen.ToString();
  171. comBox.m_comCostTen.m_loaCost.url = ResPathUtil.GetCommonGameResPath(ItemCfgArray.Instance.GetCfg(cfg.costID).res);
  172. if (comBox.m_btnBuyOne.target.data == null)
  173. {
  174. comBox.m_btnBuyOne.target.onClick.Add(OnClickBtnBuyOne);
  175. }
  176. comBox.m_btnBuyOne.target.data = boxId;
  177. if (comBox.m_btnBuyTen.target.data == null)
  178. {
  179. comBox.m_btnBuyTen.target.onClick.Add(OnClickBtnBuyTen);
  180. }
  181. comBox.m_btnBuyTen.target.data = boxId;
  182. if (comBox.m_btnPreview.target.data == null)
  183. {
  184. comBox.m_btnPreview.target.onClick.Add(OnClickBtnPreview);
  185. }
  186. comBox.m_btnPreview.target.data = boxId;
  187. obj.data = boxId;
  188. if (boxId == _activeBoxId)
  189. {
  190. long endTime = LuckyBoxDataManager.Instance.endTime;
  191. long curTime = TimeHelper.ServerNow();
  192. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  193. comBox.m_txtTime.text = string.Format("剩余{0}{1}", num, str);
  194. }
  195. UI_ComBox.ProxyEnd();
  196. }
  197. // private void UpdateBg(object param)
  198. // {
  199. // string[] resArr = LuckyBoxCfgArray.Instance.GetCfg(LuckyBoxDataManager.Instance.currentBoxId).resArr;
  200. // _bgIndex++;
  201. // if (_bgIndex >= resArr.Length) _bgIndex = 0;
  202. // GObject gObject = _ui.m_listBg.GetChildAt(0);
  203. // if (gObject == null) return;
  204. // GComponent item = gObject.asCom;
  205. // if (item.gameObjectName != "ComBox_2") return;
  206. // GLoader loaBg = item.GetChild("loaBg").asLoader;
  207. // loaBg.url = ResPathUtil.GetBgImgPath(resArr[_bgIndex]);
  208. // }
  209. private void OnBtnLeftClick()
  210. {
  211. int index = _curIndex - 1;
  212. // _curIndex--;
  213. index = Mathf.Max(0, index);
  214. _ui.m_listBg.ScrollToView(index, true);
  215. // OnListBgScroll();
  216. }
  217. private void OnBtnRightClick()
  218. {
  219. int index = _curIndex + 1;
  220. // _curIndex++;
  221. index = Mathf.Min(_ui.m_listBg.numItems - 1, index);
  222. _ui.m_listBg.ScrollToView(index, true);
  223. // OnListBgScroll();
  224. }
  225. private void OnListBgScroll()
  226. {
  227. _lcukyBoxCtrl[_curIndex].OnHide();
  228. _curIndex = _ui.m_listBg.ChildIndexToItemIndex(0);
  229. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  230. _bgIndex = 0;
  231. _valueBarController.UpdateCJ();
  232. _lcukyBoxCtrl[_curIndex].OnShown(LuckyBoxDataManager.Instance.currentBoxId);
  233. _ui.m_btnLeft.grayed = _curIndex <= 0;
  234. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  235. }
  236. private void CheckTime(object param = null)
  237. {
  238. if (LuckyBoxDataManager.Instance.currentBoxId != _activeBoxId) return;
  239. long endTime = LuckyBoxDataManager.Instance.endTime;
  240. long curTime = TimeHelper.ServerNow();
  241. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  242. GObject item = _ui.m_listBg.GetChildAt(0);
  243. if (item == null) return;
  244. GObject textField = item.asCom.GetChild("txtTime");
  245. if (textField == null) return;
  246. textField.asTextField.text = string.Format("剩余{0}{1}", num, str);
  247. }
  248. private void updateBoxEffect()
  249. {
  250. if (isActiveBoxOpen)
  251. {
  252. // int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  253. // UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(index));
  254. // string resPath = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_bt");
  255. // SceneController.AddObjectToView(_gameObject, _wrapper, item.m_holder, resPath, out _gameObject, out _wrapper);
  256. // string resPath3 = ResPathUtil.GetDressUpAnimationPath("dz_jiyuet");
  257. // SceneController.AddObjectToView(_gameObject3, _wrapper3, item.m_holder1, resPath3, out _gameObject3, out _wrapper3, 120);
  258. // string resPath1 = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_sl");
  259. // SceneController.AddObjectToView(_gameObject1, _wrapper1, _ui.m_btnBuyOne.m_holder, resPath1, out _gameObject1, out _wrapper1);
  260. // SceneController.AddObjectToView(_gameObject2, _wrapper2, _ui.m_btnBuyTen.m_holder, resPath1, out _gameObject2, out _wrapper2);
  261. // UI_ComListBgItem.ProxyEnd();
  262. }
  263. }
  264. // private void OnClickBtnExChange(EventContext context)
  265. // {
  266. // GObject obj = context.sender as GObject;
  267. // int boxId = (int)obj.data;
  268. // int storeId = boxId == LuckyBoxDataManager.BOX_ID_1 ? ConstStoreId.LUCKY_BOX_ACTIVITY_STORE_ID : ConstStoreId.LUCKY_BOX_STORE_ID;
  269. // ViewManager.Show(ViewName.CLOTHING_SHOP_VIEW, new object[] { storeId }, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  270. // this.Hide();
  271. // }
  272. private void OnClickBtnPreview(EventContext context)
  273. {
  274. GObject obj = context.sender as GObject;
  275. int boxId = (int)obj.data;
  276. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW, boxId);
  277. }
  278. private void OnClickBtnBuyOne(EventContext context)
  279. {
  280. GObject obj = context.sender as GObject;
  281. int boxId = (int)obj.data;
  282. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  283. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  284. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  285. {
  286. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  287. return;
  288. }
  289. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  290. {
  291. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  292. if (result)
  293. {
  294. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  295. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  296. }
  297. });
  298. }
  299. private void OnClickBtnBuyTen(EventContext context)
  300. {
  301. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  302. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  303. {
  304. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  305. return;
  306. }
  307. GObject obj = context.sender as GObject;
  308. int boxId = (int)obj.data;
  309. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  310. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  311. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  312. {
  313. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  314. return;
  315. }
  316. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  317. {
  318. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  319. if (result)
  320. {
  321. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  322. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  323. }
  324. });
  325. }
  326. protected override void OnHide()
  327. {
  328. base.OnHide();
  329. _valueBarController.OnHide();
  330. for (int i = 0; i < _lcukyBoxCtrl.Count; i++)
  331. {
  332. _lcukyBoxCtrl[i].OnHide();
  333. }
  334. Timers.inst.Remove(CheckTime);
  335. // Timers.inst.Remove(UpdateBg);
  336. Timers.inst.Remove(CheckGuide);
  337. // Timers.inst.Remove(UpdateTime);
  338. }
  339. protected override void RemoveEventListener()
  340. {
  341. base.RemoveEventListener();
  342. EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, OnListBgScroll);
  343. EventAgent.RemoveEventListener(ConstMessage.NUMERIC_CHANGE, OnListBgScroll);
  344. }
  345. private void OnClickBtnBack()
  346. {
  347. Reset();
  348. ViewManager.GoBackFrom(ViewName.LUCKY_BOX_VIEW);
  349. }
  350. private void OnClickBtnHome()
  351. {
  352. GameController.GoBackToMainView();
  353. }
  354. private void Reset()
  355. {
  356. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[0];
  357. }
  358. private void CheckGuide(object param)
  359. {
  360. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0)
  361. {
  362. UpdateToCheckGuide(null);
  363. }
  364. else
  365. {
  366. Timers.inst.Remove(CheckGuide);
  367. }
  368. }
  369. protected override void UpdateToCheckGuide(object param)
  370. {
  371. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  372. GObject gObject = _ui.m_listBg.GetChildAt(0);
  373. if (gObject == null) return;
  374. GButton btnBuyTen = gObject.asCom.GetChild("btnBuyTen").asButton;
  375. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX, 1, "“摘星”里可以通过星辰的力量获得服饰。", -1, true, _ui.target.height - 600);
  376. GuideController.TryGuide(btnBuyTen, ConstGuideId.LUCKY_BOX, 2, "点击摘取十次。");
  377. }
  378. protected override void TryCompleteGuide()
  379. {
  380. base.TryCompleteGuide();
  381. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  382. GuideController.TryCompleteGuideIndex(cfg.id, 2);
  383. }
  384. }
  385. }