LuckyBoxView.cs 18 KB

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