LuckyBoxView.cs 18 KB

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