// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html import GameManager from "./GameManager"; import { LQCollide } from "./lq_collide_system/lq_collide"; import NumberToImage from "./NumberToImage"; import { MiniGameSdk } from "./Sdk/MiniGameSdk"; const {ccclass, property} = cc._decorator; @ccclass export default class MapConroler extends cc.Component { static _instance: any; @property({type: [cc.Prefab], tooltip:"方块数组"}) Block_Array : Array = []; @property({type: [cc.Prefab], tooltip:"方块道具数组"}) Block_Prop : Array = []; @property(cc.Prefab) MapBlockPrefab: cc.Prefab = null; @property({type: [cc.SpriteAtlas], tooltip:"方块颜色"}) Block_Color : Array = []; @property({type: [cc.Prefab], tooltip:"墙壁数组"}) Wall_Prefab : Array = []; @property(cc.Prefab) wallTurnPrefab: cc.Prefab = null; @property(cc.Button) timeBtn: cc.Button = null; @property(cc.Button) destroyBtn: cc.Button = null; @property(cc.Button) magicBtn: cc.Button = null; @property(cc.Node) mask: cc.Node = null; @property(cc.Label) iceLabel: cc.Label = null; @property(cc.Label) hammerLabel: cc.Label = null; @property(cc.Label) magicLabel: cc.Label = null; @property(cc.Node) magicMask: cc.Node = null; @property(cc.Node) hammerMask: cc.Node = null; @property(cc.Node) freezeMask: cc.Node = null; @property(cc.Node) coinPop: cc.Node = null; timeLabel: cc.Node = null; levelLabel: cc.Node = null; new_mode: number = 0; mapWidth: number = 0; mapHeight: number = 0; wallNum: number = 0; blockNum: number = 0; mapBlocksWall: any; //地图的所有底块 mapInfo: any; //地图除了底块的其他信息 blocks: any; //方块数组 wallInfo: any; //墙壁数组 wallArray:any; // openWall: any; //开关墙数组 freezeWall: any; //冻结墙数组 riseFallBlcok:any; //升降地块数组 scheduleCallback: any;//倒计时用 homeCanTouch:boolean = true;//按钮可用状态 againCanTouch:boolean = true;//重玩按钮可以用状态 gameOver: boolean = false;//游戏结束状态 gameWin: boolean = false;//游戏胜利状态 gameStart: boolean = false;//游戏开始状态 timeNumber: number = 0; //游戏时间 用于倒计时 count_Time: number = 0; //用于统计总游戏时长 add_Time: number = 0; //复活时间 mapBlockArray: any; //地图的所有格子状态存储 pause:boolean = false;//暂停状态 hammer:boolean = false;//锤子状态 freezeArray: any; //冻结 loackArray: any; //上锁 particleEffects: cc.ParticleAsset[]; // leftDoors: []; //左门 rightDoors: any; //右门 topDoors: any; //上门 bottomDoors: any; //下门 leftDoors: any; // mapInfo: number[][] = []; onLoad () { cc.fx.GameConfig.GM_INFO.review = 0; cc.fx.GameConfig.GM_INFO.gameState = true; this.mask.opacity = 0; this.node.getChildByName("Adhesive").zIndex = 500; cc.game.setFrameRate(63); cc.fx.AudioManager._instance.playEffect("zhuan2",null); this.node.parent.parent.parent.getChildByName("zhuanchang").active = true; this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1,"down",false); this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setCompleteListener((entry) => { if (entry.animation.name === "down") { // 动画播放结束后执行的逻辑 this.node.parent.parent.parent.getChildByName("zhuanchang").active = false; } }); this.blocks = []; this.leftDoors = []; //左门 this.rightDoors = []; //右门 this.topDoors = []; //上门 this.bottomDoors = []; //下门 this.gameWin = false; this.gameOver = false; this.gameStart = false; this.homeCanTouch = true; this.againCanTouch = true; this.new_mode = 0; this.iceLabel.string = cc.fx.GameConfig.GM_INFO.freezeAmount.toString(); this.hammerLabel.string = cc.fx.GameConfig.GM_INFO.hammerAmount.toString(); this.magicLabel.string = cc.fx.GameConfig.GM_INFO.magicAmount.toString(); // cc.game.addPersistRootNode(this.node); MapConroler._instance = this; } start () { setTimeout(() => { cc.director.preloadScene("HomeScene", (err, asset) => { if (err) { console.error('动态加载 HomeScene 失败:', err); return; } }); }, 1000); // console.log("进入GameScene"); this.Block_Array = GameManager._instance.Block_Array; this.Wall_Prefab = GameManager._instance.Wall_Prefab; this.Block_Color = GameManager._instance.Block_Color; // this.particleEffects = GameManager._instance.particleEffects; this.initMap(); } startUpdate(){ if(this.gameStart == false){ this.gameStart = true; //发送数数事件——进入关卡 console.log("准备进入下一关,发送下一关进入"); cc.fx.GameTool.shushu_Track("enter_stage"); this.startTimeCutDown(); for(let i =0; i { const startTime = performance.now(); for (let i = 0; i < BLOCKS_PER_FRAME && index < blockArray.length; i++) { let blockInfo = blockArray[index]; // 缓存 Block_Array 访问 const blockPrefab = this.Block_Array[blockInfo.block]; let block = cc.instantiate(blockPrefab); block.parent = this.node; block.setPosition(cc.v2(blockInfo.position.x, blockInfo.position.y)); if (blockInfo.block != 23) { this.blockNum += 1; this.blocks.push(block); if (blockInfo.type == 1) { let info = { id: blockInfo.id + 1, block: blockInfo.block, color: blockInfo.stacking, type: 10, position: blockInfo.position, stacking: blockInfo.color }; // 缓存 Block_Array 访问 const blockUpPrefab = this.Block_Array[info.block]; let blockUp = cc.instantiate(blockUpPrefab); blockUp.parent = this.node; blockUp.setPosition(cc.v2(info.position.x, info.position.y)); this.blocks.push(blockUp); block.getComponent("Block").init(blockInfo, null, null, blockUp); blockUp.getComponent("Block").init(info, null, null, block); this.blockNum += 1; } else if (blockInfo.type == 9) { if(blockInfo.adhesiveTime < 2){ index++; continue; } index++; let info = blockArray[index]; // 缓存 Block_Array 访问 const blockUpPrefab = this.Block_Array[info.block]; let blockUp = cc.instantiate(blockUpPrefab); blockUp.parent = this.node; blockUp.setPosition(cc.v2(info.position.x, info.position.y)); this.blocks.push(blockUp); block.getComponent("Block").init(blockInfo, null, null, blockUp,false); setTimeout(() => { blockUp.getComponent("Block").init(info, null, null, block,true); }, 100); this.blockNum += 1; } else { block.getComponent("Block").init(blockInfo); } } else{ block.getComponent("Barrier").init(blockInfo); } index++; } const endTime = performance.now(); const frameTime = endTime - startTime; // 如果当前帧执行时间过长,减少每帧创建数量 if (frameTime > 16) { BLOCKS_PER_FRAME = Math.max(BLOCKS_PER_FRAME - 1, MIN_PER_FRAME); } else { BLOCKS_PER_FRAME = Math.min(BLOCKS_PER_FRAME + 1, MAX_PER_FRAME); } // 如果还有方块未创建,下一帧继续创建 if (index < blockArray.length) { // 使用 scheduleOnce 在下一帧执行 createBlocks this.scheduleOnce(() => { createBlocks(); }, 0); } else { } }; // 开始分帧创建方块 createBlocks(); } //给创建方块排序,用来降低drawcall 合批 sortBlock(allBlocks: { color: number; block: number; type: number }[]) { return allBlocks.sort((a, b) => { // 先处理 type 为 1 的情况,将其放到最后 if (a.type === 1 && b.type !== 1) { return 1; } if (a.type !== 1 && b.type === 1) { return -1; } // 对 type 不为 0 的元素按 type 排序 if (a.type !== 0 && b.type !== 0) { return a.type - b.type; } if (a.type !== 0 && b.type === 0) { return 1; } if (a.type === 0 && b.type !== 0) { return -1; } // 再处理 block 为 23 的情况,将其放到最前面 if (a.block === 23 && b.block !== 23) { return -1; } if (a.block !== 23 && b.block === 23) { return 1; } // 其余按 color 升序排列 return a.color - b.color; }); } //创建墙壁 wallInit() { let index = 0; // 当前要创建的墙壁索引 let WALLS_PER_FRAME = 1; // 初始每帧创建的墙壁数量 const MAX_PER_FRAME = 10; // 每帧最大创建数量 const MIN_PER_FRAME = 1; // 每帧最小创建数量 const createWalls = () => { const startTime = performance.now(); for (let i = 0; i < WALLS_PER_FRAME && index < this.wallInfo.length; i++) { let block = this.wallInfo[index]; let dir = this.getWllDiraction("wall", cc.v2(block.getComponent("MapBlock").posX, block.getComponent("MapBlock").posY)); if (dir != null) { this.createWall(dir, block); } index++; } const endTime = performance.now(); const frameTime = endTime - startTime; // 如果当前帧执行时间过长,减少每帧创建数量 if (frameTime > 16) { WALLS_PER_FRAME = Math.max(WALLS_PER_FRAME - 1, MIN_PER_FRAME); } else { WALLS_PER_FRAME = Math.min(WALLS_PER_FRAME + 1, MAX_PER_FRAME); } // 如果还有墙壁未创建,下一帧继续创建 if (index < this.wallInfo.length) { this.scheduleOnce(() => { createWalls(); }, 0); } else { // 所有墙壁创建完成后,创建拐角节点 this.createCornerNodes(); } }; // 开始分帧创建墙壁 createWalls(); } // ... 已有代码 ... getWllDiraction(type,pointA){ let dir = []; let pointB = cc.v2(pointA.x + 1,pointA.y); let pointC = cc.v2(pointA.x - 1,pointA.y); let pointD = cc.v2(pointA.x,pointA.y + 1); let pointE = cc.v2(pointA.x,pointA.y - 1); let opacity = 0; if(type == "wall") opacity = 255; if(type == "turn") opacity = 250; if(pointB.x < this.mapWidth){ if(this.mapBlocksWall[pointB.x][pointB.y].opacity == opacity ){ if(type == "turn" && this.mapBlocksWall[pointB.x][pointB.y].getComponent("MapBlock").direction == "right"){ } else dir.push("right"); } } if(pointC.x >= 0){ if(this.mapBlocksWall[pointC.x][pointC.y].opacity == opacity){ if(type == "turn" && this.mapBlocksWall[pointC.x][pointC.y].getComponent("MapBlock").direction == "left"){ } else dir.push("left"); } } if(pointD.y < this.mapHeight){ if(this.mapBlocksWall[pointD.x][pointD.y].opacity == opacity){ if(type == "turn" && this.mapBlocksWall[pointD.x][pointD.y].getComponent("MapBlock").direction == "up"){ } else dir.push("up"); } } if(pointE.y >= 0){ if(this.mapBlocksWall[pointE.x][pointE.y].opacity == opacity){ if(type == "turn" && this.mapBlocksWall[pointE.x][pointE.y].getComponent("MapBlock").direction == "down"){ } else dir.push("down"); } } if(dir.length > 2){ return null; } if(dir.length == 1){ if(type == "wall") return dir[0]; else return null; } else if(dir.length == 2){ return dir[0] + dir[1]; } } createCornerNodes() { // 定义拐角位置 for(let i=0; i 3) maxX = maxX-1; } else if(block.name == "block11"){ minX = width-2; if(this.mapWidth > 3) maxX = maxX-2; } else if(block.name == "block14"){ minX = width-1; maxX = maxX-1; } else if(block.name == "block16"){ minX = width-1; maxX = maxX-1; } else if(block.name == "block18"){ minX = width-1; maxX = maxX-1; } else if(block.name == "block21"){ minX = width-1; maxX = maxX-1; } return {minX:minX,maxX:maxX,minY:minY,maxY:maxY}; } //检测落点是否可以消除 checkPass(node,blocks) { let minAndMax = this.getMinAndMax(node); let minX = minAndMax.minX; let maxX = minAndMax.maxX; let minY = minAndMax.minY; let maxY = minAndMax.maxY; let nodePos = cc.v2(node.getComponent("Block").posX, node.getComponent("Block").posY); // 获取地图边界信息 let allBlocks = []; for(let i=0;i wallPos.x){ // console.log("id:",wall.getChildByName("num").getComponent(cc.Label).string); leftWalls.push(wallLeft); } } } // console.log("得到左边墙壁数组",date6); if(leftWalls.length != 0){ let result = this.detectingBlock("left",node.getComponent("Block").posX,node.getComponent("Block").posY,allBlocks); if(result == true) result = this.passWall(result,leftWalls,node); // console.log("碰到左边缘结果:",jg); if(result){ this.blockNum -= 1; this.special_Treatment(node); jg = 2; this.createParticle(node,jg); return jg; } } } if (nodePos.x >= maxX || direction[1] == true) { // const date3 = new Date().getTime(); // console.log("碰到右边缘",date3); pz += 1; let rightWalls = []; for(let i = 0; i < this.rightDoors.length; i++){ let wallRight = this.rightDoors[i].getChildByName("wall"); let wallPos = cc.v2(wallRight.getComponent("Wall").posX,wallRight.getComponent("Wall").posY); for(let i = 0; i < allBlocks.length; i++){ if(allBlocks[i].y == wallPos.y && allBlocks[i].x < wallPos.x){ // console.log("id:",wall.getChildByName("num").getComponent(cc.Label).string); rightWalls.push(wallRight); } } } if(rightWalls.length != 0){ let result = this.detectingBlock("right",node.getComponent("Block").posX,node.getComponent("Block").posY,allBlocks); if(result == true) result = this.passWall(result,rightWalls,node); // console.log("碰到右边缘结果:",jg); if(result){ this.blockNum -= 1; this.special_Treatment(node); jg = 3; this.createParticle(node,jg); return jg; } } } if (nodePos.y <= minY || direction[2] == true) { pz += 1; // const date4 = new Date().getTime(); // console.log("碰到下边缘",date4); let downWalls = []; for(let i = 0; i < this.topDoors.length; i++){ let wallBottom = this.topDoors[i].getChildByName("wall"); let wallPos = cc.v2(wallBottom.getComponent("Wall").posX,wallBottom.getComponent("Wall").posY); for(let i = 0; i < allBlocks.length; i++){ let luocha = Math.abs(allBlocks[i].y - wallPos.y) if(allBlocks[i].x == wallPos.x && allBlocks[i].y > wallPos.y && luocha <= node.getComponent("Block").shu){ // console.log("id:",wall.getChildByName("num").getComponent(cc.Label).string); downWalls.push(wallBottom); } } } if(downWalls.length != 0){ let result = this.detectingBlock("down",node.getComponent("Block").posX,node.getComponent("Block").posY,allBlocks); if(result == true) result = this.passWall(result,downWalls,node); // console.log("碰到下边缘结果:",jg); if(result){ this.blockNum -= 1; this.special_Treatment(node); jg = 1; this.createParticle(node,jg); return jg; } } } if (nodePos.y >= maxY || direction[3] == true) { pz += 1; // const date5 = new Date().getTime(); // console.log("碰到上边缘",date5); let upWalls = []; for(let i = 0; i < this.bottomDoors.length; i++){ let wallTop = this.bottomDoors[i].getChildByName("wall"); let wallPos = cc.v2(wallTop.getComponent("Wall").posX,wallTop.getComponent("Wall").posY); for(let i = 0; i < allBlocks.length; i++){ let luocha = Math.abs(allBlocks[i].y - wallPos.y) if(allBlocks[i].x == wallPos.x && allBlocks[i].y < wallPos.y && luocha <= node.getComponent("Block").shu){ upWalls.push(wallTop); } } } if(upWalls.length != 0){ let result = this.detectingBlock("up",node.getComponent("Block").posX,node.getComponent("Block").posY,allBlocks); if(result == true) result = this.passWall(result,upWalls,node); // console.log("碰到上边缘结果:",result); if(result){ this.blockNum -= 1; this.special_Treatment(node); jg = 0; this.createParticle(node,jg); return jg; } } } if(pz == 0 )jg = -1; // const date10 = new Date().getTime(); // console.log("所有检测结束耗时",date10,jg); return jg; } changeState(){ if(this.openWall.length != 0){ for(let i=0; i 0){ for(let i=0; i= 0){ if(this.mapBlocksWall[x-2][y].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x-2][y].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x-2][y].getComponent("MapBlock").block_Id != id ){ // console.log("左侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x-2][y].opacity == 249){ jg = false; return jg; } } } else if(direction == "right"){ if(this.mapBlocksWall[x+1][y].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x+1][y].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x+1][y].getComponent("MapBlock").block_Id != id ){ // console.log("右侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x+1][y].opacity == 249){ jg = false; return jg; } if((x + 2) <= this.mapBlocksWall.length-1){ if(this.mapBlocksWall[x+2][y].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x+2][y].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x+2][y].getComponent("MapBlock").block_Id != id ){ // console.log("右侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x+2][y].opacity == 249){ jg = false; return jg; } } } if(direction == "up"){ if(this.mapBlocksWall[x][y+1].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x][y+1].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x][y+1].getComponent("MapBlock").block_Id != id ){ // console.log("上侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x][y+1].opacity == 249){ jg = false; return jg; } if((y + 2) <= this.mapBlocksWall[x].length-1){ if(this.mapBlocksWall[x][y+2].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x][y+2].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x][y+2].getComponent("MapBlock").block_Id != id ){ // console.log("上侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x][y+2].opacity == 249){ jg = false; return jg; } } } if(direction == "down"){ if(this.mapBlocksWall[x][y-1].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x][y-1].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x][y-1].getComponent("MapBlock").block_Id != id ){ // console.log("下侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x][y-1].opacity == 249){ jg = false; return jg; } if((y - 2) >= 0){ if(this.mapBlocksWall[x][y-2].getComponent("MapBlock").block_Id != "" && this.mapBlocksWall[x][y-2].getComponent("MapBlock").block_Id!= "Wall" && this.mapBlocksWall[x][y-2].getComponent("MapBlock").block_Id != id ){ // console.log("下侧有物体") jg = false; return jg; } if(this.mapBlocksWall[x][y-2].opacity == 249){ jg = false; return jg; } } } // console.log("检测物体各方向是否有别的物块",jg); return jg; } checkDiraction(allBlocks,maxX,maxY){ let jg = [false,false,false,false]; for(let i=0; i 0 && jg[0] == false){ if(this.mapBlocksWall[allBlocks[i].x-1][allBlocks[i].y].opacity == 250){ jg[0] = true; } } if( jg[1] == false){ if(this.mapBlocksWall[allBlocks[i].x+1][allBlocks[i].y].opacity == 250){ jg[1] = true; } } if(allBlocks[i].y > 0 && jg[2] == false){ if(this.mapBlocksWall[allBlocks[i].x][allBlocks[i].y-1].opacity == 250){ jg[2] = true; } } if(jg[3] == false){ if(this.mapBlocksWall[allBlocks[i].x][allBlocks[i].y+1].opacity == 250){ jg[3] = true; } } } return jg; } checkLeft(allBlocks){ let jg = false; return jg; } checkRight(allBlocks,max){ let jg = false; for(let i=0; i { if(child.getComponent("Block")){ if(child.getComponent("Block").type == 4) return child } }); if(freezeBlock.length > 0){ for(let i=0; i { if(child.getComponent("Block")){ if(child.getComponent("Block").type == 3) return child } }); if(lockBlock.length > 0) for(let i=0; i { this.node.parent.parent.getChildByName("Win").active = true; }, 660); // console.log("游戏成功"); } else{ if(this.gameOver == true){ this.failLevel(null); } } } check_NewMode(){ for(let i=0; i 156){ cc.fx.GameConfig.GM_INFO.level = 156; MapConroler._instance = null; if(this.new_mode == 3){ this.node.parent.parent.getChildByName("Win").active = false; this.openNewMode(2); cc.fx.GameConfig.LEVEL_INFO_init(false,0); return; } // cc.fx.GameConfig.LEVEL_INFO_init(true); cc.fx.AudioManager._instance.playEffect("zhuan1",null); this.node.parent.parent.parent.getChildByName("zhuanchang").active = true; this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1,"up",false); // this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setCompleteListener((entry) => { // if (entry.animation.name === "up") { // // 动画播放结束后执行的逻辑 // this.node.parent.parent.parent.getChildByName("zhuanchang").active = false; // } // }); cc.director.preloadScene("HomeScene", (err, asset) => { if (err) { console.error('动态加载 Prefab 失败:', err); return; } }); setTimeout(() => { cc.director.loadScene("HomeScene"); }, 1200); } else{ // console.log("下一关"); MapConroler._instance = null; if(this.new_mode == 3){ this.node.parent.parent.getChildByName("Win").active = false; this.openNewMode(2); cc.fx.GameConfig.LEVEL_INFO_init(false,0); return; } cc.fx.AudioManager._instance.playEffect("zhuan1",null); this.node.parent.parent.parent.getChildByName("zhuanchang").active = true; this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1,"up",false); // this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setCompleteListener((entry) => { // if (entry.animation.name === "up") { // // 动画播放结束后执行的逻辑 // this.node.parent.parent.parent.getChildByName("zhuanchang").active = false; // } // }); setTimeout(() => { cc.fx.GameConfig.LEVEL_INFO_init(true,0); }, 1200); // this.node.parent.parent.parent.destroy(); } } againLevel(){ cc.fx.AudioManager._instance.playEffect("anniu_Big",null); if(cc.fx.GameConfig.GM_INFO.hp < 1){ MiniGameSdk.API.showToast("体力值不足"); return; } if(!this.againCanTouch) return; this.againCanTouch = false; MapConroler._instance = null; cc.fx.AudioManager._instance.playEffect("zhuan1",null); this.node.parent.parent.parent.getChildByName("zhuanchang").active = true; this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1,"up",false); // this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setCompleteListener((entry) => { // if (entry.animation.name === "up") { // // 动画播放结束后执行的逻辑 // this.node.parent.parent.parent.getChildByName("zhuanchang").active = false; // } // }); setTimeout(() => { cc.fx.GameConfig.LEVEL_INFO_init(true,0); }, 1200); // this.node.parent.parent.parent.destroy(); } reviewLevel(){ cc.fx.AudioManager._instance.playEffect("anniu_Big",null); let coin = 900; if(cc.fx.GameConfig.GM_INFO.review == 1) coin = 1900; else if(cc.fx.GameConfig.GM_INFO.review == 2) coin = 2500; // console.log("自身金币:",cc.fx.GameConfig.GM_INFO.coin,"消耗金币:",coin); if(cc.fx.GameConfig.GM_INFO.coin < Math.abs(coin)){ MiniGameSdk.API.showToast("金币不足,无法加时间"); return; } else{ this.runRewive(-coin); } } runRewive(data){ console.log("复活回调函数内",data); cc.fx.GameTool.changeCoin(data); MiniGameSdk.API.showToast("继续游戏"); if(cc.fx.GameConfig.GM_INFO.review < 2) cc.fx.GameConfig.GM_INFO.review += 1; this.gameOver = false; this.timeNumber = 21; this.add_Time += 20; this.pause = false; this.node.parent.parent.getChildByName("Lose").active = false; this.startTimeCutDown(); } homeBtn(){ // alert("游戏失败"); cc.fx.AudioManager._instance.playEffect("anniu_Big",null); if(!this.homeCanTouch) return; this.homeCanTouch = false; this.node.parent.parent.getChildByName("Lose").active = true; this.node.parent.parent.getChildByName("Lose").getChildByName("Time").active = false; this.node.parent.parent.getChildByName("Lose").getChildByName("Boom").active = false; this.node.parent.parent.getChildByName("Lose").getChildByName("Lock").active = false; this.node.parent.parent.getChildByName("Lose").getChildByName("Health").active = true; let data = { time:this.count_Time, add_Time:this.add_Time, result:"fail" } cc.fx.GameTool.shushu_Track("finish_stage",data); if(this.gameStart == true){ cc.fx.GameTool.setUserHealth(-1,(data)=>{ }) } // this.node.parent.parent.parent.destroy(); } returnHome(){ cc.fx.AudioManager._instance.playEffect("anniu_Big",null); if(MapConroler._instance = null){ return; } // MiniGameSdk.API.showToast("体力值减少"); MapConroler._instance = null; cc.fx.GameConfig.LEVEL_INFO_init(false,0); if(this.new_mode == 3){ this.node.parent.parent.getChildByName("Win").active = false; this.openNewMode(1); return; } cc.fx.AudioManager._instance.playEffect("zhuan1",null); this.node.parent.parent.parent.getChildByName("zhuanchang").active = true; this.node.parent.parent.parent.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1,"up",false); cc.director.preloadScene("HomeScene", (err, asset) => { if (err) { console.error('动态加载 Prefab 失败:', err); return; } }); setTimeout(() => { cc.director.loadScene("HomeScene"); }, 1200); } //判断游戏失败 failLevel(type){ this.stopTimeCutDown(); if(this.gameOver== true ||this.gameWin == true){ return; } let time = 0; this.gameOver = true; if(type == "boom"){ time = 2000; } setTimeout(() => { this.node.parent.parent.getChildByName("Lose").active = true; this.node.parent.parent.getChildByName("Lose").getChildByName("Time").active = true; this.node.parent.parent.getChildByName("Lose").getChildByName("Boom").active = false; let buyBtn = this.node.parent.parent.getChildByName("Lose").getChildByName("Time").getChildByName("buyBtn"); buyBtn.getChildByName("coin1").active = buyBtn.getChildByName("coin2").active = buyBtn.getChildByName("coin3").active =false; if(cc.fx.GameConfig.GM_INFO.review == 0)buyBtn.getChildByName("coin1").active = true; else if(cc.fx.GameConfig.GM_INFO.review == 1) buyBtn.getChildByName("coin2").active = true; else if(cc.fx.GameConfig.GM_INFO.review == 2) buyBtn.getChildByName("coin3").active = true; if(type){ if(type == "boom"){ this.node.parent.parent.getChildByName("Lose").getChildByName("Time").active = false; this.node.parent.parent.getChildByName("Lose").getChildByName("Boom").active = true; } } }, time); // console.log("游戏失败"); } //打开新模式弹窗 openNewMode(type){ let index = 0; const BLOCKS_PER_FRAME = 5; // 每帧销毁的方块数量 const destroyBlocks = () => { const endIndex = Math.min(index + BLOCKS_PER_FRAME, this.blocks.length); for (; index < endIndex; index++) { if (this.blocks[index]) { this.blocks[index].destroy(); } } // 如果还有方块未销毁,下一帧继续 if (index < this.blocks.length) { this.scheduleOnce(destroyBlocks, 0); } else { } }; // 开始分帧销毁方块 destroyBlocks(); this.node.parent.parent.getChildByName("NewMode").active = true; this.node.parent.parent.getChildByName("NewMode").getComponent("NewMode").setMode(type); } downDoor(color,type){ for(let i=0; i{ }) let data = { id:"2001", num:-1 } cc.fx.GameTool.shushu_Track("resource_cost",data); } else MiniGameSdk.API.showToast("道具使用中,请稍后再试"); } } } //解开时间冻结 openIce(){ if(this.node.parent.getChildByName("Ice").active){ this.freezeMask.active = false; let freezeBtn = this.node.parent.getChildByName("Bottom").getChildByName("timeBtn"); freezeBtn.getComponent("btnControl").setTouch(true); this.node.parent.getChildByName("Ice").active = false; this.node.parent.getChildByName("Top").getChildByName("Ice").active = false; this.pause = false; // this.startTimeCutDown(); } } handleBuySuccess(data){ this.pause = false; const timestamp = Date.now(); // console.log("回调函数内:",this.pause); let freezeBtn = this.node.parent.getChildByName("Bottom").getChildByName("timeBtn"); let hammerBtn = this.node.parent.getChildByName("Bottom").getChildByName("destroyBtn"); let magicBtn = this.node.parent.getChildByName("Bottom").getChildByName("magicBtn"); if(data == "freezeAmount") freezeBtn.getComponent("btnControl").setTouch(true); else if(data == "hammerAmount") hammerBtn.getComponent("btnControl").setTouch(true); else if(data == "magicAmount") magicBtn.getComponent("btnControl").setTouch(true); if(data == "freezeAmount"){ cc.fx.GameConfig.GM_INFO.freezeAmount += 3; let propInfo = cc.fx.StorageMessage.getStorage("prop"); propInfo.freezeAmount = cc.fx.GameConfig.GM_INFO.freezeAmount; propInfo.timestamp = timestamp; cc.fx.StorageMessage.setStorage("prop",propInfo); this.node.parent.parent.parent.getComponent("SceneManager").closePropBuy(); setTimeout(() => { this.iceLabel.string = cc.fx.GameConfig.GM_INFO.freezeAmount.toString(); MiniGameSdk.API.showToast("购买冻结时间道具成功"); }, 200); } else if(data == "magicAmount"){ cc.fx.GameConfig.GM_INFO.magicAmount += 3; let propInfo = cc.fx.StorageMessage.getStorage("prop"); propInfo.magicAmount = cc.fx.GameConfig.GM_INFO.magicAmount; propInfo.timestamp = timestamp; cc.fx.StorageMessage.setStorage("prop",propInfo); this.node.parent.parent.parent.getComponent("SceneManager").closePropBuy(); setTimeout(() => { MiniGameSdk.API.showToast("购买锤子道具成功"); this.magicLabel.string = cc.fx.GameConfig.GM_INFO.magicAmount.toString(); }, 200); } else if(data == "hammerAmount"){ cc.fx.GameConfig.GM_INFO.hammerAmount += 3; let propInfo = cc.fx.StorageMessage.getStorage("prop"); propInfo.hammerAmount = cc.fx.GameConfig.GM_INFO.hammerAmount; propInfo.timestamp = timestamp; cc.fx.StorageMessage.setStorage("prop",propInfo); this.node.parent.parent.parent.getComponent("SceneManager").closePropBuy(); setTimeout(() => { this.hammerLabel.string = cc.fx.GameConfig.GM_INFO.hammerAmount.toString(); MiniGameSdk.API.showToast("购买魔法棒道具成功"); }, 200); } } //使用锤子道具 useHammer(){ if(this.node.parent.getChildByName("Ice").active == true || this.hammer == true || this.magicMask.active== true || this.timeNumber <= 1){ return; } let hammerBtn = this.node.parent.getChildByName("Bottom").getChildByName("destroyBtn"); if(hammerBtn.getComponent("btnControl")._touch){ hammerBtn.getComponent("btnControl").setTouch(false); if(cc.fx.GameConfig.GM_INFO.hammerAmount < 1){ if(cc.fx.GameConfig.GM_INFO.coin < 1500){ MiniGameSdk.API.showToast("金币不足,无法购买道具"); hammerBtn.getComponent("btnControl").setTouch(true); return; } this.node.parent.parent.getChildByName("propWindow").active = true; this.node.parent.parent.parent.getComponent("SceneManager").openPropBuy("hammer"); } else{ if(!this.hammer){ const timestamp = Date.now(); this.hammerMask.active = true; this.hammer = true; cc.fx.GameConfig.GM_INFO.hammerAmount -= 1; if(cc.fx.GameConfig.GM_INFO.hammerAmount < 0) cc.fx.GameConfig.GM_INFO.hammerAmount = 0; this.hammerLabel.string = cc.fx.GameConfig.GM_INFO.hammerAmount.toString(); let propInfo = cc.fx.StorageMessage.getStorage("prop"); console.log("锤子道具信息:",propInfo); propInfo.hammerAmount = cc.fx.GameConfig.GM_INFO.hammerAmount; propInfo.timestamp = timestamp; cc.fx.StorageMessage.setStorage("prop",propInfo); cc.fx.GameTool.setUserProp(2002,cc.fx.GameConfig.GM_INFO.hammerAmount,(data)=>{ }) let data = { id:"2002", num:-1 } cc.fx.GameTool.shushu_Track("resource_cost",data); } else MiniGameSdk.API.showToast("道具使用中,请稍后再试"); } } } buyMagic(){ if(cc.fx.GameConfig.GM_INFO.coin < 1500){ MiniGameSdk.API.showToast("金币不足,无法购买道具"); return; } cc.fx.GameTool.buyProp(2003,this.handleBuySuccess.bind(this,"magicAmount")); } buyHammer(){ if(cc.fx.GameConfig.GM_INFO.coin < 1500){ MiniGameSdk.API.showToast("金币不足,无法购买道具"); return; } cc.fx.GameTool.buyProp(2002, this.handleBuySuccess.bind(this,"hammerAmount")); } buyFreeze(){ if(cc.fx.GameConfig.GM_INFO.coin < 1500){ MiniGameSdk.API.showToast("金币不足,无法购买道具"); return; } cc.fx.GameTool.buyProp(2001,this.handleBuySuccess.bind(this,"freezeAmount")); } //使用魔法棒随机消除两个方块 useMagic(){ if(this.node.parent.getChildByName("Ice").active == true || this.hammer == true || this.magicMask.active== true || this.timeNumber <= 1){ return; } let magicBtn = this.node.parent.getChildByName("Bottom").getChildByName("magicBtn"); if(magicBtn.getComponent("btnControl")._touch){ magicBtn.getComponent("btnControl").setTouch(false); if(cc.fx.GameConfig.GM_INFO.magicAmount < 1){ if(cc.fx.GameConfig.GM_INFO.coin < 1500){ MiniGameSdk.API.showToast("金币不足,无法购买道具"); magicBtn.getComponent("btnControl").setTouch(true); return; } this.node.parent.parent.getChildByName("propWindow").active = true; this.node.parent.parent.parent.getComponent("SceneManager").openPropBuy("magic"); // MiniGameSdk.API.showToast("魔法棒数量不足,自动购买魔法棒"); } else{ const timestamp = Date.now(); this.magicMask.active = true; setTimeout(() => { this.magicMask.active = false; magicBtn.getComponent("btnControl").setTouch(true); }, 1000); cc.fx.GameConfig.GM_INFO.magicAmount -= 1; if(cc.fx.GameConfig.GM_INFO.magicAmount < 0) cc.fx.GameConfig.GM_INFO.magicAmount = 0; this.magicLabel.string = cc.fx.GameConfig.GM_INFO.magicAmount.toString(); let propInfo = cc.fx.StorageMessage.getStorage("prop"); propInfo.magicAmount = cc.fx.GameConfig.GM_INFO.magicAmount; propInfo.timestamp = timestamp; cc.fx.StorageMessage.setStorage("prop",propInfo); cc.fx.GameTool.setUserProp(2003,cc.fx.GameConfig.GM_INFO.magicAmount,(data)=>{ }) let data = { id:"2003", num:-1 } cc.fx.GameTool.shushu_Track("resource_cost",data); let nomalArray = []; // this.magicBtn.node.active = false; for(let i =0; i 1){ nomalArray = cc.fx.GameTool.shuffleArray(nomalArray); nomalArray[0].getComponent("Block").eliminate(); let time = 0; if(nomalArray[0].getComponent("Block").type == 1 || nomalArray[0].getComponent("Block").type == 9){ time = 200; } if(time > 0){ setTimeout(() => { nomalArray[1].getComponent("Block").eliminate(); }, time); return; } else{ nomalArray[1].getComponent("Block").eliminate(); return; } } else if(nomalArray.length == 1){ nomalArray[0].getComponent("Block").eliminate(); let time = 0; if(nomalArray[0].getComponent("Block").type == 1 || nomalArray[0].getComponent("Block").type == 9){ time = 200; } setTimeout(() => { if(this.freezeArray.length == 0 && this.loackArray.length == 0){ // console.log("只剩下一个块道具使用完毕"); } else{ if(this.loackArray.length != 0){ // console.log("消除一个普通块后,消除一个带锁块"); this.loackArray[0].getComponent("Block").eliminate(); return; } else if(this.freezeArray.length!= 0){ // console.log("消除一个普通块后,消除一个冻结块"); this.freezeArray[0].getComponent("Block").eliminate(); return; } } }, 200+time); } else{ let count = 2; if(this.loackArray.length!= 0){ for(let i=0; i { if(count != 0){ for(let i=0; i 13){ this.node.scale = 1; } // this.node.scale = 1; cc.fx.GameConfig.GM_INFO.scale = this.node.scale; // this.node.scale = 0.5; // console.log(this.node.scale); // this.node.scale = 1; } //创建门的粒子特效 createParticle(block,jg){ let particle = cc.instantiate(MapConroler._instance.Block_Prop[9]); particle.parent = this.node; particle.zIndex = 1000; let width = Math.floor(block.width / 120); let height = Math.floor(block.height / 120); let name = ""; if(jg == 0 || jg == 1){ let y = block.y + block.height; let x = block.x - block.width/2; name = "top_" + width + "_" + height; if(jg == 1){ name = "bot_" + width + "_" + height; y = block.y; } if(block.anchorX == 0.5){ x = block.x; } else if(block.anchorX == 0.33){ x = block.x + block.width*(0.5-0.33); } else if(block.anchorX == 0.66){ x = block.x + block.width*(0.5-0.66); } particle.setPosition(x, y); } else if(jg == 2 || jg == 3){ name = "left_" + height + "_" + width; let x = block.x - block.width; let y = block.y + block.height/2; if(block.anchorX == 0.5){ x = block.x - block.width/2; } else if(block.anchorX == 0.33){ x = block.x - block.width*0.33; } else if(block.anchorX == 0.66){ x = block.x + block.width*0.66; } if(jg == 3){ name = "right_" + height + "_" + width; x = block.x; if(block.anchorX == 0.5){ x = block.x + block.width/2; } else if(block.anchorX == 0.33){ x = block.x + block.width*0.66; } else if(block.anchorX == 0.66){ x = block.x + block.width*0.33; } } particle.setPosition(x, y); } let particleNode = particle.getChildByName(name); let color = block.getComponent("Block").color - 1; particleNode.getComponent(cc.ParticleSystem).spriteFrame = particle.getComponent("Reduce").Block_Color[color]; particleNode.active = true; setTimeout(() => { cc.tween(particle) .to(2, {opacity: 0}) .call(() => { particle.destroy(); }) .start(); }, 1200); } removeOneBlock() { // 移除所有方块 for(let i =0 ; i