FlipGameView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using System;
  2. using System.Collections.Generic;
  3. using ET;
  4. using FairyGUI;
  5. using UI.MiniGame;
  6. using UnityEngine;
  7. namespace GFGGame
  8. {
  9. public class FlipGameView : BaseWindow
  10. {
  11. private UI_FlipGameUI _ui;
  12. //卡牌数量(配置)
  13. private int cardNum = 16;
  14. //当前卡牌数量
  15. private int currentCardNum = 16;
  16. //当前翻开卡牌
  17. //"state" 0:未翻开,1:已翻开,2:已消除
  18. private List<Dictionary<string, int>> cardList = new List<Dictionary<string, int>>();
  19. //消除所需数量(配置)
  20. private int needNum;
  21. //游戏结束时长(配置)
  22. private int gameTime = 120;
  23. //游戏当前时长倒计时
  24. private int currentGameTime = 120;
  25. //当前游戏时长
  26. private int timeIndex = 0;
  27. //进度条
  28. private float barTime = 120.0f;
  29. //行数
  30. private int rows = 4;
  31. //列数
  32. private int columns = 4;
  33. private int gameID;
  34. private CardGame gameDate;
  35. System.Random rand;
  36. //通关评价
  37. private List<int> CustemsNum = new List<int>()
  38. {
  39. 10,
  40. 30,
  41. 60,
  42. };
  43. private List<string> CustemsName = new List<string>()
  44. {
  45. "良好",
  46. "优秀",
  47. "卓越"
  48. };
  49. private List<ActivityOpenCfg> activityGameDate;
  50. //假数据
  51. private List<int> cardArray = new List<int> { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 8, 8 };
  52. //Card _c1 = new Card();
  53. //从第一次点击开始超过一秒后禁止点击,然后对翻开列表进行处理并重置,
  54. private float waitTime = 0.0f;
  55. private bool StartHit = true;
  56. private bool firstHit = true;
  57. private bool canHit = true;
  58. private EffectUI _effectUI1;
  59. private EffectUI _effectUI2;
  60. private List<EffectUI> _effectFlipList = new List<EffectUI>();
  61. private List<EffectUI> _effectRemoveList = new List<EffectUI>();
  62. public override void Dispose()
  63. {
  64. EffectUIPool.Recycle(_effectUI1);
  65. _effectUI1 = null;
  66. EffectUIPool.Recycle(_effectUI2);
  67. _effectUI2 = null;
  68. DestroyObjectFromView();
  69. if (_ui != null)
  70. {
  71. _ui.Dispose();
  72. _ui = null;
  73. }
  74. base.Dispose();
  75. }
  76. protected override void OnInit()
  77. {
  78. base.OnInit();
  79. packageName = UI_FlipGameUI.PACKAGE_NAME;
  80. _ui = UI_FlipGameUI.Create();
  81. this.viewCom = _ui.target;
  82. isfullScreen = true;
  83. isReturnView = true;
  84. _ui.m_cardList.itemRenderer = ListCardItem;
  85. _ui.m_back.onClick.Add(OnClickBtnBack);
  86. UIObjectFactory.SetPackageItemExtension("ui://MiniGame/CardComponent", typeof(Card));
  87. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_barEffect, "ui_Activity", "YXJY_Game_JinDu");
  88. _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_titleEffec, "ui_Activity", "YXJY_Game_Text");
  89. }
  90. protected override void OnShown()
  91. {
  92. base.OnShown();
  93. if ((this.viewData as object[]).Length != 0 && this.viewData != null)
  94. {
  95. gameID = (int)(this.viewData as object[])[0];
  96. }
  97. else
  98. {
  99. gameID = 10001;
  100. }
  101. gameDate = CardGameArray.Instance.GetCfg(gameID);
  102. UpdateView();
  103. UpdateList();
  104. }
  105. protected override void OnHide()
  106. {
  107. DestroyObjectFromView();
  108. cardList.Clear();
  109. Timers.inst.Remove(UpdateTime);
  110. Timers.inst.Remove(UpdateBar);
  111. Timers.inst.Remove(UpdateHit);
  112. Timers.inst.Remove(StartTime);
  113. Timers.inst.Remove(UpdateOneHit);
  114. Timers.inst.Remove(FlipAllCard);
  115. Timers.inst.Remove(ResetTouch);
  116. base.OnHide();
  117. }
  118. private void DestroyObjectFromView()
  119. {
  120. for (int i = 0; i < _effectFlipList.Count; i++)
  121. {
  122. EffectUIPool.Recycle(_effectFlipList[i]);
  123. _effectFlipList[i] = null;
  124. }
  125. _effectFlipList.Clear();
  126. for (int i = 0; i < _effectRemoveList.Count; i++)
  127. {
  128. EffectUIPool.Recycle(_effectRemoveList[i]);
  129. _effectRemoveList[i] = null;
  130. }
  131. _effectRemoveList.Clear();
  132. }
  133. private void OnClickBtnBack()
  134. {
  135. Timers.inst.Remove(UpdateTime);
  136. Timers.inst.Remove(UpdateBar);
  137. string exitTip;
  138. if (gameDate.bonusLoseArr.Length == 0)
  139. {
  140. exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  141. }
  142. else
  143. {
  144. exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
  145. }
  146. AlertUI.Show(exitTip)
  147. .SetLeftButton(true, "取消", (object data) =>
  148. {
  149. Timers.inst.Add(1.0f, 0, UpdateTime);
  150. Timers.inst.Add(0.1f, 0, UpdateBar);
  151. })
  152. .SetRightButton(true, "确定", async (object data) =>
  153. {
  154. var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.type, timeIndex, false, activityGameDate[0].id, true);
  155. this.Hide();
  156. }).SetShowCheck(false);
  157. }
  158. private void ListCardItem(int index, GObject item)
  159. {
  160. UI_cardItem cardItem = UI_cardItem.Proxy(item);
  161. Dictionary<string, int> itemInfo = new Dictionary<string, int>();
  162. cardItem.target.visible = true;
  163. cardItem.target.alpha = 1;
  164. EffectUI _effectUI1 = EffectUIPool.CreateEffectUI(cardItem.m_flipEffect, "ui_Activity", "YXJY_Game_OpenCard");
  165. _effectFlipList.Add(_effectUI1);
  166. cardItem.m_flipEffect.visible = false;
  167. EffectUI _effectUI2 = EffectUIPool.CreateEffectUI(cardItem.m_removeEffect, "ui_Activity", "YXJY_Game_CardDie");
  168. _effectRemoveList.Add(_effectUI2);
  169. cardItem.m_removeEffect.visible = false;
  170. cardItem.m_card.GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(CardCfgArray.Instance.dataArray[cardArray[index]].resName,"FlipGame");
  171. if (cardItem.target.data == null)
  172. {
  173. cardItem.target.onClick.Add(OnClickCardItem);
  174. }
  175. Card _c1 = (Card)cardItem.m_card;
  176. _c1.opened = true;
  177. _c1.SetPerspective();
  178. itemInfo.Add("index", index);
  179. itemInfo.Add("id", cardArray[index]);
  180. itemInfo.Add("state", 0);
  181. cardItem.m_id.visible = false;//text = itemInfo["id"].ToString();
  182. cardItem.target.data = itemInfo;
  183. UI_cardItem.ProxyEnd();
  184. }
  185. private void UpdateView()
  186. {
  187. gameTime = CardGameArray.Instance.GetCfg(gameID).time;
  188. for (int i = 0; i < 3; i++)
  189. {
  190. switch (i)
  191. {
  192. case 0:
  193. CustemsNum[0] = gameDate.timeStar1;
  194. break;
  195. case 1:
  196. CustemsNum[1] = gameDate.timeStar2;
  197. break;
  198. case 2:
  199. CustemsNum[2] = gameDate.timeStar3;
  200. break;
  201. }
  202. }
  203. StartHit = true;
  204. firstHit = true;
  205. canHit = true;
  206. timeIndex = 0;
  207. currentGameTime = gameTime;
  208. cardNum = columns * rows;
  209. currentCardNum = cardNum;
  210. barTime = (float)gameTime;
  211. needNum = 2;
  212. activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
  213. rand = new System.Random();
  214. //洗牌
  215. RandomGetNum();
  216. RandomCardList(cardArray, cardArray.Count);
  217. //
  218. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  219. _ui.m_ScareBar.max = gameTime;
  220. _ui.m_ScareBar.min = 0;
  221. _ui.m_ScareBar.value = gameTime;
  222. _ui.m_star1.SetPosition(((float)(gameTime - CustemsNum[0]) / (float)gameTime) * _ui.m_ScareBar.width + 70, _ui.m_star1.position.y, _ui.m_star1.position.z);
  223. _ui.m_star2.SetPosition(((float)(gameTime - CustemsNum[1]) / (float)gameTime) * _ui.m_ScareBar.width + 10, _ui.m_star1.position.y, _ui.m_star1.position.z);
  224. _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
  225. _ui.m_title.visible = false;
  226. _ui.m_titleEffec.visible = true;
  227. _ui.m_barEffect.x = _ui.m_ScareBar.width;
  228. _ui.m_cardList.touchable = false;
  229. }
  230. private void UpdateList()
  231. {
  232. _ui.m_cardList.columnCount = columns;
  233. _ui.m_cardList.numItems = cardNum;
  234. //翻转所有牌
  235. Timers.inst.Add(1.0f, 1, FlipAllCard);
  236. }
  237. private void OnClickCardItem(EventContext context)
  238. {
  239. if (!canHit)
  240. {
  241. return;
  242. }
  243. GObject cardItem = context.sender as GObject;
  244. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  245. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  246. if (StartHit)
  247. {
  248. StartHit = false;
  249. _ui.m_back.touchable = false;
  250. Timers.inst.Add(1.0f, 1, StartTime);
  251. }
  252. Card card = (Card)item.m_card;
  253. bool isSame = false;
  254. for (int i = 0; i < cardList.Count; i++)
  255. {
  256. if (cardInfo["index"] == cardList[i]["index"] && firstHit)
  257. {
  258. cardInfo["state"] = 0;
  259. //翻牌
  260. card.Turn();
  261. _ui.m_cardList.touchable = false;
  262. canHit = false;
  263. cardList.RemoveAt(0);
  264. Timers.inst.Add(0.5f, 1,UpdateOneHit);
  265. isSame = true;
  266. break;
  267. }
  268. else
  269. {
  270. if (cardInfo["index"] == cardList[i]["index"])
  271. {
  272. isSame = true;
  273. break;
  274. }
  275. }
  276. }
  277. firstHit = false;
  278. //翻牌
  279. if (!isSame)
  280. {
  281. cardInfo["state"] = 1;
  282. card.Turn();
  283. item.m_flipEffect.visible = true;
  284. cardList.Add(cardInfo);
  285. }
  286. UI_cardItem.ProxyEnd();
  287. }
  288. private void StartTime(object param = null)
  289. {
  290. _ui.m_cardList.touchable = false;
  291. canHit = false;
  292. UpdateCard();
  293. }
  294. //这里是处理已翻开的数量
  295. private void UpdateCard()
  296. {
  297. Timers.inst.Remove(StartTime);
  298. List<Dictionary<string, int>> removeList = new List<Dictionary<string, int>>();
  299. for (int i = 1; i < cardList.Count; i += 2)
  300. {
  301. if (cardList[i]["id"] == cardList[i - 1]["id"])
  302. {
  303. cardList[i]["state"] = 2;
  304. //消除
  305. UI_cardItem item1 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  306. item1.m_t0.Play();
  307. UI_cardItem.ProxyEnd();
  308. UI_cardItem item2 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  309. item2.m_t0.Play(()=> { _ui.m_back.touchable = true; });
  310. UI_cardItem.ProxyEnd();
  311. currentCardNum -= 2;
  312. removeList.Add(cardList[i - 1]);
  313. removeList.Add(cardList[i]);
  314. }
  315. }
  316. if (currentCardNum <= 0)
  317. {
  318. Gameover(true);
  319. return;
  320. }
  321. //先处理消除的列表
  322. for (int i = 0; i < removeList.Count; i++)
  323. {
  324. cardList.Remove(removeList[i]);
  325. }
  326. removeList.Clear();
  327. //后处理翻牌的列表
  328. for (int i = 1; i < cardList.Count; i++)
  329. {
  330. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  331. //翻回去
  332. Card card = (Card)item.m_card;
  333. card.Turn();
  334. item.m_flipEffect.visible = false;
  335. cardList[i - 1]["state"] = 0;
  336. UI_cardItem.ProxyEnd();
  337. if (cardList.Count % 2 == 0 && i == cardList.Count - 1)
  338. {
  339. UI_cardItem carditem = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  340. //翻回去
  341. Card cardLast = (Card)carditem.m_card;
  342. cardLast.Turn();
  343. carditem.m_flipEffect.visible = false;
  344. cardList[i]["state"] = 0;
  345. UI_cardItem.ProxyEnd();
  346. }
  347. }
  348. if (cardList.Count % 2 == 0)
  349. {
  350. cardList.Clear();
  351. }
  352. else
  353. {
  354. int j = cardList.Count;
  355. for (int i = 0; i < j - 1; i++)
  356. {
  357. cardList.RemoveAt(0);
  358. }
  359. }
  360. Timers.inst.Add(0.6f, 1, UpdateHit);
  361. }
  362. private void UpdateHit(object param = null)
  363. {
  364. StartHit = true;
  365. firstHit = true;
  366. canHit = true;
  367. _ui.m_cardList.touchable = true;
  368. _ui.m_back.touchable = true;
  369. Timers.inst.Remove(UpdateHit);
  370. }
  371. private void UpdateOneHit(object param = null)
  372. {
  373. canHit = true;
  374. _ui.m_cardList.touchable = true;
  375. Timers.inst.Remove(UpdateOneHit);
  376. }
  377. private void FlipAllCard(object param = null)
  378. {
  379. //计时器
  380. Timers.inst.Add(1.0f, 0, UpdateTime);
  381. //进度条计时器
  382. Timers.inst.Add(0.1f, 0, UpdateBar);
  383. for (int i = 0; i < cardNum; i++)
  384. {
  385. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(i));
  386. //翻回去
  387. Card card = (Card)item.m_card;
  388. card.Turn();
  389. UI_cardItem.ProxyEnd();
  390. }
  391. Timers.inst.Add(0.6f, 1, ResetTouch);
  392. }
  393. private void ResetTouch(object param = null)
  394. {
  395. _ui.m_cardList.touchable = true;
  396. }
  397. private void UpdateTime(object param = null)
  398. {
  399. timeIndex++;
  400. currentGameTime--;
  401. //_ui.m_ScareBar.value = currentGameTime;
  402. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  403. if (currentGameTime <= 0)
  404. {
  405. Gameover(false);
  406. }
  407. }
  408. private void UpdateBar(object param = null)
  409. {
  410. barTime -= 0.1f;
  411. _ui.m_ScareBar.value = barTime;
  412. _ui.m_barEffect.x -= (880.0f / (float)(gameTime * 10));
  413. }
  414. private void Gameover(bool isPass = false)
  415. {
  416. Timers.inst.Remove(UpdateTime);
  417. Timers.inst.Remove(UpdateBar);
  418. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
  419. }
  420. //洗牌
  421. private void RandomCardList(List<int> array, int length)
  422. {
  423. int index;
  424. int value;
  425. for (int i = length - 1; i >= 0; i--)
  426. {
  427. index = rand.Next(0, i + 1);
  428. value = array[i];
  429. array[i] = array[index];
  430. array[index] = value;
  431. }
  432. }
  433. private void RandomGetNum()
  434. {
  435. int numCount = CardCfgArray.Instance.dataArray.Length;
  436. List<int> tArray = new List<int>();
  437. for (int i = 0; i < numCount; i++)
  438. {
  439. tArray.Add(i);
  440. }
  441. RandomCardList(tArray, numCount);
  442. int j = 0;
  443. for (int i = 0; i < cardNum; i += 4)
  444. {
  445. cardArray[i] = tArray[j];
  446. cardArray[i + 1] = tArray[j];
  447. cardArray[i + 2] = tArray[j];
  448. cardArray[i + 3] = tArray[j];
  449. j++;
  450. }
  451. }
  452. }
  453. }