GameUI.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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 onClickRevoke() {
  666. this.is_game_playing = false
  667. Plat.showRewardVideo((success: boolean) => {
  668. this.is_game_playing = true;
  669. if (success) {
  670. if (this.old_cat_pos.length == 0) {
  671. TipsUI.inst.showTips("没有可撤回的操作");
  672. return;
  673. }
  674. let info = this.old_cat_pos.pop();
  675. if (info.move_tween != null) {
  676. info.move_tween.stop();
  677. }
  678. for (let i = 0; i < info.now_grids.length; i++) {
  679. this.grid_map.set(info.now_grids[i], 0);
  680. }
  681. info.revoke();
  682. for (let i = 0; i < info.now_grids.length; i++) {
  683. this.grid_map.set(info.now_grids[i], 1);
  684. }
  685. info.cat = this.getCatNode(info);
  686. if (this.cat_info_list.indexOf(info) == -1) {
  687. this.cat_info_list.push(info);
  688. }
  689. }
  690. })
  691. }
  692. //高亮
  693. public onClickLightCat() {
  694. this.is_game_playing = false;
  695. Plat.showRewardVideo((success: boolean) => {
  696. this.is_game_playing = true;
  697. if (success) {
  698. let arr = this.chatCatIsOut();
  699. if (arr.length == 0) {
  700. TipsUI.inst.showTips("没有可消除的动物");
  701. return;
  702. }
  703. this.maskNode.active = true;
  704. this.maskNode.zIndex = 99;
  705. for (let i = 0; i < arr.length; i++) {
  706. arr[i].cat.zIndex = 100;
  707. }
  708. }
  709. })
  710. }
  711. public fz_flag = false;
  712. public onClickResetDirection() {
  713. this.is_game_playing = false
  714. Plat.showRewardVideo((success: boolean) => {
  715. this.is_game_playing = true
  716. if (success) {
  717. if (this.cat_info_list.length == 0) {
  718. return;
  719. }
  720. this.buttonsNode.active = false;
  721. this.fz_flag = true;
  722. this.selectNode.active = true;
  723. for (let i = 0; i < this.cat_info_list.length; i++) {
  724. this.cat_info_list[i].cat.zIndex = 100;
  725. }
  726. }
  727. })
  728. }
  729. public onClickAddTime() {
  730. this.time += 60;
  731. this.updateTimeLabel();
  732. this.is_game_playing = true
  733. }
  734. public resetDirection2(info: CatInfo) {
  735. if (this.cat_info_list.length == 0) {
  736. return;
  737. }
  738. this.buttonsNode.active = true;
  739. this.fz_flag = false;
  740. this.selectNode.active = false;
  741. this.resetDirection3(info);
  742. }
  743. public resetDirection3(info: CatInfo) {
  744. info.dir = (info.dir - 1 + 2) % 4 + 1;
  745. info.now_grids.reverse();
  746. info.cat = this.getCatNode(info);
  747. }
  748. public time = 240;
  749. private onTimerUpdate() {
  750. if (this.is_game_playing == false) {
  751. return;
  752. }
  753. if (this.is_game_pause == true) {
  754. return;
  755. }
  756. this.time--;
  757. if (this.time < 0) {
  758. //游戏失败
  759. this.is_game_playing = false;
  760. setTimeout(() => {
  761. // FailUI.inst.showUI();
  762. this.onTimeOver()
  763. }, 1000);
  764. //弹窗
  765. return;
  766. }
  767. this.updateTimeLabel();
  768. }
  769. public updateTimeLabel() {
  770. let tt = Math.floor(this.time / 60);
  771. let ss = this.time % 60;
  772. this.timerLabel.string = "0" + tt + ":" + (ss < 10 ? "0" + ss : ss);
  773. }
  774. private checkDirection() {
  775. let map: Map<number, Array<CatInfo>> = new Map();
  776. let map2: Map<number, Array<CatInfo>> = new Map();
  777. for (let i = 0; i < this.cat_info_list.length; i++) {
  778. let info = this.cat_info_list[i];
  779. if (info.dir == DirectionType.left || info.dir == DirectionType.right) {
  780. let g = Math.floor(info.now_grids[0] % 10000);
  781. if (info.dir == DirectionType.right) {
  782. this.resetDirection3(info);
  783. }
  784. if (map.has(g) == false) {
  785. map.set(g, new Array());
  786. }
  787. map.get(g).push(info);
  788. }
  789. else {
  790. if (info.dir == DirectionType.down) {
  791. this.resetDirection3(info);
  792. }
  793. let g = Math.floor(info.now_grids[0] / 10000);
  794. if (map2.has(g) == false) {
  795. map2.set(g, new Array());
  796. }
  797. map2.get(g).push(info);
  798. }
  799. }
  800. map.forEach((val: Array<CatInfo>, key) => {
  801. val.sort((v1, v2) => {
  802. let a1 = Math.floor(v1.now_grids[0] / 10000);
  803. let a2 = Math.floor(v2.now_grids[0] / 10000);
  804. return a1 - a2;
  805. })
  806. let index = GameUtil.randomRange(0, val.length + 1);
  807. for (let i = index; i < val.length; i++) {
  808. this.resetDirection3(val[i]);
  809. }
  810. });
  811. map2.forEach((val: Array<CatInfo>, key) => {
  812. val.sort((v1, v2) => {
  813. let a1 = Math.floor(v1.now_grids[0] % 10000);
  814. let a2 = Math.floor(v2.now_grids[0] % 10000);
  815. return a1 - a2;
  816. })
  817. let index = GameUtil.randomRange(0, val.length + 1);
  818. for (let i = index; i < val.length; i++) {
  819. this.resetDirection3(val[i]);
  820. }
  821. });
  822. }
  823. public update() {
  824. if (this.is_game_playing == true) {
  825. if (this.cat_info_list.length == 0) {
  826. this.is_game_playing = false;
  827. this.onGameOver();
  828. }
  829. }
  830. }
  831. private onGameOver() {
  832. LocalData.lv++;
  833. setTimeout(() => {
  834. AudioMgr.playSound(AudioPath.LEVELUP);
  835. WinUI.inst.showUI();
  836. }, 1000);
  837. }
  838. public onClickPause() {
  839. this.is_game_pause = true;
  840. AudioMgr.playSound(AudioPath.CLICK);
  841. PauseUI.inst.showUI();
  842. }
  843. }