using System; using System.Collections.Generic; using ET; using FairyGUI; using UI.MiniGame; using UnityEngine; namespace GFGGame { public class TZFEGameView : BaseWindow { 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 = 4; private int columns = 4; private int mapLength = 5; //得分 private int score = 0; private int scoreMax = 0; //目标分数 private int targetNum = 128; private int gameID; private Merge2048Game gameDate; private List activityGameDate; //评价 private List CustemsNum = new List() { 240, 180, 120, }; private bool isMove; private bool isMerge; private struct NumPos { public int x; public int y; public bool isCreat; } //需要播放动效的列表 private List numPosArray = new List(); private int time = 0; 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); _ui.m_mergeBtn.onClick.Add(OnClickMergeTips); Map = new int[rows, columns]; } protected override void OnShown() { base.OnShown(); if ((this.viewData as object[]).Length != 0 && this.viewData != null) { gameID = (int)(this.viewData as object[])[0]; } else { gameID = 128; } gameDate = Merge2048GameArray.Instance.GetCfg(gameID); activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc); InitMap(); UpdateView(); UpdateList(); } protected override void OnHide() { Timers.inst.Remove(UpdateTime); base.OnHide(); } private void OnClickBtnBack() { string exitTip; if (gameDate.bonusLoseArr.Length == 0) { exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?"; } else { exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?"; } AlertUI.Show(exitTip) .SetLeftButton(true, "取消", (object data) => { }) .SetRightButton(true, "确定",async (object data) => { var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.type, time, false, activityGameDate[0].id); if (!result) return; 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() { time = 0; score = 0; targetNum = Merge2048GameArray.Instance.GetCfg(gameID).targetNum; _ui.m_score.text = string.Format("分数:{0}", score.ToString()); _ui.m_score.visible = false; rand = new System.Random(); Timers.inst.Add(1.0f, 0, UpdateTime); } 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; if(Map[x,y] == 0) { numItem.m_icon.visible = false; } else { numItem.m_icon.url = string.Format("ui://MiniGame/sgll2_{0}", Map[x, y]); numItem.m_icon.visible = true; } //播放生成和合并动效 for(int i=0; i /// 去零 /// /// 对于一行或一列元素 private void Remove0(int[] row,Direction dir,int xy = 0) { 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]; //-----这里修改需要播放动效的数字位置列表----- int x = 0; int y = 0; int nextX = 0; int nextY = 0; for (int t = 0; t < numPosArray.Count; t++) { switch (dir) { case Direction.up: x = i; y = xy; nextX = pos; nextY = xy; break; case Direction.down: x = row.Length - 1 - i; y = xy; nextX = row.Length - 1 - pos; nextY = xy; break; case Direction.left: x = xy; y = i; nextX = xy; nextY = pos; break; case Direction.right: x = xy; y = row.Length - 1 - i; nextX = xy; nextY = row.Length - 1 - pos; break; } if(numPosArray.Count != 0 && numPosArray[t].x == x && numPosArray[t].y == y && pos != i) { numPosArray.RemoveAt(t); NumPos item; item.x = nextX; item.y = nextY; item.isCreat = false; numPosArray.Add(item); } } //--------------------------------------- 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 , Direction dir, int xy) { Remove0(row,dir,xy); // 相邻相同则合并 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; //将合并的数字放入列表 NumPos item; item = MoveAddNum(i, xy, dir, row.Length-1); numPosArray.Add(item); //------- score += row[i]; UpdateScore(); if (row[i] == targetNum) { //游戏成功 GameOver(true); } isMerge = true; } } Remove0(row,dir,xy); } //将合并的数字位置放入列表的准备 private NumPos MoveAddNum(int i, int xy, Direction dir,int length) { NumPos item = new NumPos(); switch (dir) { case Direction.up: item.x = i; item.y = xy; item.isCreat = false; break; case Direction.down: item.x = length - i; item.y = xy; item.isCreat = false; break; case Direction.left: item.x = xy; item.y = i; item.isCreat = false; break; case Direction.right: item.x = xy; item.y = length - i; item.isCreat = false; break; } return item; } /// /// 上移 /// /// 原棋盘 /// 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,Direction.up,j); 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,Direction.down ,j); 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,Direction.left,i); 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,Direction.right,i); 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) { isMove = false; isMerge = false; UpdateList(); numPosArray.Clear(); } else { CheckArray(); } } 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; } private void OnClickMergeTips() { ViewManager.Show() ; } private void UpdateTime(object param = null) { _ui.m_timeNum.text = sec_to_hms(time); time++; } //将秒数转化为时分秒 duration为秒数 private string sec_to_hms(int duration) { TimeSpan ts = new TimeSpan(0, 0, duration); int _hours = 0; if (ts.Days > 0) { _hours = ts.Days * 24; } string str = ""; if (ts.Hours > 0) { str = String.Format("{0:00}", ts.Hours + _hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds); } if (ts.Hours == 0 && ts.Minutes > 0) { str = "00:"; if (_hours > 0) { str = String.Format("{0:00}", ts.Hours + _hours) + ":"; } str += String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds); } if (ts.Hours == 0 && ts.Minutes == 0) { str = "00"; if (_hours > 0) { str = String.Format("{0:00}", ts.Hours + _hours); } str += ":00:" + String.Format("{0:00}", ts.Seconds); } return str; } private void CheckArray() { 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) { // 游戏结束 GameOver(false); return; } } private void GameOver(bool target) { Timers.inst.Remove(UpdateTime); ViewManager.Show(new object[]{target, gameDate.type, time, gameDate.id }); } } }