FlipGameView.cs 13 KB

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