LuckyBoxView.cs 18 KB

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