TZFEGameVIew.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. 240,
  33. 180,
  34. 120,
  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.target.onTouchBegin.Add(OnClickBegin);
  65. _ui.target.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 = 0;
  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 = string.Format("ui://MiniGame/sgll2_{0}", targetNum);
  132. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  133. _ui.m_score.visible = false;
  134. rand = new System.Random();
  135. Timers.inst.Add(1.0f, 0, UpdateTime);
  136. }
  137. private void UpdateScore()
  138. {
  139. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  140. }
  141. private void UpdateList()
  142. {
  143. RandomCreateNum();
  144. _ui.m_numList.numItems = rows * columns;
  145. }
  146. private void ListNumItem(int index, GObject item)
  147. {
  148. UI_numItem numItem = UI_numItem.Proxy(item);
  149. int x = index / rows;
  150. int y = index % columns;
  151. if(Map[x,y] == 0)
  152. {
  153. numItem.m_icon.visible = false;
  154. }
  155. else
  156. {
  157. //numItem.m_icon.url = string.Format("ui://MiniGame/sgll2_{0}", Map[x, y]);
  158. numItem.m_icon.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, Map[x, y])[0].resName);
  159. numItem.m_icon.visible = true;
  160. }
  161. //播放生成和合并动效
  162. for(int i=0; i<numPosArray.Count; i++)
  163. {
  164. if(x == numPosArray[i].x && y == numPosArray[i].y)
  165. {
  166. if(numPosArray[i].isCreat)
  167. {
  168. numItem.m_t0.Play();
  169. numPosArray.RemoveAt(i);
  170. break;
  171. }
  172. else
  173. {
  174. numItem.m_t1.Play();
  175. numPosArray.RemoveAt(i);
  176. break;
  177. }
  178. }
  179. }
  180. UI_numItem.ProxyEnd();
  181. }
  182. //随机生成数字2(%90),4(%10)
  183. private void RandomCreateNum()
  184. {
  185. bool gameOver = false;
  186. for(int i = 0;i< rows; i++)
  187. {
  188. for(int j = 0; j< columns; j++)
  189. {
  190. if (Map[i, j] == 0)
  191. {
  192. gameOver = true;
  193. break;
  194. }
  195. }
  196. }
  197. if(!gameOver)
  198. {
  199. //GameOver(false);
  200. return;
  201. }
  202. while (true)
  203. {
  204. int x = rand.Next(0, rows);
  205. int y = rand.Next(0, columns);
  206. NumPos item;
  207. int num;
  208. int randNum = rand.Next(1, 11);
  209. if(randNum <= 9)
  210. {
  211. num = 2;
  212. }
  213. else
  214. {
  215. num = 4 ;
  216. }
  217. if (Map[x,y] == 0)
  218. {
  219. Map[x, y] = num;
  220. item.x = x;
  221. item.y = y;
  222. item.isCreat = true;
  223. numPosArray.Add(item);
  224. break;
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// 去零
  230. /// </summary>
  231. /// <param name="row">对于一行或一列元素</param>
  232. private void Remove0(int[] row,Direction dir,int xy = 0)
  233. {
  234. int pos = 0;
  235. int[] rowB = new int[row.Length];
  236. for (int i = 0; i < row.Length; i++)
  237. {
  238. rowB[i] = row[i];
  239. }
  240. for (int i = 0; i < row.Length; ++i)
  241. {
  242. if (row[i] != 0)
  243. {
  244. row[pos] = row[i];
  245. //-----这里修改需要播放动效的数字位置列表-----
  246. int x = 0;
  247. int y = 0;
  248. int nextX = 0;
  249. int nextY = 0;
  250. for (int t = 0; t < numPosArray.Count; t++)
  251. {
  252. switch (dir)
  253. {
  254. case Direction.up:
  255. x = i;
  256. y = xy;
  257. nextX = pos;
  258. nextY = xy;
  259. break;
  260. case Direction.down:
  261. x = row.Length - 1 - i;
  262. y = xy;
  263. nextX = row.Length - 1 - pos;
  264. nextY = xy;
  265. break;
  266. case Direction.left:
  267. x = xy;
  268. y = i;
  269. nextX = xy;
  270. nextY = pos;
  271. break;
  272. case Direction.right:
  273. x = xy;
  274. y = row.Length - 1 - i;
  275. nextX = xy;
  276. nextY = row.Length - 1 - pos;
  277. break;
  278. }
  279. if(numPosArray.Count != 0 && numPosArray[t].x == x && numPosArray[t].y == y && pos != i)
  280. {
  281. numPosArray.RemoveAt(t);
  282. NumPos item;
  283. item.x = nextX;
  284. item.y = nextY;
  285. item.isCreat = false;
  286. numPosArray.Add(item);
  287. }
  288. }
  289. //---------------------------------------
  290. pos++;
  291. }
  292. }
  293. for (; pos < row.Length; ++pos) row[pos] = 0;
  294. for(int i= 0; i< row.Length; i++)
  295. {
  296. if(row[i] != rowB[i])
  297. {
  298. isMove = true;
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// 合并
  304. /// </summary>
  305. /// <param name="row">对于一行或一列元素,完成一次向左合并的操作</param>
  306. private void Merge(int[] row , Direction dir, int xy)
  307. {
  308. Remove0(row,dir,xy);
  309. // 相邻相同则合并
  310. for (int i = 0; i < row.Length - 1; ++i)
  311. {
  312. if (row[i] != 0 && row[i] == row[i + 1])
  313. {
  314. row[i] *= 2;
  315. row[i + 1] = 0;
  316. //将合并的数字放入列表
  317. NumPos item;
  318. item = MoveAddNum(i, xy, dir, row.Length-1);
  319. numPosArray.Add(item);
  320. //-------
  321. score += row[i];
  322. UpdateScore();
  323. if (row[i] == targetNum)
  324. {
  325. //游戏成功
  326. GameOver(true);
  327. }
  328. isMerge = true;
  329. }
  330. }
  331. Remove0(row,dir,xy);
  332. }
  333. //将合并的数字位置放入列表的准备
  334. private NumPos MoveAddNum(int i, int xy, Direction dir,int length)
  335. {
  336. NumPos item = new NumPos();
  337. switch (dir)
  338. {
  339. case Direction.up:
  340. item.x = i;
  341. item.y = xy;
  342. item.isCreat = false;
  343. break;
  344. case Direction.down:
  345. item.x = length - i;
  346. item.y = xy;
  347. item.isCreat = false;
  348. break;
  349. case Direction.left:
  350. item.x = xy;
  351. item.y = i;
  352. item.isCreat = false;
  353. break;
  354. case Direction.right:
  355. item.x = xy;
  356. item.y = length - i;
  357. item.isCreat = false;
  358. break;
  359. }
  360. return item;
  361. }
  362. /// <summary>
  363. /// 上移
  364. /// </summary>
  365. /// <param name="map">原棋盘</param>
  366. /// <returns></returns>
  367. private void Up(int[,] map)
  368. {
  369. int[] arr = new int[rows];
  370. for (int j = 0; j < columns; ++j)
  371. {
  372. for (int i = 0; i < rows; ++i)
  373. {
  374. arr[i] = map[i, j];
  375. }
  376. Merge(arr,Direction.up,j);
  377. for (int i = 0; i < rows; ++i) map[i, j] = arr[i];
  378. }
  379. }
  380. /// <summary>
  381. /// 下移
  382. /// </summary>
  383. private int[,] Down(int[,] map)
  384. {
  385. int[] arr = new int[rows];
  386. for (int j = 0; j < columns; ++j)
  387. {
  388. for (int i = 0; i < rows; ++i)
  389. {
  390. arr[rows - 1 - i] = map[i, j];
  391. }
  392. Merge(arr,Direction.down ,j);
  393. for (int i = 0; i < rows; ++i) map[i, j] = arr[rows - 1 - i];
  394. }
  395. return map;
  396. }
  397. /// <summary>
  398. /// 左移
  399. /// </summary>
  400. private int[,] Left(int[,] map)
  401. {
  402. int[] arr = new int[columns];
  403. for (int i = 0; i < rows; ++i)
  404. {
  405. for (int j = 0; j < columns; ++j)
  406. {
  407. arr[j] = map[i, j];
  408. }
  409. Merge(arr,Direction.left,i);
  410. for (int j = 0; j < columns; ++j) map[i, j] = arr[j];
  411. }
  412. return map;
  413. }
  414. /// <summary>
  415. /// 右移
  416. /// </summary>
  417. private int[,] Right(int[,] map)
  418. {
  419. int[] arr = new int[columns];
  420. for (int i = 0; i < rows; ++i)
  421. {
  422. for (int j = 0; j < columns; ++j)
  423. {
  424. arr[columns - 1 - j] = map[i, j];
  425. }
  426. Merge(arr,Direction.right,i);
  427. for (int j = 0; j < columns; ++j) map[i, j] = arr[columns - 1 - j];
  428. }
  429. return map;
  430. }
  431. /// <summary>
  432. /// 进行一次移动操作
  433. /// </summary>
  434. /// <param name="map">原棋盘</param>
  435. /// <param name="dir">移动的方向(枚举)</param>
  436. private void Move(int[,] map, Direction dir)
  437. {
  438. switch (dir)
  439. {
  440. case Direction.up:
  441. Up(map); break;
  442. case Direction.down:
  443. Down(map); break;
  444. case Direction.left:
  445. Left(map); break;
  446. case Direction.right:
  447. Right(map); break;
  448. }
  449. if(isMerge || isMove)
  450. {
  451. isMove = false;
  452. isMerge = false;
  453. UpdateList();
  454. numPosArray.Clear();
  455. }
  456. else
  457. {
  458. CheckArray();
  459. }
  460. }
  461. private void OnClickBegin()
  462. {
  463. touchFirst = Input.mousePosition;//记录开始按下的位置
  464. }
  465. private void OnClickEnd()
  466. {
  467. touchSecond = Input.mousePosition;//记录拖动的位置
  468. if (touchSecond.x < touchFirst.x && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  469. {
  470. //向左滑动
  471. Move(Map, Direction.left);
  472. }
  473. if (touchSecond.x > touchFirst.x && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  474. {
  475. //向右滑动
  476. Move(Map, Direction.right);
  477. }
  478. if (touchSecond.y < touchFirst.y && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  479. {
  480. //向下滑动
  481. Move(Map, Direction.down);
  482. }
  483. if (touchSecond.y > touchFirst.y && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  484. {
  485. //向上滑动
  486. Move(Map, Direction.up);
  487. }
  488. touchFirst = touchSecond;
  489. }
  490. private void OnClickMergeTips()
  491. {
  492. ViewManager.Show<SyntheticRoutetipView>(new object[] { targetNum , gameDate.subType}) ;
  493. }
  494. private void UpdateTime(object param = null)
  495. {
  496. _ui.m_timeNum.text = sec_to_hms(time);
  497. time++;
  498. UpdateStar();
  499. }
  500. private void UpdateStar()
  501. {
  502. int starNum = 0;
  503. for (int i = CustemsNum.Count - 1; i >= 0; i--)
  504. {
  505. if (time <= CustemsNum[i])
  506. {
  507. starNum += 1;
  508. }
  509. }
  510. switch (starNum)
  511. {
  512. case 1:
  513. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  514. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star_grey";
  515. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  516. break;
  517. case 2:
  518. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  519. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  520. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  521. break;
  522. case 3:
  523. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  524. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  525. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  526. break;
  527. default:
  528. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star_grey";
  529. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star_grey";
  530. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  531. break;
  532. }
  533. }
  534. //将秒数转化为时分秒 duration为秒数
  535. private string sec_to_hms(int duration)
  536. {
  537. TimeSpan ts = new TimeSpan(0, 0, duration);
  538. int _hours = 0;
  539. if (ts.Days > 0)
  540. {
  541. _hours = ts.Days * 24;
  542. }
  543. string str = "";
  544. if (ts.Hours > 0)
  545. {
  546. str = String.Format("{0:00}", ts.Hours + _hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  547. }
  548. if (ts.Hours == 0 && ts.Minutes > 0)
  549. {
  550. str = "00:";
  551. if (_hours > 0)
  552. {
  553. str = String.Format("{0:00}", ts.Hours + _hours) + ":";
  554. }
  555. str += String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  556. }
  557. if (ts.Hours == 0 && ts.Minutes == 0)
  558. {
  559. str = "00";
  560. if (_hours > 0)
  561. {
  562. str = String.Format("{0:00}", ts.Hours + _hours);
  563. }
  564. str += ":00:" + String.Format("{0:00}", ts.Seconds);
  565. }
  566. return str;
  567. }
  568. private void CheckArray()
  569. {
  570. bool gameOver = false;
  571. for (int i = 0; i < rows; i++)
  572. {
  573. for (int j = 0; j < columns; j++)
  574. {
  575. if (Map[i, j] == 0)
  576. {
  577. gameOver = true;
  578. break;
  579. }
  580. }
  581. }
  582. if (!gameOver)
  583. {
  584. // 游戏结束
  585. GameOver(false);
  586. return;
  587. }
  588. }
  589. private void GameOver(bool target)
  590. {
  591. Timers.inst.Remove(UpdateTime);
  592. ViewManager.Show<ResultTipsView>(new object[]{target, gameDate.type, time, gameDate.id });
  593. }
  594. }
  595. }