ActivityHuaRongDaoView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UI.ActivityHuaRongDao;
  5. using FairyGUI;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8. namespace GFGGame
  9. {
  10. public class ActivityHuaRongDaoView : BaseWindow
  11. {
  12. private UI_ActivityHuaRongDaoUI _ui;
  13. public class Grid
  14. {
  15. public Vector2 pos;
  16. public int num;
  17. }
  18. private Grid[,] _gridArr;
  19. private readonly int _gridNum = 3;
  20. private List<GObject> _items;
  21. private bool _gameStart;
  22. private HuarongRoadGame cfg;
  23. private CancellationTokenSource cancellationTokenSource;
  24. private GObject blankItem;
  25. public override void Dispose()
  26. {
  27. if (_ui != null)
  28. {
  29. _ui.Dispose();
  30. _ui = null;
  31. }
  32. base.Dispose();
  33. }
  34. protected override void OnInit()
  35. {
  36. base.OnInit();
  37. packageName = UI_ActivityHuaRongDaoUI.PACKAGE_NAME;
  38. _ui = UI_ActivityHuaRongDaoUI.Create();
  39. viewCom = _ui.target;
  40. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("quanping_moren_bg");
  41. isReturnView = true;
  42. _ui.m_item.target.visible = false;
  43. _ui.m_btnBack.onClick.Add(OnBtnBackClick);
  44. _ui.m_btnLookPic.onClick.Add(OnClickBtnShowOriginPic);
  45. _ui.m_btnHidePic.onClick.Add(OnClickBtnHideOriginPic);
  46. _ui.m_btnRefresh.onClick.Add(OnClickBtnRefresh);
  47. InitGridInfo();
  48. CreateItems();
  49. // 初始化空白处图片
  50. if (blankItem == null)
  51. {
  52. blankItem = UIPackage.CreateObject("ActivityHuaRongDao", "item");
  53. _ui.target.AddChild(blankItem);
  54. }
  55. }
  56. protected override void OnShown()
  57. {
  58. base.OnShown();
  59. cfg = (HuarongRoadGame)viewData;
  60. blankItem.visible = false;
  61. _gameStart = false;
  62. cancellationTokenSource = new CancellationTokenSource();
  63. Task task = StartAnimation(cancellationTokenSource.Token);
  64. // 倒计时相关
  65. //ResetCountdown();
  66. //Countdown(null);
  67. //Timers.inst.Add(1, 0, Countdown);
  68. }
  69. protected override void OnHide()
  70. {
  71. base.OnHide();
  72. StopMyAsyncFunction();
  73. }
  74. /// <summary>
  75. /// 初始化所有格子的信息
  76. /// </summary>
  77. private void InitGridInfo()
  78. {
  79. _gridArr = new Grid[_gridNum, _gridNum];
  80. _items = new List<GObject>();
  81. for (int i = 0; i < _gridNum; i++)
  82. {
  83. for (int j = 0; j < _gridNum; j++)
  84. {
  85. _gridArr[i, j] = new Grid();
  86. }
  87. }
  88. }
  89. private void SetGridInfo(List<int> numList)
  90. {
  91. //List<int> numList = GetRandomArr(1, 8);
  92. Vector2 originPos = _ui.m_item.target.position;
  93. for (int i = 0; i < _gridNum; i++)
  94. {
  95. for (int j = 0; j < _gridNum; j++)
  96. {
  97. _gridArr[i, j].num = numList[i * _gridNum + j];
  98. _gridArr[i, j].pos = originPos + new Vector2(j * _ui.m_item.target.width, i * _ui.m_item.target.height);
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 检测是否是连续数组
  104. /// </summary>
  105. /// <param name="numList"></param>
  106. /// <returns></returns>
  107. private bool CheckListCorrect(List<int> numList)
  108. {
  109. for (int i = 0; i < numList.Count - 1; i++)
  110. {
  111. if (numList[i + 1] != numList[i] + 1)
  112. {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. /// <summary>
  119. /// 随机排序 min~max 连续的数字
  120. /// </summary>
  121. /// <param name="min"></param>
  122. /// <param name="max"></param>
  123. /// <returns></returns>
  124. private List<int> GetRandomArr(int min, int max)
  125. {
  126. List<int> list = new List<int>();
  127. for (int i = min; i <= max; i++)
  128. {
  129. list.Add(i);
  130. }
  131. System.Random r = new System.Random();
  132. for (int i = 0; i < list.Count; i++)
  133. {
  134. int index = r.Next(list.Count);
  135. int temp = list[index];
  136. list[index] = list[i];
  137. list[i] = temp;
  138. }
  139. // 最后一个是空的
  140. list.Add(0);
  141. return list;
  142. }
  143. private List<int> CreateIncreaseArr()
  144. {
  145. List<int> list = new List<int>();
  146. list.Add(0);
  147. for (int i = 1; i <= 8; i++)
  148. {
  149. list.Add(i);
  150. }
  151. return list;
  152. }
  153. /// <summary>
  154. /// 根据配置步数打乱数组
  155. /// </summary>
  156. /// <param name="configStep"></param>
  157. /// <returns></returns>
  158. private List<int> GetArrByConfigStep(int configStep)
  159. {
  160. List<int> list = CreateIncreaseArr();
  161. int num = 0;
  162. Vector2 zeroPos = new Vector2(0, 0);
  163. Vector2[] dirArr = { new Vector2(-1, 0), new Vector2(1, 0), new Vector2(0, -1), new Vector2(0, 1) };
  164. Vector2 lastPos = zeroPos;
  165. while (num < configStep)
  166. {
  167. int n = Random.Range(0, 4);
  168. Vector2 tempPos = zeroPos + dirArr[n];
  169. if (tempPos != lastPos && tempPos.x >= 0 && tempPos.y >= 0 && tempPos.x < _gridNum && tempPos.y < _gridNum)
  170. {
  171. list[(int)zeroPos.x * _gridNum + (int)zeroPos.y] = list[(int)tempPos.x * _gridNum + (int)tempPos.y];
  172. list[(int)tempPos.x * _gridNum + (int)tempPos.y] = 0;
  173. lastPos = zeroPos;
  174. zeroPos = tempPos;
  175. }
  176. ++num;
  177. }
  178. return list;
  179. }
  180. private void InitItems()
  181. {
  182. int itemIndex = 0;
  183. for (int i = 0; i < _gridNum; i++)
  184. {
  185. for (int j = 0; j < _gridNum; j++)
  186. {
  187. // 空格不用放item
  188. if (_gridArr[i, j].num == 0)
  189. {
  190. continue;
  191. }
  192. _items[itemIndex].position = _gridArr[i, j].pos;
  193. UI_item item = UI_item.Proxy(_items[itemIndex]);
  194. item.m_index.text = _gridArr[i, j].num.ToString();
  195. item.m_icon.url = string.Format("ui://ActivityHuaRongDao/hrd_1-{0}", _gridArr[i, j].num + 1);
  196. _items[itemIndex].data = _gridArr[i, j].num;
  197. UI_item.ProxyEnd();
  198. ++itemIndex;
  199. }
  200. }
  201. }
  202. /// <summary>
  203. /// 创建可移动的实体
  204. /// </summary>
  205. private void CreateItems()
  206. {
  207. for (int i = 1; i <= _gridNum * _gridNum - 1; i++)
  208. {
  209. GObject gObject = UIPackage.CreateObject("ActivityHuaRongDao", "item");
  210. gObject.name = "item" + i;
  211. gObject.onClick.Add(OnItemClick);
  212. _ui.m_items.target.AddChild(gObject);
  213. UI_item item = UI_item.Proxy(gObject);
  214. _items.Add(gObject);
  215. UI_item.ProxyEnd();
  216. }
  217. }
  218. private void OnItemClick(EventContext eventContext)
  219. {
  220. GObject obj = eventContext.sender as GObject;
  221. int num = (int)obj.data;
  222. Grid newGrid = CheckCanMove(num);
  223. if (newGrid != null)
  224. {
  225. _ui.m_items.target.touchable = false;
  226. obj.TweenMove(newGrid.pos, 0.1f).OnComplete(() =>
  227. {
  228. _ui.m_items.target.touchable = true;
  229. if (CheckWin())
  230. {
  231. _ui.m_maskGlobal.visible = true;
  232. Win(cancellationTokenSource.Token);
  233. }
  234. });
  235. }
  236. }
  237. private Grid CheckCanMove(int num)
  238. {
  239. int indexX = 0;
  240. int indexY = 0;
  241. // 找到格子
  242. for (int i = 0; i < _gridNum; i++)
  243. {
  244. for (int j = 0; j < _gridNum; j++)
  245. {
  246. if (_gridArr[i, j].num == num)
  247. {
  248. indexX = i;
  249. indexY = j;
  250. break;
  251. }
  252. }
  253. }
  254. // 判断格子四个方向是否有空格
  255. if (CheckHaveNullGrid(indexX - 1, indexY) || CheckHaveNullGrid(indexX, indexY - 1)
  256. || CheckHaveNullGrid(indexX + 1, indexY) || CheckHaveNullGrid(indexX, indexY + 1))
  257. {
  258. return UpdateGridInfo(num);
  259. }
  260. return null;
  261. }
  262. private bool CheckHaveNullGrid(int indexX, int indexY)
  263. {
  264. if (indexX >= 0 && indexX < _gridNum)
  265. {
  266. if (indexY >= 0 && indexY < _gridNum)
  267. {
  268. return _gridArr[indexX, indexY].num == 0;
  269. }
  270. }
  271. return false;
  272. }
  273. private Grid UpdateGridInfo(int num)
  274. {
  275. Vector2 nullGridindex = new Vector2(-1, 0);
  276. Vector2 numGridindex = new Vector2(-1, 0);
  277. for (int i = 0; i < _gridNum; i++)
  278. {
  279. for (int j = 0; j < _gridNum; j++)
  280. {
  281. if (_gridArr[i, j].num == num)
  282. {
  283. numGridindex = new Vector2(i, j);
  284. }
  285. else if (_gridArr[i, j].num == 0)
  286. {
  287. nullGridindex = new Vector2(i, j);
  288. }
  289. if (numGridindex.x != -1 && nullGridindex.x != -1)
  290. {
  291. break;
  292. }
  293. }
  294. }
  295. _gridArr[(int)numGridindex.x, (int)numGridindex.y].num = 0;
  296. _gridArr[(int)nullGridindex.x, (int)nullGridindex.y].num = num;
  297. return _gridArr[(int)nullGridindex.x, (int)nullGridindex.y];
  298. }
  299. private bool CheckWin()
  300. {
  301. int num = 0;
  302. for (int i = 0; i < _gridNum; i++)
  303. {
  304. for (int j = 0; j < _gridNum; j++)
  305. {
  306. if (i == 0 && j == 0)
  307. {
  308. num = _gridArr[i, j].num;
  309. if (num != 0)
  310. {
  311. return false;
  312. }
  313. }
  314. else
  315. {
  316. // 不连续 没有胜利
  317. if (_gridArr[i, j].num != num + 1)
  318. {
  319. return false;
  320. }
  321. // 继续检测
  322. else
  323. {
  324. ++num;
  325. // 胜利
  326. if (num == _gridNum * _gridNum - 1)
  327. {
  328. return true;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. return false;
  335. }
  336. private async Task Win(CancellationToken cancellationToken)
  337. {
  338. await Task.Delay(200, cancellationToken);
  339. blankItem.visible = true;
  340. blankItem.position = _gridArr[0, 0].pos;
  341. UI_item item = UI_item.Proxy(blankItem);
  342. item.m_icon.url = string.Format("ui://ActivityHuaRongDao/hrd_1-{0}", 1);
  343. UI_item.ProxyEnd();
  344. await Task.Delay(400, cancellationToken);
  345. // 关卡推进
  346. int passLevel = cfg.resArr[0];
  347. ActivityHuaRongDaoEntryView.curLevel = Mathf.Max(passLevel, ActivityHuaRongDaoEntryView.curLevel);
  348. EventAgent.DispatchEvent(ConstMessage.ACTIVITY_HUARONGDAO_UPDATE);
  349. // 弹出成功界面
  350. ViewManager.Show<ActivityHuaRongDaoSuccessView>(cfg);
  351. }
  352. private int countDownNum;
  353. private void Countdown(object param)
  354. {
  355. _ui.m_seconds.text = countDownNum.ToString() + "s";
  356. --countDownNum;
  357. if (countDownNum < 0)
  358. {
  359. _ui.m_tips.visible = false;
  360. Timers.inst.Remove(Countdown);
  361. _gameStart = true;
  362. return;
  363. }
  364. }
  365. private void ResetCountdown()
  366. {
  367. countDownNum = 3;
  368. _ui.m_tips.visible = true;
  369. Timers.inst.Remove(Countdown);
  370. }
  371. private void OnBtnBackClick()
  372. {
  373. if (_gameStart)
  374. {
  375. AlertUI.Show("退出游戏不保存进度,不扣除任何次数和道具,是否退出?")
  376. .SetLeftButton(true, "返回游戏")
  377. .SetRightButton(true, "仍要退出", (obj) =>
  378. {
  379. Hide();
  380. });
  381. }
  382. else
  383. {
  384. Hide();
  385. }
  386. }
  387. private async Task StartAnimation(CancellationToken cancellationToken)
  388. {
  389. try
  390. {
  391. _ui.m_hideMask.Play();
  392. _ui.m_maskGlobal.visible = true;
  393. List<int> numList = CreateIncreaseArr();
  394. SetGridInfo(numList);
  395. InitItems();
  396. CreateOriginPic();
  397. // 隐藏所有
  398. UI_item item;
  399. for (int i = 0; i < _items.Count; i++)
  400. {
  401. item = UI_item.Proxy(_items[i]);
  402. item.m_hide.Play();
  403. }
  404. // 入场
  405. for (int i = 0; i < _items.Count; i++)
  406. {
  407. item = UI_item.Proxy(_items[i]);
  408. item.m_show.Play();
  409. await Task.Delay(100, cancellationToken);
  410. }
  411. UI_item.ProxyEnd();
  412. // 出现白色遮罩,刷新数组
  413. await Task.Delay(900, cancellationToken);
  414. _ui.m_showMask.Play(() =>
  415. {
  416. _ui.m_maskGlobal.visible = false;
  417. _ui.m_hideMask.Play();
  418. _gameStart = true;
  419. });
  420. await Task.Delay(200, cancellationToken);
  421. numList = GetArrByConfigStep(cfg.step);
  422. while (CheckListCorrect(numList))
  423. {
  424. numList = GetArrByConfigStep(cfg.step);
  425. }
  426. SetGridInfo(numList);
  427. InitItems();
  428. }
  429. catch (TaskCanceledException)
  430. {
  431. //Debug.Log("异步函数被停止");
  432. }
  433. }
  434. // 取消异步函数
  435. public void StopMyAsyncFunction()
  436. {
  437. if (cancellationTokenSource != null)
  438. {
  439. cancellationTokenSource.Cancel();
  440. cancellationTokenSource.Dispose();
  441. cancellationTokenSource = null;
  442. }
  443. }
  444. /// <summary>
  445. /// 创建原图
  446. /// </summary>
  447. private void CreateOriginPic()
  448. {
  449. List<GObject> items = new List<GObject>();
  450. for (int i = 1; i <= _gridNum * _gridNum; i++)
  451. {
  452. GObject gObject = UIPackage.CreateObject("ActivityHuaRongDao", "item");
  453. _ui.m_itemsOrigin.target.AddChild(gObject);
  454. items.Add(gObject);
  455. }
  456. int itemIndex = 0;
  457. for (int i = 0; i < _gridNum; i++)
  458. {
  459. for (int j = 0; j < _gridNum; j++)
  460. {
  461. items[itemIndex].position = _gridArr[i, j].pos;
  462. UI_item item = UI_item.Proxy(items[itemIndex]);
  463. item.m_index.text = _gridArr[i, j].num.ToString();
  464. item.m_icon.url = string.Format("ui://ActivityHuaRongDao/hrd_1-{0}", _gridArr[i, j].num + 1);
  465. UI_item.ProxyEnd();
  466. ++itemIndex;
  467. }
  468. }
  469. }
  470. private void OnClickBtnShowOriginPic()
  471. {
  472. _ui.m_items.target.visible = false;
  473. _ui.m_itemsOrigin.target.visible = true;
  474. }
  475. private void OnClickBtnHideOriginPic()
  476. {
  477. _ui.m_items.target.visible = true;
  478. _ui.m_itemsOrigin.target.visible = false;
  479. }
  480. private void OnClickBtnRefresh()
  481. {
  482. StartAnimation(cancellationTokenSource.Token);
  483. }
  484. }
  485. }