FlipGameView.cs 15 KB

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