TZFEGameVIew.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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_numList.onTouchBegin.Add(OnClickBegin);
  69. _ui.m_numList.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. if (!result) return;
  116. this.Hide();
  117. });
  118. }
  119. private void InitMap()
  120. {
  121. for(int i = 0; i < rows; i++)
  122. for(int j = 0; j < columns; j++)
  123. {
  124. Map[i, j] = 0;
  125. }
  126. }
  127. private void UpdateView()
  128. {
  129. time = 1;
  130. score = 0;
  131. targetNum = Merge2048GameArray.Instance.GetCfg(gameID).targetNum;
  132. CustemsNum[0] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar3;
  133. CustemsNum[1] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar2;
  134. CustemsNum[2] = Merge2048GameArray.Instance.GetCfg(gameID).timeStar1;
  135. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  136. _ui.m_target.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, targetNum)[0].resName);
  137. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  138. _ui.m_score.visible = false;
  139. _ui.m_time.visible = false;
  140. _ui.m_timeNum.visible = false;
  141. _ui.m_title.visible = false;
  142. rand = new System.Random();
  143. UpdateStar();
  144. Timers.inst.Add(1.0f, 0, UpdateTime);
  145. }
  146. private void UpdateScore()
  147. {
  148. _ui.m_score.text = string.Format("分数:{0}", score.ToString());
  149. }
  150. private void UpdateList()
  151. {
  152. RandomCreateNum();
  153. _ui.m_numList.numItems = rows * columns;
  154. }
  155. private void ListNumItem(int index, GObject item)
  156. {
  157. UI_numItem numItem = UI_numItem.Proxy(item);
  158. int x = index / rows;
  159. int y = index % columns;
  160. if(Map[x,y] == 0)
  161. {
  162. numItem.m_icon.visible = false;
  163. }
  164. else
  165. {
  166. //numItem.m_icon.url = string.Format("ui://MiniGame/sgll2_{0}", Map[x, y]);
  167. numItem.m_icon.url = ResPathUtil.GetMiniGamePicPath(Merge2048CfgArray.Instance.GetCfgsByidAndnum(gameDate.subType, Map[x, y])[0].resName);
  168. numItem.m_icon.visible = true;
  169. }
  170. //播放生成和合并动效
  171. for(int i=0; i<numPosArray.Count; i++)
  172. {
  173. if(x == numPosArray[i].x && y == numPosArray[i].y)
  174. {
  175. if(numPosArray[i].isCreat)
  176. {
  177. numItem.m_t0.Play();
  178. numPosArray.RemoveAt(i);
  179. break;
  180. }
  181. else
  182. {
  183. numItem.m_t1.Play();
  184. numPosArray.RemoveAt(i);
  185. break;
  186. }
  187. }
  188. }
  189. UI_numItem.ProxyEnd();
  190. }
  191. //随机生成数字2(%90),4(%10)
  192. private void RandomCreateNum()
  193. {
  194. bool gameOver = false;
  195. for(int i = 0;i< rows; i++)
  196. {
  197. for(int j = 0; j< columns; j++)
  198. {
  199. if (Map[i, j] == 0)
  200. {
  201. gameOver = true;
  202. break;
  203. }
  204. }
  205. }
  206. if(!gameOver)
  207. {
  208. //GameOver(false);
  209. return;
  210. }
  211. while (true)
  212. {
  213. int x = rand.Next(0, rows);
  214. int y = rand.Next(0, columns);
  215. NumPos item;
  216. int num;
  217. int randNum = rand.Next(1, 11);
  218. if(randNum <= 9)
  219. {
  220. num = 2;
  221. }
  222. else
  223. {
  224. num = 4 ;
  225. }
  226. if (Map[x,y] == 0)
  227. {
  228. Map[x, y] = num;
  229. item.x = x;
  230. item.y = y;
  231. item.isCreat = true;
  232. numPosArray.Add(item);
  233. break;
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// 去零
  239. /// </summary>
  240. /// <param name="row">对于一行或一列元素</param>
  241. private void Remove0(int[] row,Direction dir,int xy = 0)
  242. {
  243. int pos = 0;
  244. int[] rowB = new int[row.Length];
  245. for (int i = 0; i < row.Length; i++)
  246. {
  247. rowB[i] = row[i];
  248. }
  249. for (int i = 0; i < row.Length; ++i)
  250. {
  251. if (row[i] != 0)
  252. {
  253. row[pos] = row[i];
  254. //-----这里修改需要播放动效的数字位置列表-----
  255. int x = 0;
  256. int y = 0;
  257. int nextX = 0;
  258. int nextY = 0;
  259. for (int t = 0; t < numPosArray.Count; t++)
  260. {
  261. switch (dir)
  262. {
  263. case Direction.up:
  264. x = i;
  265. y = xy;
  266. nextX = pos;
  267. nextY = xy;
  268. break;
  269. case Direction.down:
  270. x = row.Length - 1 - i;
  271. y = xy;
  272. nextX = row.Length - 1 - pos;
  273. nextY = xy;
  274. break;
  275. case Direction.left:
  276. x = xy;
  277. y = i;
  278. nextX = xy;
  279. nextY = pos;
  280. break;
  281. case Direction.right:
  282. x = xy;
  283. y = row.Length - 1 - i;
  284. nextX = xy;
  285. nextY = row.Length - 1 - pos;
  286. break;
  287. }
  288. if(numPosArray.Count != 0 && numPosArray[t].x == x && numPosArray[t].y == y && pos != i)
  289. {
  290. numPosArray.RemoveAt(t);
  291. NumPos item;
  292. item.x = nextX;
  293. item.y = nextY;
  294. item.isCreat = false;
  295. numPosArray.Add(item);
  296. }
  297. }
  298. //---------------------------------------
  299. pos++;
  300. }
  301. }
  302. for (; pos < row.Length; ++pos) row[pos] = 0;
  303. for(int i= 0; i< row.Length; i++)
  304. {
  305. if(row[i] != rowB[i])
  306. {
  307. isMove = true;
  308. }
  309. }
  310. }
  311. /// <summary>
  312. /// 合并
  313. /// </summary>
  314. /// <param name="row">对于一行或一列元素,完成一次向左合并的操作</param>
  315. private void Merge(int[] row , Direction dir, int xy)
  316. {
  317. Remove0(row,dir,xy);
  318. // 相邻相同则合并
  319. for (int i = 0; i < row.Length - 1; ++i)
  320. {
  321. if (row[i] != 0 && row[i] == row[i + 1])
  322. {
  323. row[i] *= 2;
  324. row[i + 1] = 0;
  325. //将合并的数字放入列表
  326. NumPos item;
  327. item = MoveAddNum(i, xy, dir, row.Length-1);
  328. numPosArray.Add(item);
  329. //-------
  330. score += row[i];
  331. UpdateScore();
  332. if (row[i] == targetNum)
  333. {
  334. //游戏成功
  335. GameOver(true);
  336. }
  337. isMerge = true;
  338. }
  339. }
  340. Remove0(row,dir,xy);
  341. }
  342. //将合并的数字位置放入列表的准备
  343. private NumPos MoveAddNum(int i, int xy, Direction dir,int length)
  344. {
  345. NumPos item = new NumPos();
  346. switch (dir)
  347. {
  348. case Direction.up:
  349. item.x = i;
  350. item.y = xy;
  351. item.isCreat = false;
  352. break;
  353. case Direction.down:
  354. item.x = length - i;
  355. item.y = xy;
  356. item.isCreat = false;
  357. break;
  358. case Direction.left:
  359. item.x = xy;
  360. item.y = i;
  361. item.isCreat = false;
  362. break;
  363. case Direction.right:
  364. item.x = xy;
  365. item.y = length - i;
  366. item.isCreat = false;
  367. break;
  368. }
  369. return item;
  370. }
  371. /// <summary>
  372. /// 上移
  373. /// </summary>
  374. /// <param name="map">原棋盘</param>
  375. /// <returns></returns>
  376. private void Up(int[,] map)
  377. {
  378. int[] arr = new int[rows];
  379. for (int j = 0; j < columns; ++j)
  380. {
  381. for (int i = 0; i < rows; ++i)
  382. {
  383. arr[i] = map[i, j];
  384. }
  385. Merge(arr,Direction.up,j);
  386. for (int i = 0; i < rows; ++i) map[i, j] = arr[i];
  387. }
  388. }
  389. /// <summary>
  390. /// 下移
  391. /// </summary>
  392. private int[,] Down(int[,] map)
  393. {
  394. int[] arr = new int[rows];
  395. for (int j = 0; j < columns; ++j)
  396. {
  397. for (int i = 0; i < rows; ++i)
  398. {
  399. arr[rows - 1 - i] = map[i, j];
  400. }
  401. Merge(arr,Direction.down ,j);
  402. for (int i = 0; i < rows; ++i) map[i, j] = arr[rows - 1 - i];
  403. }
  404. return map;
  405. }
  406. /// <summary>
  407. /// 左移
  408. /// </summary>
  409. private int[,] Left(int[,] map)
  410. {
  411. int[] arr = new int[columns];
  412. for (int i = 0; i < rows; ++i)
  413. {
  414. for (int j = 0; j < columns; ++j)
  415. {
  416. arr[j] = map[i, j];
  417. }
  418. Merge(arr,Direction.left,i);
  419. for (int j = 0; j < columns; ++j) map[i, j] = arr[j];
  420. }
  421. return map;
  422. }
  423. /// <summary>
  424. /// 右移
  425. /// </summary>
  426. private int[,] Right(int[,] map)
  427. {
  428. int[] arr = new int[columns];
  429. for (int i = 0; i < rows; ++i)
  430. {
  431. for (int j = 0; j < columns; ++j)
  432. {
  433. arr[columns - 1 - j] = map[i, j];
  434. }
  435. Merge(arr,Direction.right,i);
  436. for (int j = 0; j < columns; ++j) map[i, j] = arr[columns - 1 - j];
  437. }
  438. return map;
  439. }
  440. /// <summary>
  441. /// 进行一次移动操作
  442. /// </summary>
  443. /// <param name="map">原棋盘</param>
  444. /// <param name="dir">移动的方向(枚举)</param>
  445. private void Move(int[,] map, Direction dir)
  446. {
  447. switch (dir)
  448. {
  449. case Direction.up:
  450. Up(map); break;
  451. case Direction.down:
  452. Down(map); break;
  453. case Direction.left:
  454. Left(map); break;
  455. case Direction.right:
  456. Right(map); break;
  457. }
  458. if(isMerge || isMove)
  459. {
  460. isMove = false;
  461. isMerge = false;
  462. UpdateList();
  463. numPosArray.Clear();
  464. }
  465. else
  466. {
  467. CheckArray();
  468. }
  469. }
  470. private void OnClickBegin()
  471. {
  472. touchFirst = Input.mousePosition;//记录开始按下的位置
  473. }
  474. private void OnClickEnd()
  475. {
  476. touchSecond = Input.mousePosition;//记录拖动的位置
  477. if ((touchSecond.x - touchFirst.x) < -80 && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  478. {
  479. //向左滑动
  480. Move(Map, Direction.left);
  481. }
  482. if ( (touchSecond.x - touchFirst.x) > 80 && Math.Abs(touchSecond.x - touchFirst.x) > Math.Abs(touchSecond.y - touchFirst.y))
  483. {
  484. //向右滑动
  485. Move(Map, Direction.right);
  486. }
  487. if ((touchSecond.y - touchFirst.y) < -80 && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  488. {
  489. //向下滑动
  490. Move(Map, Direction.down);
  491. }
  492. if ((touchSecond.y - touchFirst.y) > 80 && Math.Abs(touchSecond.y - touchFirst.y) > Math.Abs(touchSecond.x - touchFirst.x))
  493. {
  494. //向上滑动
  495. Move(Map, Direction.up);
  496. }
  497. touchFirst = touchSecond;
  498. }
  499. private void OnClickMergeTips()
  500. {
  501. ViewManager.Show<SyntheticRoutetipView>(new object[] { targetNum , gameDate.subType}) ;
  502. }
  503. private void UpdateTime(object param = null)
  504. {
  505. _ui.m_timeNum.text = sec_to_hms(time);
  506. time++;
  507. //UpdateStar();
  508. }
  509. private void UpdateStar()
  510. {
  511. int starNum = 0;
  512. for (int i = CustemsNum.Count - 1; i >= 0; i--)
  513. {
  514. if (time <= CustemsNum[i])
  515. {
  516. starNum += 1;
  517. }
  518. }
  519. switch (starNum)
  520. {
  521. case 1:
  522. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  523. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star_grey";
  524. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  525. break;
  526. case 2:
  527. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  528. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  529. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star_grey";
  530. break;
  531. case 3:
  532. _ui.m_star1.url = "ui://MiniGame/tb_zx_pt_star";
  533. _ui.m_star2.url = "ui://MiniGame/tb_zx_pt_star";
  534. _ui.m_star3.url = "ui://MiniGame/tb_zx_pt_star";
  535. break;
  536. default:
  537. _ui.m_star1.visible = false;
  538. _ui.m_star2.visible = false;
  539. _ui.m_star3.visible = false;
  540. break;
  541. }
  542. }
  543. //将秒数转化为时分秒 duration为秒数
  544. private string sec_to_hms(int duration)
  545. {
  546. TimeSpan ts = new TimeSpan(0, 0, duration);
  547. int _hours = 0;
  548. if (ts.Days > 0)
  549. {
  550. _hours = ts.Days * 24;
  551. }
  552. string str = "";
  553. if (ts.Hours > 0)
  554. {
  555. str = String.Format("{0:00}", ts.Hours + _hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  556. }
  557. if (ts.Hours == 0 && ts.Minutes > 0)
  558. {
  559. str = "00:";
  560. if (_hours > 0)
  561. {
  562. str = String.Format("{0:00}", ts.Hours + _hours) + ":";
  563. }
  564. str += String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
  565. }
  566. if (ts.Hours == 0 && ts.Minutes == 0)
  567. {
  568. str = "00";
  569. if (_hours > 0)
  570. {
  571. str = String.Format("{0:00}", ts.Hours + _hours);
  572. }
  573. str += ":00:" + String.Format("{0:00}", ts.Seconds);
  574. }
  575. return str;
  576. }
  577. private void CheckArray()
  578. {
  579. bool gameOver = false;
  580. for (int i = 0; i < rows; i++)
  581. {
  582. for (int j = 0; j < columns; j++)
  583. {
  584. if (Map[i, j] == 0)
  585. {
  586. gameOver = true;
  587. break;
  588. }
  589. }
  590. }
  591. if (!gameOver)
  592. {
  593. // 游戏结束
  594. GameOver(false);
  595. return;
  596. }
  597. }
  598. private void GameOver(bool target)
  599. {
  600. Timers.inst.Remove(UpdateTime);
  601. ViewManager.Show<ResultTipsView>(new object[]{target, gameDate.type, time, gameDate.id });
  602. }
  603. }
  604. }