LuckyBoxView.cs 19 KB

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