LuckyBoxView.cs 19 KB

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