TZFEGameVIew.cs 14 KB

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