FlipGameView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 = 4;
  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 List<int> cardArray = new List<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,true);
  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 = ResPathUtil.GetMiniGamePicPath(CardCfgArray.Instance.dataArray[cardArray[index]].resName);
  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. cardNum = columns * rows;
  165. currentCardNum = cardNum;
  166. barTime = (float)gameTime;
  167. needNum = 2;
  168. activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
  169. rand = new System.Random();
  170. //洗牌
  171. RandomGetNum();
  172. RandomCardList(cardArray, cardArray.Count);
  173. //
  174. _ui.m_timeText.text = string.Format("{0}", currentGameTime);
  175. _ui.m_ScareBar.max = gameTime;
  176. _ui.m_ScareBar.min = 0;
  177. _ui.m_ScareBar.value = gameTime;
  178. _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);
  179. _ui.m_star2.SetPosition(((float)(gameTime - CustemsNum[1]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
  180. _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
  181. //计时器
  182. Timers.inst.Add(1.0f, 0, UpdateTime);
  183. //进度条计时器
  184. Timers.inst.Add(0.1f, 0, UpdateBar);
  185. }
  186. private void UpdateList()
  187. {
  188. _ui.m_cardList.columnCount = columns;
  189. _ui.m_cardList.numItems = cardNum;
  190. }
  191. private void OnClickCardItem(EventContext context)
  192. {
  193. GObject cardItem = context.sender as GObject;
  194. Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
  195. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
  196. Card card = (Card)item.m_card;
  197. if (cardList.Count == 0)
  198. {
  199. //当前翻开列表为空,直接处理翻开,添加
  200. cardInfo["state"] = 1;
  201. //翻牌
  202. card.Turn();
  203. _ui.m_cardList.touchable = false;
  204. Timers.inst.Add(waitTime, 1, canTouch);
  205. //
  206. cardList.Add(cardInfo);
  207. }
  208. else
  209. {
  210. bool isLast = false;
  211. for (int i = 0; i < cardList.Count; i++)
  212. {
  213. //点击到已翻开的就翻回去,
  214. if (cardInfo["index"] == cardList[i]["index"])
  215. {
  216. //翻牌
  217. card.Turn();
  218. _ui.m_cardList.touchable = false;
  219. Timers.inst.Add(waitTime, 1, canTouch);
  220. //
  221. cardList.RemoveAt(i);
  222. isLast = true;
  223. break;
  224. }
  225. }
  226. if (!isLast)
  227. {
  228. //翻牌
  229. card.Turn();
  230. _ui.m_cardList.touchable = false;
  231. cardInfo["state"] = 1;
  232. cardList.Add(cardInfo);
  233. Timers.inst.Add(waitTime, 1, UpdateCard);
  234. }
  235. }
  236. UI_cardItem.ProxyEnd();
  237. }
  238. //这里是处理已翻开的数量
  239. private void UpdateCard(object param = null)
  240. {
  241. bool isClear = true;
  242. for (int i = 1; i < cardList.Count; i++)
  243. {
  244. if(cardList[0]["id"] != cardList[i]["id"])
  245. {
  246. isClear = false;
  247. break;
  248. }
  249. isClear = true;
  250. }
  251. if(isClear)
  252. {
  253. if (cardList.Count == needNum)
  254. {
  255. for (int i = 0; i < cardList.Count; i++)
  256. {
  257. cardList[i]["state"] = 2;
  258. //消除
  259. _ui.m_cardList.GetChildAt(cardList[i]["index"]).visible = false;
  260. }
  261. currentCardNum -= cardList.Count;
  262. cardList.Clear();
  263. if(currentCardNum <= 0)
  264. {
  265. Gameover(true);
  266. }
  267. }
  268. _ui.m_cardList.touchable = true;
  269. }
  270. else
  271. {
  272. _ui.m_cardList.touchable = false;
  273. //= UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[0]["index"]));
  274. for (int i = 0; i < cardList.Count; i++)
  275. {
  276. UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
  277. //翻回去
  278. Card card = (Card)item.m_card;
  279. card.Turn();
  280. //card = null;
  281. UI_cardItem.ProxyEnd();
  282. }
  283. Timers.inst.Add(waitTime, 1, canTouch);
  284. cardList.Clear();
  285. }
  286. }
  287. private void UpdateTime(object param = null)
  288. {
  289. timeIndex++;
  290. currentGameTime--;
  291. //_ui.m_ScareBar.value = currentGameTime;
  292. _ui.m_timeText.text = string.Format ("{0}", currentGameTime);
  293. if(currentGameTime <= 0)
  294. {
  295. Gameover(false);
  296. }
  297. }
  298. private void UpdateBar(object param = null)
  299. {
  300. barTime -= 0.1f;
  301. _ui.m_ScareBar.value = barTime;
  302. }
  303. private void canTouch(object param = null)
  304. {
  305. _ui.m_cardList.touchable = true;
  306. }
  307. private void Gameover(bool isPass = false)
  308. {
  309. Timers.inst.Remove(UpdateTime);
  310. Timers.inst.Remove(UpdateBar);
  311. ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
  312. }
  313. //洗牌
  314. private void RandomCardList(List<int> array, int length)
  315. {
  316. int index;
  317. int value;
  318. for (int i = length - 1; i >= 0; i--)
  319. {
  320. index = rand.Next(0, i + 1);
  321. value = array[i];
  322. array[i] = array[index];
  323. array[index] = value;
  324. }
  325. }
  326. private void RandomGetNum()
  327. {
  328. int numCount = CardCfgArray.Instance.dataArray.Length;
  329. List<int> tArray = new List<int>();
  330. for(int i=0;i<numCount;i++)
  331. {
  332. tArray.Add(i);
  333. }
  334. RandomCardList(tArray, numCount);
  335. for(int i = 0;i <cardNum;i+=2)
  336. {
  337. cardArray[i] = tArray[i];
  338. cardArray[i + 1] = tArray[i];
  339. }
  340. }
  341. }
  342. }