TZFEGameVIew.cs 20 KB

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