TZFEGameVIew.cs 17 KB

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