FlipGameView.cs 17 KB

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