ActivityGetYuanXiaoView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. using System.Collections;
  2. using UnityEngine;
  3. using UI.ActivityGetYuanXiao;
  4. using FairyGUI;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using cfg.GfgCfg;
  9. namespace GFGGame
  10. {
  11. public class ActivityGetYuanXiaoView : BaseView
  12. {
  13. private UI_ActivityGetYuanXiaoUI _ui;
  14. private List<YuanXiaoItem> items = new List<YuanXiaoItem>();
  15. private PickUpGameCfg _cfg;
  16. private int _activityID;
  17. private int _countTime;
  18. private int _score;
  19. private Dictionary<int, int> _collectDict;
  20. private Vector3 _catcherInitPos;
  21. private bool _firstIn = true;
  22. private bool _gamePause = false;
  23. private float _timer;
  24. private Dictionary<string, EffectUI> _effectUIDic = new Dictionary<string, EffectUI>();
  25. public override void Dispose()
  26. {
  27. // Clear Effect
  28. foreach (var v in _effectUIDic)
  29. {
  30. EffectUIPool.Recycle(v.Value);
  31. }
  32. _effectUIDic.Clear();
  33. if (_ui != null)
  34. {
  35. _ui.Dispose();
  36. }
  37. _ui = null;
  38. base.Dispose();
  39. }
  40. protected override void OnInit()
  41. {
  42. base.OnInit();
  43. packageName = UI_ActivityGetYuanXiaoUI.PACKAGE_NAME;
  44. _ui = UI_ActivityGetYuanXiaoUI.Create();
  45. viewCom = _ui.target;
  46. isReturnView = true;
  47. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("xyx_clfg_bg1");
  48. _ui.m_collectList.itemRenderer = CollectListRenderer;
  49. _ui.m_btnBack.onClick.Add(OnBtnBack);
  50. AddEffect();
  51. }
  52. protected override void OnShown()
  53. {
  54. base.OnShown();
  55. object[] arr = viewData as object[];
  56. _cfg = (PickUpGameCfg)arr[0];
  57. _activityID = (int)arr[1];
  58. if (_firstIn)
  59. {
  60. _firstIn = false;
  61. _catcherInitPos = _ui.m_catcher.target.position;
  62. }
  63. ShowGameView();
  64. }
  65. protected override void OnHide()
  66. {
  67. base.OnHide();
  68. HideGameView();
  69. }
  70. protected override void AddEventListener()
  71. {
  72. base.AddEventListener();
  73. EventAgent.AddEventListener(ConstMessage.ACTIVITY_GETYUANXIAO_RESET, ResetGame);
  74. }
  75. protected override void RemoveEventListener()
  76. {
  77. base.RemoveEventListener();
  78. EventAgent.RemoveEventListener(ConstMessage.ACTIVITY_GETYUANXIAO_RESET, ResetGame);
  79. }
  80. /// <summary>
  81. /// 初始化元宵的FGUI组件
  82. /// </summary>
  83. /// <returns></returns>
  84. YuanXiaoItem InitYuanXiaoItemUICom()
  85. {
  86. GComponent gcom = UIPackage.CreateObject("ActivityGetYuanXiao", "YuanXiaoItem").asCom;
  87. _ui.m_YuanXiaoParent.target.AddChild(gcom);
  88. YuanXiaoItem item = new YuanXiaoItem();
  89. item.CreateItem(gcom, _ui.m_catcher.m_catcher, this);
  90. return item;
  91. }
  92. public void ClearYuanXiaoList()
  93. {
  94. for (int i = 0; i < items.Count; i++)
  95. {
  96. items[i].Destroy();
  97. }
  98. items.Clear();
  99. }
  100. /// <summary>
  101. /// 从对象池中取出对象
  102. /// </summary>
  103. /// <param name="type"></param>
  104. /// <returns></returns>
  105. public YuanXiaoItem GetYuanXiaoItem(int type)
  106. {
  107. YuanXiaoItem item = items.Find(x => x.GetVisible() == false);
  108. if (item == null)
  109. {
  110. item = InitYuanXiaoItemUICom();
  111. }
  112. item.Init(type);
  113. return item;
  114. }
  115. public void RestoreYuanXiaoItem(YuanXiaoItem item)
  116. {
  117. item.SetVisible(false);
  118. }
  119. /// <summary>
  120. /// 生成元宵
  121. /// </summary>
  122. /// <param name="param"></param>
  123. public void CreateItems(object param)
  124. {
  125. if (!isShowing)
  126. {
  127. return;
  128. }
  129. int num = _cfg.ResId.Count;
  130. List<int> roadIndexs = GetRandomRoads(num);
  131. for (int i = 0; i < num; i++)
  132. {
  133. PickUpCfg pickUpCfg =
  134. CommonDataManager.Tables.TblPickUpCfg.DataList.Where(a => a.Id == _cfg.Res[0]).ToList()[
  135. _cfg.ResId[i] - 1];
  136. float time = pickUpCfg.Speed * 1f / 1000;
  137. YuanXiaoItem item = GetYuanXiaoItem(_cfg.ResId[i]);
  138. items.Add(item);
  139. int score = pickUpCfg.Score;
  140. item.SetScore(score);
  141. Vector3 startPos = _ui.target.GetChild("start" + roadIndexs[i]).position;
  142. Vector3 endPos = startPos;
  143. endPos.y = _ui.m_end0.y;
  144. item.Move(startPos, endPos, time, () => RestoreYuanXiaoItem(item));
  145. }
  146. }
  147. /// <summary>
  148. /// 随机路径
  149. /// </summary>
  150. /// <param name="count"></param>
  151. /// <returns></returns>
  152. private List<int> GetRandomRoads(int count)
  153. {
  154. List<int> resultList = new List<int>();
  155. List<int> list = new List<int>();
  156. for (int i = 0; i < 4; i++)
  157. {
  158. list.Add(i);
  159. }
  160. for (int j = 0; j < count; j++)
  161. {
  162. int randomIndex = Random.Range(0, list.Count);
  163. resultList.Add(list[randomIndex]);
  164. list.RemoveAt(randomIndex);
  165. }
  166. return resultList;
  167. }
  168. /// <summary>
  169. /// 接住元宵
  170. /// </summary>
  171. /// <param name="item"></param>
  172. public void GetYuanXiao(YuanXiaoItem item)
  173. {
  174. // 回收组件
  175. RestoreYuanXiaoItem(item);
  176. if (_countTime == 0)
  177. {
  178. return;
  179. }
  180. // 显示得分标志
  181. _score += item.GetScore();
  182. _ui.m_score.text = _score.ToString();
  183. if (_ui.m_c1.selectedIndex == 0)
  184. {
  185. UpdateCollectProgress(item.GetYuanXiaoType());
  186. }
  187. else
  188. {
  189. UpdateScore();
  190. }
  191. ShowFlyScore(item.GetScore());
  192. _ui.m_catcher.m_getEffect.visible = false;
  193. _ui.m_catcher.m_getEffect.visible = true;
  194. }
  195. private void ShowFlyScore(int score)
  196. {
  197. UI_flyScore flyItem = UI_flyScore.Proxy(_ui.m_catcher.m_flyscore.target);
  198. flyItem.m_score.text = score.ToString();
  199. flyItem.m_t0.Stop();
  200. _ui.m_catcher.m_flyscore.target.visible = true;
  201. flyItem.m_t0.Play(() => { _ui.m_catcher.m_flyscore.target.visible = false; });
  202. UI_flyScore.ProxyEnd();
  203. }
  204. public void SetDragRect()
  205. {
  206. _ui.m_catcher.target.visible = true;
  207. _ui.m_catcher.target.draggable = true;
  208. //_ui.m_catcher.target.dragBounds = new Rect(-_ui.m_catcher.target.width / 2, _ui.m_catcher.target.y, Stage.inst.width + _ui.m_catcher.target.width, 0);
  209. Vector2 dragLeftPos = _ui.m_startLeft.LocalToGlobal(Vector2.zero) / (UIContentScaler.scaleFactor);
  210. Vector2 dragRightPos = _ui.m_startRight.LocalToGlobal(Vector2.zero) / (UIContentScaler.scaleFactor);
  211. _ui.m_catcher.target.dragBounds = new Rect(dragLeftPos.x, _ui.m_catcher.target.y,
  212. _ui.m_startRight.x - _ui.m_startLeft.x, 0);
  213. }
  214. private void StartGame()
  215. {
  216. CreateItems(null);
  217. UpdateTime(null);
  218. Timers.inst.Add(1, 0, UpdateTime);
  219. Timers.inst.AddUpdate(UpdateGame);
  220. Timers.inst.Add(1, 0, CheckActivityEnd);
  221. }
  222. private async void PlayStartAnim()
  223. {
  224. GRoot.inst.touchable = false;
  225. _ui.m_startEffect.visible = false;
  226. _ui.m_startEffect.visible = true;
  227. await Task.Delay(820);
  228. GRoot.inst.touchable = true;
  229. StartGame();
  230. }
  231. private void UpdateTime(object param)
  232. {
  233. if (!isShowing)
  234. {
  235. return;
  236. }
  237. _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
  238. --_countTime;
  239. if (_countTime < 0)
  240. {
  241. Timers.inst.Remove(UpdateTime);
  242. bool win = true;
  243. if (_ui.m_c1.selectedIndex == 0)
  244. {
  245. foreach (var v in _collectDict)
  246. {
  247. if (v.Value > 0)
  248. {
  249. win = false;
  250. break;
  251. }
  252. }
  253. }
  254. else
  255. {
  256. if (_score < _cfg.TargetScore)
  257. {
  258. win = false;
  259. }
  260. }
  261. GameEnd(win);
  262. }
  263. }
  264. private async void GameEnd(bool win)
  265. {
  266. //_ui.m_catcher.target.draggable = false;
  267. // 游戏结束时隐藏catcher,同时也作为游戏结束的标志
  268. _ui.m_catcher.target.visible = false;
  269. HideGameView();
  270. if (win)
  271. {
  272. ViewManager.Show<ModalStatusView>("加载中...");
  273. // 请求游戏结束协议
  274. await MiniGameProxy.ReqMiniGameEnd(_cfg.Id, _cfg.Type, 0, true, _activityID, false);
  275. ViewManager.Hide<ModalStatusView>();
  276. ViewManager.Show<ActivityGetYuanXiaoSuccessView>(_cfg);
  277. }
  278. else
  279. {
  280. ViewManager.Show<ActivityGetYuanXiaoFailView>();
  281. }
  282. }
  283. private void InitData()
  284. {
  285. _gamePause = false;
  286. _timer = 0;
  287. _countTime = _cfg.Time;
  288. _score = 0;
  289. _collectDict = new Dictionary<int, int>();
  290. items = new List<YuanXiaoItem>();
  291. }
  292. private void InitView()
  293. {
  294. _ui.m_startEffect.visible = false;
  295. _ui.m_catcher.m_getEffect.visible = false;
  296. _ui.m_catcher.m_flyscore.target.visible = false;
  297. _ui.m_catcher.m_c2.selectedIndex =
  298. _cfg.Id > CommonDataManager.Tables.TblPickUpGameCfg.DataList[0].Id ? 1 : 0;
  299. _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
  300. _ui.m_score.text = _score.ToString();
  301. _ui.m_catcher.target.position = _catcherInitPos;
  302. InitTarget();
  303. }
  304. private void InitTarget()
  305. {
  306. // 分数
  307. if (_cfg.TargetId.Count == 0)
  308. {
  309. _ui.m_targetScore.text = _cfg.TargetScore.ToString();
  310. _ui.m_c1.selectedIndex = 1;
  311. UpdateScore();
  312. }
  313. // 收集
  314. else
  315. {
  316. for (int i = 0; i < _cfg.TargetId.Count; i++)
  317. {
  318. _collectDict.Add(_cfg.TargetId[i].Key, _cfg.TargetId[i].Val);
  319. }
  320. _ui.m_c1.selectedIndex = 0;
  321. _ui.m_collectList.numItems = _cfg.TargetId.Count;
  322. }
  323. }
  324. private void UpdateScore()
  325. {
  326. _ui.m_progress.SetVar("cur", _score.ToString()).SetVar("target", _cfg.TargetScore.ToString()).FlushVars();
  327. if (_ui.m_catcher.target.visible && _score >= _cfg.TargetScore)
  328. {
  329. GameEnd(true);
  330. }
  331. }
  332. private void UpdateCollectProgress(int collectType)
  333. {
  334. if (!_collectDict.ContainsKey(collectType))
  335. {
  336. return;
  337. }
  338. if (_collectDict[collectType] > 0)
  339. {
  340. --_collectDict[collectType];
  341. }
  342. _ui.m_collectList.numItems = _cfg.TargetId.Count;
  343. foreach (var v in _collectDict)
  344. {
  345. if (v.Value > 0)
  346. {
  347. return;
  348. }
  349. }
  350. if (_ui.m_catcher.target.visible)
  351. {
  352. GameEnd(true);
  353. }
  354. }
  355. private void CollectListRenderer(int index, GObject item)
  356. {
  357. UI_targetItem target = UI_targetItem.Proxy(item);
  358. target.m_iconType.url = string.Format("ui://ActivityGetYuanXiao/clfg_mb_{0}", _cfg.TargetId[index].Key);
  359. int num = _collectDict[_cfg.TargetId[index].Key];
  360. if (num == 0)
  361. {
  362. target.m_c1.selectedIndex = 1;
  363. }
  364. else
  365. {
  366. target.m_c1.selectedIndex = 0;
  367. target.m_num.text = num.ToString();
  368. }
  369. UI_targetItem.ProxyEnd();
  370. }
  371. private void ShowGameView()
  372. {
  373. InitData();
  374. InitView();
  375. SetDragRect();
  376. PlayStartAnim();
  377. }
  378. private void HideGameView()
  379. {
  380. Timers.inst.Remove(UpdateTime);
  381. Timers.inst.Remove(UpdateGame);
  382. Timers.inst.Remove(CheckActivityEnd);
  383. ClearYuanXiaoList();
  384. }
  385. private void ResetGame()
  386. {
  387. HideGameView();
  388. ShowGameView();
  389. }
  390. private void OnBtnBack()
  391. {
  392. // 暂停游戏
  393. PauseGame();
  394. AlertMiniGame alertInfo = new AlertMiniGame();
  395. alertInfo.desc = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  396. alertInfo.btnConfirmClickAction = () => { Hide(); };
  397. alertInfo.btnCancelClickAction = () => { ContinueGame(); };
  398. ViewManager.Show<AlertMiniGameView>(alertInfo);
  399. }
  400. private void PauseGame()
  401. {
  402. _gamePause = true;
  403. Timers.inst.Remove(UpdateTime);
  404. for (int i = 0; i < items.Count; i++)
  405. {
  406. items[i].SetPause(true);
  407. }
  408. }
  409. private void ContinueGame()
  410. {
  411. _gamePause = false;
  412. Timers.inst.Add(1, 0, UpdateTime);
  413. for (int i = 0; i < items.Count; i++)
  414. {
  415. items[i].SetPause(false);
  416. }
  417. }
  418. /// <summary>
  419. /// 计时器,每隔一段时间创建掉落的元宵
  420. /// </summary>
  421. /// <param name="param"></param>
  422. private void UpdateGame(object param)
  423. {
  424. if (_gamePause)
  425. {
  426. return;
  427. }
  428. _timer += Time.deltaTime * 1000;
  429. if (_timer >= _cfg.DropTime)
  430. {
  431. CreateItems(null);
  432. _timer = 0;
  433. }
  434. }
  435. private void AddEffect()
  436. {
  437. EffectUIPool.CreateEffectUI(_ui.m_catcher.m_getEffect, "ui_Activity", "YXJ_Catch",
  438. onComplete: (effect) =>
  439. {
  440. if (effect != null)
  441. {
  442. _effectUIDic.Add("YXJ_Catch", effect);
  443. }
  444. });
  445. EffectUIPool.CreateEffectUI(_ui.m_startEffect, "ui_Activity", "YXJ_text_start",
  446. onComplete: (effect) =>
  447. {
  448. if (effect != null)
  449. {
  450. _effectUIDic.Add("YXJ_text_start", effect);
  451. }
  452. });
  453. EffectUIPool.CreateEffectUI(_ui.m_bgEffect, "ui_Activity", "YXJ_gameing_bg_tx",
  454. onComplete: (effect) =>
  455. {
  456. if (effect != null)
  457. {
  458. _effectUIDic.Add("YXJ_gameing_bg_tx", effect);
  459. }
  460. });
  461. }
  462. private void CheckActivityEnd(object param)
  463. {
  464. if (!ActivityGetYuanXiaoDataManager.Instance.CheckOpen())
  465. {
  466. _ui.m_catcher.target.visible = false;
  467. // 活动结束时强行退回主界面
  468. ViewManager.Show<MainUIView>(null, true);
  469. ViewManager.DeleteViewStackCountDown("MainUIView");
  470. }
  471. }
  472. }
  473. }