1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- const { ccclass, property } = cc._decorator;
- import DirectionType from '../datas/DirectionType';
- import CatInfo from '../datas/CatInfo';
- import PrefabUtil from '../utils/manager/PrefabUtil';
- import GameUtil from '../utils/util/GameUtil';
- import UIbase from '../utils/UIbase';
- import TipsUI from './TipsUI';
- import LocalData from '../manager/LocalData';
- import WinUI from './WinUI';
- import FailUI from './FailUI';
- import PauseUI from './PauseUI';
- import StepOverUI from './StepOverUI';
- import TimeOverUI from './TimeOverUI';
- import Plat from '../utils/Palt';
- import AudioPath from '../datas/AudioPath';
- import AudioMgr from '../manager/AudioMgr';
- @ccclass
- export default class GameUI extends UIbase {
- private static _inst: GameUI;
- public static get inst() {
- if (this._inst == null) {
- let v = cc.instantiate(PrefabUtil.get("GameUI"));
- this._inst = v.getComponent(GameUI);
- }
- return this._inst;
- }
- @property(cc.Node)
- catContainer: cc.Node = null;
- @property(cc.Node)
- showNode: cc.Node = null;
- @property(cc.Node)
- maskNode: cc.Node = null; //遮罩
- @property(cc.Node)
- buttonsNode: cc.Node = null; //按钮组
- @property(cc.Node)
- selectNode: cc.Node = null; //选择视图
- @property(cc.Label)
- timerLabel: cc.Label = null; //计时器
- @property(cc.Label)
- curLevel: cc.Label = null; //关卡
- @property(cc.Label)
- stepLabel: cc.Label = null;//步数计数器
- // public grids:Map<number,GridInfo>=new Map();
- public grid_num_list: Array<number> = []
- public grid_map: Map<number, number> = new Map();
- public cat_info_list: Array<CatInfo> = [];
- public horizontal_count = 20;
- public vertical_count = 20;
- public ww = 50;
- public old_cat_pos: Array<CatInfo> = [];
- public is_game_playing = false;
- public is_game_pause = false;
- private stepCount = 0;
- start() {
- setInterval(() => {
- this.onTimerUpdate();
- }, 1000);
- }
- public resetGameData() {
- this.grid_num_list = [];
- this.grid_map = new Map();
- this.cat_info_list = [];
- this.catContainer.removeAllChildren();
- this.maskNode.parent = this.catContainer;
- this.selectNode.active = false
- }
- private updateStepStr() {
- this.stepLabel.string = this.stepCount + ""
- }
- public addStepCount() {
- this.stepCount += 60
- this.updateStepStr()
- this.is_game_playing = true;
- }
- private deleteStepCount() {
- this.stepCount--
- this.updateStepStr()
- if (this.stepCount <= 0) {
- this.onStepOver()
- }
- }
- public onTimeOver() {
- this.is_game_playing = false;
- // FailUI.inst.showUI();
- TimeOverUI.inst.showUI()
- }
- public onStepOver() {
- this.is_game_playing = false;
- StepOverUI.inst.showUI()
- }
- public onStartGame() {
- this.is_game_pause = false;
- this.resetGameData();
- this.showUI();
- this.randomCatInfos();
- this.updateUI();
- this.scheduleOnce(() => {
- this.checkShow();
- }, 0.1)
- this.curLevel.string = "第 " + LocalData.lv + " 关";
- this.updateTimeLabel();
- }
- private setLvData() {
- let lv = LocalData.lv;
- if (lv == 1) {
- this.vertical_count = 6;
- this.horizontal_count = 6;
- this.catContainer.scale = 1.5;
- this.time = 60;
- }
- else if (lv == 2) {
- this.vertical_count = 25;
- this.horizontal_count = 25;
- this.catContainer.scale = 1;
- this.time = 240;
- }
- else {
- this.vertical_count = 25;
- this.horizontal_count = 25;
- this.time = 240;
- this.catContainer.scale = 0.8;
- }
- }
- private dirs: Array<number> = [1, 2, 3, 4];
- public randomCatInfos() {
- this.setLvData();
- this.cat_info_list = [];
- for (let i = 0; i < this.vertical_count; i++) {
- for (let k = 0; k < this.horizontal_count; k++) {
- this.grid_num_list.push(i * 10000 + k);
- this.grid_map.set(i * 10000 + k, 0);
- }
- }
- GameUtil.randomRangeSort(this.grid_num_list);
- let index = 0;
- while (true) {
- if (this.grid_num_list.length == 0) {
- break;
- }
- GameUtil.randomRangeSort(this.dirs);
- let flag = false;
- if (GameUtil.randomRange(0, 100) < 50) {
- for (let i = 0; i < 4; i++) {
- let id = this.grid_num_list[0];
- let id2 = this.getNextGrid(id, this.dirs[i]);
- if (id2 == -1) {
- continue;
- }
- if (this.grid_map.get(id2) == 1) {
- continue;
- }
- let info = new CatInfo();
- info.dir = this.dirs[i];
- info.now_grids.push(id);
- info.now_grids.push(id2);
- this.grid_map.set(id, 1);
- this.grid_map.set(id2, 1);
- GameUtil.deleteValue(this.grid_num_list, id);
- GameUtil.deleteValue(this.grid_num_list, id2);
- this.cat_info_list.push(info);
- flag = true;
- break;
- }
- }
- if (flag == false) {
- // console.warn("咋回事呀");
- this.grid_num_list.shift();
- }
- index++;
- if (index > 10000) {
- break;
- }
- }
- if (LocalData.lv == 1 && this.canAllCatOut() == false) {
- this.randomCatInfos();
- }
- if (this.cat_info_list.length < 4) {
- this.randomCatInfos();
- }
- }
- public getNextGrid(id: number, dir: number) {
- let i = Math.floor(id / 10000);
- let k = id % 10000;
- if (dir == DirectionType.up) {
- k--;
- }
- else if (dir == DirectionType.down) {
- k++;
- }
- else if (dir == DirectionType.left) {
- i--;
- }
- else if (dir == DirectionType.right) {
- i++;
- }
- let val = i * 10000 + k;
- if (this.grid_map.has(val) == true) {
- return val;
- }
- else {
- return -1;
- }
- }
- public updateUI() {
- for (let i = 0; i < this.cat_info_list.length; i++) {
- let cat = this.getCatNode(this.cat_info_list[i]);
- cat.parent = this.catContainer;
- cat.on(cc.Node.EventType.TOUCH_END, this.onClickCatNode, this)
- this.cat_info_list[i].cat = cat;
- cat.opacity = 0;
- }
- }
- public getCatNode(info: CatInfo): cc.Node {
- let node: cc.Node;
- if (info.cat == null) {
- node = cc.instantiate(PrefabUtil.get("CatItem"));
- // console.warn("创建的羊---"+(<any>node)._id)
- }
- else {
- node = info.cat;
- }
- let x = Math.floor(info.now_grids[0] / 10000);
- let y = info.now_grids[0] % 10000;
- x = (x * this.ww - this.ww / 2 * this.vertical_count) + this.ww / 2;
- y = (this.ww / 2 * this.horizontal_count - y * this.ww) - this.ww / 2;
- if (info.dir == DirectionType.up) {
- y += this.ww / 2;
- node.angle = 0
- }
- else if (info.dir == DirectionType.down) {
- y -= this.ww / 2;
- node.angle = 180
- }
- else if (info.dir == DirectionType.left) {
- x -= this.ww / 2;
- node.angle = 90
- }
- else if (info.dir == DirectionType.right) {
- x += this.ww / 2;
- node.angle = 270
- }
- node.x = x;
- node.y = y;
- return node;
- }
- public onClickCatNode(event: cc.Event.EventTouch) {
- AudioMgr.playSound(AudioPath.SHEEP);
- if (this.is_game_playing == false) {
- return;
- }
- this.maskNode.active = false;
- for (let i = 0; i < this.cat_info_list.length; i++) {
- this.cat_info_list[i].cat.zIndex = 0;
- }
- // console.warn("被点击的羊---"+(<any>event.target)._id)
- for (let i = 0; i < this.cat_info_list.length; i++) {
- // console.warn("被查询的羊---"+(<any>this.yangs[i].cat)._id)
- if (this.cat_info_list[i].cat == event.target) {
- // console.warn(this.yangs[i].grids[0]+"的羊被点击了");
- if (this.fz_flag == true) {
- this.resetDirection2(this.cat_info_list[i]);
- }
- else {
- this.onMoveCat(this.cat_info_list[i]);
- }
- return;
- }
- }
- }
- //检测出来移动位置
- public onMoveCat(info: CatInfo) {
- for (let i = 0; i < info.now_grids.length; i++) {
- this.grid_map.set(info.now_grids[i], 0);
- }
- let i = Math.floor(info.now_grids[0] / 10000);
- let k = info.now_grids[0] % 10000;
- if (info.move_tween && info.lastPosition) {
- info.move_tween.stop()
- info.cat.x = info.lastPosition.x;
- info.cat.y = info.lastPosition.y;
- }
- info.lastPosition = new cc.Vec2(info.cat.x, info.cat.y)
- const startX = info.cat.x;
- const startY = info.cat.y;
- let add = 0;
- this.deleteStepCount()
- if (info.dir == DirectionType.up) {
- k -= 1;
- while (true) {
- add += 1;
- if (k - add < 0) {
- add = 100;
- break;
- }
- if (this.grid_map.get(i * 10000 + (k - add)) == 1) {
- add -= 1;
- break;
- }
- }
- if (add != 0) {
- info.push_last();
- this.old_cat_pos.push(info);
- }
- if (add == 100) {
- //可以消失
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { y: info.cat.y + 15 })
- .to(5, { y: info.cat.y + 2000 })
- .call(() => {
- // console.warn("向上移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- this.removeCat(info);
- }
- else {
- //碰撞
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { y: info.cat.y + 15 })
- .to(0.2 * add, { y: info.cat.y + 15 + this.ww * add })
- .delay(0.1)
- .to(0.1, { y: info.cat.y + 15 + this.ww * add - 15 })
- .call(() => {
- // console.warn("向上移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- for (let i = 0; i < info.now_grids.length; i++) {
- let a = Math.floor(info.now_grids[i] / 10000);
- let b = info.now_grids[i] % 10000;
- b = b - add;
- info.now_grids[i] = a * 10000 + b;
- this.grid_map.set(info.now_grids[i], 1);
- }
- }
- }
- else if (info.dir == DirectionType.down) {
- k += 1;
- while (true) {
- add += 1;
- if (k + add >= this.horizontal_count) {
- add = 100;
- break;
- }
- if (this.grid_map.get(i * 10000 + (k + add)) == 1) {
- add -= 1;
- break;
- }
- }
- if (add != 0) {
- info.push_last();
- this.old_cat_pos.push(info);
- }
- if (add == 100) {
- //可以消失
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { y: info.cat.y - 15 })
- .to(5, { y: info.cat.y - 2000 })
- .call(() => {
- // console.warn("向上移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- this.removeCat(info);
- }
- else {
- //碰撞
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { y: info.cat.y - 15 })
- .to(0.2 * add, { y: info.cat.y - 15 - this.ww * add })
- .delay(0.1)
- .to(0.1, { y: info.cat.y - 15 - this.ww * add + 15 })
- .call(() => {
- // console.warn("向下移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- for (let i = 0; i < info.now_grids.length; i++) {
- let a = Math.floor(info.now_grids[i] / 10000);
- let b = info.now_grids[i] % 10000;
- b = b + add;
- info.now_grids[i] = a * 10000 + b;
- this.grid_map.set(info.now_grids[i], 1);
- }
- }
- }
- else if (info.dir == DirectionType.left) {
- i -= 1;
- while (true) {
- add += 1;
- if (i - add < 0) {
- add = 100;
- break;
- }
- if (this.grid_map.get((i - add) * 10000 + k) == 1) {
- add -= 1;
- break;
- }
- }
- if (add != 0) {
- info.push_last();
- this.old_cat_pos.push(info);
- }
- if (add == 100) {
- //可以消失
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { x: info.cat.x - 15 })
- .to(5, { x: info.cat.x - 2000 })
- .call(() => {
- // console.warn("向上移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- this.removeCat(info);
- }
- else {
- //碰撞
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { x: info.cat.x - 15 })
- .to(0.2 * add, { x: info.cat.x - 15 - this.ww * add })
- .delay(0.1)
- .to(0.1, { x: info.cat.x - 15 - this.ww * add + 15 })
- .call(() => {
- // console.warn("向上移出完成");
- info.move_tween = null;
- })
- .start();
- for (let i = 0; i < info.now_grids.length; i++) {
- let a = Math.floor(info.now_grids[i] / 10000);
- let b = info.now_grids[i] % 10000;
- a = a - add;
- info.now_grids[i] = a * 10000 + b;
- this.grid_map.set(info.now_grids[i], 1);
- }
- }
- }
- else if (info.dir == DirectionType.right) {
- i += 1;
- while (true) {
- add += 1;
- if (i + add >= this.vertical_count) {
- add = 100;
- break;
- }
- if (this.grid_map.get((i + add) * 10000 + k) == 1) {
- add -= 1;
- break;
- }
- }
- if (add != 0) {
- info.push_last();
- this.old_cat_pos.push(info);
- }
- if (add == 100) {
- //可以消失
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { x: info.cat.x + 15 })
- .to(5, { x: info.cat.x + 2000 })
- .call(() => {
- // console.warn("向上移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- this.removeCat(info);
- }
- else {
- //碰撞
- info.move_tween = cc.tween(info.cat)
- .to(0.1, { x: info.cat.x + 15 })
- .to(0.2 * add, { x: info.cat.x + 15 + this.ww * add })
- .delay(0.1)
- .to(0.1, { x: info.cat.x + 15 + this.ww * add - 15 })
- .call(() => {
- // console.warn("向下移出完成");
- info.move_tween = null;
- info.lastPosition = null;
- })
- .start();
- for (let i = 0; i < info.now_grids.length; i++) {
- let a = Math.floor(info.now_grids[i] / 10000);
- let b = info.now_grids[i] % 10000;
- a = a + add;
- info.now_grids[i] = a * 10000 + b;
- this.grid_map.set(info.now_grids[i], 1);
- }
- }
- }
- }
- public removeCat(info: CatInfo) {
- for (let i = 0; i < this.cat_info_list.length; i++) {
- if (this.cat_info_list[i] == info) {
- // console.warn("删除的羊---" + (<any>this.cat_info_list[i].cat)._id)
- this.cat_info_list.splice(i, 1);
- }
- }
- }
- //检测展示
- public checkShow() {
- let arr = [];
- let show_fw = this.node.convertToWorldSpaceAR(new cc.Vec2(0, 0));
- let up = show_fw.y - this.showNode.height / 2;
- let down = show_fw.y + this.showNode.height / 2;
- let left = show_fw.x - this.showNode.width / 2;
- let right = show_fw.x + this.showNode.width / 2;
- for (let i = 0; i < this.cat_info_list.length; i++) {
- let cat = this.cat_info_list[i];
- let curr_pos = cat.cat.parent.convertToWorldSpaceAR(cat.cat.position);
- if (curr_pos.x < left) {
- arr.push(cat);
- continue;
- }
- if (curr_pos.x > right) {
- arr.push(cat);
- continue;
- }
- if (curr_pos.y < up) {
- arr.push(cat);
- continue;
- }
- if (curr_pos.y > down) {
- arr.push(cat);
- continue;
- }
- }
- for (let i = 0; i < arr.length; i++) {
- for (let k = 0; k < this.cat_info_list.length; k++) {
- if (this.cat_info_list[k] == arr[i]) {
- this.cat_info_list[k].cat.parent = null;
- for (let a = 0; a < this.cat_info_list[k].now_grids.length; a++) {
- this.grid_map.set(this.cat_info_list[k].now_grids[a], 0);
- }
- // console.warn("不展示的羊---"+(<any>this.yangs[k].cat)._id)
- this.cat_info_list.splice(k, 1);
- break;
- }
- }
- }
- this.checkDirection();
- for (let i = 0; i < this.cat_info_list.length; i++) {
- let cat = this.cat_info_list[i];
- cat.cat.scale = 0;
- cat.cat.opacity = 255;
- cc.tween(cat.cat)
- .delay(GameUtil.randomRange(100, 1500) / 1000)
- .to(0.5, { scale: 1 })
- .start();
- }
- this.is_game_playing = true;
- this.stepCount = this.cat_info_list.length + 20
- this.updateStepStr()
- }
- //检测可以跑出去的羊
- public chatCatIsOut(): Array<CatInfo> {
- let arr: Array<CatInfo> = [];
- for (let a = 0; a < this.cat_info_list.length; a++) {
- let info = this.cat_info_list[a];
- let i = Math.floor(info.now_grids[0] / 10000);
- let k = info.now_grids[0] % 10000;
- if (this.grid_map.get(info.now_grids[0]) == 0) {
- continue;
- }
- let add = 0;
- if (info.dir == DirectionType.up) {
- k -= 1;
- while (true) {
- add += 1;
- if (k - add < 0) {
- add = 100;
- break;
- }
- if (this.grid_map.get(i * 10000 + (k - add)) == 1) {
- add -= 1;
- break;
- }
- }
- if (add == 100) {
- arr.push(info);
- }
- }
- else if (info.dir == DirectionType.down) {
- k += 1;
- while (true) {
- add += 1;
- if (k + add >= this.horizontal_count) {
- add = 100;
- break;
- }
- if (this.grid_map.get(i * 10000 + (k + add)) == 1) {
- add -= 1;
- break;
- }
- }
- if (add == 100) {
- arr.push(info);
- }
- }
- else if (info.dir == DirectionType.left) {
- i -= 1;
- while (true) {
- add += 1;
- if (i - add < 0) {
- add = 100;
- break;
- }
- if (this.grid_map.get((i - add) * 10000 + k) == 1) {
- add -= 1;
- break;
- }
- }
- if (add == 100) {
- arr.push(info);
- }
- }
- else if (info.dir == DirectionType.right) {
- i += 1;
- while (true) {
- add += 1;
- if (i + add >= this.vertical_count) {
- add = 100;
- break;
- }
- if (this.grid_map.get((i + add) * 10000 + k) == 1) {
- add -= 1;
- break;
- }
- }
- if (add == 100) {
- arr.push(info);
- }
- }
- }
- return arr;
- }
- public canAllCatOut() {
- let jilu: Array<number> = [];
- while (true) {
- let aa = this.chatCatIsOut();
- if (aa.length == 0) {
- // console.error("不可全部消除");
- break;
- }
- for (let i = 0; i < aa.length; i++) {
- for (let k = 0; k < aa[i].now_grids.length; k++) {
- jilu.push(aa[i].now_grids[k]);
- this.grid_map.set(aa[i].now_grids[k], 0);
- }
- }
- if (jilu.length == this.cat_info_list.length * 2) {
- // console.error("可以全部消除")
- for (let i = 0; i < jilu.length; i++) {
- this.grid_map.set(jilu[i], 1);
- }
- return true;
- }
- }
- return false;
- }
- //洗牌
- public onClickXipai() {
- Plat.shareAppMessage();
- this.scheduleOnce(() => {
- TipsUI.inst.showTips("已洗牌");
- if (true) {
- if (this.cat_info_list.length == 0) {
- return;
- }
- for (let i = 0; i < this.cat_info_list.length; i++) {
- if (Math.random() < 0.5) {
- this.resetDirection2(this.cat_info_list[i]);
- }
- }
- }
- }, 2)
- }
- //撤回
- public onClickRevoke() {
- this.is_game_playing = false
- Plat.showRewardVideo((success: boolean) => {
- this.is_game_playing = true;
- if (success) {
- if (this.old_cat_pos.length == 0) {
- TipsUI.inst.showTips("没有可撤回的操作");
- return;
- }
- let info = this.old_cat_pos.pop();
- if (info.move_tween != null) {
- info.move_tween.stop();
- }
- for (let i = 0; i < info.now_grids.length; i++) {
- this.grid_map.set(info.now_grids[i], 0);
- }
- info.revoke();
- for (let i = 0; i < info.now_grids.length; i++) {
- this.grid_map.set(info.now_grids[i], 1);
- }
- info.cat = this.getCatNode(info);
- if (this.cat_info_list.indexOf(info) == -1) {
- this.cat_info_list.push(info);
- }
- }
- })
- }
- //高亮
- public onClickLightCat() {
- this.is_game_playing = false;
- Plat.showRewardVideo((success: boolean) => {
- this.is_game_playing = true;
- if (success) {
- let arr = this.chatCatIsOut();
- if (arr.length == 0) {
- TipsUI.inst.showTips("没有可消除的动物");
- return;
- }
- this.maskNode.active = true;
- this.maskNode.zIndex = 99;
- for (let i = 0; i < arr.length; i++) {
- arr[i].cat.zIndex = 100;
- }
- }
- })
- }
- public fz_flag = false;
- public onClickResetDirection() {
- this.is_game_playing = false
- Plat.showRewardVideo((success: boolean) => {
- this.is_game_playing = true
- if (success) {
- if (this.cat_info_list.length == 0) {
- return;
- }
- this.buttonsNode.active = false;
- this.fz_flag = true;
- this.selectNode.active = true;
- for (let i = 0; i < this.cat_info_list.length; i++) {
- this.cat_info_list[i].cat.zIndex = 100;
- }
- }
- })
- }
- public onClickAddTime() {
- this.time += 60;
- this.updateTimeLabel();
- this.is_game_playing = true
- }
- public resetDirection2(info: CatInfo) {
- if (this.cat_info_list.length == 0) {
- return;
- }
- this.buttonsNode.active = true;
- this.fz_flag = false;
- this.selectNode.active = false;
- this.resetDirection3(info);
- }
- public resetDirection3(info: CatInfo) {
- info.dir = (info.dir - 1 + 2) % 4 + 1;
- info.now_grids.reverse();
- info.cat = this.getCatNode(info);
- }
- public time = 240;
- private onTimerUpdate() {
- if (this.is_game_playing == false) {
- return;
- }
- if (this.is_game_pause == true) {
- return;
- }
- this.time--;
- if (this.time < 0) {
- //游戏失败
- this.is_game_playing = false;
- setTimeout(() => {
- // FailUI.inst.showUI();
- this.onTimeOver()
- }, 1000);
- //弹窗
- return;
- }
- this.updateTimeLabel();
- }
- public updateTimeLabel() {
- let tt = Math.floor(this.time / 60);
- let ss = this.time % 60;
- this.timerLabel.string = "0" + tt + ":" + (ss < 10 ? "0" + ss : ss);
- }
- private checkDirection() {
- let map: Map<number, Array<CatInfo>> = new Map();
- let map2: Map<number, Array<CatInfo>> = new Map();
- for (let i = 0; i < this.cat_info_list.length; i++) {
- let info = this.cat_info_list[i];
- if (info.dir == DirectionType.left || info.dir == DirectionType.right) {
- let g = Math.floor(info.now_grids[0] % 10000);
- if (info.dir == DirectionType.right) {
- this.resetDirection3(info);
- }
- if (map.has(g) == false) {
- map.set(g, new Array());
- }
- map.get(g).push(info);
- }
- else {
- if (info.dir == DirectionType.down) {
- this.resetDirection3(info);
- }
- let g = Math.floor(info.now_grids[0] / 10000);
- if (map2.has(g) == false) {
- map2.set(g, new Array());
- }
- map2.get(g).push(info);
- }
- }
- map.forEach((val: Array<CatInfo>, key) => {
- val.sort((v1, v2) => {
- let a1 = Math.floor(v1.now_grids[0] / 10000);
- let a2 = Math.floor(v2.now_grids[0] / 10000);
- return a1 - a2;
- })
- let index = GameUtil.randomRange(0, val.length + 1);
- for (let i = index; i < val.length; i++) {
- this.resetDirection3(val[i]);
- }
- });
- map2.forEach((val: Array<CatInfo>, key) => {
- val.sort((v1, v2) => {
- let a1 = Math.floor(v1.now_grids[0] % 10000);
- let a2 = Math.floor(v2.now_grids[0] % 10000);
- return a1 - a2;
- })
- let index = GameUtil.randomRange(0, val.length + 1);
- for (let i = index; i < val.length; i++) {
- this.resetDirection3(val[i]);
- }
- });
- }
- public update() {
- if (this.is_game_playing == true) {
- if (this.cat_info_list.length == 0) {
- this.is_game_playing = false;
- this.onGameOver();
- }
- }
- }
- private onGameOver() {
- LocalData.lv++;
- setTimeout(() => {
- AudioMgr.playSound(AudioPath.LEVELUP);
- WinUI.inst.showUI();
- }, 1000);
- }
- public onClickPause() {
- this.is_game_pause = true;
- AudioMgr.playSound(AudioPath.CLICK);
- PauseUI.inst.showUI();
- }
- }
|