LuckyBoxView.cs 16 KB

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