FlipGameView.cs 10.0 KB

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