FlipGameView.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 = 2;
  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 , 7, 7 , 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. //CustemsNum = ;
  132. //计时器
  133. Timers.inst.Add(1.0f, 0, UpdateTime);
  134. }
  135. private void UpdateList()
  136. {
  137. _ui.m_cardList.columnCount = columns;
  138. _ui.m_cardList.numItems = cardNum;
  139. }
  140. private void OnClickCardItem(EventContext context)
  141. {
  142. GObject cardItem = context.sender as GObject;
  143. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  144. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  145. Card card = (Card)item.m_card;
  146. if (cardList.Count == 0)
  147. {
  148. //当前翻开列表为空,直接处理翻开,添加
  149. cardInfo["state"] = 1;
  150. //翻牌
  151. card.Turn();
  152. _ui.m_cardList.touchable = false;
  153. Timers.inst.Add(waitTime, 1, canTouch);
  154. //
  155. cardList.Add(cardInfo);
  156. }
  157. else
  158. {
  159. bool isLast = false;
  160. for (int i = 0; i < cardList.Count; i++)
  161. {
  162. //点击到已翻开的就翻回去,
  163. if (cardInfo["index"] == cardList[i]["index"])
  164. {
  165. //翻牌
  166. card.Turn();
  167. _ui.m_cardList.touchable = false;
  168. Timers.inst.Add(waitTime, 1, canTouch);
  169. //
  170. cardList.RemoveAt(i);
  171. isLast = true;
  172. break;
  173. }
  174. }
  175. if (!isLast)
  176. {
  177. //翻牌
  178. card.Turn();
  179. _ui.m_cardList.touchable = false;
  180. cardInfo["state"] = 1;
  181. cardList.Add(cardInfo);
  182. Timers.inst.Add(waitTime, 1, UpdateCard);
  183. }
  184. }
  185. UI_cardItem.ProxyEnd();
  186. }
  187. //这里是处理已翻开的数量
  188. private void UpdateCard(object param = null)
  189. {
  190. bool isClear = true;
  191. for (int i = 1; i < cardList.Count; i++)
  192. {
  193. if(cardList[0]["id"] != cardList[i]["id"])
  194. {
  195. isClear = false;
  196. break;
  197. }
  198. isClear = true;
  199. }
  200. if(isClear)
  201. {
  202. if (cardList.Count == needNum)
  203. {
  204. for (int i = 0; i < cardList.Count; i++)
  205. {
  206. cardList[i]["state"] = 2;
  207. //消除
  208. _ui.m_cardList.GetChildAt(cardList[i]["index"]).visible = false;
  209. }
  210. currentCardNum -= cardList.Count;
  211. cardList.Clear();
  212. _ui.m_cardList.touchable = true;
  213. if(currentCardNum <= 0)
  214. {
  215. Gameover(true);
  216. }
  217. }
  218. }
  219. else
  220. {
  221. _ui.m_cardList.touchable = false;
  222. //= UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[0]["index"]));
  223. for (int i = 0; i < cardList.Count; i++)
  224. {
  225. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  226. //翻回去
  227. Card card = (Card)item.m_card;
  228. card.Turn();
  229. //card = null;
  230. UI_cardItem.ProxyEnd();
  231. }
  232. Timers.inst.Add(waitTime, 1, canTouch);
  233. cardList.Clear();
  234. }
  235. }
  236. private void UpdateTime(object param = null)
  237. {
  238. currentGameTime--;
  239. _ui.m_ScareBar.value = currentGameTime;
  240. _ui.m_timeText.text = string.Format ("倒计时:{0}", currentGameTime);
  241. if(currentGameTime <= 0)
  242. {
  243. Timers.inst.Remove(UpdateTime);
  244. Gameover(false);
  245. }
  246. }
  247. private void canTouch(object param = null)
  248. {
  249. _ui.m_cardList.touchable = true;
  250. }
  251. private void Gameover(bool isPass = false)
  252. {
  253. Timers.inst.Remove(UpdateTime);
  254. if (!isPass)
  255. {
  256. AlertUI.Show("游戏时间到")
  257. .SetLeftButton(false)
  258. .SetRightButton(true, "确定", (object data) =>
  259. {
  260. this.Hide();
  261. });
  262. }
  263. else
  264. {
  265. string custemsName = "";
  266. for(int i = CustemsNum.Count - 1; i>=0; i--)
  267. {
  268. if(currentGameTime >= CustemsNum[i])
  269. {
  270. custemsName = CustemsName[i];
  271. break;
  272. }
  273. }
  274. AlertUI.Show(string.Format("恭喜通过,所用时长:{0},通关评价:{1}", currentGameTime, custemsName))
  275. .SetLeftButton(false)
  276. .SetRightButton(true, "确定", (object data) =>
  277. {
  278. this.Hide();
  279. });
  280. }
  281. }
  282. //洗牌
  283. private void RandomCardList(int[] array, int length)
  284. {
  285. int index;
  286. int value;
  287. for (int i = length - 1; i >= 0; i--)
  288. {
  289. index = rand.Next(0, i + 1);
  290. value = array[i];
  291. array[i] = array[index];
  292. array[index] = value;
  293. }
  294. }
  295. }
  296. }