FlipGameView.cs 14 KB

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