FlipGameView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. base.OnHide();
  115. }
  116. private void DestroyObjectFromView()
  117. {
  118. for (int i = 0; i < _effectFlipList.Count; i++)
  119. {
  120. EffectUIPool.Recycle(_effectFlipList[i]);
  121. _effectFlipList[i] = null;
  122. }
  123. _effectFlipList.Clear();
  124. for (int i = 0; i < _effectRemoveList.Count; i++)
  125. {
  126. EffectUIPool.Recycle(_effectRemoveList[i]);
  127. _effectRemoveList[i] = null;
  128. }
  129. _effectRemoveList.Clear();
  130. }
  131. private void OnClickBtnBack()
  132. {
  133. Timers.inst.Remove(UpdateTime);
  134. Timers.inst.Remove(UpdateBar);
  135. string exitTip;
  136. if (gameDate.bonusLoseArr.Length == 0)
  137. {
  138. exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  139. }
  140. else
  141. {
  142. exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
  143. }
  144. AlertUI.Show(exitTip)
  145. .SetLeftButton(true, "取消", (object data) =>
  146. {
  147. Timers.inst.Add(1.0f, 0, UpdateTime);
  148. Timers.inst.Add(0.1f, 0, UpdateBar);
  149. })
  150. .SetRightButton(true, "确定", async (object data) =>
  151. {
  152. var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.type, timeIndex, false, activityGameDate[0].id, true);
  153. if (!result) return;
  154. this.Hide();
  155. });
  156. }
  157. private void ListCardItem(int index, GObject item)
  158. {
  159. UI_cardItem cardItem = UI_cardItem.Proxy(item);
  160. Dictionary<string, int> itemInfo = new Dictionary<string, int>();
  161. cardItem.target.visible = true;
  162. cardItem.target.alpha = 1;
  163. EffectUI _effectUI1 = EffectUIPool.CreateEffectUI(cardItem.m_flipEffect, "ui_Activity", "YXJY_Game_OpenCard");
  164. _effectFlipList.Add(_effectUI1);
  165. cardItem.m_flipEffect.visible = false;
  166. EffectUI _effectUI2 = EffectUIPool.CreateEffectUI(cardItem.m_removeEffect, "ui_Activity", "YXJY_Game_CardDie");
  167. _effectRemoveList.Add(_effectUI2);
  168. cardItem.m_removeEffect.visible = false;
  169. cardItem.m_card.GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(CardCfgArray.Instance.dataArray[cardArray[index]].resName);
  170. if (cardItem.target.data == null)
  171. {
  172. cardItem.target.onClick.Add(OnClickCardItem);
  173. }
  174. Card _c1 = (Card)cardItem.m_card;
  175. _c1.opened = false;
  176. _c1.SetPerspective();
  177. itemInfo.Add("index", index);
  178. itemInfo.Add("id", cardArray[index]);
  179. itemInfo.Add("state", 0);
  180. cardItem.m_id.text = itemInfo["id"].ToString();
  181. cardItem.target.data = itemInfo;
  182. UI_cardItem.ProxyEnd();
  183. }
  184. private void UpdateView()
  185. {
  186. gameTime = CardGameArray.Instance.GetCfg(gameID).time;
  187. for (int i = 0; i < 3; i++)
  188. {
  189. switch (i)
  190. {
  191. case 0:
  192. CustemsNum[0] = gameDate.timeStar1;
  193. break;
  194. case 1:
  195. CustemsNum[1] = gameDate.timeStar2;
  196. break;
  197. case 2:
  198. CustemsNum[2] = gameDate.timeStar3;
  199. break;
  200. }
  201. }
  202. StartHit = true;
  203. firstHit = true;
  204. canHit = true;
  205. timeIndex = 0;
  206. currentGameTime = gameTime;
  207. cardNum = columns * rows;
  208. currentCardNum = cardNum;
  209. barTime = (float)gameTime;
  210. needNum = 2;
  211. activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
  212. rand = new System.Random();
  213. //洗牌
  214. RandomGetNum();
  215. RandomCardList(cardArray, cardArray.Count);
  216. //
  217. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  218. _ui.m_ScareBar.max = gameTime;
  219. _ui.m_ScareBar.min = 0;
  220. _ui.m_ScareBar.value = gameTime;
  221. _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);
  222. _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);
  223. _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
  224. _ui.m_title.visible = false;
  225. _ui.m_titleEffec.visible = true;
  226. _ui.m_barEffect.x = _ui.m_ScareBar.width;
  227. _ui.m_cardList.touchable = true;
  228. ////计时器
  229. //Timers.inst.Add(1.0f, 0, UpdateTime);
  230. ////进度条计时器
  231. //Timers.inst.Add(0.1f, 0, UpdateBar);
  232. }
  233. private void UpdateList()
  234. {
  235. _ui.m_cardList.columnCount = columns;
  236. _ui.m_cardList.numItems = cardNum;
  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. 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();
  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. Timers.inst.Remove(UpdateHit);
  369. }
  370. private void UpdateOneHit(object param = null)
  371. {
  372. canHit = true;
  373. _ui.m_cardList.touchable = true;
  374. Timers.inst.Remove(UpdateOneHit);
  375. }
  376. private void UpdateTime(object param = null)
  377. {
  378. timeIndex++;
  379. currentGameTime--;
  380. //_ui.m_ScareBar.value = currentGameTime;
  381. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  382. if (currentGameTime <= 0)
  383. {
  384. Gameover(false);
  385. }
  386. }
  387. private void UpdateBar(object param = null)
  388. {
  389. barTime -= 0.1f;
  390. _ui.m_ScareBar.value = barTime;
  391. _ui.m_barEffect.x -= (880.0f / (float)(gameTime * 10));
  392. }
  393. private void Gameover(bool isPass = false)
  394. {
  395. Timers.inst.Remove(UpdateTime);
  396. Timers.inst.Remove(UpdateBar);
  397. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
  398. }
  399. //洗牌
  400. private void RandomCardList(List<int> array, int length)
  401. {
  402. int index;
  403. int value;
  404. for (int i = length - 1; i >= 0; i--)
  405. {
  406. index = rand.Next(0, i + 1);
  407. value = array[i];
  408. array[i] = array[index];
  409. array[index] = value;
  410. }
  411. }
  412. private void RandomGetNum()
  413. {
  414. int numCount = CardCfgArray.Instance.dataArray.Length;
  415. List<int> tArray = new List<int>();
  416. for (int i = 0; i < numCount; i++)
  417. {
  418. tArray.Add(i);
  419. }
  420. RandomCardList(tArray, numCount);
  421. for (int i = 0; i < cardNum; i += 2)
  422. {
  423. cardArray[i] = tArray[i];
  424. cardArray[i + 1] = tArray[i];
  425. }
  426. }
  427. }
  428. }