| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 | 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<ActivityOpenCfg> activityGameDate;        //评价        private List<int> CustemsNum = new List<int>()        {            0,            0,            0,        };        private bool isMove;        private bool isMerge;        private struct NumPos        {            public int x;            public int y;            public bool isCreat;        }        //需要播放动效的列表        private List<NumPos> numPosArray = new List<NumPos>();        private int time = 0;        private EffectUI _effectUI1;        public override void Dispose()        {            EffectUIPool.Recycle(_effectUI1);            _effectUI1 = null;            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;            isReturnView = true;            _ui.m_numList.itemRenderer = ListNumItem;            _ui.m_mask.onTouchBegin.Add(OnClickBegin);            _ui.m_mask.onTouchEnd.Add(OnClickEnd);            _ui.m_backBtn.onClick.Add(OnClickBtnBack);            _ui.m_mergeBtn.onClick.Add(OnClickMergeTips);            Map = new int[rows, columns];            _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_effectTitle, "ui_Activity", "SGLL_Game_Text");        }        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,true);                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 = 1;            score = 0;            targetNum = Merge2048GameArray.Instance.GetCfg(gameID).targetNum;            CustemsNum[0] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar3;            CustemsNum[1] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar2;            CustemsNum[2] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar1;            _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";            _ui.m_target.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, targetNum)[0].resName, "TZFEGame");            _ui.m_score.text = string.Format("分数:{0}", score.ToString());            _ui.m_score.visible = false;            _ui.m_time.visible = false;            _ui.m_timeNum.visible = false;            _ui.m_title.visible = false;            rand = new System.Random();            UpdateStar();            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.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, Map[x, y])[0].resName, "TZFEGame");                numItem.m_icon.visible = true;            }            //播放生成和合并动效            for(int i=0; i<numPosArray.Count; i++)            {                if(x == numPosArray[i].x && y == numPosArray[i].y)                {                    if(numPosArray[i].isCreat)                    {                        numItem.m_t0.Play();                        numPosArray.RemoveAt(i);                        break;                    }                    else                    {                        numItem.m_t1.Play();                        numPosArray.RemoveAt(i);                        break;                    }                }            }            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)            {                //GameOver(false);                return;            }            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;                }            }        }        /// <summary>        /// 去零        /// </summary>        /// <param name="row">对于一行或一列元素</param>        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;                }            }        }        /// <summary>        /// 合并        /// </summary>        /// <param name="row">对于一行或一列元素,完成一次向左合并的操作</param>        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;        }        /// <summary>        /// 上移        /// </summary>        /// <param name="map">原棋盘</param>        /// <returns></returns>        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];            }        }        /// <summary>        /// 下移        /// </summary>        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;        }        /// <summary>        /// 左移        /// </summary>        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;        }        /// <summary>        /// 右移        /// </summary>        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;        }        /// <summary>        /// 进行一次移动操作        /// </summary>        /// <param name="map">原棋盘</param>        /// <param name="dir">移动的方向(枚举)</param>        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<SyntheticRoutetipView>(new object[] { targetNum , gameDate.subType}) ;        }        private void UpdateTime(object param = null)        {            _ui.m_timeNum.text = sec_to_hms(time);            time++;            //UpdateStar();        }        private void UpdateStar()        {            int starNum = 0;            for (int i = CustemsNum.Count - 1; i >= 0; i--)            {                if (time <= CustemsNum[i])                {                    starNum += 1;                }            }            switch (starNum)            {                case 1:                    _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";                    _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star_grey";                    _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";                    break;                case 2:                    _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";                    _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";                    _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";                    break;                case 3:                    _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";                    _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";                    _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";                    break;                default:                    _ui.m_star1.visible = false;                    _ui.m_star2.visible = false;                    _ui.m_star3.visible = false;                    break;            }        }        //将秒数转化为时分秒 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<ResultTipsView>(new object[]{target, gameDate.type, time, gameDate.id });        }    }}
 |