GameUI.ts 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. const { ccclass, property } = cc._decorator;
  2. import DirectionType from '../datas/DirectionType';
  3. import CatInfo from '../datas/CatInfo';
  4. import PrefabUtil from '../utils/manager/PrefabUtil';
  5. import GameUtil from '../utils/util/GameUtil';
  6. import UIbase from '../utils/UIbase';
  7. import TipsUI from './TipsUI';
  8. import LocalData from '../manager/LocalData';
  9. import WinUI from './WinUI';
  10. import FailUI from './FailUI';
  11. import PauseUI from './PauseUI';
  12. import StepOverUI from './StepOverUI';
  13. import TimeOverUI from './TimeOverUI';
  14. import Plat from '../utils/Palt';
  15. import AudioPath from '../datas/AudioPath';
  16. import AudioMgr from '../manager/AudioMgr';
  17. @ccclass
  18. export default class GameUI extends UIbase {
  19. private static _inst: GameUI;
  20. public static get inst() {
  21. if (this._inst == null) {
  22. let v = cc.instantiate(PrefabUtil.get("GameUI"));
  23. this._inst = v.getComponent(GameUI);
  24. }
  25. return this._inst;
  26. }
  27. @property(cc.Node)
  28. catContainer: cc.Node = null;
  29. @property(cc.Node)
  30. showNode: cc.Node = null;
  31. @property(cc.Node)
  32. maskNode: cc.Node = null; //遮罩
  33. @property(cc.Node)
  34. buttonsNode: cc.Node = null; //按钮组
  35. @property(cc.Node)
  36. selectNode: cc.Node = null; //选择视图
  37. @property(cc.Label)
  38. timerLabel: cc.Label = null; //计时器
  39. @property(cc.Label)
  40. curLevel: cc.Label = null; //关卡
  41. @property(cc.Label)
  42. stepLabel: cc.Label = null;//步数计数器
  43. // public grids:Map<number,GridInfo>=new Map();
  44. public grid_num_list: Array<number> = []
  45. public grid_map: Map<number, number> = new Map();
  46. public cat_info_list: Array<CatInfo> = [];
  47. public horizontal_count = 20;
  48. public vertical_count = 20;
  49. public ww = 50;
  50. public old_cat_pos: Array<CatInfo> = [];
  51. public is_game_playing = false;
  52. public is_game_pause = false;
  53. private stepCount = 0;
  54. start() {
  55. setInterval(() => {
  56. this.onTimerUpdate();
  57. }, 1000);
  58. }
  59. public resetGameData() {
  60. this.grid_num_list = [];
  61. this.grid_map = new Map();
  62. this.cat_info_list = [];
  63. this.catContainer.removeAllChildren();
  64. this.maskNode.parent = this.catContainer;
  65. this.selectNode.active = false
  66. }
  67. private updateStepStr() {
  68. this.stepLabel.string = this.stepCount + ""
  69. }
  70. public addStepCount() {
  71. this.stepCount += 60
  72. this.updateStepStr()
  73. this.is_game_playing = true;
  74. }
  75. private deleteStepCount() {
  76. this.stepCount--
  77. this.updateStepStr()
  78. if (this.stepCount <= 0) {
  79. this.onStepOver()
  80. }
  81. }
  82. public onTimeOver() {
  83. this.is_game_playing = false;
  84. // FailUI.inst.showUI();
  85. TimeOverUI.inst.showUI()
  86. }
  87. public onStepOver() {
  88. this.is_game_playing = false;
  89. StepOverUI.inst.showUI()
  90. }
  91. public onStartGame() {
  92. this.is_game_pause = false;
  93. this.resetGameData();
  94. this.showUI();
  95. this.randomCatInfos();
  96. this.updateUI();
  97. this.scheduleOnce(() => {
  98. this.checkShow();
  99. }, 0.1)
  100. this.curLevel.string = "第 " + LocalData.lv + " 关";
  101. this.updateTimeLabel();
  102. }
  103. private setLvData() {
  104. let lv = LocalData.lv;
  105. if (lv == 1) {
  106. this.vertical_count = 6;
  107. this.horizontal_count = 6;
  108. this.catContainer.scale = 1.5;
  109. this.time = 60;
  110. }
  111. else if (lv == 2) {
  112. this.vertical_count = 25;
  113. this.horizontal_count = 25;
  114. this.catContainer.scale = 1;
  115. this.time = 240;
  116. }
  117. else {
  118. this.vertical_count = 25;
  119. this.horizontal_count = 25;
  120. this.time = 240;
  121. this.catContainer.scale = 0.8;
  122. }
  123. }
  124. private dirs: Array<number> = [1, 2, 3, 4];
  125. public randomCatInfos() {
  126. this.setLvData();
  127. this.cat_info_list = [];
  128. for (let i = 0; i < this.vertical_count; i++) {
  129. for (let k = 0; k < this.horizontal_count; k++) {
  130. this.grid_num_list.push(i * 10000 + k);
  131. this.grid_map.set(i * 10000 + k, 0);
  132. }
  133. }
  134. GameUtil.randomRangeSort(this.grid_num_list);
  135. let index = 0;
  136. while (true) {
  137. if (this.grid_num_list.length == 0) {
  138. break;
  139. }
  140. GameUtil.randomRangeSort(this.dirs);
  141. let flag = false;
  142. if (GameUtil.randomRange(0, 100) < 50) {
  143. for (let i = 0; i < 4; i++) {
  144. let id = this.grid_num_list[0];
  145. let id2 = this.getNextGrid(id, this.dirs[i]);
  146. if (id2 == -1) {
  147. continue;
  148. }
  149. if (this.grid_map.get(id2) == 1) {
  150. continue;
  151. }
  152. let info = new CatInfo();
  153. info.dir = this.dirs[i];
  154. info.now_grids.push(id);
  155. info.now_grids.push(id2);
  156. this.grid_map.set(id, 1);
  157. this.grid_map.set(id2, 1);
  158. GameUtil.deleteValue(this.grid_num_list, id);
  159. GameUtil.deleteValue(this.grid_num_list, id2);
  160. this.cat_info_list.push(info);
  161. flag = true;
  162. break;
  163. }
  164. }
  165. if (flag == false) {
  166. // console.warn("咋回事呀");
  167. this.grid_num_list.shift();
  168. }
  169. index++;
  170. if (index > 10000) {
  171. break;
  172. }
  173. }
  174. if (LocalData.lv == 1 && this.canAllCatOut() == false) {
  175. this.randomCatInfos();
  176. }
  177. if (this.cat_info_list.length < 4) {
  178. this.randomCatInfos();
  179. }
  180. }
  181. public getNextGrid(id: number, dir: number) {
  182. let i = Math.floor(id / 10000);
  183. let k = id % 10000;
  184. if (dir == DirectionType.up) {
  185. k--;
  186. }
  187. else if (dir == DirectionType.down) {
  188. k++;
  189. }
  190. else if (dir == DirectionType.left) {
  191. i--;
  192. }
  193. else if (dir == DirectionType.right) {
  194. i++;
  195. }
  196. let val = i * 10000 + k;
  197. if (this.grid_map.has(val) == true) {
  198. return val;
  199. }
  200. else {
  201. return -1;
  202. }
  203. }
  204. public updateUI() {
  205. for (let i = 0; i < this.cat_info_list.length; i++) {
  206. let cat = this.getCatNode(this.cat_info_list[i]);
  207. cat.parent = this.catContainer;
  208. cat.on(cc.Node.EventType.TOUCH_END, this.onClickCatNode, this)
  209. this.cat_info_list[i].cat = cat;
  210. cat.opacity = 0;
  211. }
  212. }
  213. public getCatNode(info: CatInfo): cc.Node {
  214. let node: cc.Node;
  215. if (info.cat == null) {
  216. node = cc.instantiate(PrefabUtil.get("CatItem"));
  217. // console.warn("创建的羊---"+(<any>node)._id)
  218. }
  219. else {
  220. node = info.cat;
  221. }
  222. let x = Math.floor(info.now_grids[0] / 10000);
  223. let y = info.now_grids[0] % 10000;
  224. x = (x * this.ww - this.ww / 2 * this.vertical_count) + this.ww / 2;
  225. y = (this.ww / 2 * this.horizontal_count - y * this.ww) - this.ww / 2;
  226. if (info.dir == DirectionType.up) {
  227. y += this.ww / 2;
  228. node.angle = 0
  229. }
  230. else if (info.dir == DirectionType.down) {
  231. y -= this.ww / 2;
  232. node.angle = 180
  233. }
  234. else if (info.dir == DirectionType.left) {
  235. x -= this.ww / 2;
  236. node.angle = 90
  237. }
  238. else if (info.dir == DirectionType.right) {
  239. x += this.ww / 2;
  240. node.angle = 270
  241. }
  242. node.x = x;
  243. node.y = y;
  244. return node;
  245. }
  246. public onClickCatNode(event: cc.Event.EventTouch) {
  247. AudioMgr.playSound(AudioPath.SHEEP);
  248. if (this.is_game_playing == false) {
  249. return;
  250. }
  251. this.maskNode.active = false;
  252. for (let i = 0; i < this.cat_info_list.length; i++) {
  253. this.cat_info_list[i].cat.zIndex = 0;
  254. }
  255. // console.warn("被点击的羊---"+(<any>event.target)._id)
  256. for (let i = 0; i < this.cat_info_list.length; i++) {
  257. // console.warn("被查询的羊---"+(<any>this.yangs[i].cat)._id)
  258. if (this.cat_info_list[i].cat == event.target) {
  259. // console.warn(this.yangs[i].grids[0]+"的羊被点击了");
  260. if (this.fz_flag == true) {
  261. this.resetDirection2(this.cat_info_list[i]);
  262. }
  263. else {
  264. this.onMoveCat(this.cat_info_list[i]);
  265. }
  266. return;
  267. }
  268. }
  269. }
  270. //检测出来移动位置
  271. public onMoveCat(info: CatInfo) {
  272. for (let i = 0; i < info.now_grids.length; i++) {
  273. this.grid_map.set(info.now_grids[i], 0);
  274. }
  275. let i = Math.floor(info.now_grids[0] / 10000);
  276. let k = info.now_grids[0] % 10000;
  277. if (info.move_tween && info.lastPosition) {
  278. info.move_tween.stop()
  279. info.cat.x = info.lastPosition.x;
  280. info.cat.y = info.lastPosition.y;
  281. }
  282. info.lastPosition = new cc.Vec2(info.cat.x, info.cat.y)
  283. const startX = info.cat.x;
  284. const startY = info.cat.y;
  285. let add = 0;
  286. this.deleteStepCount()
  287. if (info.dir == DirectionType.up) {
  288. k -= 1;
  289. while (true) {
  290. add += 1;
  291. if (k - add < 0) {
  292. add = 100;
  293. break;
  294. }
  295. if (this.grid_map.get(i * 10000 + (k - add)) == 1) {
  296. add -= 1;
  297. break;
  298. }
  299. }
  300. if (add != 0) {
  301. info.push_last();
  302. this.old_cat_pos.push(info);
  303. }
  304. if (add == 100) {
  305. //可以消失
  306. info.move_tween = cc.tween(info.cat)
  307. .to(0.1, { y: info.cat.y + 15 })
  308. .to(5, { y: info.cat.y + 2000 })
  309. .call(() => {
  310. // console.warn("向上移出完成");
  311. info.move_tween = null;
  312. info.lastPosition = null;
  313. })
  314. .start();
  315. this.removeCat(info);
  316. }
  317. else {
  318. //碰撞
  319. info.move_tween = cc.tween(info.cat)
  320. .to(0.1, { y: info.cat.y + 15 })
  321. .to(0.2 * add, { y: info.cat.y + 15 + this.ww * add })
  322. .delay(0.1)
  323. .to(0.1, { y: info.cat.y + 15 + this.ww * add - 15 })
  324. .call(() => {
  325. // console.warn("向上移出完成");
  326. info.move_tween = null;
  327. info.lastPosition = null;
  328. })
  329. .start();
  330. for (let i = 0; i < info.now_grids.length; i++) {
  331. let a = Math.floor(info.now_grids[i] / 10000);
  332. let b = info.now_grids[i] % 10000;
  333. b = b - add;
  334. info.now_grids[i] = a * 10000 + b;
  335. this.grid_map.set(info.now_grids[i], 1);
  336. }
  337. }
  338. }
  339. else if (info.dir == DirectionType.down) {
  340. k += 1;
  341. while (true) {
  342. add += 1;
  343. if (k + add >= this.horizontal_count) {
  344. add = 100;
  345. break;
  346. }
  347. if (this.grid_map.get(i * 10000 + (k + add)) == 1) {
  348. add -= 1;
  349. break;
  350. }
  351. }
  352. if (add != 0) {
  353. info.push_last();
  354. this.old_cat_pos.push(info);
  355. }
  356. if (add == 100) {
  357. //可以消失
  358. info.move_tween = cc.tween(info.cat)
  359. .to(0.1, { y: info.cat.y - 15 })
  360. .to(5, { y: info.cat.y - 2000 })
  361. .call(() => {
  362. // console.warn("向上移出完成");
  363. info.move_tween = null;
  364. info.lastPosition = null;
  365. })
  366. .start();
  367. this.removeCat(info);
  368. }
  369. else {
  370. //碰撞
  371. info.move_tween = cc.tween(info.cat)
  372. .to(0.1, { y: info.cat.y - 15 })
  373. .to(0.2 * add, { y: info.cat.y - 15 - this.ww * add })
  374. .delay(0.1)
  375. .to(0.1, { y: info.cat.y - 15 - this.ww * add + 15 })
  376. .call(() => {
  377. // console.warn("向下移出完成");
  378. info.move_tween = null;
  379. info.lastPosition = null;
  380. })
  381. .start();
  382. for (let i = 0; i < info.now_grids.length; i++) {
  383. let a = Math.floor(info.now_grids[i] / 10000);
  384. let b = info.now_grids[i] % 10000;
  385. b = b + add;
  386. info.now_grids[i] = a * 10000 + b;
  387. this.grid_map.set(info.now_grids[i], 1);
  388. }
  389. }
  390. }
  391. else if (info.dir == DirectionType.left) {
  392. i -= 1;
  393. while (true) {
  394. add += 1;
  395. if (i - add < 0) {
  396. add = 100;
  397. break;
  398. }
  399. if (this.grid_map.get((i - add) * 10000 + k) == 1) {
  400. add -= 1;
  401. break;
  402. }
  403. }
  404. if (add != 0) {
  405. info.push_last();
  406. this.old_cat_pos.push(info);
  407. }
  408. if (add == 100) {
  409. //可以消失
  410. info.move_tween = cc.tween(info.cat)
  411. .to(0.1, { x: info.cat.x - 15 })
  412. .to(5, { x: info.cat.x - 2000 })
  413. .call(() => {
  414. // console.warn("向上移出完成");
  415. info.move_tween = null;
  416. info.lastPosition = null;
  417. })
  418. .start();
  419. this.removeCat(info);
  420. }
  421. else {
  422. //碰撞
  423. info.move_tween = cc.tween(info.cat)
  424. .to(0.1, { x: info.cat.x - 15 })
  425. .to(0.2 * add, { x: info.cat.x - 15 - this.ww * add })
  426. .delay(0.1)
  427. .to(0.1, { x: info.cat.x - 15 - this.ww * add + 15 })
  428. .call(() => {
  429. // console.warn("向上移出完成");
  430. info.move_tween = null;
  431. })
  432. .start();
  433. for (let i = 0; i < info.now_grids.length; i++) {
  434. let a = Math.floor(info.now_grids[i] / 10000);
  435. let b = info.now_grids[i] % 10000;
  436. a = a - add;
  437. info.now_grids[i] = a * 10000 + b;
  438. this.grid_map.set(info.now_grids[i], 1);
  439. }
  440. }
  441. }
  442. else if (info.dir == DirectionType.right) {
  443. i += 1;
  444. while (true) {
  445. add += 1;
  446. if (i + add >= this.vertical_count) {
  447. add = 100;
  448. break;
  449. }
  450. if (this.grid_map.get((i + add) * 10000 + k) == 1) {
  451. add -= 1;
  452. break;
  453. }
  454. }
  455. if (add != 0) {
  456. info.push_last();
  457. this.old_cat_pos.push(info);
  458. }
  459. if (add == 100) {
  460. //可以消失
  461. info.move_tween = cc.tween(info.cat)
  462. .to(0.1, { x: info.cat.x + 15 })
  463. .to(5, { x: info.cat.x + 2000 })
  464. .call(() => {
  465. // console.warn("向上移出完成");
  466. info.move_tween = null;
  467. info.lastPosition = null;
  468. })
  469. .start();
  470. this.removeCat(info);
  471. }
  472. else {
  473. //碰撞
  474. info.move_tween = cc.tween(info.cat)
  475. .to(0.1, { x: info.cat.x + 15 })
  476. .to(0.2 * add, { x: info.cat.x + 15 + this.ww * add })
  477. .delay(0.1)
  478. .to(0.1, { x: info.cat.x + 15 + this.ww * add - 15 })
  479. .call(() => {
  480. // console.warn("向下移出完成");
  481. info.move_tween = null;
  482. info.lastPosition = null;
  483. })
  484. .start();
  485. for (let i = 0; i < info.now_grids.length; i++) {
  486. let a = Math.floor(info.now_grids[i] / 10000);
  487. let b = info.now_grids[i] % 10000;
  488. a = a + add;
  489. info.now_grids[i] = a * 10000 + b;
  490. this.grid_map.set(info.now_grids[i], 1);
  491. }
  492. }
  493. }
  494. }
  495. public removeCat(info: CatInfo) {
  496. for (let i = 0; i < this.cat_info_list.length; i++) {
  497. if (this.cat_info_list[i] == info) {
  498. // console.warn("删除的羊---" + (<any>this.cat_info_list[i].cat)._id)
  499. this.cat_info_list.splice(i, 1);
  500. }
  501. }
  502. }
  503. //检测展示
  504. public checkShow() {
  505. let arr = [];
  506. let show_fw = this.node.convertToWorldSpaceAR(new cc.Vec2(0, 0));
  507. let up = show_fw.y - this.showNode.height / 2;
  508. let down = show_fw.y + this.showNode.height / 2;
  509. let left = show_fw.x - this.showNode.width / 2;
  510. let right = show_fw.x + this.showNode.width / 2;
  511. for (let i = 0; i < this.cat_info_list.length; i++) {
  512. let cat = this.cat_info_list[i];
  513. let curr_pos = cat.cat.parent.convertToWorldSpaceAR(cat.cat.position);
  514. if (curr_pos.x < left) {
  515. arr.push(cat);
  516. continue;
  517. }
  518. if (curr_pos.x > right) {
  519. arr.push(cat);
  520. continue;
  521. }
  522. if (curr_pos.y < up) {
  523. arr.push(cat);
  524. continue;
  525. }
  526. if (curr_pos.y > down) {
  527. arr.push(cat);
  528. continue;
  529. }
  530. }
  531. for (let i = 0; i < arr.length; i++) {
  532. for (let k = 0; k < this.cat_info_list.length; k++) {
  533. if (this.cat_info_list[k] == arr[i]) {
  534. this.cat_info_list[k].cat.parent = null;
  535. for (let a = 0; a < this.cat_info_list[k].now_grids.length; a++) {
  536. this.grid_map.set(this.cat_info_list[k].now_grids[a], 0);
  537. }
  538. // console.warn("不展示的羊---"+(<any>this.yangs[k].cat)._id)
  539. this.cat_info_list.splice(k, 1);
  540. break;
  541. }
  542. }
  543. }
  544. this.checkDirection();
  545. for (let i = 0; i < this.cat_info_list.length; i++) {
  546. let cat = this.cat_info_list[i];
  547. cat.cat.scale = 0;
  548. cat.cat.opacity = 255;
  549. cc.tween(cat.cat)
  550. .delay(GameUtil.randomRange(100, 1500) / 1000)
  551. .to(0.5, { scale: 1 })
  552. .start();
  553. }
  554. this.is_game_playing = true;
  555. this.stepCount = this.cat_info_list.length + 20
  556. this.updateStepStr()
  557. }
  558. //检测可以跑出去的羊
  559. public chatCatIsOut(): Array<CatInfo> {
  560. let arr: Array<CatInfo> = [];
  561. for (let a = 0; a < this.cat_info_list.length; a++) {
  562. let info = this.cat_info_list[a];
  563. let i = Math.floor(info.now_grids[0] / 10000);
  564. let k = info.now_grids[0] % 10000;
  565. if (this.grid_map.get(info.now_grids[0]) == 0) {
  566. continue;
  567. }
  568. let add = 0;
  569. if (info.dir == DirectionType.up) {
  570. k -= 1;
  571. while (true) {
  572. add += 1;
  573. if (k - add < 0) {
  574. add = 100;
  575. break;
  576. }
  577. if (this.grid_map.get(i * 10000 + (k - add)) == 1) {
  578. add -= 1;
  579. break;
  580. }
  581. }
  582. if (add == 100) {
  583. arr.push(info);
  584. }
  585. }
  586. else if (info.dir == DirectionType.down) {
  587. k += 1;
  588. while (true) {
  589. add += 1;
  590. if (k + add >= this.horizontal_count) {
  591. add = 100;
  592. break;
  593. }
  594. if (this.grid_map.get(i * 10000 + (k + add)) == 1) {
  595. add -= 1;
  596. break;
  597. }
  598. }
  599. if (add == 100) {
  600. arr.push(info);
  601. }
  602. }
  603. else if (info.dir == DirectionType.left) {
  604. i -= 1;
  605. while (true) {
  606. add += 1;
  607. if (i - add < 0) {
  608. add = 100;
  609. break;
  610. }
  611. if (this.grid_map.get((i - add) * 10000 + k) == 1) {
  612. add -= 1;
  613. break;
  614. }
  615. }
  616. if (add == 100) {
  617. arr.push(info);
  618. }
  619. }
  620. else if (info.dir == DirectionType.right) {
  621. i += 1;
  622. while (true) {
  623. add += 1;
  624. if (i + add >= this.vertical_count) {
  625. add = 100;
  626. break;
  627. }
  628. if (this.grid_map.get((i + add) * 10000 + k) == 1) {
  629. add -= 1;
  630. break;
  631. }
  632. }
  633. if (add == 100) {
  634. arr.push(info);
  635. }
  636. }
  637. }
  638. return arr;
  639. }
  640. public canAllCatOut() {
  641. let jilu: Array<number> = [];
  642. while (true) {
  643. let aa = this.chatCatIsOut();
  644. if (aa.length == 0) {
  645. // console.error("不可全部消除");
  646. break;
  647. }
  648. for (let i = 0; i < aa.length; i++) {
  649. for (let k = 0; k < aa[i].now_grids.length; k++) {
  650. jilu.push(aa[i].now_grids[k]);
  651. this.grid_map.set(aa[i].now_grids[k], 0);
  652. }
  653. }
  654. if (jilu.length == this.cat_info_list.length * 2) {
  655. // console.error("可以全部消除")
  656. for (let i = 0; i < jilu.length; i++) {
  657. this.grid_map.set(jilu[i], 1);
  658. }
  659. return true;
  660. }
  661. }
  662. return false;
  663. }
  664. //洗牌
  665. public onClickXipai() {
  666. Plat.shareAppMessage();
  667. this.scheduleOnce(() => {
  668. TipsUI.inst.showTips("已洗牌");
  669. if (true) {
  670. if (this.cat_info_list.length == 0) {
  671. return;
  672. }
  673. for (let i = 0; i < this.cat_info_list.length; i++) {
  674. if (Math.random() < 0.5) {
  675. this.resetDirection2(this.cat_info_list[i]);
  676. }
  677. }
  678. }
  679. }, 2)
  680. }
  681. //撤回
  682. public onClickRevoke() {
  683. this.is_game_playing = false
  684. Plat.showRewardVideo((success: boolean) => {
  685. this.is_game_playing = true;
  686. if (success) {
  687. if (this.old_cat_pos.length == 0) {
  688. TipsUI.inst.showTips("没有可撤回的操作");
  689. return;
  690. }
  691. let info = this.old_cat_pos.pop();
  692. if (info.move_tween != null) {
  693. info.move_tween.stop();
  694. }
  695. for (let i = 0; i < info.now_grids.length; i++) {
  696. this.grid_map.set(info.now_grids[i], 0);
  697. }
  698. info.revoke();
  699. for (let i = 0; i < info.now_grids.length; i++) {
  700. this.grid_map.set(info.now_grids[i], 1);
  701. }
  702. info.cat = this.getCatNode(info);
  703. if (this.cat_info_list.indexOf(info) == -1) {
  704. this.cat_info_list.push(info);
  705. }
  706. }
  707. })
  708. }
  709. //高亮
  710. public onClickLightCat() {
  711. this.is_game_playing = false;
  712. Plat.showRewardVideo((success: boolean) => {
  713. this.is_game_playing = true;
  714. if (success) {
  715. let arr = this.chatCatIsOut();
  716. if (arr.length == 0) {
  717. TipsUI.inst.showTips("没有可消除的动物");
  718. return;
  719. }
  720. this.maskNode.active = true;
  721. this.maskNode.zIndex = 99;
  722. for (let i = 0; i < arr.length; i++) {
  723. arr[i].cat.zIndex = 100;
  724. }
  725. }
  726. })
  727. }
  728. public fz_flag = false;
  729. public onClickResetDirection() {
  730. this.is_game_playing = false
  731. Plat.showRewardVideo((success: boolean) => {
  732. this.is_game_playing = true
  733. if (success) {
  734. if (this.cat_info_list.length == 0) {
  735. return;
  736. }
  737. this.buttonsNode.active = false;
  738. this.fz_flag = true;
  739. this.selectNode.active = true;
  740. for (let i = 0; i < this.cat_info_list.length; i++) {
  741. this.cat_info_list[i].cat.zIndex = 100;
  742. }
  743. }
  744. })
  745. }
  746. public onClickAddTime() {
  747. this.time += 60;
  748. this.updateTimeLabel();
  749. this.is_game_playing = true
  750. }
  751. public resetDirection2(info: CatInfo) {
  752. if (this.cat_info_list.length == 0) {
  753. return;
  754. }
  755. this.buttonsNode.active = true;
  756. this.fz_flag = false;
  757. this.selectNode.active = false;
  758. this.resetDirection3(info);
  759. }
  760. public resetDirection3(info: CatInfo) {
  761. info.dir = (info.dir - 1 + 2) % 4 + 1;
  762. info.now_grids.reverse();
  763. info.cat = this.getCatNode(info);
  764. }
  765. public time = 240;
  766. private onTimerUpdate() {
  767. if (this.is_game_playing == false) {
  768. return;
  769. }
  770. if (this.is_game_pause == true) {
  771. return;
  772. }
  773. this.time--;
  774. if (this.time < 0) {
  775. //游戏失败
  776. this.is_game_playing = false;
  777. setTimeout(() => {
  778. // FailUI.inst.showUI();
  779. this.onTimeOver()
  780. }, 1000);
  781. //弹窗
  782. return;
  783. }
  784. this.updateTimeLabel();
  785. }
  786. public updateTimeLabel() {
  787. let tt = Math.floor(this.time / 60);
  788. let ss = this.time % 60;
  789. this.timerLabel.string = "0" + tt + ":" + (ss < 10 ? "0" + ss : ss);
  790. }
  791. private checkDirection() {
  792. let map: Map<number, Array<CatInfo>> = new Map();
  793. let map2: Map<number, Array<CatInfo>> = new Map();
  794. for (let i = 0; i < this.cat_info_list.length; i++) {
  795. let info = this.cat_info_list[i];
  796. if (info.dir == DirectionType.left || info.dir == DirectionType.right) {
  797. let g = Math.floor(info.now_grids[0] % 10000);
  798. if (info.dir == DirectionType.right) {
  799. this.resetDirection3(info);
  800. }
  801. if (map.has(g) == false) {
  802. map.set(g, new Array());
  803. }
  804. map.get(g).push(info);
  805. }
  806. else {
  807. if (info.dir == DirectionType.down) {
  808. this.resetDirection3(info);
  809. }
  810. let g = Math.floor(info.now_grids[0] / 10000);
  811. if (map2.has(g) == false) {
  812. map2.set(g, new Array());
  813. }
  814. map2.get(g).push(info);
  815. }
  816. }
  817. map.forEach((val: Array<CatInfo>, key) => {
  818. val.sort((v1, v2) => {
  819. let a1 = Math.floor(v1.now_grids[0] / 10000);
  820. let a2 = Math.floor(v2.now_grids[0] / 10000);
  821. return a1 - a2;
  822. })
  823. let index = GameUtil.randomRange(0, val.length + 1);
  824. for (let i = index; i < val.length; i++) {
  825. this.resetDirection3(val[i]);
  826. }
  827. });
  828. map2.forEach((val: Array<CatInfo>, key) => {
  829. val.sort((v1, v2) => {
  830. let a1 = Math.floor(v1.now_grids[0] % 10000);
  831. let a2 = Math.floor(v2.now_grids[0] % 10000);
  832. return a1 - a2;
  833. })
  834. let index = GameUtil.randomRange(0, val.length + 1);
  835. for (let i = index; i < val.length; i++) {
  836. this.resetDirection3(val[i]);
  837. }
  838. });
  839. }
  840. public update() {
  841. if (this.is_game_playing == true) {
  842. if (this.cat_info_list.length == 0) {
  843. this.is_game_playing = false;
  844. this.onGameOver();
  845. }
  846. }
  847. }
  848. private onGameOver() {
  849. LocalData.lv++;
  850. setTimeout(() => {
  851. AudioMgr.playSound(AudioPath.LEVELUP);
  852. WinUI.inst.showUI();
  853. }, 1000);
  854. }
  855. public onClickPause() {
  856. this.is_game_pause = true;
  857. AudioMgr.playSound(AudioPath.CLICK);
  858. PauseUI.inst.showUI();
  859. }
  860. }