FlipGameView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 : BaseView
  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 = 0;
  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. //假数据
  50. private int[] cardArray = new int[] { 1,1, 2,2, 3, 3 , 4, 4 , 5, 5 , 6, 6 , 8, 8 , 8, 8 };
  51. Card _c1 = new Card();
  52. private float waitTime = 0.6f;
  53. public override void Dispose()
  54. {
  55. if (_ui != null)
  56. {
  57. _ui.Dispose();
  58. _ui = null;
  59. }
  60. base.Dispose();
  61. }
  62. protected override void OnInit()
  63. {
  64. base.OnInit();
  65. packageName = UI_FlipGameUI.PACKAGE_NAME;
  66. _ui = UI_FlipGameUI.Create();
  67. this.viewCom = _ui.target;
  68. isfullScreen = true;
  69. _ui.m_cardList.itemRenderer = ListCardItem;
  70. _ui.m_back.onClick.Add(OnClickBtnBack);
  71. UIObjectFactory.SetPackageItemExtension("ui://MiniGame/CardComponent", typeof(Card));
  72. }
  73. protected override void OnShown()
  74. {
  75. base.OnShown();
  76. if ((this.viewData as object[]).Length != 0 && this.viewData != null)
  77. {
  78. gameID = (int)(this.viewData as object[])[0];
  79. }
  80. else
  81. {
  82. gameID = 10001;
  83. }
  84. gameDate = CardGameArray.Instance.GetCfg(gameID);
  85. UpdateView();
  86. UpdateList();
  87. }
  88. protected override void OnHide()
  89. {
  90. _ui.m_cardList.RemoveChildrenToPool();
  91. base.OnHide();
  92. cardList.Clear();
  93. Timers.inst.Remove(UpdateTime);
  94. Timers.inst.Remove(canTouch);
  95. Timers.inst.Remove(UpdateCard);
  96. Timers.inst.Remove(UpdateBar);
  97. }
  98. private void OnClickBtnBack()
  99. {
  100. Timers.inst.Remove(UpdateTime);
  101. Timers.inst.Remove(UpdateBar);
  102. AlertUI.Show("是否退出游戏")
  103. .SetLeftButton(true, "取消", (object data) =>
  104. {
  105. Timers.inst.Add(1.0f, 0, UpdateTime);
  106. Timers.inst.Add(0.1f, 0, UpdateBar);
  107. })
  108. .SetRightButton(true, "确定", (object data) =>
  109. {
  110. this.Hide();
  111. });
  112. }
  113. private void ListCardItem(int index, GObject item)
  114. {
  115. UI_cardItem cardItem = UI_cardItem.Proxy(item);
  116. Dictionary<string, int> itemInfo = new Dictionary<string, int>();
  117. //备注的后续都读表
  118. //cardItem.m_icon.url = "";
  119. if (cardItem.target.data == null)
  120. {
  121. cardItem.target.onClick.Add(OnClickCardItem);
  122. }
  123. _c1 = (Card)cardItem.m_card;
  124. _c1.opened = false;
  125. _c1.SetPerspective();
  126. itemInfo.Add("index", index);
  127. itemInfo.Add("id", cardArray[index]);
  128. itemInfo.Add("state", 0);
  129. cardItem.m_id.text = itemInfo["id"].ToString();
  130. cardItem.target.data = itemInfo;
  131. UI_cardItem.ProxyEnd();
  132. }
  133. private void UpdateView()
  134. {
  135. gameTime = CardGameArray.Instance.GetCfg(gameID).time;
  136. for(int i = 0;i<3;i++)
  137. {
  138. switch (i)
  139. {
  140. case 0:
  141. CustemsNum[0] = gameDate.timeStar1;
  142. break;
  143. case 1:
  144. CustemsNum[1] = gameDate.timeStar2;
  145. break;
  146. case 2:
  147. CustemsNum[2] = gameDate.timeStar3;
  148. break;
  149. }
  150. }
  151. timeIndex = 0;
  152. currentGameTime = gameTime;
  153. currentCardNum = cardNum;
  154. barTime = (float)gameTime;
  155. needNum = 2;
  156. rand = new System.Random();
  157. //洗牌
  158. RandomCardList(cardArray, cardArray.Length);
  159. //
  160. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  161. _ui.m_ScareBar.max = gameTime;
  162. _ui.m_ScareBar.min = 0;
  163. _ui.m_ScareBar.value = gameTime;
  164. _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);
  165. _ui.m_star2.SetPosition(((float)(gameTime - CustemsNum[1]) / (float)gameTime) * _ui.m_ScareBar.width + 70, _ui.m_star1.position.y, _ui.m_star1.position.z);
  166. _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width + 70, _ui.m_star1.position.y, _ui.m_star1.position.z);
  167. //计时器
  168. Timers.inst.Add(1.0f, 0, UpdateTime);
  169. //进度条计时器
  170. Timers.inst.Add(0.1f, 0, UpdateBar);
  171. }
  172. private void UpdateList()
  173. {
  174. _ui.m_cardList.columnCount = columns;
  175. _ui.m_cardList.numItems = cardNum;
  176. }
  177. private void OnClickCardItem(EventContext context)
  178. {
  179. GObject cardItem = context.sender as GObject;
  180. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  181. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  182. Card card = (Card)item.m_card;
  183. if (cardList.Count == 0)
  184. {
  185. //当前翻开列表为空,直接处理翻开,添加
  186. cardInfo["state"] = 1;
  187. //翻牌
  188. card.Turn();
  189. _ui.m_cardList.touchable = false;
  190. Timers.inst.Add(waitTime, 1, canTouch);
  191. //
  192. cardList.Add(cardInfo);
  193. }
  194. else
  195. {
  196. bool isLast = false;
  197. for (int i = 0; i < cardList.Count; i++)
  198. {
  199. //点击到已翻开的就翻回去,
  200. if (cardInfo["index"] == cardList[i]["index"])
  201. {
  202. //翻牌
  203. card.Turn();
  204. _ui.m_cardList.touchable = false;
  205. Timers.inst.Add(waitTime, 1, canTouch);
  206. //
  207. cardList.RemoveAt(i);
  208. isLast = true;
  209. break;
  210. }
  211. }
  212. if (!isLast)
  213. {
  214. //翻牌
  215. card.Turn();
  216. _ui.m_cardList.touchable = false;
  217. cardInfo["state"] = 1;
  218. cardList.Add(cardInfo);
  219. Timers.inst.Add(waitTime, 1, UpdateCard);
  220. }
  221. }
  222. UI_cardItem.ProxyEnd();
  223. }
  224. //这里是处理已翻开的数量
  225. private void UpdateCard(object param = null)
  226. {
  227. bool isClear = true;
  228. for (int i = 1; i < cardList.Count; i++)
  229. {
  230. if(cardList[0]["id"] != cardList[i]["id"])
  231. {
  232. isClear = false;
  233. break;
  234. }
  235. isClear = true;
  236. }
  237. if(isClear)
  238. {
  239. if (cardList.Count == needNum)
  240. {
  241. for (int i = 0; i < cardList.Count; i++)
  242. {
  243. cardList[i]["state"] = 2;
  244. //消除
  245. _ui.m_cardList.GetChildAt(cardList[i]["index"]).visible = false;
  246. }
  247. currentCardNum -= cardList.Count;
  248. cardList.Clear();
  249. if(currentCardNum <= 0)
  250. {
  251. Gameover(true);
  252. }
  253. }
  254. _ui.m_cardList.touchable = true;
  255. }
  256. else
  257. {
  258. _ui.m_cardList.touchable = false;
  259. //= UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[0]["index"]));
  260. for (int i = 0; i < cardList.Count; i++)
  261. {
  262. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  263. //翻回去
  264. Card card = (Card)item.m_card;
  265. card.Turn();
  266. //card = null;
  267. UI_cardItem.ProxyEnd();
  268. }
  269. Timers.inst.Add(waitTime, 1, canTouch);
  270. cardList.Clear();
  271. }
  272. }
  273. private void UpdateTime(object param = null)
  274. {
  275. timeIndex++;
  276. currentGameTime--;
  277. //_ui.m_ScareBar.value = currentGameTime;
  278. _ui.m_timeText.text = string.Format ("{0}", currentGameTime);
  279. if(currentGameTime <= 0)
  280. {
  281. Gameover(false);
  282. }
  283. }
  284. private void UpdateBar(object param = null)
  285. {
  286. barTime -= 0.1f;
  287. _ui.m_ScareBar.value = barTime;
  288. }
  289. private void canTouch(object param = null)
  290. {
  291. _ui.m_cardList.touchable = true;
  292. }
  293. private void Gameover(bool isPass = false)
  294. {
  295. Timers.inst.Remove(UpdateTime);
  296. Timers.inst.Remove(UpdateBar);
  297. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
  298. }
  299. //洗牌
  300. private void RandomCardList(int[] array, int length)
  301. {
  302. int index;
  303. int value;
  304. for (int i = length - 1; i >= 0; i--)
  305. {
  306. index = rand.Next(0, i + 1);
  307. value = array[i];
  308. array[i] = array[index];
  309. array[index] = value;
  310. }
  311. }
  312. }
  313. }