LuckyBoxView.cs 16 KB

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