TZFEGameVIew.cs 18 KB

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