FlipGameView.cs 12 KB

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