123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UI.ActivityHuaRongDao;
- using FairyGUI;
- using System.Threading.Tasks;
- using System.Threading;
- namespace GFGGame
- {
- public class ActivityHuaRongDaoView : BaseWindow
- {
- private UI_ActivityHuaRongDaoUI _ui;
- public class Grid
- {
- public Vector2 pos;
- public int num;
- }
- private Grid[,] _gridArr;
- private readonly int _gridNum = 3;
- private List<GObject> _items;
- private bool _gameStart;
- private HuarongRoadGame cfg;
- private CancellationTokenSource cancellationTokenSource;
- private GObject blankItem;
- public override void Dispose()
- {
- if (_ui != null)
- {
- _ui.Dispose();
- _ui = null;
- }
- base.Dispose();
- }
- protected override void OnInit()
- {
- base.OnInit();
- packageName = UI_ActivityHuaRongDaoUI.PACKAGE_NAME;
- _ui = UI_ActivityHuaRongDaoUI.Create();
- viewCom = _ui.target;
- _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("quanping_moren_bg");
- isReturnView = true;
- _ui.m_item.target.visible = false;
- _ui.m_btnBack.onClick.Add(OnBtnBackClick);
- _ui.m_btnLookPic.onClick.Add(OnClickBtnShowOriginPic);
- _ui.m_btnHidePic.onClick.Add(OnClickBtnHideOriginPic);
- _ui.m_btnRefresh.onClick.Add(OnClickBtnRefresh);
- InitGridInfo();
- CreateItems();
- // 初始化空白处图片
- if (blankItem == null)
- {
- blankItem = UIPackage.CreateObject("ActivityHuaRongDao", "item");
- _ui.target.AddChild(blankItem);
- }
- }
- protected override void OnShown()
- {
- base.OnShown();
- cfg = (HuarongRoadGame)viewData;
- blankItem.visible = false;
- _gameStart = false;
- cancellationTokenSource = new CancellationTokenSource();
- Task task = StartAnimation(cancellationTokenSource.Token);
- // 倒计时相关
- //ResetCountdown();
- //Countdown(null);
- //Timers.inst.Add(1, 0, Countdown);
- }
- protected override void OnHide()
- {
- base.OnHide();
- StopMyAsyncFunction();
- }
- /// <summary>
- /// 初始化所有格子的信息
- /// </summary>
- private void InitGridInfo()
- {
- _gridArr = new Grid[_gridNum, _gridNum];
- _items = new List<GObject>();
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- _gridArr[i, j] = new Grid();
- }
- }
- }
- private void SetGridInfo(List<int> numList)
- {
- //List<int> numList = GetRandomArr(1, 8);
- Vector2 originPos = _ui.m_item.target.position;
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- _gridArr[i, j].num = numList[i * _gridNum + j];
- _gridArr[i, j].pos = originPos + new Vector2(j * _ui.m_item.target.width, i * _ui.m_item.target.height);
- }
- }
- }
- /// <summary>
- /// 检测是否是连续数组
- /// </summary>
- /// <param name="numList"></param>
- /// <returns></returns>
- private bool CheckListCorrect(List<int> numList)
- {
- for (int i = 0; i < numList.Count - 1; i++)
- {
- if (numList[i + 1] != numList[i] + 1)
- {
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// 随机排序 min~max 连续的数字
- /// </summary>
- /// <param name="min"></param>
- /// <param name="max"></param>
- /// <returns></returns>
- private List<int> GetRandomArr(int min, int max)
- {
- List<int> list = new List<int>();
- for (int i = min; i <= max; i++)
- {
- list.Add(i);
- }
- System.Random r = new System.Random();
- for (int i = 0; i < list.Count; i++)
- {
- int index = r.Next(list.Count);
- int temp = list[index];
- list[index] = list[i];
- list[i] = temp;
- }
- // 最后一个是空的
- list.Add(0);
- return list;
- }
- private List<int> CreateIncreaseArr()
- {
- List<int> list = new List<int>();
- list.Add(0);
- for (int i = 1; i <= 8; i++)
- {
- list.Add(i);
- }
- return list;
- }
- /// <summary>
- /// 根据配置步数打乱数组
- /// </summary>
- /// <param name="configStep"></param>
- /// <returns></returns>
- private List<int> GetArrByConfigStep(int configStep)
- {
- List<int> list = CreateIncreaseArr();
- int num = 0;
- Vector2 zeroPos = new Vector2(0, 0);
- Vector2[] dirArr = { new Vector2(-1, 0), new Vector2(1, 0), new Vector2(0, -1), new Vector2(0, 1) };
- Vector2 lastPos = zeroPos;
- while (num < configStep)
- {
- int n = Random.Range(0, 4);
- Vector2 tempPos = zeroPos + dirArr[n];
- if (tempPos != lastPos && tempPos.x >= 0 && tempPos.y >= 0 && tempPos.x < _gridNum && tempPos.y < _gridNum)
- {
- list[(int)zeroPos.x * _gridNum + (int)zeroPos.y] = list[(int)tempPos.x * _gridNum + (int)tempPos.y];
- list[(int)tempPos.x * _gridNum + (int)tempPos.y] = 0;
- lastPos = zeroPos;
- zeroPos = tempPos;
- }
- ++num;
- }
- return list;
- }
- private void InitItems()
- {
- int itemIndex = 0;
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- // 空格不用放item
- if (_gridArr[i, j].num == 0)
- {
- continue;
- }
- _items[itemIndex].position = _gridArr[i, j].pos;
- UI_item item = UI_item.Proxy(_items[itemIndex]);
- item.m_index.text = _gridArr[i, j].num.ToString();
- item.m_icon.url = string.Format("ui://ActivityHuaRongDao/hrd_1-{0}", _gridArr[i, j].num + 1);
- _items[itemIndex].data = _gridArr[i, j].num;
- UI_item.ProxyEnd();
- ++itemIndex;
- }
- }
- }
- /// <summary>
- /// 创建可移动的实体
- /// </summary>
- private void CreateItems()
- {
- for (int i = 1; i <= _gridNum * _gridNum - 1; i++)
- {
- GObject gObject = UIPackage.CreateObject("ActivityHuaRongDao", "item");
- gObject.name = "item" + i;
- gObject.onClick.Add(OnItemClick);
- _ui.m_items.target.AddChild(gObject);
- UI_item item = UI_item.Proxy(gObject);
- _items.Add(gObject);
- UI_item.ProxyEnd();
- }
- }
- private void OnItemClick(EventContext eventContext)
- {
- GObject obj = eventContext.sender as GObject;
- int num = (int)obj.data;
- Grid newGrid = CheckCanMove(num);
- if (newGrid != null)
- {
- _ui.m_items.target.touchable = false;
- obj.TweenMove(newGrid.pos, 0.1f).OnComplete(() =>
- {
- _ui.m_items.target.touchable = true;
- if (CheckWin())
- {
- _ui.m_maskGlobal.visible = true;
- Win(cancellationTokenSource.Token);
- }
- });
- }
- }
- private Grid CheckCanMove(int num)
- {
- int indexX = 0;
- int indexY = 0;
- // 找到格子
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- if (_gridArr[i, j].num == num)
- {
- indexX = i;
- indexY = j;
- break;
- }
- }
- }
- // 判断格子四个方向是否有空格
- if (CheckHaveNullGrid(indexX - 1, indexY) || CheckHaveNullGrid(indexX, indexY - 1)
- || CheckHaveNullGrid(indexX + 1, indexY) || CheckHaveNullGrid(indexX, indexY + 1))
- {
- return UpdateGridInfo(num);
- }
- return null;
- }
- private bool CheckHaveNullGrid(int indexX, int indexY)
- {
- if (indexX >= 0 && indexX < _gridNum)
- {
- if (indexY >= 0 && indexY < _gridNum)
- {
- return _gridArr[indexX, indexY].num == 0;
- }
- }
- return false;
- }
- private Grid UpdateGridInfo(int num)
- {
- Vector2 nullGridindex = new Vector2(-1, 0);
- Vector2 numGridindex = new Vector2(-1, 0);
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- if (_gridArr[i, j].num == num)
- {
- numGridindex = new Vector2(i, j);
- }
- else if (_gridArr[i, j].num == 0)
- {
- nullGridindex = new Vector2(i, j);
- }
- if (numGridindex.x != -1 && nullGridindex.x != -1)
- {
- break;
- }
- }
- }
- _gridArr[(int)numGridindex.x, (int)numGridindex.y].num = 0;
- _gridArr[(int)nullGridindex.x, (int)nullGridindex.y].num = num;
- return _gridArr[(int)nullGridindex.x, (int)nullGridindex.y];
- }
- private bool CheckWin()
- {
- int num = 0;
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- if (i == 0 && j == 0)
- {
- num = _gridArr[i, j].num;
- if (num != 0)
- {
- return false;
- }
- }
- else
- {
- // 不连续 没有胜利
- if (_gridArr[i, j].num != num + 1)
- {
- return false;
- }
- // 继续检测
- else
- {
- ++num;
- // 胜利
- if (num == _gridNum * _gridNum - 1)
- {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- private async Task Win(CancellationToken cancellationToken)
- {
- await Task.Delay(200, cancellationToken);
- blankItem.visible = true;
- blankItem.position = _gridArr[0, 0].pos;
- UI_item item = UI_item.Proxy(blankItem);
- item.m_icon.url = string.Format("ui://ActivityHuaRongDao/hrd_1-{0}", 1);
- UI_item.ProxyEnd();
- await Task.Delay(400, cancellationToken);
- // 关卡推进
- int passLevel = cfg.resArr[0];
- ActivityHuaRongDaoEntryView.curLevel = Mathf.Max(passLevel, ActivityHuaRongDaoEntryView.curLevel);
- EventAgent.DispatchEvent(ConstMessage.ACTIVITY_HUARONGDAO_UPDATE);
- // 弹出成功界面
- ViewManager.Show<ActivityHuaRongDaoSuccessView>(cfg);
- }
- private int countDownNum;
- private void Countdown(object param)
- {
- _ui.m_seconds.text = countDownNum.ToString() + "s";
- --countDownNum;
- if (countDownNum < 0)
- {
- _ui.m_tips.visible = false;
- Timers.inst.Remove(Countdown);
- _gameStart = true;
- return;
- }
- }
- private void ResetCountdown()
- {
- countDownNum = 3;
- _ui.m_tips.visible = true;
- Timers.inst.Remove(Countdown);
- }
- private void OnBtnBackClick()
- {
- if (_gameStart)
- {
- AlertUI.Show("退出游戏不保存进度,不扣除任何次数和道具,是否退出?")
- .SetLeftButton(true, "返回游戏")
- .SetRightButton(true, "仍要退出", (obj) =>
- {
- Hide();
- });
- }
- else
- {
- Hide();
- }
- }
- private async Task StartAnimation(CancellationToken cancellationToken)
- {
- try
- {
- _ui.m_hideMask.Play();
- _ui.m_maskGlobal.visible = true;
- List<int> numList = CreateIncreaseArr();
- SetGridInfo(numList);
- InitItems();
- CreateOriginPic();
- // 隐藏所有
- UI_item item;
- for (int i = 0; i < _items.Count; i++)
- {
- item = UI_item.Proxy(_items[i]);
- item.m_hide.Play();
- }
- // 入场
- for (int i = 0; i < _items.Count; i++)
- {
- item = UI_item.Proxy(_items[i]);
- item.m_show.Play();
- await Task.Delay(100, cancellationToken);
- }
- UI_item.ProxyEnd();
- // 出现白色遮罩,刷新数组
- await Task.Delay(900, cancellationToken);
- _ui.m_showMask.Play(() =>
- {
- _ui.m_maskGlobal.visible = false;
- _ui.m_hideMask.Play();
- _gameStart = true;
- });
- await Task.Delay(200, cancellationToken);
- numList = GetArrByConfigStep(cfg.step);
- while (CheckListCorrect(numList))
- {
- numList = GetArrByConfigStep(cfg.step);
- }
- SetGridInfo(numList);
- InitItems();
- }
- catch (TaskCanceledException)
- {
- //Debug.Log("异步函数被停止");
- }
- }
- // 取消异步函数
- public void StopMyAsyncFunction()
- {
- if (cancellationTokenSource != null)
- {
- cancellationTokenSource.Cancel();
- cancellationTokenSource.Dispose();
- cancellationTokenSource = null;
- }
- }
- /// <summary>
- /// 创建原图
- /// </summary>
- private void CreateOriginPic()
- {
- List<GObject> items = new List<GObject>();
- for (int i = 1; i <= _gridNum * _gridNum; i++)
- {
- GObject gObject = UIPackage.CreateObject("ActivityHuaRongDao", "item");
- _ui.m_itemsOrigin.target.AddChild(gObject);
- items.Add(gObject);
- }
- int itemIndex = 0;
- for (int i = 0; i < _gridNum; i++)
- {
- for (int j = 0; j < _gridNum; j++)
- {
- items[itemIndex].position = _gridArr[i, j].pos;
- UI_item item = UI_item.Proxy(items[itemIndex]);
- item.m_index.text = _gridArr[i, j].num.ToString();
- item.m_icon.url = string.Format("ui://ActivityHuaRongDao/hrd_1-{0}", _gridArr[i, j].num + 1);
- UI_item.ProxyEnd();
- ++itemIndex;
- }
- }
- }
- private void OnClickBtnShowOriginPic()
- {
- _ui.m_items.target.visible = false;
- _ui.m_itemsOrigin.target.visible = true;
- }
- private void OnClickBtnHideOriginPic()
- {
- _ui.m_items.target.visible = true;
- _ui.m_itemsOrigin.target.visible = false;
- }
- private void OnClickBtnRefresh()
- {
- StartAnimation(cancellationTokenSource.Token);
- }
- }
- }
|