// 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 SceneManager from "./SceneManager";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";
import Animation = cc.Animation;
const { ccclass, property } = cc._decorator;



@ccclass
export default class MapConroler extends cc.Component {
    static _instance: any;


    @property({ type: [cc.Prefab], tooltip: "方块数组" })
    Block_Array: Array<cc.Prefab> = [];

    @property({ type: [cc.Prefab], tooltip: "方块道具数组" })
    Block_Prop: Array<cc.Prefab> = [];

    @property(cc.Prefab)
    MapBlockPrefab: cc.Prefab = null;

    @property({ type: [cc.SpriteAtlas], tooltip: "方块颜色" })
    Block_Color: Array<cc.SpriteAtlas> = [];


    @property({ type: [cc.Prefab], tooltip: "墙壁数组" })
    Wall_Prefab: Array<cc.Prefab> = [];



    @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)
    magics: cc.Node = 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)
    iceNode: cc.Node = null;
    @property(cc.Node)
    hammerNode: cc.Node = null;
    @property(cc.Node)
    magicNode: cc.Node = 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)
    guideNode: cc.Node = null;

    @property(cc.Node)
    coinPop: cc.Node = null;

    timeLabel: cc.Node = null;
    levelLabel: cc.Node = null;
    coin: cc.Node = null;

    new_mode: number = 0;
    mapWidth: number = 0;
    mapHeight: number = 0;
    wallNum: number = 0;
    blockNum: number = 0;
    reviewState: boolean = false;
    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;//锤子状态
    ishammer: 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.reviewBoom = 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.reviewState = false;
        this.homeCanTouch = true;
        this.againCanTouch = true;
        this.new_mode = 0;
        this.count_Time = 0;
        this.add_Time = 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();
        this.setPropNum();
        // 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();

    }
    //道具数量
    setPropNum() {
        //如果道具数量大于0，显示 cc.fx.GameConfig.GM_INFO.freezeAmount，如果为零显示'mul10'

        if (cc.fx.GameConfig.GM_INFO.freezeAmount > 0) {
            this.iceNode.children[0].active = false;
            this.iceNode.children[1].active = true;
            this.iceNode.children[2].active = true;
            NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.freezeAmount, 20, 15, "mul", this.iceNode.children[2], true);
        } else {
            this.iceNode.children[0].active = true
            this.iceNode.children[1].active = false;
            this.iceNode.children[2].active = false;
        }
        if (cc.fx.GameConfig.GM_INFO.hammerAmount > 0) {
            this.hammerNode.children[1].active = true;
            this.hammerNode.children[0].active = false;
            this.hammerNode.children[2].active = true;
            NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.hammerAmount, 20, 15, "mul", this.hammerNode.children[2], true);
        } else {
            this.hammerNode.children[0].active = true;
            this.hammerNode.children[1].active = false;
            this.hammerNode.children[2].active = false;
        }
        if (cc.fx.GameConfig.GM_INFO.magicAmount > 0) {
            this.magicNode.children[1].active = true;
            this.magicNode.children[0].active = false;
            this.magicNode.children[2].active = true;
            NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.magicAmount, 20, 15, "mul", this.magicNode.children[2], true);
        } else {
            this.magicNode.children[0].active = true
            this.magicNode.children[1].active = false;
            this.magicNode.children[2].active = false;
        }
    }
    startUpdate() {
        if (this.gameStart == false) {
            this.gameStart = true;
            cc.fx.GameTool.setUserHealth(-1, (data) => {
            })
            this.count_Time = Date.now();
            //发送数数事件——进入关卡
            console.log("准备进入下一关,发送下一关进入");
            if ((cc.fx.GameConfig.GM_INFO.level + 1) == 1) {
                this.guideNode.getComponent(Animation).stop();
                this.guideNode.active = false;
            }
            cc.fx.GameTool.shushu_Track("enter_stage");
            this.startTimeCutDown();
        }
    }

    initMap() {

        // this.node.getChildByName("Wall").zIndex = 10;
        this.timeLabel = this.node.parent.getChildByName("Top").getChildByName("time");
        this.levelLabel = this.node.parent.getChildByName("Top").getChildByName("level");
        this.coin = this.node.parent.getChildByName("Top").getChildByName("shop").getChildByName("coin");
        this.timeNumber = cc.fx.GameConfig.LEVEL_INFO[0].time;
        console.log("当前关卡时间", this.timeNumber);

        this.add_Time = 0;
        NumberToImage.getTimeMargin(this.timeNumber, 50, "time_", this.timeLabel)

        this.updateCoin();
        // var timeTemp = cc.fx.GameTool.getTimeMargin(this.timeNumber);
        // this.timeLabel.string = timeTemp.toString();
        NumberToImage.numberToImageNodes((cc.fx.GameConfig.GM_INFO.level + 1), 50, 20, "level_", this.levelLabel, true)
        console.log("当前关卡", cc.fx.GameConfig.GM_INFO.level + 1);
        //this.startTimeCutDown();

        this.mapWidth = cc.fx.GameConfig.LEVEL_INFO[0].map[0];
        this.mapHeight = cc.fx.GameConfig.LEVEL_INFO[0].map[1];
        let gap = cc.fx.GameConfig.LEVEL_INFO[0].gap;
        let risefall = cc.fx.GameConfig.LEVEL_INFO[0].risefall;

        this.pause = false;
        this.hammer = false;
        this.ishammer = false;
        this.wallNum = 0;
        this.setMapInfo();
        this.blocks = [];
        this.mapBlocksWall = [];
        this.mapInfo = [];
        this.wallInfo = [];
        this.openWall = [];
        this.wallArray = [];
        this.freezeWall = [];
        this.riseFallBlcok = [];
        this.mapBlockArray = [];
        this.freezeArray = [];
        this.loackArray = [];
        this.count_Time = 0;
        this.add_Time = 0;


        let startX = this.mapWidth % 2 == 0 ? -(this.mapWidth - 1) * 60 : -(this.mapWidth - 1) * 60;
        let startY = this.mapHeight % 2 == 0 ? -(this.mapHeight - 1) * 60 : -(this.mapHeight - 1) * 60;
        // startX =(this.mapWidth-1)*60 + 60;
        // startY =-(this.mapHeight-1)*60 - 60;

        for (let i = 0; i < this.mapWidth; i++) {
            this.mapBlocksWall[i] = [];

            for (let j = 0; j < this.mapHeight; j++) {
                let block = cc.instantiate(this.MapBlockPrefab);
                block.parent = this.node.getChildByName("mapBlock");
                block.getComponent("MapBlock").init(i, j);
                // block.getChildByName("num").getComponent(cc.Label).string = i + ":" + j;
                block.setPosition(cc.v2(startX + i * 120, startY + j * 120))
                if (risefall != null) {
                    if (this.mapRiseFall(cc.v2(i, j), risefall, block)) {
                        this.riseFallBlcok.push(block);
                    }
                }
                if (gap != null) {
                    if (!this.mapGap(cc.v2(i, j), gap) || i == 0 || i == this.mapWidth - 1
                        || j == 0 || j == this.mapHeight - 1) {
                        block.opacity = 254;
                        block.removeComponent(cc.Sprite);
                        block.removeAllChildren();
                        this.wallInfo.push(block);
                    }
                    else this.mapInfo.push(block);
                }
                else {
                    if (i == 0 || i == this.mapWidth - 1
                        || j == 0 || j == this.mapHeight - 1) {
                        block.opacity = 254;
                        block.removeComponent(cc.Sprite);
                        block.removeAllChildren();
                        this.wallInfo.push(block);
                    }
                    else this.mapInfo.push(block);
                }

                this.mapBlocksWall[i].push(block);
            }
        }

        this.wallInit();
        this.blockInit();
        if ((cc.fx.GameConfig.GM_INFO.level + 1) == 1) {
            // 如果是第一关，显示引导
            this.guideNode.active = true;
            this.guideNode.zIndex = 1000;
            this.guideNode.getComponent(Animation).play();
        }

    }

    //创建方块
    // ... 已有代码 ...

    //创建方块
    // ... 已有代码 ...




    blockInit() {
        let blockArray = cc.fx.GameConfig.BLOCK_INFO[0];
        blockArray = this.sortBlock(blockArray);
        //console.log("创建方块", blockArray);
        let index = 0; // 当前要创建的方块索引
        let BLOCKS_PER_FRAME = 1; // 初始每帧创建的方块数量
        const MAX_PER_FRAME = 10; // 每帧最大创建数量
        const MIN_PER_FRAME = 1; // 每帧最小创建数量

        const createBlocks = () => {
            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 < this.wallInfo.length; i++) {
            let block = this.wallInfo[i];
            if (block.opacity != 250) {
                let dir = this.getWllDiraction("turn", cc.v2(block.getComponent("MapBlock").posX, block.getComponent("MapBlock").posY));
                if (dir != null) {
                    this.createTurn(dir, block, block.getComponent("MapBlock").posX, block.getComponent("MapBlock").posY);
                }
            }

        }

    }

    createWall(direction, node) {
        let wall = null;
        node.getComponent("MapBlock").setDiraction(direction);
        switch (direction) {
            case "right":
                node.opacity = 250;
                wall = cc.instantiate(this.Wall_Prefab[4]);
                this.leftDoors.push(wall);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                node.opacity = 250;
                wall.getChildByName("wall").getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                //wall.getChildByName("wall").getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.getChildByName("wall"));
                this.wallNum += 1;
                if (wall.getChildByName("wall").getComponent("Wall").special == 2)
                    this.openWall.push(wall);
                else if (wall.getChildByName("wall").getComponent("Wall").special == 3)
                    this.freezeWall.push(wall);

                break;
            case "left":
                node.opacity = 250;
                wall = cc.instantiate(this.Wall_Prefab[3]);
                // wall.parent = this.node.getChildByName("Wall");
                this.rightDoors.push(wall);
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                wall.getChildByName("wall").getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                this.setDoorInfo(wall.getChildByName("wall"));
                //wall.getChildByName("wall").getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.wallNum += 1;
                if (wall.getChildByName("wall").getComponent("Wall").special == 2)
                    this.openWall.push(wall);
                else if (wall.getChildByName("wall").getComponent("Wall").special == 3)
                    this.freezeWall.push(wall);
                break;
            case "up":
                node.opacity = 250;
                wall = cc.instantiate(this.Wall_Prefab[5]);

                this.topDoors.push(wall);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));

                wall.getChildByName("wall").getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                this.setDoorInfo(wall.getChildByName("wall"));
                //wall.getChildByName("wall").getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.wallNum += 1;
                if (wall.getChildByName("wall").getComponent("Wall").special == 2)
                    this.openWall.push(wall);
                else if (wall.getChildByName("wall").getComponent("Wall").special == 3)
                    this.freezeWall.push(wall);
                break;
            case "down":
                node.opacity = 250;
                wall = cc.instantiate(this.Wall_Prefab[0]);
                this.bottomDoors.push(wall);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                // this.setDoorInfo(wall.getChildByName("wall"));
                wall.getChildByName("wall").getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                this.setDoorInfo(wall.getChildByName("wall"));
                //wall.getChildByName("wall").getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.wallNum += 1;
                if (wall.getChildByName("wall").getComponent("Wall").special == 2)
                    this.openWall.push(wall);
                else if (wall.getChildByName("wall").getComponent("Wall").special == 3)
                    this.freezeWall.push(wall);
                break;
            case "upright": case "rightup":
                node.opacity = 249;
                wall = cc.instantiate(this.Wall_Prefab[7]);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                wall.children[1].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                wall.children[2].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                //wall.children[0].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[0]);
                this.wallNum += 1;
                //wall.children[1].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[1]);
                this.wallNum += 1;

                break;
            case "upleft": case "leftup":
                node.opacity = 249;
                wall = cc.instantiate(this.Wall_Prefab[6]);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                wall.children[1].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                wall.children[2].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                //wall.children[0].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[0]);
                this.wallNum += 1;
                //wall.children[1].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[1]);
                this.wallNum += 1;

                break;
            case "downright": case "rightdown":
                node.opacity = 249;
                wall = cc.instantiate(this.Wall_Prefab[2]);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                wall.children[1].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                wall.children[2].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                //wall.children[0].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[0]);
                this.wallNum += 1;
                //wall.children[1].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[1]);
                this.wallNum += 1;

                break;
            case "downleft": case "leftdown":
                node.opacity = 249;
                wall = cc.instantiate(this.Wall_Prefab[1]);
                // wall.parent = this.node.getChildByName("Wall");
                wall.parent = this.node;
                wall.setPosition(cc.v2(node.x, node.y));
                wall.children[1].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                wall.children[2].getComponent("Wall").init(null, node.getComponent("MapBlock").posX, node.getComponent("MapBlock").posY, direction);
                //wall.children[0].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[0]);
                this.wallNum += 1;
                //wall.children[1].getChildByName("num").getComponent(cc.Label).string = this.wallNum.toString();
                this.setDoorInfo(wall.children[1]);
                this.wallNum += 1;

                break;

        }


        // if(wallTurn){
        //     wallTurn.getComponent("wallTunr").init(wall);
        // }
    }

    setDoorInfo(wall) {
        let doorInfo = cc.fx.GameConfig.WALL_INFO[0];

        if (doorInfo) {
            for (let j = 0; j < doorInfo.length; j++) {
                if (doorInfo[j].num == this.wallNum) {
                    wall.getComponent("Wall").init(doorInfo[j], null, null, null);
                    this.wallArray.push(wall.parent);
                }
            }
        }
    }

    createTurn(direction, node, posX, posY) {
        let wall = null;
        wall = cc.instantiate(this.wallTurnPrefab);
        wall.parent = this.node;
        wall.setPosition(cc.v2(node.x, node.y));
        switch (direction) {
            case "upright": case "rightup":
                wall.angle = 0;
                wall.getChildByName("icon").angle = 0;

                break;
            case "upleft": case "leftup":
                wall.angle = 90;
                wall.getChildByName("icon").angle = -90;
                wall.getChildByName("icon").y += 5;
                wall.getChildByName("icon").x -= 1.3;
                break;
            case "downright": case "rightdown":
                wall.angle = 270;
                wall.getChildByName("icon").angle = -270;
                wall.getChildByName("icon").x += 2.5;
                wall.getChildByName("icon").y += 1;
                break;
            case "downleft": case "leftdown":
                wall.angle = 180;
                wall.getChildByName("icon").angle = -180;
                wall.getChildByName("icon").x += 4.5;
                wall.getChildByName("icon").y += 3.2;
                break;
        }

        // if(direction == "rightdown" || direction == "downright") {
        //     wall.zIndex = 50 + posX - posY*3;
        // }
        // else 
        //     wall.zIndex = 50 + posX - posY*3;

        if (direction == "up" || direction == "leftup" || direction == "upleft"
        ) {
            wall.zIndex = 100 + posX - posY * 3;
        }
        else if (direction == "rightup" || direction == "upright" || direction == "rightdown" || direction == "downright" || direction == "downleft") {
            wall.zIndex = 30 + posX - posY * 3;
        }
        else if (direction == "left" || direction == "leftdown") {
            wall.zIndex = 70 + posX - posY * 3;
        }
        else wall.zIndex = 70 + posX - posY * 3;

        // wall.getChildByName("num").angle = -wall.angle;
        //wall.getChildByName("num").getComponent(cc.Label).string = direction;
        //wall.getChildByName("num").getComponent(cc.Label).string = wall.zIndex+ "";
        // console.log(posX,posY,wall.zIndex);
        // this.wallNum +=  1;
        // wall.getChildByName("num").getComponent(cc.Label).string = this.wallNum + "";
    }

    //地图底块缺口判断
    mapGap(point, gap) {
        for (let i = 0; i < gap.length; i++) {
            if (point.x == gap[i].x && point.y == gap[i].y) {
                return false;
            }
        }
        return true;
    }

    //升降地块判断
    mapRiseFall(point, risefall, block) {
        for (let i = 0; i < risefall.length; i++) {
            if (point.x == risefall[i].pos.x && point.y == risefall[i].pos.y) {
                block.getChildByName("risefall").active = true;
                block.getChildByName('risefall').getChildByName("color").getComponent(cc.Label).string = risefall[i].color + "";
                block.getChildByName('risefall').addComponent(cc.BoxCollider);
                block.getChildByName('risefall').getComponent(cc.BoxCollider).size = cc.size(110, 110);
                return true;
            }
        }
        return false;
    }
    //升降地块
    changeRiseFall(color, down) {
        color = color.toString();
        for (let i = 0; i < this.riseFallBlcok.length; i++) {
            if (color == this.riseFallBlcok[i].getChildByName("risefall").getChildByName("color").getComponent(cc.Label).string) {
                if (down) {
                    this.riseFallBlcok[i].getChildByName("risefall").active = false;
                    this.riseFallBlcok[i].getChildByName('risefall').removeComponent(cc.BoxCollider);
                }
                else {
                    this.riseFallBlcok[i].getChildByName("risefall").active = true;
                    this.riseFallBlcok[i].getChildByName('risefall').addComponent(cc.BoxCollider);
                    this.riseFallBlcok[i].getChildByName('risefall').getComponent(cc.BoxCollider).size = cc.size(110, 110);
                }
            }

        }
    }



    //查询叠加快id
    foundDownBlock(id) {
        for (let i = 0; i < this.blocks.length; i++) {
            if (this.blocks[i].getComponent("Block").blockId == id) {
                return this.blocks[i];
            }
        }
        return null;
    }

    //删除块
    removeBlock(id) {
        for (let i = 0; i < this.blocks.length; i++) {
            if (this.blocks[i].getComponent("Block").blockId == id) {
                return this.blocks[i];
            }
        }
        return null;
    }

    getMinAndMax(block) {
        let width = Math.floor(block.width / 120);
        let height = Math.floor(block.height / 120);

        let minX = width;
        let maxX = this.mapWidth - 2;
        let minY = 1;
        let maxY = this.mapHeight - 2 - height + 1;

        if (block.name == "block10") {
            minX = width - 1;
            if (this.mapWidth > 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 < blocks.length; i++) {
            allBlocks.push(cc.v2(nodePos.x + blocks[i].x, nodePos.y + blocks[i].y));
        }

        // const date1 = new Date().getTime();
        // console.log("将小块放入数组的时间",date1);

        let jg = -1;
        let pz = 0;
        // 获取所有墙壁节点
        // let wall = this.node;
        let direction = this.checkDiraction(allBlocks, maxX, maxY);

        // 判断方块是否在地图边缘
        if (nodePos.x <= minX || direction[0] == true) {
            // const date2 = new Date().getTime();
            // console.log("碰到左边缘",date2);
            pz += 1;
            let leftWalls = [];
            for (let i = 0; i < this.leftDoors.length; i++) {
                let wallLeft = this.leftDoors[i].getChildByName("wall");
                let wallPos = cc.v2(wallLeft.getComponent("Wall").posX, wallLeft.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);
                        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 < this.openWall.length; i++) {
                this.openWall[i].getChildByName("wall").getComponent("Wall").changeLock();
            }
        }

        if (this.freezeWall.length != 0) {
            for (let i = 0; i < this.freezeWall.length; i++) {
                this.freezeWall[i].getChildByName("wall").getComponent("Wall").changeFreeze();
            }
        }
    }

    //检测是否可以通过门
    passWall(jg, wallArray, node) {
        for (let i = 0; i < wallArray.length; i++) {
            if (wallArray[i].getComponent("Wall").special == 2) {
                console.log("尝试通过开关门:", wallArray[i].getComponent("Wall").open);
            }
            if (wallArray[i].getComponent("Wall").special == 2 && wallArray[i].getComponent("Wall").open == false) {
                jg = false;
                break;
            }
            //console.log(wallArray[i].getChildByName("wall").getComponent("Wall").color,node.getComponent("Block").color)
            if (wallArray[i].getComponent("Wall").color != node.getComponent("Block").color) {

                jg = false;
                break;
            }
            if (node.getComponent("Block").type == 5) {
                if (wallArray[i].getComponent("Wall").special != 1) {
                    jg = false;
                    break;
                }
            }
        }
        if (jg == true && node.getComponent("Block").type == 5) {
            for (let j = 0; j < wallArray.length; j++) {
                if (wallArray[j].getComponent("Wall").special == 1 && wallArray[j].getComponent("Wall").wall_Info.length != 0) {
                    wallArray[j].getComponent("Wall").playStarDoor();
                }
            }
        }
        // const date8 = new Date().getTime();
        // console.log("检测颜色是否能够通过门",date8);
        return jg;
    }

    //检测方块和门中间有没有夹杂其他块
    detectingBlock(direction, posX, posY, blocks) {
        let jg = true;
        let id = "";
        if (blocks.length > 0) {
            for (let i = 0; i < blocks.length; i++) {
                let pos = blocks[i];
                let x = pos.x;
                let y = pos.y;
                id = this.mapBlocksWall[x][y].getComponent("MapBlock").block_Id;
                jg = this.checkAllDirections(id, direction, x, y);
                if (!jg) {
                    // console.log("方向检测没过");
                    i = 1000000;
                    return jg;
                }
            }
        }
        // const date7 = new Date().getTime();
        // console.log("方向检测通过",date7,jg);
        return jg;
    }

    //检测物体各方向是否有别的物块，防止碰到墙壁了但是是凹凸形状，墙与块之间有阻挡物块
    checkAllDirections(id, direction, x, y) {
        let jg = true;

        if (direction == "left") {
            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) >= 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 < allBlocks.length; i++) {
            if (allBlocks[i].x > 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 < allBlocks.length; i++) {

        }
        return jg;
    }

    checkDown(allBlocks) {
        let jg = false;
        for (let i = 0; i < allBlocks.length; i++) {

        }
        return jg;
    }

    checkUp(allBlocks, max) {
        let jg = false;
        for (let i = 0; i < allBlocks.length; i++) {

        }
        return jg;
    }


    //特殊处理，方块带道具或者需要消除冰块
    special_Treatment(node) {
        let freezeBlock = this.node.children.filter(child => {
            if (child.getComponent("Block")) {
                if (child.getComponent("Block").type == 4)
                    return child
            }
        });
        if (freezeBlock.length > 0) {
            for (let i = 0; i < freezeBlock.length; i++) {
                freezeBlock[i].getChildByName("freeze").getComponent("Freeze").reduce(1);
            }
        }

        if (node.getComponent("Block").type == 2 || node.getComponent("Block").type == 4) {
            let lockBlock = this.node.children.filter(child => {
                if (child.getComponent("Block")) {
                    if (child.getComponent("Block").type == 3)
                        return child
                }
            });
            if (lockBlock.length > 0)
                for (let i = 0; i < lockBlock.length; i++) {
                    lockBlock[i].getChildByName("lock").getComponent("Lock").reduce();
                }

        }

        for (let i = 0; i < this.blocks.length; i++) {
            if (this.blocks[i] == node) {
                this.blocks.splice(i, 1);
            }
        }

    }

    judgeWin(number) {
        this.blockNum = this.blocks.length;
        if (number == 1) {
            if ((this.blockNum == 0 || this.blockNum == 1) && !this.gameWin && !this.gameOver) {
                this.stopTimeCutDown();
                this.stopBoom();
            }
        }
        else if (number == 0) {
            if (this.blockNum == 0 && !this.gameWin && !this.gameOver) {
                this.stopTimeCutDown();
                this.stopBoom();
            }
        }

    }

    //判断游戏成功下一关
    nextLevel() {
        this.pause = false;
        this.openIce();

        this.blockNum = this.blocks.length;

        if (this.blockNum == 0 && !this.gameWin && !this.gameOver) {
            // alert("游戏成功");
            MiniGameSdk.API.showToast(cc.fx.GameConfig.GM_INFO.level);
            this.gameWin = true;
            if (cc.fx.GameConfig.GM_INFO.hp < 5) {
                console.log("恢复一点体力", cc.fx.GameConfig.GM_INFO.level);
                cc.fx.GameTool.setUserHealth(1, (data) => {
                })
            }
            this.stopTimeCutDown();
            // console.log("即将上报成功________________________:",this.add_Time);
            // console.log("成功消除一个",this.add_Time);
            cc.fx.GameTool.changeCoin(40);
            const data = {
                change_reason: "level",
                id: (1001 + ""),
                num: 40
            }
            cc.fx.GameTool.shushu_Track("resource_get", data);
            let overTime = Date.now();
            this.count_Time = overTime - this.count_Time;
            cc.fx.GameTool.addLevel(this.count_Time, this.add_Time);
            const winCOIN = cc.find("Canvas"); // 假设 Canvas 节点
            if (winCOIN) {
                const wincoin = winCOIN.getComponent(SceneManager);
                if (wincoin) {
                    wincoin.updateWinCoin();
                } else {
                    console.log("JiaZai 组件未找到");
                }
            } else {
                console.log("JiaZai 节点未找到");
            }
            this.check_NewMode();
            setTimeout(() => {
                this.node.parent.parent.getChildByName("Win").active = true;
            }, 660);
            // console.log("游戏成功");
        }
        else {
            if (this.gameOver == true) {
                this.failLevel("time");
            }
        }
    }

    check_NewMode() {
        for (let i = 0; i < cc.fx.GameConfig.NEW_LEVEL.length; i++) {
            if ((cc.fx.GameConfig.GM_INFO.level + 1) == cc.fx.GameConfig.NEW_LEVEL[i].level) {
                this.new_mode = 3;
            }
        }
    }

    winLevel() {
        MiniGameSdk.API.showToast(cc.fx.GameConfig.GM_INFO.level);
        if (this.node.parent.parent.getChildByName("Win").
            getChildByName("nextBtn").getComponent("btnControl")._touch == false) {
            return;
        }
        this.node.parent.parent.getChildByName("Win").
            getChildByName("nextBtn").getComponent("btnControl").setTouch(false);
        cc.fx.AudioManager._instance.playEffect("anniu_Big", null);
        console.log("下一关", cc.fx.GameConfig.GM_INFO.level);
        this.uploadToCloud(cc.fx.GameConfig.GM_INFO.level + 1);
        if (cc.fx.GameConfig.GM_INFO.level > 249) {
            cc.fx.GameConfig.GM_INFO.level = 249;
            MiniGameSdk.API.showToast("每周更新，敬请期待！");
            setTimeout(() => {
                this.node.parent.parent.getChildByName("Win").
                    getChildByName("nextBtn").getComponent("btnControl").setTouch(true);
            }, 500);

        }
        else {
            // console.log("下一关");
            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);
            setTimeout(() => {
                cc.fx.GameConfig.LEVEL_INFO_init(true, 0);
            }, 1200);
        }
    }

    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(event, type) {
        if (this.reviewState == true) {
            return;
        }
        this.reviewState = true;
        cc.fx.AudioManager._instance.playEffect("anniu_Big", null);

        let coin = 900;
        let data = {
            type: type,
            coin: -coin
        }
        if (type == "time") {
            if (cc.fx.GameConfig.GM_INFO.review == 1) coin = 1900;
            else if (cc.fx.GameConfig.GM_INFO.review == 2) coin = 2500;
        }
        else if (type == "boom") {
            if (cc.fx.GameConfig.GM_INFO.reviewBoom == 1) coin = 1900;
            else if (cc.fx.GameConfig.GM_INFO.reviewBoom == 2) coin = 2500;
        }
        data.coin = -coin;
        // console.log("自身金币:",cc.fx.GameConfig.GM_INFO.coin,"消耗金币:",coin);
        if (cc.fx.GameConfig.GM_INFO.coin < Math.abs(coin)) {
            MiniGameSdk.API.showToast("金币不足，无法加时间");
            setTimeout(() => {
                this.openShop();
                this.reviewState = false;
            }, 500);

            return;
        }
        else {
            this.runRewive(data);
        }
    }

    //执行复活函数
    runRewive(data) {
        console.log("复活回调函数内", data);
        cc.fx.GameTool.changeCoin(data.coin);
        const dataTemp = {
            change_reason: "游戏内复活消耗金币",
            id: (1001 + ""),
            num: data.coin
        }
        cc.fx.GameTool.shushu_Track("resource_cost", dataTemp);
        MiniGameSdk.API.showToast("继续游戏");
        this.updateCoin();
        // NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true);
        if (data.type == "time") {
            if (cc.fx.GameConfig.GM_INFO.review < 2)
                cc.fx.GameConfig.GM_INFO.review += 1;
        }
        else if (data.type == "boom") {
            if (cc.fx.GameConfig.GM_INFO.reviewBoom < 2)
                cc.fx.GameConfig.GM_INFO.reviewBoom += 1;
        }


        this.gameOver = false;
        this.gameWin = false;
        this.pause = false;
        if (data.type == "time") {
            this.timeNumber = 21;
            this.add_Time += 20;
        }

        this.blockNum = this.blocks.length;
        if (this.blockNum == 0) this.nextLevel();

        this.node.parent.parent.getChildByName("Lose").active = false;
        setTimeout(() => {
            this.reviewState = false;
        }, 200);
        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 overTime = Date.now();
        this.count_Time = overTime - this.count_Time;
        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();
    }
    uploadToCloud(level: number) {
        if (typeof wx !== 'undefined') {
            //@ts-ignore
            wx.setUserCloudStorage({
                KVDataList: [
                    { key: 'level', value: String(level) }
                ],
                success: () => console.log('✅ 数据上传成功'),
                fail: (err) => console.error('❌ 上传失败', err)
            });
        }
    }
    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) {
        if (this.gameOver == true || this.gameWin == true) {
            return;
        }
        this.stopTimeCutDown();
        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;
            if (type) {
                if (type == "time") {
                    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 == "boom") {
                    this.node.parent.parent.getChildByName("Lose").getChildByName("Time").active = false;
                    this.node.parent.parent.getChildByName("Lose").getChildByName("Boom").active = true;
                    let buyBtn = this.node.parent.parent.getChildByName("Lose").getChildByName("Boom").getChildByName("buyBtn");
                    buyBtn.getChildByName("coin1").active = buyBtn.getChildByName("coin2").active = buyBtn.getChildByName("coin3").active = false;
                    if (cc.fx.GameConfig.GM_INFO.reviewBoom == 0) buyBtn.getChildByName("coin1").active = true;
                    else if (cc.fx.GameConfig.GM_INFO.reviewBoom == 1) buyBtn.getChildByName("coin2").active = true;
                    else if (cc.fx.GameConfig.GM_INFO.reviewBoom == 2) buyBtn.getChildByName("coin3").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 < this.wallArray.length; i++) {
            if (this.wallArray[i].getChildByName("wall").getComponent("Wall").color == color) {
                if (type) {
                    if (type == 5) {
                        if (this.wallArray[i].getChildByName("wall").getComponent("Wall").special == 1) {
                            this.wallArray[i].getChildByName("wall").getComponent("Wall").downDoor();
                        }
                    }
                    else this.wallArray[i].getChildByName("wall").getComponent("Wall").downDoor();
                }
                else this.wallArray[i].getChildByName("wall").getComponent("Wall").downDoor();
            }
        }
    }

    upDoor() {
        for (let i = 0; i < this.wallArray.length; i++) {
            if (this.wallArray[i].getChildByName("wall").opacity == 0) {
                this.wallArray[i].getChildByName("wall").getComponent("Wall").upDoor();
            }
        }
    }

    //开始倒计时
    startTimeCutDown() {
        this.startBoom();
        this.scheduleCallback = function () {
            if (this.pause) return;
            if (this.timeNumber <= 0) {
                this.stopTimeCutDown();
                var timeTemp = cc.fx.GameTool.getTimeMargin(this.timeNumber);
                NumberToImage.getTimeMargin(this.timeNumber, 50, "time_", this.timeLabel)
                // this.timeLabel.string = timeTemp.toString();
                if (!this.pause) this.failLevel("time");
            }
            else {
                this.timeNumber -= 1;
                var timeTemp = cc.fx.GameTool.getTimeMargin(this.timeNumber);
                NumberToImage.getTimeMargin(this.timeNumber, 50, "time_", this.timeLabel)
                // this.timeLabel.string = timeTemp.toString();
            }
        }.bind(this);
        this.schedule(this.scheduleCallback, 1);
    }
    // 停止倒计时
    stopTimeCutDown() {
        if (this.scheduleCallback) {
            this.unschedule(this.scheduleCallback);
        }
    }

    //使用时间道具
    useTimeProp() {
        if (this.node.parent.getChildByName("Ice").active == true || this.hammer == true
            || this.magicMask.active == true || this.timeNumber <= 1 || this.gameOver || this.gameWin) {
            return;
        }
        let freezeBtn = this.node.parent.getChildByName("Bottom").getChildByName("timeBtn");
        if (freezeBtn.getComponent("btnControl")._touch) {
            freezeBtn.getComponent("btnControl").setTouch(false);
            if (cc.fx.GameConfig.GM_INFO.freezeAmount < 1) {
                // if (cc.fx.GameConfig.GM_INFO.coin < 1500) {
                //     MiniGameSdk.API.showToast("金币不足，无法购买道具");
                //     setTimeout(() => {
                //         this.openShop();
                //         freezeBtn.getComponent("btnControl").setTouch(true);
                //     }, 500);

                //     return;
                // }
                this.node.parent.parent.getChildByName("propWindow").active = true;
                this.node.parent.parent.parent.getComponent("SceneManager").openPropBuy("freeze");
            }
            else {
                if (!this.node.parent.getChildByName("Ice").active && !this.pause) {
                    const timestamp = Date.now();
                    this.freezeMask.active = true;
                    this.stopBoom();
                    this.pause = true;
                    this.node.parent.getChildByName("Ice").active = true;
                    // this.node.parent.getChildByName("Top").getChildByName("Ice").active = true;

                    cc.fx.GameConfig.GM_INFO.freezeAmount -= 1;
                    if (cc.fx.GameConfig.GM_INFO.freezeAmount < 0)
                        cc.fx.GameConfig.GM_INFO.freezeAmount = 0;
                    this.setPropNum();
                    // this.iceLabel.string = cc.fx.GameConfig.GM_INFO.freezeAmount.toString();
                    let propInfo = cc.fx.StorageMessage.getStorage("prop");
                    propInfo.freezeAmount = cc.fx.GameConfig.GM_INFO.freezeAmount;
                    propInfo.timestamp = timestamp;
                    cc.fx.StorageMessage.setStorage("prop", propInfo);
                    cc.fx.GameTool.setUserProp(2001, cc.fx.GameConfig.GM_INFO.freezeAmount, (data) => {
                    })
                    let data = {
                        change_reason: "使用道具",
                        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.startBoom();
            // this.startTimeCutDown();
        }
    }

    handleBuySuccess(data) {
        this.node.parent.parent.parent.getComponent("SceneManager").resetBtn();
        this.updateCoin();
        this.pause = false;
        this.startBoom();
        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.setPropNum();
                // 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.setPropNum();
                // this.magicLabel.string = cc.fx.GameConfig.GM_INFO.magicAmount.toString();
            }, 200);

        }
        else if (data == "hammerAmount") {
            cc.fx.GameConfig.GM_INFO.hammerAmount += 1;
            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.setPropNum();
                // this.hammerLabel.string = cc.fx.GameConfig.GM_INFO.hammerAmount.toString();
                MiniGameSdk.API.showToast("购买锤子成功");
            }, 200);

        }



    }

    updateCoin() {
        NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true);
    }

    //使用锤子道具
    useHammer() {
        if (this.node.parent.getChildByName("Ice").active == true || this.hammer == true
            || this.magicMask.active == true || this.timeNumber <= 1 || this.gameOver || this.gameWin) {
            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) {
                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;
                    this.ishammer = true;
                    cc.fx.GameConfig.GM_INFO.hammerAmount -= 1;
                    if (cc.fx.GameConfig.GM_INFO.hammerAmount < 0)
                        cc.fx.GameConfig.GM_INFO.hammerAmount = 0;
                    this.setPropNum();
                    // 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 = {
                        change_reason: "使用道具",
                        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("金币不足，无法购买道具");
            this.node.parent.parent.parent.getComponent("SceneManager").resetBtn();
            setTimeout(() => {
                this.openShop();
            }, 500);
            return;
        }
        cc.fx.GameTool.buyProp(2003, this.handleBuySuccess.bind(this, "magicAmount"));
    }

    buyHammer() {
        if (cc.fx.GameConfig.GM_INFO.coin < 1000) {
            MiniGameSdk.API.showToast("金币不足，无法购买道具");
            this.node.parent.parent.parent.getComponent("SceneManager").resetBtn();
            setTimeout(() => {
                this.openShop();
            }, 500);
            return;
        }
        cc.fx.GameTool.buyProp(2002, this.handleBuySuccess.bind(this, "hammerAmount"));
    }

    buyFreeze() {
        if (cc.fx.GameConfig.GM_INFO.coin < 1500) {
            MiniGameSdk.API.showToast("金币不足，无法购买道具");
            this.node.parent.parent.parent.getComponent("SceneManager").resetBtn();
            setTimeout(() => {
                this.openShop();
            }, 500);
            return;
        }
        cc.fx.GameTool.buyProp(2001, this.handleBuySuccess.bind(this, "freezeAmount"));
    }

    openShop() {
        const winCOIN = cc.find("Canvas"); // 假设 Canvas 节点
        if (winCOIN) {
            const wincoin = winCOIN.getComponent(SceneManager);
            if (wincoin) {
                wincoin.openShop();
            } else {
                console.log("JiaZai 组件未找到");
            }
        } else {
            console.log("JiaZai 节点未找到");
        }
    }

    //使用魔法棒随机消除两个方块
    useMagic() {
        if (this.node.parent.getChildByName("Ice").active == true || this.hammer == true
            || this.magicMask.active == true || this.timeNumber <= 1 || this.gameOver || this.gameWin) {
            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("金币不足，无法购买道具");
                //     setTimeout(() => {
                //         this.openShop();
                //         magicBtn.getComponent("btnControl").setTouch(true);
                //     }, 500);

                //     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);

                // this.magics.active = true;
                // this.magics.getComponent(sp.Skeleton).setAnimation(1, "play", false);
                // //监听播放完后节点隐藏
                // this.magics.getComponent(sp.Skeleton).setCompleteListener((entry) => {
                //     if (entry.animation.name === "play") {
                //         // 动画播放结束后执行的逻辑
                //         this.magics.active = false;
                //     }
                // });

                cc.fx.GameConfig.GM_INFO.magicAmount -= 1;
                if (cc.fx.GameConfig.GM_INFO.magicAmount < 0)
                    cc.fx.GameConfig.GM_INFO.magicAmount = 0;
                this.setPropNum();
                // this.magicLabel.string = cc.fx.GameConfig.GM_INFO.magicAmount.toString();
                let propInfo = cc.fx.StorageMessage.getStorage("prop");
                if (propInfo) {
                    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 = {
                        change_reason: "使用道具",
                        id: "2003",
                        num: -1
                    }
                    cc.fx.GameTool.shushu_Track("resource_cost", data);
                }

                let nomalArray = [];
                // this.magicBtn.node.active = false;
                for (let i = 0; i < this.blocks.length; i++) {
                    // console.log("方块类型",this.blocks[i].getComponent("Block").type);
                    if (this.blocks[i].getComponent("Block").type == 3) {
                        this.loackArray.push(this.blocks[i]);
                    }
                    else if (this.blocks[i].getComponent("Block").type == 4) {
                        this.freezeArray.push(this.blocks[i]);
                    }
                    else if (this.blocks[i].getComponent("Block").type == 10) {
                        if (this.blocks[i].getComponent("Block").block_Info.node) {
                            nomalArray.push(this.blocks[i].getComponent("Block").block_Info.node);
                        }
                        // else nomalArray.push(this.blocks[i]);
                    }
                    else {
                        nomalArray.push(this.blocks[i]);
                    }
                }
                if (nomalArray.length > 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;
                    }
                    nomalArray[1].getComponent("Block").eliminate();
                    // 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 < this.loackArray.length; i++) {
                //             // console.log("没有普通快，魔法消除一个带锁块");
                //             this.loackArray[i].getComponent("Block").eliminate();
                //             count -= 1;
                //             if (count == 0) {
                //                 break;
                //             }
                //         }
                //     }

                //     setTimeout(() => {
                //         if (count != 0) {
                //             for (let i = 0; i < this.freezeArray.length; i++) {
                //                 // console.log("没有普通快，魔法消除一个冻结块");
                //                 // this.freezeArray[i].getComponent("Block").eliminate();
                //                 count -= 1;
                //                 if (count == 0) {
                //                     break;
                //                 }
                //             }
                //         }
                //     }, 100);
                // }
            }
        }
    }

    //按下暂停按钮
    usePause() {
        if (this.pause) {
            this.pause = false;
            this.startBoom();
        }
        else {
            this.pause = true;
            this.stopBoom();
        }
    }



    //根据关卡设置地图大小
    setMapInfo() {
        let width = 0;
        if (this.mapWidth < 10) {
            width = 0.2;
        }
        else if (this.mapWidth < 17) {
            width = 0.15 - (this.mapWidth - 11) * 0.01;
        }

        this.node.scale = 1.6 + (6 - this.mapWidth) * width;

        if (this.mapWidth == 8 && this.mapHeight > 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 < this.blocks.length; i++) {
            if (this.blocks[i].opacity == 0) {
                this.blocks[i].destroy();
                this.blocks.splice(i, 1);
            }
        }
    }

    startBoom() {
        for (let i = 0; i < this.blocks.length; i++) {
            if (this.blocks[i].getComponent("Block").type == 6) {
                this.blocks[i].getChildByName("boom").getComponent("Boom").startBoom();
            }
        }
    }

    stopBoom() {
        for (let i = 0; i < this.blocks.length; i++) {
            if (this.blocks[i].getComponent("Block").type == 6) {
                this.blocks[i].getChildByName("boom").getComponent("Boom").stopBoom();
            }
        }
    }



    update(dt) {

    }
}
