FlipGameView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_barEffect, "ui_Activity", "YXJY_Game_JinDu");
  90. _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_titleEffec, "ui_Activity", "YXJY_Game_Text");
  91. }
  92. protected override void OnShown()
  93. {
  94. base.OnShown();
  95. if ((this.viewData as object[]).Length != 0 && this.viewData != null)
  96. {
  97. gameID = (int)(this.viewData as object[])[0];
  98. }
  99. else
  100. {
  101. gameID = 10001;
  102. }
  103. gameDate = CommonDataManager.Tables.TblCardGameCfg.GetOrDefault(gameID);
  104. UpdateView();
  105. UpdateList();
  106. }
  107. protected override void OnHide()
  108. {
  109. DestroyObjectFromView();
  110. cardList.Clear();
  111. Timers.inst.Remove(UpdateTime);
  112. Timers.inst.Remove(UpdateBar);
  113. Timers.inst.Remove(UpdateHit);
  114. Timers.inst.Remove(StartTime);
  115. Timers.inst.Remove(UpdateOneHit);
  116. Timers.inst.Remove(FlipAllCard);
  117. Timers.inst.Remove(ResetTouch);
  118. base.OnHide();
  119. }
  120. private void DestroyObjectFromView()
  121. {
  122. for (int i = 0; i < _effectFlipList.Count; i++)
  123. {
  124. EffectUIPool.Recycle(_effectFlipList[i]);
  125. _effectFlipList[i] = null;
  126. }
  127. _effectFlipList.Clear();
  128. for (int i = 0; i < _effectRemoveList.Count; i++)
  129. {
  130. EffectUIPool.Recycle(_effectRemoveList[i]);
  131. _effectRemoveList[i] = null;
  132. }
  133. _effectRemoveList.Clear();
  134. }
  135. private void OnClickBtnBack()
  136. {
  137. Timers.inst.Remove(UpdateTime);
  138. Timers.inst.Remove(UpdateBar);
  139. string exitTip;
  140. if (gameDate.BonusLose.Count == 0)
  141. {
  142. exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  143. }
  144. else
  145. {
  146. exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
  147. }
  148. AlertUI.Show(exitTip)
  149. .SetLeftButton(true, "取消", (object data) =>
  150. {
  151. Timers.inst.Add(1.0f, 0, UpdateTime);
  152. Timers.inst.Add(0.1f, 0, UpdateBar);
  153. })
  154. .SetRightButton(true, "确定", async (object data) =>
  155. {
  156. var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.Type, timeIndex, false,
  157. activityGameDate[0].Id, true);
  158. this.Hide();
  159. }).SetShowCheck(false);
  160. }
  161. private void ListCardItem(int index, GObject item)
  162. {
  163. UI_cardItem cardItem = UI_cardItem.Proxy(item);
  164. Dictionary<string, int> itemInfo = new Dictionary<string, int>();
  165. cardItem.target.visible = true;
  166. cardItem.target.alpha = 1;
  167. EffectUI _effectUI1 =
  168. EffectUIPool.CreateEffectUI(cardItem.m_flipEffect, "ui_Activity", "YXJY_Game_OpenCard");
  169. _effectFlipList.Add(_effectUI1);
  170. cardItem.m_flipEffect.visible = false;
  171. EffectUI _effectUI2 =
  172. EffectUIPool.CreateEffectUI(cardItem.m_removeEffect, "ui_Activity", "YXJY_Game_CardDie");
  173. _effectRemoveList.Add(_effectUI2);
  174. cardItem.m_removeEffect.visible = false;
  175. cardItem.m_card.GetChild("icon").icon =
  176. ResPathUtil.GetMiniGamePicPath(CommonDataManager.Tables.TblCardCfg.DataList[cardArray[index]].ResName,
  177. "FlipGame");
  178. if (cardItem.target.data == null)
  179. {
  180. cardItem.target.onClick.Add(OnClickCardItem);
  181. }
  182. Card _c1 = (Card)cardItem.m_card;
  183. _c1.opened = true;
  184. _c1.SetPerspective();
  185. itemInfo.Add("index", index);
  186. itemInfo.Add("id", cardArray[index]);
  187. itemInfo.Add("state", 0);
  188. cardItem.m_id.visible = false; //text = itemInfo["id"].ToString();
  189. cardItem.target.data = itemInfo;
  190. UI_cardItem.ProxyEnd();
  191. }
  192. private void UpdateView()
  193. {
  194. gameTime = CommonDataManager.Tables.TblCardGameCfg.GetOrDefault(gameID).Time;
  195. for (int i = 0; i < 3; i++)
  196. {
  197. switch (i)
  198. {
  199. case 0:
  200. CustemsNum[0] = gameDate.TimeStar1;
  201. break;
  202. case 1:
  203. CustemsNum[1] = gameDate.TimeStar2;
  204. break;
  205. case 2:
  206. CustemsNum[2] = gameDate.TimeStar3;
  207. break;
  208. }
  209. }
  210. StartHit = true;
  211. firstHit = true;
  212. canHit = true;
  213. timeIndex = 0;
  214. currentGameTime = gameTime;
  215. cardNum = columns * rows;
  216. currentCardNum = cardNum;
  217. barTime = (float)gameTime;
  218. needNum = 2;
  219. activityGameDate = CommonDataManager.Tables.TblActivityOpenCfg.DataList
  220. .Where(a => a.Type == ConstLimitTimeActivityType.ActLimitStlyc).ToList();
  221. rand = new System.Random();
  222. //洗牌
  223. RandomGetNum();
  224. RandomCardList(cardArray, cardArray.Count);
  225. //
  226. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  227. _ui.m_ScareBar.max = gameTime;
  228. _ui.m_ScareBar.min = 0;
  229. _ui.m_ScareBar.value = gameTime;
  230. _ui.m_star1.SetPosition(((float)(gameTime - CustemsNum[0]) / (float)gameTime) * _ui.m_ScareBar.width + 70,
  231. _ui.m_star1.position.y, _ui.m_star1.position.z);
  232. _ui.m_star2.SetPosition(((float)(gameTime - CustemsNum[1]) / (float)gameTime) * _ui.m_ScareBar.width + 10,
  233. _ui.m_star1.position.y, _ui.m_star1.position.z);
  234. _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width,
  235. _ui.m_star1.position.y, _ui.m_star1.position.z);
  236. _ui.m_title.visible = false;
  237. _ui.m_titleEffec.visible = true;
  238. _ui.m_barEffect.x = _ui.m_ScareBar.width;
  239. _ui.m_cardList.touchable = false;
  240. }
  241. private void UpdateList()
  242. {
  243. _ui.m_cardList.columnCount = columns;
  244. _ui.m_cardList.numItems = cardNum;
  245. //翻转所有牌
  246. Timers.inst.Add(1.0f, 1, FlipAllCard);
  247. }
  248. private void OnClickCardItem(EventContext context)
  249. {
  250. if (!canHit)
  251. {
  252. return;
  253. }
  254. GObject cardItem = context.sender as GObject;
  255. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  256. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  257. if (StartHit)
  258. {
  259. StartHit = false;
  260. _ui.m_back.touchable = false;
  261. Timers.inst.Add(1.0f, 1, StartTime);
  262. }
  263. Card card = (Card)item.m_card;
  264. bool isSame = false;
  265. for (int i = 0; i < cardList.Count; i++)
  266. {
  267. if (cardInfo["index"] == cardList[i]["index"] && firstHit)
  268. {
  269. cardInfo["state"] = 0;
  270. //翻牌
  271. card.Turn();
  272. _ui.m_cardList.touchable = false;
  273. canHit = false;
  274. cardList.RemoveAt(0);
  275. Timers.inst.Add(0.5f, 1, UpdateOneHit);
  276. isSame = true;
  277. break;
  278. }
  279. else
  280. {
  281. if (cardInfo["index"] == cardList[i]["index"])
  282. {
  283. isSame = true;
  284. break;
  285. }
  286. }
  287. }
  288. firstHit = false;
  289. //翻牌
  290. if (!isSame)
  291. {
  292. cardInfo["state"] = 1;
  293. card.Turn();
  294. item.m_flipEffect.visible = true;
  295. cardList.Add(cardInfo);
  296. }
  297. UI_cardItem.ProxyEnd();
  298. }
  299. private void StartTime(object param = null)
  300. {
  301. _ui.m_cardList.touchable = false;
  302. canHit = false;
  303. UpdateCard();
  304. }
  305. //这里是处理已翻开的数量
  306. private void UpdateCard()
  307. {
  308. Timers.inst.Remove(StartTime);
  309. List<Dictionary<string, int>> removeList = new List<Dictionary<string, int>>();
  310. for (int i = 1; i < cardList.Count; i += 2)
  311. {
  312. if (cardList[i]["id"] == cardList[i - 1]["id"])
  313. {
  314. cardList[i]["state"] = 2;
  315. //消除
  316. UI_cardItem item1 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  317. item1.m_t0.Play();
  318. UI_cardItem.ProxyEnd();
  319. UI_cardItem item2 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  320. item2.m_t0.Play(() => { _ui.m_back.touchable = true; });
  321. UI_cardItem.ProxyEnd();
  322. currentCardNum -= 2;
  323. removeList.Add(cardList[i - 1]);
  324. removeList.Add(cardList[i]);
  325. }
  326. }
  327. if (currentCardNum <= 0)
  328. {
  329. Gameover(true);
  330. return;
  331. }
  332. //先处理消除的列表
  333. for (int i = 0; i < removeList.Count; i++)
  334. {
  335. cardList.Remove(removeList[i]);
  336. }
  337. removeList.Clear();
  338. //后处理翻牌的列表
  339. for (int i = 1; i < cardList.Count; i++)
  340. {
  341. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  342. //翻回去
  343. Card card = (Card)item.m_card;
  344. card.Turn();
  345. item.m_flipEffect.visible = false;
  346. cardList[i - 1]["state"] = 0;
  347. UI_cardItem.ProxyEnd();
  348. if (cardList.Count % 2 == 0 && i == cardList.Count - 1)
  349. {
  350. UI_cardItem carditem = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  351. //翻回去
  352. Card cardLast = (Card)carditem.m_card;
  353. cardLast.Turn();
  354. carditem.m_flipEffect.visible = false;
  355. cardList[i]["state"] = 0;
  356. UI_cardItem.ProxyEnd();
  357. }
  358. }
  359. if (cardList.Count % 2 == 0)
  360. {
  361. cardList.Clear();
  362. }
  363. else
  364. {
  365. int j = cardList.Count;
  366. for (int i = 0; i < j - 1; i++)
  367. {
  368. cardList.RemoveAt(0);
  369. }
  370. }
  371. Timers.inst.Add(0.6f, 1, UpdateHit);
  372. }
  373. private void UpdateHit(object param = null)
  374. {
  375. StartHit = true;
  376. firstHit = true;
  377. canHit = true;
  378. _ui.m_cardList.touchable = true;
  379. _ui.m_back.touchable = true;
  380. Timers.inst.Remove(UpdateHit);
  381. }
  382. private void UpdateOneHit(object param = null)
  383. {
  384. canHit = true;
  385. _ui.m_cardList.touchable = true;
  386. Timers.inst.Remove(UpdateOneHit);
  387. }
  388. private void FlipAllCard(object param = null)
  389. {
  390. //计时器
  391. Timers.inst.Add(1.0f, 0, UpdateTime);
  392. //进度条计时器
  393. Timers.inst.Add(0.1f, 0, UpdateBar);
  394. for (int i = 0; i < cardNum; i++)
  395. {
  396. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(i));
  397. //翻回去
  398. Card card = (Card)item.m_card;
  399. card.Turn();
  400. UI_cardItem.ProxyEnd();
  401. }
  402. Timers.inst.Add(0.6f, 1, ResetTouch);
  403. }
  404. private void ResetTouch(object param = null)
  405. {
  406. _ui.m_cardList.touchable = true;
  407. }
  408. private void UpdateTime(object param = null)
  409. {
  410. timeIndex++;
  411. currentGameTime--;
  412. //_ui.m_ScareBar.value = currentGameTime;
  413. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  414. if (currentGameTime <= 0)
  415. {
  416. Gameover(false);
  417. }
  418. }
  419. private void UpdateBar(object param = null)
  420. {
  421. barTime -= 0.1f;
  422. _ui.m_ScareBar.value = barTime;
  423. _ui.m_barEffect.x -= (880.0f / (float)(gameTime * 10));
  424. }
  425. private void Gameover(bool isPass = false)
  426. {
  427. Timers.inst.Remove(UpdateTime);
  428. Timers.inst.Remove(UpdateBar);
  429. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.Type, timeIndex, gameDate.Id });
  430. }
  431. //洗牌
  432. private void RandomCardList(List<int> array, int length)
  433. {
  434. int index;
  435. int value;
  436. for (int i = length - 1; i >= 0; i--)
  437. {
  438. index = rand.Next(0, i + 1);
  439. value = array[i];
  440. array[i] = array[index];
  441. array[index] = value;
  442. }
  443. }
  444. private void RandomGetNum()
  445. {
  446. int numCount = CommonDataManager.Tables.TblCardCfg.DataList.Count;
  447. List<int> tArray = new List<int>();
  448. for (int i = 0; i < numCount; i++)
  449. {
  450. tArray.Add(i);
  451. }
  452. RandomCardList(tArray, numCount);
  453. int j = 0;
  454. for (int i = 0; i < cardNum; i += 4)
  455. {
  456. cardArray[i] = tArray[j];
  457. cardArray[i + 1] = tArray[j];
  458. cardArray[i + 2] = tArray[j];
  459. cardArray[i + 3] = tArray[j];
  460. j++;
  461. }
  462. }
  463. }
  464. }