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 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 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. //羲和
  182. _dressUpObjUIXiHe.ResetSceneObj(100, false, false, null, false);
  183. _dressUpObjUIXiHe.dressUpObj.PutOnSuitCfg(201013, true, null, false, false);
  184. _dressUpObjUIXiHe.UpdateWrapper(holder);
  185. //常曦
  186. _dressUpObjUIChangXi.ResetSceneObj(100, false, false, null, false);
  187. _dressUpObjUIChangXi.dressUpObj.PutOnSuitCfg(201006, true, null, false, false);
  188. _dressUpObjUIChangXi.UpdateWrapper(holder1);
  189. }
  190. }
  191. // private void UpdateTime(object param)
  192. // {
  193. // GTextField txtTime = _ui.m_listBg.GetChildAt(0).asCom.GetChild("txtTime").asTextField;
  194. // txtTime.text = TimeUtil.FormattingTime(LuckyBoxDataManager.Instance.startTime, LuckyBoxDataManager.Instance.startTime);
  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. _curIndex--;
  211. _curIndex = Mathf.Max(0, _curIndex);
  212. _ui.m_listBg.ScrollToView(_curIndex, true);
  213. OnListBgScroll();
  214. }
  215. private void OnBtnRightClick()
  216. {
  217. _curIndex++;
  218. _curIndex = Mathf.Min(_ui.m_listBg.numItems - 1, _curIndex);
  219. _ui.m_listBg.ScrollToView(_curIndex, true);
  220. OnListBgScroll();
  221. }
  222. private void OnListBgScroll()
  223. {
  224. _curIndex = _ui.m_listBg.ChildIndexToItemIndex(0);
  225. LuckyBoxDataManager.Instance.currentBoxId = LuckyBoxDataManager.Instance.luckyBoxIds[_curIndex];
  226. _bgIndex = 0;
  227. _valueBarController.UpdateCJ();
  228. _ui.m_btnLeft.grayed = _curIndex <= 0;
  229. _ui.m_btnRight.grayed = _curIndex >= _ui.m_listBg.numItems - 1;
  230. // if (LuckyBoxDataManager.Instance.currentBoxId == LuckyBoxDataManager.BOX_ID_1)
  231. // {
  232. // Timers.inst.Add(1, 0, CheckTime);
  233. // }
  234. // else
  235. // {
  236. // Timers.inst.Remove(CheckTime);
  237. // }
  238. }
  239. private void CheckTime(object param = null)
  240. {
  241. if (LuckyBoxDataManager.Instance.currentBoxId != LuckyBoxDataManager.BOX_ID_1) return;
  242. long endTime = LuckyBoxDataManager.Instance.endTime;
  243. long curTime = TimeHelper.ServerNow();
  244. TimeUtil.FormattingTime(curTime, endTime, out int num, out string str);
  245. GObject item = _ui.m_listBg.GetChildAt(0);
  246. if (item == null) return;
  247. GObject textField = item.asCom.GetChild("txtTime");
  248. if (textField == null) return;
  249. textField.asTextField.text = string.Format("剩余{0}{1}", num, str);
  250. }
  251. private void updateBoxEffect()
  252. {
  253. if (isActiveBoxOpen)
  254. {
  255. // int index = Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.BOX_ID_1);
  256. // UI_ComListBgItem item = UI_ComListBgItem.Proxy(_ui.m_listBg.GetChildAt(index));
  257. // string resPath = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_bt");
  258. // SceneController.AddObjectToView(_gameObject, _wrapper, item.m_holder, resPath, out _gameObject, out _wrapper);
  259. // string resPath3 = ResPathUtil.GetDressUpAnimationPath("dz_jiyuet");
  260. // SceneController.AddObjectToView(_gameObject3, _wrapper3, item.m_holder1, resPath3, out _gameObject3, out _wrapper3, 120);
  261. // string resPath1 = ResPathUtil.GetViewEffectPath("ui_cj", "ui_cj_sl");
  262. // SceneController.AddObjectToView(_gameObject1, _wrapper1, _ui.m_btnBuyOne.m_holder, resPath1, out _gameObject1, out _wrapper1);
  263. // SceneController.AddObjectToView(_gameObject2, _wrapper2, _ui.m_btnBuyTen.m_holder, resPath1, out _gameObject2, out _wrapper2);
  264. // UI_ComListBgItem.ProxyEnd();
  265. }
  266. }
  267. private void OnClickBtnExChange(EventContext context)
  268. {
  269. GObject obj = context.sender as GObject;
  270. int boxId = (int)obj.data;
  271. int storeId = boxId == LuckyBoxDataManager.BOX_ID_1 ? ConstStoreId.LUCKY_BOX_ACTIVITY_STORE_ID : ConstStoreId.LUCKY_BOX_STORE_ID;
  272. ViewManager.Show(ViewName.CLOTHING_SHOP_VIEW, new object[] { storeId }, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  273. this.Hide();
  274. }
  275. private void OnClickBtnPreview(EventContext context)
  276. {
  277. GObject obj = context.sender as GObject;
  278. int boxId = (int)obj.data;
  279. ViewManager.Show(ViewName.LUCKY_BOX_PRE_SHOW_VIEW, boxId);
  280. }
  281. private void OnClickBtnBuyOne(EventContext context)
  282. {
  283. GObject obj = context.sender as GObject;
  284. int boxId = (int)obj.data;
  285. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  286. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  287. if (boughtCount + LuckyBoxDataManager.ONCE_TIME > luckyBoxCfg.maxCount)
  288. {
  289. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  290. return;
  291. }
  292. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.ONCE_TIME, async () =>
  293. {
  294. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.ONCE_TIME);
  295. if (result)
  296. {
  297. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  298. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  299. }
  300. });
  301. }
  302. private void OnClickBtnBuyTen(EventContext context)
  303. {
  304. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX);
  305. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX) <= 0 && (GuideDataManager.currentGuideId == 0 || GuideDataManager.currentGuideIdIndex != 2))
  306. {
  307. //防止点击太快,在引导开启前就被点击到,导致引导卡死
  308. return;
  309. }
  310. GObject obj = context.sender as GObject;
  311. int boxId = (int)obj.data;
  312. LuckyBoxCfg luckyBoxCfg = LuckyBoxCfgArray.Instance.GetCfg(boxId);
  313. int boughtCount = GameGlobal.myNumericComponent.GetAsInt(luckyBoxCfg.numericType);
  314. if (boughtCount + LuckyBoxDataManager.TEN_TIME > luckyBoxCfg.maxCount)
  315. {
  316. PromptController.Instance.ShowFloatTextPrompt("抽奖次数不足");
  317. return;
  318. }
  319. LuckyBoxDataManager.Instance.CheckItemEnough(boxId, LuckyBoxDataManager.TEN_TIME, async () =>
  320. {
  321. bool result = await LuckyBoxSProxy.ReqGetBonus(boxId, LuckyBoxDataManager.TEN_TIME);
  322. if (result)
  323. {
  324. ViewManager.Show(ViewName.LUCKY_BOX_STAR_VIEW, null, new object[] { ViewName.LUCKY_BOX_VIEW, boxId });
  325. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.ZAI_XING, 2);
  326. }
  327. });
  328. }
  329. protected override void OnHide()
  330. {
  331. base.OnHide();
  332. _valueBarController.OnHide();
  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. }