TZFEGameVIew.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 TZFEGameView : BaseWindow
  10. {
  11. private UI_TZFEGameView _ui;
  12. private int[,] Map;
  13. enum Direction { up, down, left, right};
  14. private Vector2 touchFirst = Vector2.zero;//手指开始按下的位置
  15. private Vector2 touchSecond = Vector2.zero;//手指拖动的位置
  16. private System.Random rand;
  17. //这个是数组的行数和列数,mapLength一般采用行列一样
  18. private int rows = 4;
  19. private int columns = 4;
  20. private int mapLength = 5;
  21. //得分
  22. private int score = 0;
  23. private int scoreMax = 0;
  24. //目标分数
  25. private int targetNum = 128;
  26. private int gameID;
  27. private Merge2048Game gameDate;
  28. private List<ActivityOpenCfg> activityGameDate;
  29. //评价
  30. private List<int> CustemsNum = new List<int>()
  31. {
  32. 0,
  33. 0,
  34. 0,
  35. };
  36. private bool isMove;
  37. private bool isMerge;
  38. private struct NumPos
  39. {
  40. public int x;
  41. public int y;
  42. public bool isCreat;
  43. }
  44. //需要播放动效的列表
  45. private List<NumPos> numPosArray = new List<NumPos>();
  46. private int time = 0;
  47. public override void Dispose()
  48. {
  49. if (_ui != null)
  50. {
  51. _ui.Dispose();
  52. _ui = null;
  53. }
  54. base.Dispose();
  55. }
  56. protected override void OnInit()
  57. {
  58. base.OnInit();
  59. packageName = UI_TZFEGameView.PACKAGE_NAME;
  60. _ui = UI_TZFEGameView.Create();
  61. this.viewCom = _ui.target;
  62. isfullScreen = true;
  63. _ui.m_numList.itemRenderer = ListNumItem;
  64. _ui.m_numList.onTouchBegin.Add(OnClickBegin);
  65. _ui.m_numList.onTouchEnd.Add(OnClickEnd);
  66. _ui.m_backBtn.onClick.Add(OnClickBtnBack);
  67. _ui.m_mergeBtn.onClick.Add(OnClickMergeTips);
  68. Map = new int[rows, columns];
  69. }
  70. protected override void OnShown()
  71. {
  72. base.OnShown();
  73. if ((this.viewData as object[]).Length != 0 && this.viewData != null)
  74. {
  75. gameID = (int)(this.viewData as object[])[0];
  76. }
  77. else
  78. {
  79. gameID = 128;
  80. }
  81. gameDate = Merge2048GameArray.Instance.GetCfg(gameID);
  82. activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
  83. InitMap();
  84. UpdateView();
  85. UpdateList();
  86. }
  87. protected override void OnHide()
  88. {
  89. Timers.inst.Remove(UpdateTime);
  90. base.OnHide();
  91. }
  92. private void OnClickBtnBack()
  93. {
  94. string exitTip;
  95. if (gameDate.bonusLoseArr.Length == 0)
  96. {
  97. exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  98. }
  99. else
  100. {
  101. exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
  102. }
  103. AlertUI.Show(exitTip)
  104. .SetLeftButton(true, "取消", (object data) =>
  105. {
  106. })
  107. .SetRightButton(true, "确定",async (object data) =>
  108. {
  109. var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.type, time, false, activityGameDate[0].id,true);
  110. if (!result) return;
  111. this.Hide();
  112. });
  113. }
  114. private void InitMap()
  115. {
  116. for(int i = 0; i < rows; i++)
  117. for(int j = 0; j < columns; j++)
  118. {
  119. Map[i, j] = 0;
  120. }
  121. }
  122. private void UpdateView()
  123. {
  124. time = 1;
  125. score = 0;
  126. targetNum = Merge2048GameArray.Instance.GetCfg(gameID).targetNum;
  127. CustemsNum[0] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar3;
  128. CustemsNum[1] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar2;
  129. CustemsNum[2] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar1;
  130. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  131. _ui.m_target.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, targetNum)[0].resName);
  132. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  133. _ui.m_score.visible = false;
  134. _ui.m_time.visible = false;
  135. _ui.m_timeNum.visible = false;
  136. rand = new System.Random();
  137. UpdateStar();
  138. Timers.inst.Add(1.0f, 0, UpdateTime);
  139. }
  140. private void UpdateScore()
  141. {
  142. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  143. }
  144. private void UpdateList()
  145. {
  146. RandomCreateNum();
  147. _ui.m_numList.numItems = rows * columns;
  148. }
  149. private void ListNumItem(int index, GObject item)
  150. {
  151. UI_numItem numItem = UI_numItem.Proxy(item);
  152. int x = index / rows;
  153. int y = index % columns;
  154. if(Map[x,y] == 0)
  155. {
  156. numItem.m_icon.visible = false;
  157. }
  158. else
  159. {
  160. //numItem.m_icon.url = string.Format("ui://MiniGame/sgll2_{0}", Map[x, y]);
  161. numItem.m_icon.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, Map[x, y])[0].resName);
  162. numItem.m_icon.visible = true;
  163. }
  164. //播放生成和合并动效
  165. for(int i=0; i<numPosArray.Count; i++)
  166. {
  167. if(x == numPosArray[i].x && y == numPosArray[i].y)
  168. {
  169. if(numPosArray[i].isCreat)
  170. {
  171. numItem.m_t0.Play();
  172. numPosArray.RemoveAt(i);
  173. break;
  174. }
  175. else
  176. {
  177. numItem.m_t1.Play();
  178. numPosArray.RemoveAt(i);
  179. break;
  180. }
  181. }
  182. }
  183. UI_numItem.ProxyEnd();
  184. }
  185. //随机生成数字2(%90),4(%10)
  186. private void RandomCreateNum()
  187. {
  188. bool gameOver = false;
  189. for(int i = 0;i< rows; i++)
  190. {
  191. for(int j = 0; j< columns; j++)
  192. {
  193. if (Map[i, j] == 0)
  194. {
  195. gameOver = true;
  196. break;
  197. }
  198. }
  199. }
  200. if(!gameOver)
  201. {
  202. //GameOver(false);
  203. return;
  204. }
  205. while (true)
  206. {
  207. int x = rand.Next(0, rows);
  208. int y = rand.Next(0, columns);
  209. NumPos item;
  210. int num;
  211. int randNum = rand.Next(1, 11);
  212. if(randNum <= 9)
  213. {
  214. num = 2;
  215. }
  216. else
  217. {
  218. num = 4 ;
  219. }
  220. if (Map[x,y] == 0)
  221. {
  222. Map[x, y] = num;
  223. item.x = x;
  224. item.y = y;
  225. item.isCreat = true;
  226. numPosArray.Add(item);
  227. break;
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// 去零
  233. /// </summary>
  234. /// <param name="row">对于一行或一列元素</param>
  235. private void Remove0(int[] row,Direction dir,int xy = 0)
  236. {
  237. int pos = 0;
  238. int[] rowB = new int[row.Length];
  239. for (int i = 0; i < row.Length; i++)
  240. {
  241. rowB[i] = row[i];
  242. }
  243. for (int i = 0; i < row.Length; ++i)
  244. {
  245. if (row[i] != 0)
  246. {
  247. row[pos] = row[i];
  248. //-----这里修改需要播放动效的数字位置列表-----
  249. int x = 0;
  250. int y = 0;
  251. int nextX = 0;
  252. int nextY = 0;
  253. for (int t = 0; t < numPosArray.Count; t++)
  254. {
  255. switch (dir)
  256. {
  257. case Direction.up:
  258. x = i;
  259. y = xy;
  260. nextX = pos;
  261. nextY = xy;
  262. break;
  263. case Direction.down:
  264. x = row.Length - 1 - i;
  265. y = xy;
  266. nextX = row.Length - 1 - pos;
  267. nextY = xy;
  268. break;
  269. case Direction.left:
  270. x = xy;
  271. y = i;
  272. nextX = xy;
  273. nextY = pos;
  274. break;
  275. case Direction.right:
  276. x = xy;
  277. y = row.Length - 1 - i;
  278. nextX = xy;
  279. nextY = row.Length - 1 - pos;
  280. break;
  281. }
  282. if(numPosArray.Count != 0 && numPosArray[t].x == x && numPosArray[t].y == y && pos != i)
  283. {
  284. numPosArray.RemoveAt(t);
  285. NumPos item;
  286. item.x = nextX;
  287. item.y = nextY;
  288. item.isCreat = false;
  289. numPosArray.Add(item);
  290. }
  291. }
  292. //---------------------------------------
  293. pos++;
  294. }
  295. }
  296. for (; pos < row.Length; ++pos) row[pos] = 0;
  297. for(int i= 0; i< row.Length; i++)
  298. {
  299. if(row[i] != rowB[i])
  300. {
  301. isMove = true;
  302. }
  303. }
  304. }
  305. /// <summary>
  306. /// 合并
  307. /// </summary>
  308. /// <param name="row">对于一行或一列元素,完成一次向左合并的操作</param>
  309. private void Merge(int[] row , Direction dir, int xy)
  310. {
  311. Remove0(row,dir,xy);
  312. // 相邻相同则合并
  313. for (int i = 0; i < row.Length - 1; ++i)
  314. {
  315. if (row[i] != 0 && row[i] == row[i + 1])
  316. {
  317. row[i] *= 2;
  318. row[i + 1] = 0;
  319. //将合并的数字放入列表
  320. NumPos item;
  321. item = MoveAddNum(i, xy, dir, row.Length-1);
  322. numPosArray.Add(item);
  323. //-------
  324. score += row[i];
  325. UpdateScore();
  326. if (row[i] == targetNum)
  327. {
  328. //游戏成功
  329. GameOver(true);
  330. }
  331. isMerge = true;
  332. }
  333. }
  334. Remove0(row,dir,xy);
  335. }
  336. //将合并的数字位置放入列表的准备
  337. private NumPos MoveAddNum(int i, int xy, Direction dir,int length)
  338. {
  339. NumPos item = new NumPos();
  340. switch (dir)
  341. {
  342. case Direction.up:
  343. item.x = i;
  344. item.y = xy;
  345. item.isCreat = false;
  346. break;
  347. case Direction.down:
  348. item.x = length - i;
  349. item.y = xy;
  350. item.isCreat = false;
  351. break;
  352. case Direction.left:
  353. item.x = xy;
  354. item.y = i;
  355. item.isCreat = false;
  356. break;
  357. case Direction.right:
  358. item.x = xy;
  359. item.y = length - i;
  360. item.isCreat = false;
  361. break;
  362. }
  363. return item;
  364. }
  365. /// <summary>
  366. /// 上移
  367. /// </summary>
  368. /// <param name="map">原棋盘</param>
  369. /// <returns></returns>
  370. private void Up(int[,] map)
  371. {
  372. int[] arr = new int[rows];
  373. for (int j = 0; j < columns; ++j)
  374. {
  375. for (int i = 0; i < rows; ++i)
  376. {
  377. arr[i] = map[i, j];
  378. }
  379. Merge(arr,Direction.up,j);
  380. for (int i = 0; i < rows; ++i) map[i, j] = arr[i];
  381. }
  382. }
  383. /// <summary>
  384. /// 下移
  385. /// </summary>
  386. private int[,] Down(int[,] map)
  387. {
  388. int[] arr = new int[rows];
  389. for (int j = 0; j < columns; ++j)
  390. {
  391. for (int i = 0; i < rows; ++i)
  392. {
  393. arr[rows - 1 - i] = map[i, j];
  394. }
  395. Merge(arr,Direction.down ,j);
  396. for (int i = 0; i < rows; ++i) map[i, j] = arr[rows - 1 - i];
  397. }
  398. return map;
  399. }
  400. /// <summary>
  401. /// 左移
  402. /// </summary>
  403. private int[,] Left(int[,] map)
  404. {
  405. int[] arr = new int[columns];
  406. for (int i = 0; i < rows; ++i)
  407. {
  408. for (int j = 0; j < columns; ++j)
  409. {
  410. arr[j] = map[i, j];
  411. }
  412. Merge(arr,Direction.left,i);
  413. for (int j = 0; j < columns; ++j) map[i, j] = arr[j];
  414. }
  415. return map;
  416. }
  417. /// <summary>
  418. /// 右移
  419. /// </summary>
  420. private int[,] Right(int[,] map)
  421. {
  422. int[] arr = new int[columns];
  423. for (int i = 0; i < rows; ++i)
  424. {
  425. for (int j = 0; j < columns; ++j)
  426. {
  427. arr[columns - 1 - j] = map[i, j];
  428. }
  429. Merge(arr,Direction.right,i);
  430. for (int j = 0; j < columns; ++j) map[i, j] = arr[columns - 1 - j];
  431. }
  432. return map;
  433. }
  434. /// <summary>
  435. /// 进行一次移动操作
  436. /// </summary>
  437. /// <param name="map">原棋盘</param>
  438. /// <param name="dir">移动的方向(枚举)</param>
  439. private void Move(int[,] map, Direction dir)
  440. {
  441. switch (dir)
  442. {
  443. case Direction.up:
  444. Up(map); break;
  445. case Direction.down:
  446. Down(map); break;
  447. case Direction.left:
  448. Left(map); break;
  449. case Direction.right:
  450. Right(map); break;
  451. }
  452. if(isMerge || isMove)
  453. {
  454. isMove = false;
  455. isMerge = false;
  456. UpdateList();
  457. numPosArray.Clear();
  458. }
  459. else
  460. {
  461. CheckArray();
  462. }
  463. }
  464. private void OnClickBegin()
  465. {
  466. touchFirst = Input.mousePosition;//记录开始按下的位置
  467. }
  468. private void OnClickEnd()
  469. {
  470. touchSecond = Input.mousePosition;//记录拖动的位置
  471. if ((touchSecond.x - touchFirst.x) < -80 && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  472. {
  473. //向左滑动
  474. Move(Map, Direction.left);
  475. }
  476. if ( (touchSecond.x - touchFirst.x) > 80 && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  477. {
  478. //向右滑动
  479. Move(Map, Direction.right);
  480. }
  481. if ((touchSecond.y - touchFirst.y) < -80 && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  482. {
  483. //向下滑动
  484. Move(Map, Direction.down);
  485. }
  486. if ((touchSecond.y - touchFirst.y) > 80 && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  487. {
  488. //向上滑动
  489. Move(Map, Direction.up);
  490. }
  491. touchFirst = touchSecond;
  492. }
  493. private void OnClickMergeTips()
  494. {
  495. ViewManager.Show<SyntheticRoutetipView>(new object[] { targetNum , gameDate.subType}) ;
  496. }
  497. private void UpdateTime(object param = null)
  498. {
  499. _ui.m_timeNum.text = sec_to_hms(time);
  500. time++;
  501. //UpdateStar();
  502. }
  503. private void UpdateStar()
  504. {
  505. int starNum = 0;
  506. for (int i = CustemsNum.Count - 1; i >= 0; i--)
  507. {
  508. if (time <= CustemsNum[i])
  509. {
  510. starNum += 1;
  511. }
  512. }
  513. switch (starNum)
  514. {
  515. case 1:
  516. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  517. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star_grey";
  518. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  519. break;
  520. case 2:
  521. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  522. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  523. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  524. break;
  525. case 3:
  526. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  527. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  528. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  529. break;
  530. default:
  531. _ui.m_star1.visible = false;
  532. _ui.m_star2.visible = false;
  533. _ui.m_star3.visible = false;
  534. break;
  535. }
  536. }
  537. //将秒数转化为时分秒 duration为秒数
  538. private string sec_to_hms(int duration)
  539. {
  540. TimeSpan ts = new TimeSpan(0, 0, duration);
  541. int _hours = 0;
  542. if (ts.Days > 0)
  543. {
  544. _hours = ts.Days * 24;
  545. }
  546. string str = "";
  547. if (ts.Hours > 0)
  548. {
  549. str = String.Format("{0:00}", ts.Hours + _hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  550. }
  551. if (ts.Hours == 0 && ts.Minutes > 0)
  552. {
  553. str = "00:";
  554. if (_hours > 0)
  555. {
  556. str = String.Format("{0:00}", ts.Hours + _hours) + ":";
  557. }
  558. str += String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  559. }
  560. if (ts.Hours == 0 && ts.Minutes == 0)
  561. {
  562. str = "00";
  563. if (_hours > 0)
  564. {
  565. str = String.Format("{0:00}", ts.Hours + _hours);
  566. }
  567. str += ":00:" + String.Format("{0:00}", ts.Seconds);
  568. }
  569. return str;
  570. }
  571. private void CheckArray()
  572. {
  573. bool gameOver = false;
  574. for (int i = 0; i < rows; i++)
  575. {
  576. for (int j = 0; j < columns; j++)
  577. {
  578. if (Map[i, j] == 0)
  579. {
  580. gameOver = true;
  581. break;
  582. }
  583. }
  584. }
  585. if (!gameOver)
  586. {
  587. // 游戏结束
  588. GameOver(false);
  589. return;
  590. }
  591. }
  592. private void GameOver(bool target)
  593. {
  594. Timers.inst.Remove(UpdateTime);
  595. ViewManager.Show<ResultTipsView>(new object[]{target, gameDate.type, time, gameDate.id });
  596. }
  597. }
  598. }