FlipGameView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. List<Dictionary<string, int>> removeList = new List<Dictionary<string, int>>();
  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 = CardGameArray.Instance.GetCfg(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(UpdateCard);
  115. base.OnHide();
  116. }
  117. private void DestroyObjectFromView()
  118. {
  119. for (int i = 0; i < _effectFlipList.Count; i++)
  120. {
  121. EffectUIPool.Recycle(_effectFlipList[i]);
  122. _effectFlipList[i] = null;
  123. }
  124. _effectFlipList.Clear();
  125. for (int i = 0; i < _effectRemoveList.Count; i++)
  126. {
  127. EffectUIPool.Recycle(_effectRemoveList[i]);
  128. _effectRemoveList[i] = null;
  129. }
  130. _effectRemoveList.Clear();
  131. }
  132. private void OnClickBtnBack()
  133. {
  134. Timers.inst.Remove(UpdateTime);
  135. Timers.inst.Remove(UpdateBar);
  136. string exitTip;
  137. if(gameDate.bonusLoseArr.Length == 0)
  138. {
  139. exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  140. }
  141. else
  142. {
  143. exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
  144. }
  145. AlertUI.Show(exitTip)
  146. .SetLeftButton(true, "取消", (object data) =>
  147. {
  148. Timers.inst.Add(1.0f, 0, UpdateTime);
  149. Timers.inst.Add(0.1f, 0, UpdateBar);
  150. })
  151. .SetRightButton(true, "确定", async (object data) =>
  152. {
  153. var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.type, timeIndex, false, activityGameDate[0].id,true);
  154. if (!result) return;
  155. this.Hide();
  156. });
  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);
  171. if (cardItem.target.data == null)
  172. {
  173. cardItem.target.onClick.Add(OnClickCardItem);
  174. }
  175. Card _c1 = (Card)cardItem.m_card;
  176. _c1.opened = false;
  177. _c1.SetPerspective();
  178. itemInfo.Add("index", index);
  179. itemInfo.Add("id", cardArray[index]);
  180. itemInfo.Add("state", 0);
  181. cardItem.m_id.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 = true;
  229. //计时器
  230. Timers.inst.Add(1.0f, 0, UpdateTime);
  231. //进度条计时器
  232. Timers.inst.Add(0.1f, 0, UpdateBar);
  233. }
  234. private void UpdateList()
  235. {
  236. _ui.m_cardList.columnCount = columns;
  237. _ui.m_cardList.numItems = cardNum;
  238. }
  239. private void OnClickCardItem(EventContext context)
  240. {
  241. if(!canHit)
  242. {
  243. return;
  244. }
  245. GObject cardItem = context.sender as GObject;
  246. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  247. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  248. if(StartHit)
  249. {
  250. StartHit = 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. //.
  263. cardList.RemoveAt(0);
  264. isSame = true;
  265. break;
  266. }
  267. else
  268. {
  269. if (cardInfo["index"] == cardList[i]["index"])
  270. {
  271. isSame = true;
  272. break;
  273. }
  274. }
  275. }
  276. firstHit = false;
  277. //翻牌
  278. if (!isSame)
  279. {
  280. cardInfo["state"] = 1;
  281. card.Turn();
  282. item.m_flipEffect.visible = true;
  283. cardList.Add(cardInfo);
  284. }
  285. UI_cardItem.ProxyEnd();
  286. }
  287. private void StartTime(object param = null)
  288. {
  289. _ui.m_cardList.touchable = false;
  290. canHit = false;
  291. removeList = new List<Dictionary<string, int>>();
  292. for (int i = 1; i < cardList.Count; i += 2)
  293. {
  294. if (cardList[i]["id"] == cardList[i - 1]["id"])
  295. {
  296. cardList[i]["state"] = 2;
  297. //消除
  298. UI_cardItem item1 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  299. item1.m_t0.Play();
  300. UI_cardItem.ProxyEnd();
  301. UI_cardItem item2 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  302. item2.m_t0.Play();
  303. UI_cardItem.ProxyEnd();
  304. currentCardNum -= 2;
  305. removeList.Add(cardList[i - 1]);
  306. removeList.Add(cardList[i]);
  307. }
  308. }
  309. if (currentCardNum <= 0)
  310. {
  311. Gameover(true);
  312. return;
  313. }
  314. //先处理消除的列表
  315. for (int i = 0; i < removeList.Count; i++)
  316. {
  317. cardList.Remove(removeList[i]);
  318. }
  319. removeList.Clear();
  320. //后处理翻牌的列表
  321. for (int i = 1; i < cardList.Count - 1; i++)
  322. {
  323. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  324. //翻回去
  325. Card card = (Card)item.m_card;
  326. card.Turn();
  327. item.m_flipEffect.visible = false;
  328. cardList[i - 1]["state"] = 0;
  329. UI_cardItem.ProxyEnd();
  330. if (cardList.Count % 2 == 0 && i == cardList.Count - 1)
  331. {
  332. UI_cardItem carditem = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  333. //翻回去
  334. Card cardLast = (Card)carditem.m_card;
  335. cardLast.Turn();
  336. carditem.m_flipEffect.visible = false;
  337. cardList[i]["state"] = 0;
  338. UI_cardItem.ProxyEnd();
  339. }
  340. }
  341. Timers.inst.Add(0.5f,1, UpdateCard);
  342. }
  343. //这里是处理已翻开的数量
  344. private void UpdateCard(object param = null)
  345. {
  346. Timers.inst.Remove(StartTime);
  347. //后处理翻牌的列表
  348. for (int i = cardList.Count - 1; i < cardList.Count; i++)
  349. {
  350. if( i-1 < 0)
  351. {
  352. break;
  353. }
  354. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
  355. //翻回去
  356. Card card = (Card)item.m_card;
  357. card.Turn();
  358. item.m_flipEffect.visible = false;
  359. cardList[i - 1]["state"] = 0;
  360. UI_cardItem.ProxyEnd();
  361. if (cardList.Count % 2 == 0 && i == cardList.Count - 1)
  362. {
  363. UI_cardItem carditem = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  364. //翻回去
  365. Card cardLast = (Card)carditem.m_card;
  366. cardLast.Turn();
  367. carditem.m_flipEffect.visible = false;
  368. cardList[i]["state"] = 0;
  369. UI_cardItem.ProxyEnd();
  370. }
  371. }
  372. if (cardList.Count % 2 == 0)
  373. {
  374. cardList.Clear();
  375. }
  376. else
  377. {
  378. int j = cardList.Count;
  379. for(int i = 0;i < j-1;i++)
  380. {
  381. cardList.RemoveAt(0);
  382. }
  383. }
  384. Timers.inst.Add(0.5f, 1, UpdateHit);
  385. }
  386. private void UpdateHit(object param = null)
  387. {
  388. StartHit = true;
  389. firstHit = true;
  390. canHit = true;
  391. _ui.m_cardList.touchable = true;
  392. Timers.inst.Remove(UpdateHit);
  393. }
  394. private void UpdateTime(object param = null)
  395. {
  396. timeIndex++;
  397. currentGameTime--;
  398. //_ui.m_ScareBar.value = currentGameTime;
  399. _ui.m_timeText.text = string.Format ("{0}", currentGameTime);
  400. if(currentGameTime <= 0)
  401. {
  402. Gameover(false);
  403. }
  404. }
  405. private void UpdateBar(object param = null)
  406. {
  407. barTime -= 0.1f;
  408. _ui.m_ScareBar.value = barTime;
  409. _ui.m_barEffect.x -= (880.0f/ (float)(gameTime*10));
  410. }
  411. private void Gameover(bool isPass = false)
  412. {
  413. Timers.inst.Remove(UpdateTime);
  414. Timers.inst.Remove(UpdateBar);
  415. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
  416. }
  417. //洗牌
  418. private void RandomCardList(List<int> array, int length)
  419. {
  420. int index;
  421. int value;
  422. for (int i = length - 1; i >= 0; i--)
  423. {
  424. index = rand.Next(0, i + 1);
  425. value = array[i];
  426. array[i] = array[index];
  427. array[index] = value;
  428. }
  429. }
  430. private void RandomGetNum()
  431. {
  432. int numCount = CardCfgArray.Instance.dataArray.Length;
  433. List<int> tArray = new List<int>();
  434. for(int i=0;i<numCount;i++)
  435. {
  436. tArray.Add(i);
  437. }
  438. RandomCardList(tArray, numCount);
  439. for(int i = 0;i <cardNum;i+=2)
  440. {
  441. cardArray[i] = tArray[i];
  442. cardArray[i + 1] = tArray[i];
  443. }
  444. }
  445. }
  446. }