ActivityGetYuanXiaoView.cs 15 KB

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