TZFEGameVIew.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using cfg.GfgCfg;
  5. using ET;
  6. using FairyGUI;
  7. using UI.MiniGame;
  8. using UnityEngine;
  9. namespace GFGGame
  10. {
  11. public class TZFEGameView : BaseWindow
  12. {
  13. private UI_TZFEGameView _ui;
  14. private int[,] Map;
  15. enum Direction
  16. {
  17. up,
  18. down,
  19. left,
  20. right
  21. };
  22. private Vector2 touchFirst = Vector2.zero; //手指开始按下的位置
  23. private Vector2 touchSecond = Vector2.zero; //手指拖动的位置
  24. private System.Random rand;
  25. //这个是数组的行数和列数,mapLength一般采用行列一样
  26. private int rows = 4;
  27. private int columns = 4;
  28. private int mapLength = 5;
  29. //得分
  30. private int score = 0;
  31. private int scoreMax = 0;
  32. //目标分数
  33. private int targetNum = 128;
  34. private int gameID;
  35. private Merge2048GameCfg gameDate;
  36. private List<ActivityOpenCfg> activityGameDate;
  37. //评价
  38. private List<int> CustemsNum = new List<int>()
  39. {
  40. 0,
  41. 0,
  42. 0,
  43. };
  44. private bool isMove;
  45. private bool isMerge;
  46. private struct NumPos
  47. {
  48. public int x;
  49. public int y;
  50. public bool isCreat;
  51. }
  52. //需要播放动效的列表
  53. private List<NumPos> numPosArray = new List<NumPos>();
  54. private int time = 0;
  55. private EffectUI _effectUI1;
  56. public override void Dispose()
  57. {
  58. EffectUIPool.Recycle(_effectUI1);
  59. _effectUI1 = null;
  60. if (_ui != null)
  61. {
  62. _ui.Dispose();
  63. _ui = null;
  64. }
  65. base.Dispose();
  66. }
  67. protected override void OnInit()
  68. {
  69. base.OnInit();
  70. packageName = UI_TZFEGameView.PACKAGE_NAME;
  71. _ui = UI_TZFEGameView.Create();
  72. this.viewCom = _ui.target;
  73. isfullScreen = true;
  74. isReturnView = true;
  75. _ui.m_numList.itemRenderer = ListNumItem;
  76. _ui.m_mask.onTouchBegin.Add(OnClickBegin);
  77. _ui.m_mask.onTouchEnd.Add(OnClickEnd);
  78. _ui.m_backBtn.onClick.Add(OnClickBtnBack);
  79. _ui.m_mergeBtn.onClick.Add(OnClickMergeTips);
  80. Map = new int[rows, columns];
  81. EffectUIPool.CreateEffectUI(_ui.m_effectTitle, "ui_Activity", "SGLL_Game_Text",
  82. onComplete: (effect) =>
  83. {
  84. if (effect != null)
  85. {
  86. _effectUI1 = effect;
  87. }
  88. });
  89. }
  90. protected override void OnShown()
  91. {
  92. base.OnShown();
  93. if ((this.viewData as object[]).Length != 0 && this.viewData != null)
  94. {
  95. gameID = (int)(this.viewData as object[])[0];
  96. }
  97. else
  98. {
  99. gameID = 128;
  100. }
  101. gameDate = CommonDataManager.Tables.TblMerge2048GameCfg.GetOrDefault(gameID);
  102. activityGameDate = CommonDataManager.Tables.TblActivityOpenCfg.DataList
  103. .Where(a => a.Type == ConstLimitTimeActivityType.ActLimitStlyc).ToList();
  104. InitMap();
  105. UpdateView();
  106. UpdateList();
  107. }
  108. protected override void OnHide()
  109. {
  110. Timers.inst.Remove(UpdateTime);
  111. base.OnHide();
  112. }
  113. private void OnClickBtnBack()
  114. {
  115. string exitTip;
  116. if (gameDate.BonusLose.Count == 0)
  117. {
  118. exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
  119. }
  120. else
  121. {
  122. exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
  123. }
  124. AlertUI.Show(exitTip)
  125. .SetLeftButton(true, "取消", (object data) => { })
  126. .SetRightButton(true, "确定", async (object data) =>
  127. {
  128. var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.Type, time, false,
  129. activityGameDate[0].Id, true);
  130. this.Hide();
  131. });
  132. }
  133. private void InitMap()
  134. {
  135. for (int i = 0; i < rows; i++)
  136. for (int j = 0; j < columns; j++)
  137. {
  138. Map[i, j] = 0;
  139. }
  140. }
  141. private void UpdateView()
  142. {
  143. time = 1;
  144. score = 0;
  145. targetNum = CommonDataManager.Tables.TblMerge2048GameCfg.GetOrDefault(gameID).TargetNum;
  146. CustemsNum[0] = CommonDataManager.Tables.TblMerge2048GameCfg.GetOrDefault(gameID).TimeStar3;
  147. CustemsNum[1] = CommonDataManager.Tables.TblMerge2048GameCfg.GetOrDefault(gameID).TimeStar2;
  148. CustemsNum[2] = CommonDataManager.Tables.TblMerge2048GameCfg.GetOrDefault(gameID).TimeStar1;
  149. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  150. _ui.m_target.url = ResPathUtil.GetMiniGamePicPath(
  151. CommonDataManager.Tables.TblMerge2048Cfg.DataList
  152. .Where(a => a.Id == gameDate.SubType && a.Num == targetNum).ToList()[0].ResName,
  153. "TZFEGame");
  154. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  155. _ui.m_score.visible = false;
  156. _ui.m_time.visible = false;
  157. _ui.m_timeNum.visible = false;
  158. _ui.m_title.visible = false;
  159. rand = new System.Random();
  160. UpdateStar();
  161. Timers.inst.Add(1.0f, 0, UpdateTime);
  162. }
  163. private void UpdateScore()
  164. {
  165. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  166. }
  167. private void UpdateList()
  168. {
  169. RandomCreateNum();
  170. _ui.m_numList.numItems = rows * columns;
  171. }
  172. private void ListNumItem(int index, GObject item)
  173. {
  174. UI_numItem numItem = UI_numItem.Proxy(item);
  175. int x = index / rows;
  176. int y = index % columns;
  177. if (Map[x, y] == 0)
  178. {
  179. numItem.m_icon.visible = false;
  180. }
  181. else
  182. {
  183. //numItem.m_icon.url = string.Format("ui://MiniGame/sgll2_{0}", Map[x, y]);
  184. numItem.m_icon.url = ResPathUtil.GetMiniGamePicPath(
  185. CommonDataManager.Tables.TblMerge2048Cfg.DataList
  186. .Where(a => a.Id == gameDate.SubType && a.Num == Map[x, y]).ToList()[0].ResName, "TZFEGame");
  187. numItem.m_icon.visible = true;
  188. }
  189. //播放生成和合并动效
  190. for (int i = 0; i < numPosArray.Count; i++)
  191. {
  192. if (x == numPosArray[i].x && y == numPosArray[i].y)
  193. {
  194. if (numPosArray[i].isCreat)
  195. {
  196. numItem.m_t0.Play();
  197. numPosArray.RemoveAt(i);
  198. break;
  199. }
  200. else
  201. {
  202. numItem.m_t1.Play();
  203. numPosArray.RemoveAt(i);
  204. break;
  205. }
  206. }
  207. }
  208. UI_numItem.ProxyEnd();
  209. }
  210. //随机生成数字2(%90),4(%10)
  211. private void RandomCreateNum()
  212. {
  213. bool gameOver = false;
  214. for (int i = 0; i < rows; i++)
  215. {
  216. for (int j = 0; j < columns; j++)
  217. {
  218. if (Map[i, j] == 0)
  219. {
  220. gameOver = true;
  221. break;
  222. }
  223. }
  224. }
  225. if (!gameOver)
  226. {
  227. //GameOver(false);
  228. return;
  229. }
  230. while (true)
  231. {
  232. int x = rand.Next(0, rows);
  233. int y = rand.Next(0, columns);
  234. NumPos item;
  235. int num;
  236. int randNum = rand.Next(1, 11);
  237. if (randNum <= 9)
  238. {
  239. num = 2;
  240. }
  241. else
  242. {
  243. num = 4;
  244. }
  245. if (Map[x, y] == 0)
  246. {
  247. Map[x, y] = num;
  248. item.x = x;
  249. item.y = y;
  250. item.isCreat = true;
  251. numPosArray.Add(item);
  252. break;
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// 去零
  258. /// </summary>
  259. /// <param name="row">对于一行或一列元素</param>
  260. private void Remove0(int[] row, Direction dir, int xy = 0)
  261. {
  262. int pos = 0;
  263. int[] rowB = new int[row.Length];
  264. for (int i = 0; i < row.Length; i++)
  265. {
  266. rowB[i] = row[i];
  267. }
  268. for (int i = 0; i < row.Length; ++i)
  269. {
  270. if (row[i] != 0)
  271. {
  272. row[pos] = row[i];
  273. //-----这里修改需要播放动效的数字位置列表-----
  274. int x = 0;
  275. int y = 0;
  276. int nextX = 0;
  277. int nextY = 0;
  278. for (int t = 0; t < numPosArray.Count; t++)
  279. {
  280. switch (dir)
  281. {
  282. case Direction.up:
  283. x = i;
  284. y = xy;
  285. nextX = pos;
  286. nextY = xy;
  287. break;
  288. case Direction.down:
  289. x = row.Length - 1 - i;
  290. y = xy;
  291. nextX = row.Length - 1 - pos;
  292. nextY = xy;
  293. break;
  294. case Direction.left:
  295. x = xy;
  296. y = i;
  297. nextX = xy;
  298. nextY = pos;
  299. break;
  300. case Direction.right:
  301. x = xy;
  302. y = row.Length - 1 - i;
  303. nextX = xy;
  304. nextY = row.Length - 1 - pos;
  305. break;
  306. }
  307. if (numPosArray.Count != 0 && numPosArray[t].x == x && numPosArray[t].y == y && pos != i)
  308. {
  309. numPosArray.RemoveAt(t);
  310. NumPos item;
  311. item.x = nextX;
  312. item.y = nextY;
  313. item.isCreat = false;
  314. numPosArray.Add(item);
  315. }
  316. }
  317. //---------------------------------------
  318. pos++;
  319. }
  320. }
  321. for (; pos < row.Length; ++pos) row[pos] = 0;
  322. for (int i = 0; i < row.Length; i++)
  323. {
  324. if (row[i] != rowB[i])
  325. {
  326. isMove = true;
  327. }
  328. }
  329. }
  330. /// <summary>
  331. /// 合并
  332. /// </summary>
  333. /// <param name="row">对于一行或一列元素,完成一次向左合并的操作</param>
  334. private void Merge(int[] row, Direction dir, int xy)
  335. {
  336. Remove0(row, dir, xy);
  337. // 相邻相同则合并
  338. for (int i = 0; i < row.Length - 1; ++i)
  339. {
  340. if (row[i] != 0 && row[i] == row[i + 1])
  341. {
  342. row[i] *= 2;
  343. row[i + 1] = 0;
  344. //将合并的数字放入列表
  345. NumPos item;
  346. item = MoveAddNum(i, xy, dir, row.Length - 1);
  347. numPosArray.Add(item);
  348. //-------
  349. score += row[i];
  350. UpdateScore();
  351. if (row[i] == targetNum)
  352. {
  353. //游戏成功
  354. GameOver(true);
  355. }
  356. isMerge = true;
  357. }
  358. }
  359. Remove0(row, dir, xy);
  360. }
  361. //将合并的数字位置放入列表的准备
  362. private NumPos MoveAddNum(int i, int xy, Direction dir, int length)
  363. {
  364. NumPos item = new NumPos();
  365. switch (dir)
  366. {
  367. case Direction.up:
  368. item.x = i;
  369. item.y = xy;
  370. item.isCreat = false;
  371. break;
  372. case Direction.down:
  373. item.x = length - i;
  374. item.y = xy;
  375. item.isCreat = false;
  376. break;
  377. case Direction.left:
  378. item.x = xy;
  379. item.y = i;
  380. item.isCreat = false;
  381. break;
  382. case Direction.right:
  383. item.x = xy;
  384. item.y = length - i;
  385. item.isCreat = false;
  386. break;
  387. }
  388. return item;
  389. }
  390. /// <summary>
  391. /// 上移
  392. /// </summary>
  393. /// <param name="map">原棋盘</param>
  394. /// <returns></returns>
  395. private void Up(int[,] map)
  396. {
  397. int[] arr = new int[rows];
  398. for (int j = 0; j < columns; ++j)
  399. {
  400. for (int i = 0; i < rows; ++i)
  401. {
  402. arr[i] = map[i, j];
  403. }
  404. Merge(arr, Direction.up, j);
  405. for (int i = 0; i < rows; ++i) map[i, j] = arr[i];
  406. }
  407. }
  408. /// <summary>
  409. /// 下移
  410. /// </summary>
  411. private int[,] Down(int[,] map)
  412. {
  413. int[] arr = new int[rows];
  414. for (int j = 0; j < columns; ++j)
  415. {
  416. for (int i = 0; i < rows; ++i)
  417. {
  418. arr[rows - 1 - i] = map[i, j];
  419. }
  420. Merge(arr, Direction.down, j);
  421. for (int i = 0; i < rows; ++i) map[i, j] = arr[rows - 1 - i];
  422. }
  423. return map;
  424. }
  425. /// <summary>
  426. /// 左移
  427. /// </summary>
  428. private int[,] Left(int[,] map)
  429. {
  430. int[] arr = new int[columns];
  431. for (int i = 0; i < rows; ++i)
  432. {
  433. for (int j = 0; j < columns; ++j)
  434. {
  435. arr[j] = map[i, j];
  436. }
  437. Merge(arr, Direction.left, i);
  438. for (int j = 0; j < columns; ++j) map[i, j] = arr[j];
  439. }
  440. return map;
  441. }
  442. /// <summary>
  443. /// 右移
  444. /// </summary>
  445. private int[,] Right(int[,] map)
  446. {
  447. int[] arr = new int[columns];
  448. for (int i = 0; i < rows; ++i)
  449. {
  450. for (int j = 0; j < columns; ++j)
  451. {
  452. arr[columns - 1 - j] = map[i, j];
  453. }
  454. Merge(arr, Direction.right, i);
  455. for (int j = 0; j < columns; ++j) map[i, j] = arr[columns - 1 - j];
  456. }
  457. return map;
  458. }
  459. /// <summary>
  460. /// 进行一次移动操作
  461. /// </summary>
  462. /// <param name="map">原棋盘</param>
  463. /// <param name="dir">移动的方向(枚举)</param>
  464. private void Move(int[,] map, Direction dir)
  465. {
  466. switch (dir)
  467. {
  468. case Direction.up:
  469. Up(map); break;
  470. case Direction.down:
  471. Down(map); break;
  472. case Direction.left:
  473. Left(map); break;
  474. case Direction.right:
  475. Right(map); break;
  476. }
  477. if (isMerge || isMove)
  478. {
  479. isMove = false;
  480. isMerge = false;
  481. UpdateList();
  482. numPosArray.Clear();
  483. }
  484. else
  485. {
  486. CheckArray();
  487. }
  488. }
  489. private void OnClickBegin()
  490. {
  491. touchFirst = Input.mousePosition; //记录开始按下的位置
  492. }
  493. private void OnClickEnd()
  494. {
  495. touchSecond = Input.mousePosition; //记录拖动的位置
  496. if (touchSecond.x < touchFirst.x &&
  497. Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  498. {
  499. //向左滑动
  500. Move(Map, Direction.left);
  501. }
  502. if (touchSecond.x > touchFirst.x &&
  503. Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  504. {
  505. //向右滑动
  506. Move(Map, Direction.right);
  507. }
  508. if (touchSecond.y < touchFirst.y &&
  509. Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  510. {
  511. //向下滑动
  512. Move(Map, Direction.down);
  513. }
  514. if (touchSecond.y > touchFirst.y &&
  515. Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  516. {
  517. //向上滑动
  518. Move(Map, Direction.up);
  519. }
  520. touchFirst = touchSecond;
  521. }
  522. private void OnClickMergeTips()
  523. {
  524. ViewManager.Show<SyntheticRoutetipView>(new object[] { targetNum, gameDate.SubType });
  525. }
  526. private void UpdateTime(object param = null)
  527. {
  528. _ui.m_timeNum.text = sec_to_hms(time);
  529. time++;
  530. //UpdateStar();
  531. }
  532. private void UpdateStar()
  533. {
  534. int starNum = 0;
  535. for (int i = CustemsNum.Count - 1; i >= 0; i--)
  536. {
  537. if (time <= CustemsNum[i])
  538. {
  539. starNum += 1;
  540. }
  541. }
  542. switch (starNum)
  543. {
  544. case 1:
  545. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  546. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star_grey";
  547. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  548. break;
  549. case 2:
  550. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  551. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  552. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  553. break;
  554. case 3:
  555. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  556. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  557. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  558. break;
  559. default:
  560. _ui.m_star1.visible = false;
  561. _ui.m_star2.visible = false;
  562. _ui.m_star3.visible = false;
  563. break;
  564. }
  565. }
  566. //将秒数转化为时分秒 duration为秒数
  567. private string sec_to_hms(int duration)
  568. {
  569. TimeSpan ts = new TimeSpan(0, 0, duration);
  570. int _hours = 0;
  571. if (ts.Days > 0)
  572. {
  573. _hours = ts.Days * 24;
  574. }
  575. string str = "";
  576. if (ts.Hours > 0)
  577. {
  578. str = String.Format("{0:00}", ts.Hours + _hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" +
  579. String.Format("{0:00}", ts.Seconds);
  580. }
  581. if (ts.Hours == 0 && ts.Minutes > 0)
  582. {
  583. str = "00:";
  584. if (_hours > 0)
  585. {
  586. str = String.Format("{0:00}", ts.Hours + _hours) + ":";
  587. }
  588. str += String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  589. }
  590. if (ts.Hours == 0 && ts.Minutes == 0)
  591. {
  592. str = "00";
  593. if (_hours > 0)
  594. {
  595. str = String.Format("{0:00}", ts.Hours + _hours);
  596. }
  597. str += ":00:" + String.Format("{0:00}", ts.Seconds);
  598. }
  599. return str;
  600. }
  601. private void CheckArray()
  602. {
  603. bool gameOver = false;
  604. for (int i = 0; i < rows; i++)
  605. {
  606. for (int j = 0; j < columns; j++)
  607. {
  608. if (Map[i, j] == 0)
  609. {
  610. gameOver = true;
  611. break;
  612. }
  613. }
  614. }
  615. if (!gameOver)
  616. {
  617. // 游戏结束
  618. GameOver(false);
  619. return;
  620. }
  621. }
  622. private void GameOver(bool target)
  623. {
  624. Timers.inst.Remove(UpdateTime);
  625. ViewManager.Show<ResultTipsView>(new object[] { target, gameDate.Type, time, gameDate.Id });
  626. }
  627. }
  628. }