TZFEGameVIew.cs 17 KB

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