FlipGameView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. //cardItem.m_card.GetChild("icon").icon = "ui://MiniGame/";
  130. if (cardItem.target.data == null)
  131. {
  132. cardItem.target.onClick.Add(OnClickCardItem);
  133. }
  134. _c1 = (Card)cardItem.m_card;
  135. _c1.opened = false;
  136. _c1.SetPerspective();
  137. itemInfo.Add("index", index);
  138. itemInfo.Add("id", cardArray[index]);
  139. itemInfo.Add("state", 0);
  140. cardItem.m_id.text = itemInfo["id"].ToString();
  141. cardItem.target.data = itemInfo;
  142. UI_cardItem.ProxyEnd();
  143. }
  144. private void UpdateView()
  145. {
  146. gameTime = CardGameArray.Instance.GetCfg(gameID).time;
  147. for(int i = 0;i<3;i++)
  148. {
  149. switch (i)
  150. {
  151. case 0:
  152. CustemsNum[0] = gameDate.timeStar1;
  153. break;
  154. case 1:
  155. CustemsNum[1] = gameDate.timeStar2;
  156. break;
  157. case 2:
  158. CustemsNum[2] = gameDate.timeStar3;
  159. break;
  160. }
  161. }
  162. timeIndex = 0;
  163. currentGameTime = gameTime;
  164. currentCardNum = cardNum;
  165. barTime = (float)gameTime;
  166. needNum = 2;
  167. activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
  168. rand = new System.Random();
  169. //洗牌
  170. RandomCardList(cardArray, cardArray.Length);
  171. //
  172. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  173. _ui.m_ScareBar.max = gameTime;
  174. _ui.m_ScareBar.min = 0;
  175. _ui.m_ScareBar.value = gameTime;
  176. _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);
  177. _ui.m_star2.SetPosition(((float)(gameTime - CustemsNum[1]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
  178. _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
  179. //计时器
  180. Timers.inst.Add(1.0f, 0, UpdateTime);
  181. //进度条计时器
  182. Timers.inst.Add(0.1f, 0, UpdateBar);
  183. }
  184. private void UpdateList()
  185. {
  186. _ui.m_cardList.columnCount = columns;
  187. _ui.m_cardList.numItems = cardNum;
  188. }
  189. private void OnClickCardItem(EventContext context)
  190. {
  191. GObject cardItem = context.sender as GObject;
  192. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  193. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  194. Card card = (Card)item.m_card;
  195. if (cardList.Count == 0)
  196. {
  197. //当前翻开列表为空,直接处理翻开,添加
  198. cardInfo["state"] = 1;
  199. //翻牌
  200. card.Turn();
  201. _ui.m_cardList.touchable = false;
  202. Timers.inst.Add(waitTime, 1, canTouch);
  203. //
  204. cardList.Add(cardInfo);
  205. }
  206. else
  207. {
  208. bool isLast = false;
  209. for (int i = 0; i < cardList.Count; i++)
  210. {
  211. //点击到已翻开的就翻回去,
  212. if (cardInfo["index"] == cardList[i]["index"])
  213. {
  214. //翻牌
  215. card.Turn();
  216. _ui.m_cardList.touchable = false;
  217. Timers.inst.Add(waitTime, 1, canTouch);
  218. //
  219. cardList.RemoveAt(i);
  220. isLast = true;
  221. break;
  222. }
  223. }
  224. if (!isLast)
  225. {
  226. //翻牌
  227. card.Turn();
  228. _ui.m_cardList.touchable = false;
  229. cardInfo["state"] = 1;
  230. cardList.Add(cardInfo);
  231. Timers.inst.Add(waitTime, 1, UpdateCard);
  232. }
  233. }
  234. UI_cardItem.ProxyEnd();
  235. }
  236. //这里是处理已翻开的数量
  237. private void UpdateCard(object param = null)
  238. {
  239. bool isClear = true;
  240. for (int i = 1; i < cardList.Count; i++)
  241. {
  242. if(cardList[0]["id"] != cardList[i]["id"])
  243. {
  244. isClear = false;
  245. break;
  246. }
  247. isClear = true;
  248. }
  249. if(isClear)
  250. {
  251. if (cardList.Count == needNum)
  252. {
  253. for (int i = 0; i < cardList.Count; i++)
  254. {
  255. cardList[i]["state"] = 2;
  256. //消除
  257. _ui.m_cardList.GetChildAt(cardList[i]["index"]).visible = false;
  258. }
  259. currentCardNum -= cardList.Count;
  260. cardList.Clear();
  261. if(currentCardNum <= 0)
  262. {
  263. Gameover(true);
  264. }
  265. }
  266. _ui.m_cardList.touchable = true;
  267. }
  268. else
  269. {
  270. _ui.m_cardList.touchable = false;
  271. //= UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[0]["index"]));
  272. for (int i = 0; i < cardList.Count; i++)
  273. {
  274. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  275. //翻回去
  276. Card card = (Card)item.m_card;
  277. card.Turn();
  278. //card = null;
  279. UI_cardItem.ProxyEnd();
  280. }
  281. Timers.inst.Add(waitTime, 1, canTouch);
  282. cardList.Clear();
  283. }
  284. }
  285. private void UpdateTime(object param = null)
  286. {
  287. timeIndex++;
  288. currentGameTime--;
  289. //_ui.m_ScareBar.value = currentGameTime;
  290. _ui.m_timeText.text = string.Format ("{0}", currentGameTime);
  291. if(currentGameTime <= 0)
  292. {
  293. Gameover(false);
  294. }
  295. }
  296. private void UpdateBar(object param = null)
  297. {
  298. barTime -= 0.1f;
  299. _ui.m_ScareBar.value = barTime;
  300. }
  301. private void canTouch(object param = null)
  302. {
  303. _ui.m_cardList.touchable = true;
  304. }
  305. private void Gameover(bool isPass = false)
  306. {
  307. Timers.inst.Remove(UpdateTime);
  308. Timers.inst.Remove(UpdateBar);
  309. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
  310. }
  311. //洗牌
  312. private void RandomCardList(int[] array, int length)
  313. {
  314. int index;
  315. int value;
  316. for (int i = length - 1; i >= 0; i--)
  317. {
  318. index = rand.Next(0, i + 1);
  319. value = array[i];
  320. array[i] = array[index];
  321. array[index] = value;
  322. }
  323. }
  324. }
  325. }