ActivityGetYuanXiaoView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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.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, Screen.width + _ui.m_catcher.target.width, 0);
  209. }
  210. private void StartGame()
  211. {
  212. CreateItems(null);
  213. UpdateTime(null);
  214. Timers.inst.Add(1, 0, UpdateTime);
  215. Timers.inst.AddUpdate(UpdateGame);
  216. }
  217. private async void PlayStartAnim()
  218. {
  219. GRoot.inst.touchable = false;
  220. _ui.m_startEffect.visible = false;
  221. _ui.m_startEffect.visible = true;
  222. await Task.Delay(820);
  223. GRoot.inst.touchable = true;
  224. StartGame();
  225. }
  226. private void UpdateTime(object param)
  227. {
  228. if (!isShowing)
  229. {
  230. return;
  231. }
  232. _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
  233. --_countTime;
  234. if (_countTime < 0)
  235. {
  236. Timers.inst.Remove(UpdateTime);
  237. GameEnd();
  238. }
  239. }
  240. private async void GameEnd()
  241. {
  242. //_ui.m_catcher.target.draggable = false;
  243. _ui.m_catcher.target.visible = false;
  244. HideGameView();
  245. bool win = true;
  246. if (_ui.m_c1.selectedIndex == 0)
  247. {
  248. foreach (var v in _collectDict)
  249. {
  250. if (v.Value > 0)
  251. {
  252. win = false;
  253. break;
  254. }
  255. }
  256. }
  257. else
  258. {
  259. if (_score < _cfg.targetScore)
  260. {
  261. win = false;
  262. }
  263. }
  264. //ViewManager.Show<ModalStatusView>("加载中...");
  265. //// 请求游戏结束协议
  266. //await MiniGameProxy.ReqMiniGameEnd(_cfg.id, _cfg.type, 0, win, _activityID, false);
  267. //ViewManager.Hide<ModalStatusView>();
  268. if (win)
  269. {
  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. }
  321. private void UpdateCollectProgress(int collectType)
  322. {
  323. if (!_collectDict.ContainsKey(collectType))
  324. {
  325. return;
  326. }
  327. if (_collectDict[collectType] > 0)
  328. {
  329. --_collectDict[collectType];
  330. }
  331. _ui.m_collectList.numItems = _cfg.targetIdArr.Length;
  332. }
  333. private void CollectListRenderer(int index, GObject item)
  334. {
  335. UI_targetItem target = UI_targetItem.Proxy(item);
  336. target.m_iconType.url = string.Format("ui://ActivityGetYuanXiao/yx_mb_{0}", _cfg.targetIdArr[index][0]);
  337. int num = _collectDict[_cfg.targetIdArr[index][0]];
  338. if (num == 0)
  339. {
  340. target.m_c1.selectedIndex = 1;
  341. }
  342. else
  343. {
  344. target.m_c1.selectedIndex = 0;
  345. target.m_num.text = num.ToString();
  346. }
  347. UI_targetItem.ProxyEnd();
  348. }
  349. private void ShowGameView()
  350. {
  351. InitData();
  352. InitView();
  353. SetDragRect();
  354. PlayStartAnim();
  355. }
  356. private void HideGameView()
  357. {
  358. Timers.inst.Remove(UpdateTime);
  359. Timers.inst.Remove(UpdateGame);
  360. ClearYuanXiaoList();
  361. }
  362. private void ResetGame()
  363. {
  364. HideGameView();
  365. ShowGameView();
  366. }
  367. private void OnBtnBack()
  368. {
  369. // 暂停游戏
  370. PauseGame();
  371. AlertMiniGame alertInfo = new AlertMiniGame();
  372. alertInfo.desc = "确认是否退出游戏?退出游戏直接按失败结算";
  373. alertInfo.btnConfirmClickAction = () =>
  374. {
  375. Hide();
  376. };
  377. alertInfo.btnCancelClickAction = () =>
  378. {
  379. ContinueGame();
  380. };
  381. ViewManager.Show<AlertMiniGameView>(alertInfo);
  382. }
  383. private void PauseGame()
  384. {
  385. _gamePause = true;
  386. Timers.inst.Remove(UpdateTime);
  387. for (int i = 0; i < items.Count; i++)
  388. {
  389. items[i].SetPause(true);
  390. }
  391. }
  392. private void ContinueGame()
  393. {
  394. _gamePause = false;
  395. Timers.inst.Add(1, 0, UpdateTime);
  396. for (int i = 0; i < items.Count; i++)
  397. {
  398. items[i].SetPause(false);
  399. }
  400. }
  401. private void UpdateGame(object param)
  402. {
  403. if (_gamePause)
  404. {
  405. return;
  406. }
  407. _timer += Time.deltaTime * 1000;
  408. if(_timer >= _cfg.dropTime)
  409. {
  410. CreateItems(null);
  411. _timer = 0;
  412. }
  413. }
  414. }
  415. }