ActivityGetYuanXiaoView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. if (_ui != null)
  31. {
  32. _ui.Dispose();
  33. }
  34. _ui = null;
  35. base.Dispose();
  36. }
  37. protected override void OnInit()
  38. {
  39. base.OnInit();
  40. packageName = UI_ActivityGetYuanXiaoUI.PACKAGE_NAME;
  41. _ui = UI_ActivityGetYuanXiaoUI.Create();
  42. viewCom = _ui.target;
  43. isReturnView = true;
  44. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("hd_yx_bg_2");
  45. _ui.m_collectList.itemRenderer = CollectListRenderer;
  46. _ui.m_btnBack.onClick.Add(OnBtnBack);
  47. _effectUIDic.Add("YXJ_Catch", EffectUIPool.CreateEffectUI(_ui.m_catcher.m_getEffect, "ui_Activity", "YXJ_Catch"));
  48. _effectUIDic.Add("YXJ_text_start", EffectUIPool.CreateEffectUI(_ui.m_startEffect, "ui_Activity", "YXJ_text_start"));
  49. _effectUIDic.Add("YXJ_gameing_bg_tx", EffectUIPool.CreateEffectUI(_ui.m_bgEffect, "ui_Activity", "YXJ_gameing_bg_tx"));
  50. }
  51. protected override void OnShown()
  52. {
  53. base.OnShown();
  54. object[] arr = viewData as object[];
  55. _cfg = (PickUpGame)arr[0];
  56. _activityID = (int)arr[1];
  57. if (_firstIn)
  58. {
  59. _firstIn = false;
  60. _catcherInitPos = _ui.m_catcher.target.position;
  61. }
  62. ShowGameView();
  63. }
  64. protected override void OnHide()
  65. {
  66. base.OnHide();
  67. HideGameView();
  68. }
  69. protected override void AddEventListener()
  70. {
  71. base.AddEventListener();
  72. EventAgent.AddEventListener(ConstMessage.ACTIVITY_GETYUANXIAO_RESET, ResetGame);
  73. }
  74. protected override void RemoveEventListener()
  75. {
  76. base.RemoveEventListener();
  77. EventAgent.RemoveEventListener(ConstMessage.ACTIVITY_GETYUANXIAO_RESET, ResetGame);
  78. }
  79. /// <summary>
  80. /// 初始化元宵的FGUI组件
  81. /// </summary>
  82. /// <returns></returns>
  83. YuanXiaoItem InitItemUICom()
  84. {
  85. GComponent gcom = UIPackage.CreateObject("ActivityGetYuanXiao", "YuanXiaoItem").asCom;
  86. _ui.m_YuanXiaoParent.target.AddChild(gcom);
  87. YuanXiaoItem item = new YuanXiaoItem();
  88. item.CreateItem(gcom, _ui.m_catcher.m_catcher, this);
  89. return item;
  90. }
  91. public void ClearYuanXiaoList()
  92. {
  93. for (int i = 0; i < items.Count; i++)
  94. {
  95. items[i].Destroy();
  96. }
  97. items.Clear();
  98. }
  99. /// <summary>
  100. /// 从对象池中取出对象
  101. /// </summary>
  102. /// <param name="type"></param>
  103. /// <returns></returns>
  104. public YuanXiaoItem Get(int type)
  105. {
  106. YuanXiaoItem item = items.Find(x => x.GetVisible() == false);
  107. if (item == null)
  108. {
  109. item = InitItemUICom();
  110. }
  111. item.Init(type);
  112. return item;
  113. }
  114. public void Restore(YuanXiaoItem item)
  115. {
  116. item.SetVisible(false);
  117. }
  118. /// <summary>
  119. /// 生成元宵
  120. /// </summary>
  121. /// <param name="param"></param>
  122. public void CreateItems(object param)
  123. {
  124. if (!isShowing)
  125. {
  126. return;
  127. }
  128. int num = _cfg.resIdArr.Length;
  129. List<int> roadIndexs = GetRandomRoads(num);
  130. for (int i = 0; i < num; i++)
  131. {
  132. PickUpCfg pickUpCfg = PickUpCfgArray.Instance.GetCfgsByid(_cfg.resArr[0])[_cfg.resIdArr[i] - 1];
  133. float time = pickUpCfg.speed * 1f / 1000;
  134. YuanXiaoItem item = Get(_cfg.resIdArr[i]);
  135. items.Add(item);
  136. int score = pickUpCfg.score;
  137. item.SetScore(score);
  138. Vector3 startPos = _ui.target.GetChild("start" + roadIndexs[i]).position;
  139. Vector3 endPos = startPos;
  140. endPos.y = _ui.m_end0.y;
  141. item.Move(startPos, endPos, time, () => Restore(item));
  142. }
  143. }
  144. /// <summary>
  145. /// 随机路径
  146. /// </summary>
  147. /// <param name="count"></param>
  148. /// <returns></returns>
  149. private List<int> GetRandomRoads(int count)
  150. {
  151. List<int> resultList = new List<int>();
  152. List<int> list = new List<int>();
  153. for (int i = 0; i < 4; i++)
  154. {
  155. list.Add(i);
  156. }
  157. for (int j = 0; j < count; j++)
  158. {
  159. int randomIndex = Random.Range(0, list.Count);
  160. resultList.Add(list[randomIndex]);
  161. list.RemoveAt(randomIndex);
  162. }
  163. return resultList;
  164. }
  165. /// <summary>
  166. /// 接住元宵
  167. /// </summary>
  168. /// <param name="item"></param>
  169. public void GetYuanXiao(YuanXiaoItem item)
  170. {
  171. // 回收组件
  172. Restore(item);
  173. if (_countTime == 0)
  174. {
  175. return;
  176. }
  177. // 显示得分标志
  178. _score += item.GetScore();
  179. _ui.m_score.text = _score.ToString();
  180. if (_ui.m_c1.selectedIndex == 0)
  181. {
  182. UpdateCollectProgress(item.GetYuanXiaoType());
  183. }
  184. else
  185. {
  186. UpdateScore();
  187. }
  188. ShowFlyScore(item.GetScore());
  189. _ui.m_catcher.m_getEffect.visible = false;
  190. _ui.m_catcher.m_getEffect.visible = true;
  191. }
  192. private void ShowFlyScore(int score)
  193. {
  194. UI_flyScore flyItem = UI_flyScore.Proxy(_ui.m_catcher.m_flyscore.target);
  195. flyItem.m_score.text = score.ToString();
  196. flyItem.m_t0.Stop();
  197. _ui.m_catcher.m_flyscore.target.visible = true;
  198. flyItem.m_t0.Play(() =>
  199. {
  200. _ui.m_catcher.m_flyscore.target.visible = false;
  201. });
  202. UI_flyScore.ProxyEnd();
  203. }
  204. public void SetDragRect()
  205. {
  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, Screen.width + _ui.m_catcher.target.width, 0);
  208. }
  209. private void StartGame()
  210. {
  211. CreateItems(null);
  212. Timers.inst.Add(1, 0, UpdateTime);
  213. Timers.inst.AddUpdate(UpdateGame);
  214. }
  215. private async void PlayStartAnim()
  216. {
  217. GRoot.inst.touchable = false;
  218. _ui.m_startEffect.visible = false;
  219. _ui.m_startEffect.visible = true;
  220. await Task.Delay(820);
  221. GRoot.inst.touchable = true;
  222. StartGame();
  223. }
  224. private void UpdateTime(object param)
  225. {
  226. if (!isShowing)
  227. {
  228. return;
  229. }
  230. _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
  231. --_countTime;
  232. if (_countTime < 0)
  233. {
  234. Timers.inst.Remove(UpdateTime);
  235. GameEnd();
  236. }
  237. }
  238. private void GameEnd()
  239. {
  240. _ui.m_catcher.target.draggable = false;
  241. HideGameView();
  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. if (win)
  262. {
  263. ViewManager.Show<ActivityGetYuanXiaoSuccessView>(_cfg);
  264. }
  265. else
  266. {
  267. ViewManager.Show<ActivityGetYuanXiaoFailView>();
  268. }
  269. }
  270. private void InitData()
  271. {
  272. _gamePause = false;
  273. _timer = 0;
  274. _countTime = _cfg.time;
  275. _score = 0;
  276. _collectDict = new Dictionary<int, int>();
  277. items = new List<YuanXiaoItem>();
  278. }
  279. private void InitView()
  280. {
  281. _ui.m_startEffect.visible = false;
  282. _ui.m_catcher.m_getEffect.visible = false;
  283. _ui.m_catcher.m_flyscore.target.visible = false;
  284. _ui.m_catcher.m_c2.selectedIndex = _cfg.id > PickUpGameArray.Instance.dataArray[0].id ? 1 : 0;
  285. _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
  286. _ui.m_score.text = _score.ToString();
  287. _ui.m_catcher.target.position = _catcherInitPos;
  288. InitTarget();
  289. }
  290. private void InitTarget()
  291. {
  292. // 分数
  293. if (_cfg.targetIdArr.Length == 0)
  294. {
  295. _ui.m_targetScore.text = _cfg.targetScore.ToString();
  296. _ui.m_c1.selectedIndex = 1;
  297. UpdateScore();
  298. }
  299. // 收集
  300. else
  301. {
  302. for (int i = 0; i < _cfg.targetIdArr.Length; i++)
  303. {
  304. _collectDict.Add(_cfg.targetIdArr[i][0], _cfg.targetIdArr[i][1]);
  305. }
  306. _ui.m_c1.selectedIndex = 0;
  307. _ui.m_collectList.numItems = _cfg.targetIdArr.Length;
  308. }
  309. }
  310. private void UpdateScore()
  311. {
  312. _ui.m_progress.SetVar("cur", _score.ToString()).SetVar("target", _cfg.targetScore.ToString()).FlushVars();
  313. }
  314. private void UpdateCollectProgress(int collectType)
  315. {
  316. if (!_collectDict.ContainsKey(collectType))
  317. {
  318. return;
  319. }
  320. if (_collectDict[collectType] > 0)
  321. {
  322. --_collectDict[collectType];
  323. }
  324. _ui.m_collectList.numItems = _cfg.targetIdArr.Length;
  325. }
  326. private void CollectListRenderer(int index, GObject item)
  327. {
  328. UI_targetItem target = UI_targetItem.Proxy(item);
  329. target.m_iconType.url = string.Format("ui://ActivityGetYuanXiao/yx_mb_{0}", _cfg.targetIdArr[index][0]);
  330. int num = _collectDict[_cfg.targetIdArr[index][0]];
  331. if (num == 0)
  332. {
  333. target.m_c1.selectedIndex = 1;
  334. }
  335. else
  336. {
  337. target.m_c1.selectedIndex = 0;
  338. target.m_num.text = num.ToString();
  339. }
  340. UI_targetItem.ProxyEnd();
  341. }
  342. private void ShowGameView()
  343. {
  344. InitData();
  345. InitView();
  346. SetDragRect();
  347. PlayStartAnim();
  348. }
  349. private void HideGameView()
  350. {
  351. Timers.inst.Remove(UpdateTime);
  352. Timers.inst.Remove(UpdateGame);
  353. ClearYuanXiaoList();
  354. }
  355. private void ResetGame()
  356. {
  357. HideGameView();
  358. ShowGameView();
  359. }
  360. private void OnBtnBack()
  361. {
  362. // 暂停游戏
  363. PauseGame();
  364. AlertMiniGame alertInfo = new AlertMiniGame();
  365. alertInfo.desc = "确认是否退出游戏?退出游戏直接按失败结算";
  366. alertInfo.btnConfirmClickAction = () =>
  367. {
  368. Hide();
  369. };
  370. alertInfo.btnCancelClickAction = () =>
  371. {
  372. ContinueGame();
  373. };
  374. ViewManager.Show<AlertMiniGameView>(alertInfo);
  375. }
  376. private void PauseGame()
  377. {
  378. _gamePause = true;
  379. Timers.inst.Remove(UpdateTime);
  380. for (int i = 0; i < items.Count; i++)
  381. {
  382. items[i].SetPause(true);
  383. }
  384. }
  385. private void ContinueGame()
  386. {
  387. _gamePause = false;
  388. Timers.inst.Add(1, 0, UpdateTime);
  389. for (int i = 0; i < items.Count; i++)
  390. {
  391. items[i].SetPause(false);
  392. }
  393. }
  394. private void UpdateGame(object param)
  395. {
  396. if (_gamePause)
  397. {
  398. return;
  399. }
  400. _timer += Time.deltaTime * 1000;
  401. if(_timer >= _cfg.dropTime)
  402. {
  403. CreateItems(null);
  404. _timer = 0;
  405. }
  406. }
  407. }
  408. }