FlipGameView.cs 18 KB

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