TZFEGameVIew.cs 17 KB

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