using System; using System.Collections.Generic; using ET; using FairyGUI; using UI.MiniGame; using UnityEngine; namespace GFGGame { public class TZFEGameView : BaseView { private UI_TZFEGameView _ui; private int[,] Map; enum Direction { up, down, left, right }; private Vector2 touchFirst = Vector2.zero;//手指开始按下的位置 private Vector2 touchSecond = Vector2.zero;//手指拖动的位置 private System.Random rand; //这个是数组的行数和列数,mapLength一般采用行列一样 private int rows = 5; private int columns = 5; private int mapLength = 5; //得分 private int score = 0; private int scoreMax = 0; private int targetNum = 2048; private bool isMove; private bool isMerge; private struct NumPos { public int x; public int y; public bool isCreat; } private List numPosArray = new List(); public override void Dispose() { if (_ui != null) { _ui.Dispose(); _ui = null; } base.Dispose(); } protected override void OnInit() { base.OnInit(); packageName = UI_TZFEGameView.PACKAGE_NAME; _ui = UI_TZFEGameView.Create(); this.viewCom = _ui.target; isfullScreen = true; _ui.m_numList.itemRenderer = ListNumItem; _ui.target.onTouchBegin.Add(OnClickBegin); _ui.target.onTouchEnd.Add(OnClickEnd); _ui.m_backBtn.onClick.Add(OnClickBtnBack); Map = new int[rows, columns]; } protected override void OnShown() { base.OnShown(); InitMap(); UpdateView(); UpdateList(); } protected override void OnHide() { base.OnHide(); } private void OnClickBtnBack() { this.Hide(); } private void InitMap() { for(int i = 0; i < rows; i++) for(int j = 0; j < columns; j++) { Map[i, j] = 0; } } private void UpdateView() { score = 0; targetNum = 2048; _ui.m_score.text = string.Format("分数:{0}", score.ToString()); rand = new System.Random(); } private void UpdateScore() { _ui.m_score.text = string.Format("分数:{0}", score.ToString()); } private void UpdateList() { RandomCreateNum(); _ui.m_numList.numItems = rows * columns; } private void ListNumItem(int index, GObject item) { UI_numItem numItem = UI_numItem.Proxy(item); int x = index / rows; int y = index % columns; numItem.m_numTxt.text = Map[x,y].ToString(); //这个后续根据数组内容替换图片 //_ui.m_icon.url = UI_numItem.ProxyEnd(); } //随机生成数字2(%90),4(%10) private void RandomCreateNum() { bool gameOver = false; for(int i = 0;i< rows; i++) { for(int j = 0; j< columns; j++) { if (Map[i, j] == 0) { gameOver = true; break; } } } if(!gameOver) { // 还需要加,无法移动,无法合并时,游戏结束 } while (true) { int x = rand.Next(0, rows); int y = rand.Next(0, columns); NumPos item; int num; int randNum = rand.Next(1, 11); if(randNum <= 9) { num = 2; } else { num = 4 ; } if (Map[x,y] == 0) { Map[x, y] = num; item.x = x; item.y = y; item.isCreat = true; numPosArray.Add(item); break; } } } /// /// 去零 /// /// 对于一行或一列元素 private void Remove0(int[] row) { int pos = 0; int[] rowB = new int[row.Length]; for (int i = 0; i < row.Length; i++) { rowB[i] = row[i]; } for (int i = 0; i < row.Length; ++i) { if (row[i] != 0) { row[pos] = row[i]; pos++; } } for (; pos < row.Length; ++pos) row[pos] = 0; for(int i= 0; i< row.Length; i++) { if(row[i] != rowB[i]) { isMove = true; } } } /// /// 合并 /// /// 对于一行或一列元素,完成一次向左合并的操作 private void Merge(int[] row , int colrow = 0) { Remove0(row); // 相邻相同则合并 for (int i = 0; i < row.Length - 1; ++i) { if (row[i] != 0 && row[i] == row[i + 1]) { row[i] *= 2; row[i + 1] = 0; score += row[i]; UpdateScore(); if (row[i] == targetNum) { //游戏结束 } isMerge = true; } } Remove0(row); } /// /// 上移 /// /// 原棋盘 /// private void Up(int[,] map) { int[] arr = new int[rows]; for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) arr[i] = map[i, j]; Merge(arr); for (int i = 0; i < rows; ++i) map[i, j] = arr[i]; } } /// /// 下移 /// private int[,] Down(int[,] map) { int[] arr = new int[rows]; for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) arr[rows - 1 - i] = map[i, j]; Merge(arr); for (int i = 0; i < rows; ++i) map[i, j] = arr[rows - 1 - i]; } return map; } /// /// 左移 /// private int[,] Left(int[,] map) { int[] arr = new int[columns]; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) arr[j] = map[i, j]; Merge(arr); for (int j = 0; j < columns; ++j) map[i, j] = arr[j]; } return map; } /// /// 右移 /// private int[,] Right(int[,] map) { int[] arr = new int[columns]; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) arr[columns - 1 - j] = map[i, j]; Merge(arr); for (int j = 0; j < columns; ++j) map[i, j] = arr[columns - 1 - j]; } return map; } /// /// 进行一次移动操作 /// /// 原棋盘 /// 移动的方向(枚举) private void Move(int[,] map, Direction dir) { switch (dir) { case Direction.up: Up(map); break; case Direction.down: Down(map); break; case Direction.left: Left(map); break; case Direction.right: Right(map); break; } if(isMerge || isMove) { isMerge = false; isMove = false; UpdateList(); numPosArray.Clear(); } } private void OnClickBegin() { touchFirst = Input.mousePosition;//记录开始按下的位置 } private void OnClickEnd() { touchSecond = Input.mousePosition;//记录拖动的位置 if (touchSecond.x < touchFirst.x && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y)) { //向左滑动 Move(Map, Direction.left); } if (touchSecond.x > touchFirst.x && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y)) { //向右滑动 Move(Map, Direction.right); } if (touchSecond.y < touchFirst.y && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x)) { //向下滑动 Move(Map, Direction.down); } if (touchSecond.y > touchFirst.y && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x)) { //向上滑动 Move(Map, Direction.up); } touchFirst = touchSecond; } } }