FlipGameView.cs 10 KB

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