ActivityGetYuanXiaoView.cs 12 KB

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