TZFEGameVIew.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. private List<NumPos> numPosArray = new List<NumPos>();
  34. public override void Dispose()
  35. {
  36. if (_ui != null)
  37. {
  38. _ui.Dispose();
  39. _ui = null;
  40. }
  41. base.Dispose();
  42. }
  43. protected override void OnInit()
  44. {
  45. base.OnInit();
  46. packageName = UI_TZFEGameView.PACKAGE_NAME;
  47. _ui = UI_TZFEGameView.Create();
  48. this.viewCom = _ui.target;
  49. isfullScreen = true;
  50. _ui.m_numList.itemRenderer = ListNumItem;
  51. _ui.target.onTouchBegin.Add(OnClickBegin);
  52. _ui.target.onTouchEnd.Add(OnClickEnd);
  53. _ui.m_backBtn.onClick.Add(OnClickBtnBack);
  54. Map = new int[rows, columns];
  55. }
  56. protected override void OnShown()
  57. {
  58. base.OnShown();
  59. InitMap();
  60. UpdateView();
  61. UpdateList();
  62. }
  63. protected override void OnHide()
  64. {
  65. base.OnHide();
  66. }
  67. private void OnClickBtnBack()
  68. {
  69. this.Hide();
  70. }
  71. private void InitMap()
  72. {
  73. for(int i = 0; i < rows; i++)
  74. for(int j = 0; j < columns; j++)
  75. {
  76. Map[i, j] = 0;
  77. }
  78. }
  79. private void UpdateView()
  80. {
  81. score = 0;
  82. targetNum = 2048;
  83. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  84. rand = new System.Random();
  85. }
  86. private void UpdateScore()
  87. {
  88. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  89. }
  90. private void UpdateList()
  91. {
  92. RandomCreateNum();
  93. _ui.m_numList.numItems = rows * columns;
  94. }
  95. private void ListNumItem(int index, GObject item)
  96. {
  97. UI_numItem numItem = UI_numItem.Proxy(item);
  98. int x = index / rows;
  99. int y = index % columns;
  100. numItem.m_numTxt.text = Map[x,y].ToString();
  101. //这个后续根据数组内容替换图片
  102. //_ui.m_icon.url =
  103. UI_numItem.ProxyEnd();
  104. }
  105. //随机生成数字2(%90),4(%10)
  106. private void RandomCreateNum()
  107. {
  108. bool gameOver = false;
  109. for(int i = 0;i< rows; i++)
  110. {
  111. for(int j = 0; j< columns; j++)
  112. {
  113. if (Map[i, j] == 0)
  114. {
  115. gameOver = true;
  116. break;
  117. }
  118. }
  119. }
  120. if(!gameOver)
  121. {
  122. // 还需要加,无法移动,无法合并时,游戏结束
  123. }
  124. while (true)
  125. {
  126. int x = rand.Next(0, rows);
  127. int y = rand.Next(0, columns);
  128. NumPos item;
  129. int num;
  130. int randNum = rand.Next(1, 11);
  131. if(randNum <= 9)
  132. {
  133. num = 2;
  134. }
  135. else
  136. {
  137. num = 4 ;
  138. }
  139. if (Map[x,y] == 0)
  140. {
  141. Map[x, y] = num;
  142. item.x = x;
  143. item.y = y;
  144. item.isCreat = true;
  145. numPosArray.Add(item);
  146. break;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 去零
  152. /// </summary>
  153. /// <param name="row">对于一行或一列元素</param>
  154. private void Remove0(int[] row)
  155. {
  156. int pos = 0;
  157. int[] rowB = new int[row.Length];
  158. for (int i = 0; i < row.Length; i++)
  159. {
  160. rowB[i] = row[i];
  161. }
  162. for (int i = 0; i < row.Length; ++i)
  163. {
  164. if (row[i] != 0)
  165. {
  166. row[pos] = row[i];
  167. pos++;
  168. }
  169. }
  170. for (; pos < row.Length; ++pos) row[pos] = 0;
  171. for(int i= 0; i< row.Length; i++)
  172. {
  173. if(row[i] != rowB[i])
  174. {
  175. isMove = true;
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// 合并
  181. /// </summary>
  182. /// <param name="row">对于一行或一列元素,完成一次向左合并的操作</param>
  183. private void Merge(int[] row , int colrow = 0)
  184. {
  185. Remove0(row);
  186. // 相邻相同则合并
  187. for (int i = 0; i < row.Length - 1; ++i)
  188. {
  189. if (row[i] != 0 && row[i] == row[i + 1])
  190. {
  191. row[i] *= 2;
  192. row[i + 1] = 0;
  193. score += row[i];
  194. UpdateScore();
  195. if (row[i] == targetNum)
  196. {
  197. //游戏结束
  198. }
  199. isMerge = true;
  200. }
  201. }
  202. Remove0(row);
  203. }
  204. /// <summary>
  205. /// 上移
  206. /// </summary>
  207. /// <param name="map">原棋盘</param>
  208. /// <returns></returns>
  209. private void Up(int[,] map)
  210. {
  211. int[] arr = new int[rows];
  212. for (int j = 0; j < columns; ++j)
  213. {
  214. for (int i = 0; i < rows; ++i) arr[i] = map[i, j];
  215. Merge(arr);
  216. for (int i = 0; i < rows; ++i) map[i, j] = arr[i];
  217. }
  218. }
  219. /// <summary>
  220. /// 下移
  221. /// </summary>
  222. private int[,] Down(int[,] map)
  223. {
  224. int[] arr = new int[rows];
  225. for (int j = 0; j < columns; ++j)
  226. {
  227. for (int i = 0; i < rows; ++i) arr[rows - 1 - i] = map[i, j];
  228. Merge(arr);
  229. for (int i = 0; i < rows; ++i) map[i, j] = arr[rows - 1 - i];
  230. }
  231. return map;
  232. }
  233. /// <summary>
  234. /// 左移
  235. /// </summary>
  236. private int[,] Left(int[,] map)
  237. {
  238. int[] arr = new int[columns];
  239. for (int i = 0; i < rows; ++i)
  240. {
  241. for (int j = 0; j < columns; ++j) arr[j] = map[i, j];
  242. Merge(arr);
  243. for (int j = 0; j < columns; ++j) map[i, j] = arr[j];
  244. }
  245. return map;
  246. }
  247. /// <summary>
  248. /// 右移
  249. /// </summary>
  250. private int[,] Right(int[,] map)
  251. {
  252. int[] arr = new int[columns];
  253. for (int i = 0; i < rows; ++i)
  254. {
  255. for (int j = 0; j < columns; ++j) arr[columns - 1 - j] = map[i, j];
  256. Merge(arr);
  257. for (int j = 0; j < columns; ++j) map[i, j] = arr[columns - 1 - j];
  258. }
  259. return map;
  260. }
  261. /// <summary>
  262. /// 进行一次移动操作
  263. /// </summary>
  264. /// <param name="map">原棋盘</param>
  265. /// <param name="dir">移动的方向(枚举)</param>
  266. private void Move(int[,] map, Direction dir)
  267. {
  268. switch (dir)
  269. {
  270. case Direction.up:
  271. Up(map); break;
  272. case Direction.down:
  273. Down(map); break;
  274. case Direction.left:
  275. Left(map); break;
  276. case Direction.right:
  277. Right(map); break;
  278. }
  279. if(isMerge || isMove)
  280. {
  281. isMerge = false;
  282. isMove = false;
  283. UpdateList();
  284. numPosArray.Clear();
  285. }
  286. }
  287. private void OnClickBegin()
  288. {
  289. touchFirst = Input.mousePosition;//记录开始按下的位置
  290. }
  291. private void OnClickEnd()
  292. {
  293. touchSecond = Input.mousePosition;//记录拖动的位置
  294. if (touchSecond.x < touchFirst.x && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  295. {
  296. //向左滑动
  297. Move(Map, Direction.left);
  298. }
  299. if (touchSecond.x > touchFirst.x && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  300. {
  301. //向右滑动
  302. Move(Map, Direction.right);
  303. }
  304. if (touchSecond.y < touchFirst.y && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  305. {
  306. //向下滑动
  307. Move(Map, Direction.down);
  308. }
  309. if (touchSecond.y > touchFirst.y && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  310. {
  311. //向上滑动
  312. Move(Map, Direction.up);
  313. }
  314. touchFirst = touchSecond;
  315. }
  316. }
  317. }