FlipGameView.cs 10 KB

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