LuckyBoxView.cs 17 KB

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