// 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 MapConroler from "./Map";
import NumberToImage from "./NumberToImage";

const { ccclass, property } = cc._decorator;

export enum WallSpecial {
    /*普通门*/
    "普通门" = 0,
    /*星星门*/
    "星星门" = 1,
    /*开关门*/
    "开关门" = 2,
    /*冻结门 */
    "冻结门" = 3,
    /*冻结门 */
    "旋转门顺时针" = 4,
    /*旋转门逆时针*/
    "旋转门逆时针" = 5,
    /*上锁门*/
    "上锁门" = 6,

}

export enum WallType {

    /*普通地块 */
    "门横向下" = 0,
    /*起点地块 */
    "门横向上" = 1,
    /*湿地 */
    "门竖向右" = 2,
    /*山峰 */
    "门竖向左" = 3,
    /*终点地块 */
    "墙横向下" = 4,
    /*息壤 */
    "墙横向上" = 5,
    /*加固 */
    "墙竖向右" = 6,
    /*加固 */
    "墙竖向左" = 7,
}

export enum WallColor {
    /*普通地块 */
    "紫色" = 0,
    /*湿地 */
    "黄色" = 1,
    /*山峰 */
    "绿色" = 2,
    /*终点地块 */
    "蓝色" = 3,
    /*息壤 */
    "粉色" = 4,
    /*加固 */
    "橘黄色" = 5,
    /*加固 */
    "青色" = 6,
    /*加固 */
    "白色" = 7,
    /*普通地块 */
    "红色" = 8,
    /*普通地块 */
    "灰色" = 9,
    "暗" = 10,
    "默认色" = 11,
}

@ccclass
export default class Wall extends cc.Component {

    @property(cc.Label)
    number: cc.Label = null;

    @property({
        tooltip: '墙或者门的方向',
        type: cc.Enum(WallType),
    })
    type: WallType = WallType.墙横向下;

    @property({
        tooltip: '墙或者门的方向',
        type: cc.Enum(WallSpecial),
    })
    special: WallSpecial = WallSpecial.普通门;

    @property({
        tooltip: '门的颜色',
        type: cc.Enum(WallColor),
    })
    color: WallColor = WallColor.紫色;

    @property(cc.Node)
    maskNode: cc.Node = null; // 遮罩节点

    @property(cc.Float)
    maskWidth: number = 138; // 遮罩宽度

    @property(cc.Float)
    maskHeight: number = 69; // 遮罩高度

    @property(cc.SpriteAtlas)
    wall_SpriteFrames: cc.SpriteAtlas = null;

    @property(cc.SpriteAtlas)
    down_SpriteFrames: cc.SpriteAtlas = null;

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


    posX: number;
    posY: number;
    direction: any;
    wall_Info: any;
    teamDoors: any;
    openNode: cc.Node;
    revolvingNode: cc.Node;
    freezeNode: cc.Node;
    open: boolean;
    freezeNumber: number;
    num: number;
    leftTween: any;
    rightTween: any;
    colorArray: any;
    colorStartArray: any;
    colorLength: number = 0;    // 颜色数组的长度
    longAndShort: number[] = []; // 长短变化的门
    order: boolean = true; // 顺序门
    length: number = 0; // 门的长度
    specialBackup: any; // 特殊门的备份，但是不备份 旋转门

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        // console.log(this.type);
    }

    jsonDeepClone<T>(obj: T): T {
        return JSON.parse(JSON.stringify(obj));
    }

    init(wall_Info, posX: number, posY: number, direction: any) {
        this.colorArray = [];
        this.colorStartArray = [];
        this.colorLength = 0;
        this.wall_Info = this.jsonDeepClone(wall_Info);

        // this.open = true;
        if (wall_Info == null) {
            this.posX = posX;
            this.posY = posY;

            if (direction) this.direction = direction;
            // console.log("门方向赋值",direction);
            if (direction == "up") {
                this.node.parent.zIndex = 100 + this.posX - this.posY * 3;
            }
            else if (direction == "down" || direction == "right") {
                this.node.parent.zIndex = 20 + this.posX - this.posY * 3;
            }
            else if (direction == "downleft" || direction == "downright" || direction == "rightup"
                || direction == "rightdown") {
                this.node.parent.zIndex = 20 + this.posX - this.posY * 3;
            }
            else if (direction == "left" || direction == "leftup" || direction == "upleft") {
                this.node.parent.zIndex = 70 + this.posX - this.posY * 3;
            }
            else if (direction == "leftdown" || direction == "upright") {
                this.node.parent.zIndex = 100 + this.posX - this.posY * 3;
            }
            else this.node.parent.zIndex = 70 + this.posX - this.posY * 3;

            MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id = "Wall";
            //console.log(this.posX,this.posY,MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id);
            // console.log(this.posX,this.posY,this.node.zIndex);
            //this.node.getChildByName("num").getComponent(cc.Label).string = direction;
            //this.node.getChildByName("num").getComponent(cc.Label).string = ":" + this.node.parent.zIndex;
        }
        if (wall_Info != null) {
            this.specialBackup = 0;
            this.color = this.wall_Info.color;
            this.special = this.wall_Info.special;
            if (this.special != WallSpecial.旋转门顺时针 && this.special != WallSpecial.旋转门逆时针) {
                this.specialBackup = this.special;
            }
            else {
                this.special = this.wall_Info.special;
            }
            this.num = this.wall_Info.num;
            this.length = this.wall_Info.length;
            let name2 = "rotate" + this.wall_Info.length;
            this.revolvingNode = this.node.parent.getChildByName("revolving").getChildByName(name2);
            if (this.wall_Info.colorArray) {
                this.colorArray = this.wall_Info.colorArray.split('').map(char => parseInt(char) + 1);
                this.colorStartArray = this.wall_Info.colorArray.split('').map(char => parseInt(char) + 1);
                this.colorLength = this.colorStartArray.length;
                MapConroler._instance.changeColor = true;
                MapConroler._instance.colorDoors.push(this);
            }
            if (this.revolvingNode) {
                this.revolvingNode.parent.zIndex = 999;
                this.revolvingNode.active = false;
            }
            this.initType();
            if (this.wall_Info.length > 0) {
                if (!this.wall_Info.longAndShort) this.initColor(this.wall_Info.length);
                MapConroler._instance.wall_Pass.push(this);
            }
            else {
                this.node.getComponent(cc.Sprite).spriteFrame = null;
            }
            if (this.posX != null) {
                MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id = "Wall";
                //console.log(this.posX,this.posY,MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id);
            }

            // 长短变化的门
            if (this.wall_Info.longAndShort) {
                this.longAndShort = this.wall_Info.longAndShort.toString().split('').map(Number);
                this.order = this.wall_Info.order;
            }

        }
        // setTimeout(() => {
        //     this.node.getChildByName("num").getComponent(cc.Label).string = ":" + this.node.parent.zIndex;
        // }, 1000);

    }

    setTeamDoors(over) {
        if (this.wall_Info.length > 0) {
            this.setLongAndShort();
            MapConroler._instance.teamDoors = [];
        }
        MapConroler._instance.teamDoors.push(this);
        this.teamDoors = MapConroler._instance.teamDoors;
        if (over == true) {
            this.setLongAndShort();
        }
    }

    //创建门的颜色
    initColor(length: number) {
        let direction = this.node.parent.name;
        let double = 0;
        if (direction == "left" || direction == "right") {
            double = 3;
        }

        if (this.wall_SpriteFrames) {
            let name = this.color + "color" + (length + double);
            var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
            if (this.node.getComponent(cc.Sprite)) this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            if (this.node.getChildByName("icon")) this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
            // if (this.node.getChildByName("length")) this.node.parent.getChildByName("length").getComponent(cc.Sprite).spriteFrame = spriteFrame;
        }
        if (this.down_SpriteFrames) {
            let name2 = this.color + "down" + (length + double);
            var downFrame = this.down_SpriteFrames._spriteFrames[name2];
            this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = downFrame;
        }
    }

    //创建特殊类型门
    initType() {

        switch (this.special) {
            case WallSpecial.星星门:
                let star = cc.instantiate(MapConroler._instance.Block_Prop[this.special]);
                star.parent = this.node.parent;
                // console.log("门的方向",this.direction,"长度",this.wall_Info.length);
                // star.scaleX = star.scaleY = 0.5;
                if (this.wall_Info.length > 0) {
                    if (this.direction == "right" || this.direction == "left") {
                        star.children[this.wall_Info.length + 2].active = true;
                    }
                    else if (this.direction == "up" || this.direction == "down") {
                        star.children[this.wall_Info.length - 1].active = true;
                    }
                }
                star.setPosition(this.node.width / 2 + this.node.x, this.node.height / 2 + this.node.y);
                break;
            case WallSpecial.开关门:
                let name = "open" + this.wall_Info.length;
                this.openNode = this.node.parent.getChildByName("open").getChildByName(name);
                this.openNode.active = true;
                if (this.wall_Info.lock == false) {
                    this.open = true;
                    this.openNode.children[0].scaleX *= 0.01;
                    this.openNode.children[1].scaleX *= 0.01;
                }
                else {
                    this.open = false;
                }
                break;
            case WallSpecial.冻结门:
                let freeze = "freeze" + this.wall_Info.length;
                this.freezeNode = this.node.parent.getChildByName("freeze").getChildByName(freeze);
                this.freezeNode.active = true;

                if (this.wall_Info.freeze) {
                    this.freezeNumber = this.wall_Info.freeze;
                    NumberToImage.numberToImageNodes(this.freezeNumber, 20, 8, "lock_", this.freezeNode.getChildByName("num"), false);
                }
                break;
            case WallSpecial.旋转门顺时针: case WallSpecial.旋转门逆时针:
                if (this.wall_Info.length > 0) {
                    // let name2 = "rotate" + this.wall_Info.length;
                    // this.revolvingNode = this.node.parent.getChildByName("revolving").getChildByName(name2);
                    this.revolvingNode.active = true;
                    // if (this.special == WallSpecial.旋转门逆时针) {
                    //     this.revolvingNode.children[0].scaleX = -this.revolvingNode.children[0].scaleX;
                    // }
                }
                break;
            case WallSpecial.上锁门:
                let lock = cc.instantiate(MapConroler._instance.Block_Prop[3]);
                lock.parent = this.node.parent;
                lock.color = cc.color(251, 158, 7, 255);
                if (this.wall_Info.length == 0) {
                    lock.opacity = 0;
                }
                lock.scaleX = lock.scaleY = 0.65;
                if (this.direction == "up") {
                    lock.setPosition((this.wall_Info.length - 1) * 60 - 10, 38);
                    if (this.wall_Info.length == 1) lock.x += 5;
                }
                else if (this.direction == "down") {
                    lock.setPosition((this.wall_Info.length - 1) * 60 - 10, -38);
                    if (this.wall_Info.length == 1) lock.x += 5;
                }
                if (this.direction == "right") {
                    lock.setPosition(30, (this.wall_Info.length - 1) * 60);
                    if (this.wall_Info.length == 1) lock.y += 10;
                }
                else if (this.direction == "left") {
                    lock.setPosition(-46, (this.wall_Info.length - 1) * 60);
                    if (this.wall_Info.length == 1) lock.y += 10;
                }
                lock.getComponent("Lock").init(this.wall_Info.lockTime, "wall", null);
                break;
        }
        if (this.wall_Info.colorArray) {
            if (this.wall_Info.length > 0) {
                this.maskNode.active = true;
                this.setMask();
            }
        }

    }

    //播放星星门通过
    playStarDoor() {
        if (this.node.parent.getChildByName("star")) {
            let star = this.node.parent.getChildByName("star");
            for (let i = 0; i < star.children.length; i++) {
                if (star.children[i].active == true) {
                    let starChild = star.children[i];
                    for (let j = 0; j < starChild.children.length; j++) {
                        starChild.children[j].active = true
                        starChild.children[j].getComponent(sp.Skeleton).setAnimation(1, "taopao1", false);
                    }
                }
            }
        }
    }


    //改变开关门状态
    changeLock() {
        this.open = !this.open;
        // console.log("开关门状态改变", this.open);
        if (!this.openNode.active) {
            this.openNode.active = true;
        }


        // 停止之前的动画
        if (this.leftTween) {
            this.leftTween.stop();
            this.leftTween = null;
        }
        if (this.rightTween) {
            this.rightTween.stop();
            this.rightTween = null;
        }
        if (this.open == true) {
            if (this.openNode.children[0].scaleX < 1 && this.openNode.children[0].scaleX > 0.01) {
                this.openNode.children[0].scaleX = 1;
            }
        }
        else {
            if (this.openNode.children[0].scaleX < 1 && this.openNode.children[0].scaleX > 0.01) {
                this.openNode.children[0].scaleX = 0.01;
            }
        }


        let fill = this.openNode.children[0].scaleX == 1 ? 0.01 : 1;
        if (this.openNode.children[0].scaleX < 0) fill = -fill;
        // console.log("目标",fill);

        // 存储动画实例以便后续停止
        this.leftTween = cc.tween(this.openNode.children[0])
            .to(0.3, { scaleX: this.openNode.children[0].scaleX < 0 ? -fill : fill })
            .call(() => {
                // console.log("左边完成");
                this.leftTween = null; // 动画完成后清除引用
            })
            .start();

        this.rightTween = cc.tween(this.openNode.children[1])
            .to(0.3, { scaleX: this.openNode.children[1].scaleX < 0 ? -fill : fill })
            .call(() => {
                // console.log("右边完成");
                this.rightTween = null; // 动画完成后清除引用
            })
            .start();
    }

    changeFreeze() {
        this.freezeNumber -= 1;
        if (this.freezeNumber == 0) {
            // console.log(this.node.uuid);
            // this.freezeNode.parent.active = false;
            this.resetFreeze();
        }
        else {
            NumberToImage.numberToImageNodes(this.freezeNumber, 20, 8, "lock_", this.freezeNode.getChildByName("num"), false);
        }
    }

    downDoor() {
        if (this.openNode) {
            if (this.openNode.children[0].scaleX == 1)
                return;
        }
        if (this.freezeNode) {
            if (this.freezeNode.active == true)
                return;
        }
        if (this.special == 4 || this.special == 5 || this.special == 6) {
            return;
        }

        if (this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame == null)
            return;

        this.node.opacity = 0;
        if (this.special == WallSpecial.星星门) {
            this.node.parent.getChildByName("star").y -= 10;
        }
        this.node.parent.getChildByName("down").active = true;
        if (this.colorArray.length > 0) {
            let direction = this.node.parent.name;
            let double = 0;
            if (direction == "left" || direction == "right") {
                double = 3;
            }
            let color = this.colorArray[1];

            if (this.down_SpriteFrames) {
                let name = color + "down" + (this.length + double);
                var spriteFrame = this.down_SpriteFrames._spriteFrames[name];
                this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
            }
            this.maskNode.getChildByName("color_down").opacity = 255;
            this.maskNode.getChildByName("color_icon").opacity = 0;
        }
    }

    upDoor() {
        if (this.special == WallSpecial.星星门) {
            this.node.parent.getChildByName("star").y += 10;
        }
        this.node.parent.getChildByName("down").active = false;
        if (this.colorArray.length > 0) {
            this.maskNode.getChildByName("color_down").opacity = 0;
            this.maskNode.getChildByName("color_icon").opacity = 255;
            let direction = this.node.parent.name;
            let double = 0;
            if (direction == "left" || direction == "right") {
                double = 3;
            }
            if (this.colorArray.length > 1) {
                let color = this.colorArray[1];
                if (this.wall_SpriteFrames) {
                    let name = color + "color" + (this.length + double);
                    var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
                    this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            }
            else {
                this.maskNode.getChildByName("color_icon").opacity = 0;
            }
        }

        this.node.opacity = 250;
    }

    resetFreeze() {
        this.specialBackup = 0;
        if (this.special == WallSpecial.冻结门) {
            this.special = 0;
        }
        if (this.freezeNode) {
            cc.fx.AudioManager._instance.playEffect("freezeDoor", null);
            this.freezeNode.active = false;
        }
        // if (this.wall_Info.length != 0) {
        //     let spine = cc.instantiate(this.freezeSpine);
        //     spine.parent = this.node.parent;
        //     spine.setPosition(this.node.width / 2 + this.node.x + 60 / 2 * (this.wall_Info.length - 1), this.node.height / 2 + this.node.y);
        //     spine.getComponent(sp.Skeleton).setAnimation(1, "bingkuai", false);
        //     console.log("添加一个动画", spine.getPosition());
        // }
    }

    //旋转门旋转
    changeRevolvingWall() {
        if (this.revolvingNode) {
            // console.log("自身编号", this.wall_Info.num, this.special);
            if (this.special == WallSpecial.旋转门顺时针 || this.special == WallSpecial.旋转门逆时针) {
                this.revolvingNode.active = true;
            }
            else {
                this.revolvingNode.active = false;
            }
        }
    }

    //变色门变色总方法
    changeColorWall() {
        // console.log("通过此门，调用次门数组", this.colorArray);
        // console.log("改變顏色：", this.wall_Info.length);
        for (let i = 0; i < this.teamDoors.length; i++) {
            setTimeout(() => {
                this.teamDoors[i].changeColor();
            }, i * 10);
        }
    }

    //变色门变色具体执行方法
    changeColor() {
        // console.log("变色门变色数组数量", this.colorArray, this.node.uuid);
        if (this.colorArray.length >= 1 && this.color == this.colorArray[0]) {
            let firstItem = this.colorArray.shift(); // 移除第一项
            // console.log("移除颜色:", this.color);
            // this.colorArray.push(firstItem); // 将第一项添加到数组末尾
            if (this.colorArray.length <= 2) {
                for (let i = 0; i < this.colorStartArray.length; i++) {
                    this.colorArray.push(this.colorStartArray[i]);
                    // console.log("将颜色放入数组:", this.colorStartArray[i])
                }
            }
        }
        // console.log("变色门变色数组数量", this.colorArray, this.node.uuid);
        this.updateColor();
    }

    changeDeleteColor(color) {
        for (let i = 0; i < this.teamDoors.length; i++) {
            setTimeout(() => {
                this.teamDoors[i].tempDeleteColor(color);
            }, i * 10);
        }
    }

    tempDeleteColor(color) {
        if (this.colorArray.length > 1) {
            let index = this.colorArray.indexOf(color);
            if (index !== -1) {
                // console.log("删除颜色:", color, this.colorArray);
                this.colorArray.splice(index, 1);
                if (this.colorArray.length <= 2) {
                    for (let i = 0; i < this.colorStartArray.length; i++) {
                        this.colorArray.push(this.colorStartArray[i]);
                    }
                }
                this.updateColor();
            }
        }

    }

    //更新变色门颜色
    updateColor() {
        this.color = this.colorArray[0];
        // console.log("改變顏色", this.color);
        let direction = this.node.parent.name;
        let double = 0;
        if (direction == "left" || direction == "right") {
            double = 3;
        }
        if (this.wall_Info.length > 0) {
            this.upDoor();
            this.setMask();

            if (this.wall_SpriteFrames) {
                let name = this.color + "color" + (this.wall_Info.length + double);
                // console.log("变色名称:", name);
                var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
                if (this.node.getComponent(cc.Sprite)) this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                if (this.node.getChildByName("icon")) this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
                this.node.getComponent(cc.Sprite).fillRange = 0;
                cc.tween(this.node.getComponent(cc.Sprite))
                    .to(0.2, { fillRange: 1 })
                    .start();

            }
            if (this.down_SpriteFrames) {
                let name2 = this.color + "down" + (this.wall_Info.length + double);
                // console.log("按下名称:", name2);
                var downFrame = this.down_SpriteFrames._spriteFrames[name2];
                this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = downFrame;
                this.node.parent.getChildByName("down").getComponent(cc.Sprite).fillRange = 0;
                cc.tween(this.node.parent.getChildByName("down").getComponent(cc.Sprite))
                    .to(0.2, { fillRange: 1 })
                    .start();
            }
        }
    }

    //变色门删除颜色, 第一个参数为颜色，第二个参数为是否立刻刷新颜色， 第三个参数为是否删除起始颜色
    removeColor(color, type) {
        // console.log("整体删除颜色");
        if (this.colorStartArray.length > 1) {
            let index = this.colorStartArray.indexOf(color);
            if (index !== -1) {
                this.colorStartArray.splice(index, 1);
            }
        }
        if (this.colorArray.length > 1) {
            for (let j = 0; j < this.colorArray.length; j++) {
                if (this.colorArray[j] == color) {
                    this.colorArray.splice(j, 1);
                    // console.log("删除方法中，该墙体有该颜色", j)
                    j--;
                }
            }
            if (this.colorArray.length <= 2) {
                for (let i = 0; i < this.colorStartArray.length; i++) {
                    this.colorArray.push(this.colorStartArray[i]);
                    // console.log("将颜色放入数组:", this.colorStartArray[i])
                }
            }
            if (type == true) this.updateColor();
        }
        // console.log(this.wall_Info.length, "删除颜色:", color, "删除后数组:", this.colorArray, "删除起始颜色:", this.colorStartArray);
    }

    //添加变色门遮罩
    setMask() {
        let direction = this.node.parent.name;
        let double = 0;
        if (direction == "left" || direction == "right") {
            double = 3;
        }
        if (this.colorArray.length > 1) {
            let color = this.colorArray[1];
            if (this.wall_SpriteFrames) {
                let name = color + "color" + (this.wall_Info.length + double);
                var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
                this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
                this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).fillRange = 0;
                cc.tween(this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite))
                    .to(0.2, { fillRange: 1 })
                    .start();
            }
            if (this.down_SpriteFrames) {
                let name2 = color + "down" + (this.wall_Info.length + double);
                var downFrame = this.down_SpriteFrames._spriteFrames[name2];
                this.maskNode.getChildByName("color_down").getComponent(cc.Sprite).spriteFrame = downFrame;
                this.maskNode.getChildByName("color_down").getComponent(cc.Sprite).fillRange = 0;
                cc.tween(this.maskNode.getChildByName("color_down").getComponent(cc.Sprite))
                    .to(0.2, { fillRange: 1 })
                    .start();
            }
        }
        else {
            this.maskNode.getChildByName("color_icon").opacity = 0;
        }
        // 设置遮罩节点大小和位置
        if (this.node.parent.name == "down") {
            if (this.wall_Info.length == 3) {
                this.maskNode.width = 290;
                this.maskNode.height = 69;
                this.maskNode.position = cc.v3(116, -39, 0); // 居中
            }
            else if (this.wall_Info.length == 2) {
                this.maskNode.width = 178;
                this.maskNode.height = 69;
                this.maskNode.position = cc.v3(54, -39, 0); // 居中
            }
            else if (this.wall_Info.length == 1) {
                this.maskNode.width = 70;
                this.maskNode.height = 69;
                this.maskNode.position = cc.v3(-7, -39, 0); // 居中
            }
        }
        else if (this.node.parent.name == "up") {
            if (this.wall_Info.length == 3) {
                this.maskNode.width = 290;
                this.maskNode.height = 69;
                this.maskNode.position = cc.v3(116, 34, 0); // 居中
            }
            else if (this.wall_Info.length == 2) {
                this.maskNode.width = 178;
                this.maskNode.height = 69;
                this.maskNode.position = cc.v3(56, 34, 0); // 居中
            }
            else if (this.wall_Info.length == 1) {
                this.maskNode.width = 70;
                this.maskNode.height = 69;
                this.maskNode.position = cc.v3(-2.44, 34, 0); // 居中
            }
        }
        else if (this.node.parent.name == "right") {
            if (this.wall_Info.length == 3) {
                this.maskNode.width = 69;
                this.maskNode.height = 290;
                this.maskNode.position = cc.v3(37, 120, 0); // 居中
            }
            else if (this.wall_Info.length == 2) {
                this.maskNode.width = 69;
                this.maskNode.height = 178;
                this.maskNode.position = cc.v3(37, 60, 0); // 居中
            }
            else if (this.wall_Info.length == 1) {
                this.maskNode.width = 69;
                this.maskNode.height = 70;
                this.maskNode.position = cc.v3(37, 0, 0); // 居中
                this.maskNode.anchorY = 0.35;
            }
        }
        else if (this.node.parent.name == "left") {
            if (this.wall_Info.length == 3) {
                this.maskNode.width = 69;
                this.maskNode.height = 290;
                this.maskNode.position = cc.v3(-39.5, 120, 0); // 居中
            }
            else if (this.wall_Info.length == 2) {
                this.maskNode.width = 69;
                this.maskNode.height = 178;
                this.maskNode.position = cc.v3(-39.5, 60, 0); // 居中
            }
            else if (this.wall_Info.length == 1) {
                this.maskNode.width = 69;
                this.maskNode.height = 70;
                this.maskNode.position = cc.v3(-39.5, 0, 0); // 居中
                this.maskNode.anchorY = 0.35;
            }
        }
    }

    //初始化的时候，处理一组门的长短变化状态
    setLongAndShort() {
        //数组为空返回
        if (MapConroler._instance.teamDoors.length <= 0) {
            return;
        }
        //上一个墙壁数组并不是长短门返回
        if (!MapConroler._instance.teamDoors[0].wall_Info.longAndShort) {
            return;
        }
        let start = MapConroler._instance.teamDoors[0];
        let over = MapConroler._instance.teamDoors[0].length != 3 ? MapConroler._instance.teamDoors[1] : MapConroler._instance.teamDoors[2];
        let end = null;
        if (MapConroler._instance.teamDoors[0].length == 3 && MapConroler._instance.teamDoors[0].longAndShort[0] == 1) {
            end = MapConroler._instance.teamDoors[1];
        }
        if (MapConroler._instance.teamDoors[0].longAndShort[0] == MapConroler._instance.teamDoors[0].wall_Info.length) {
            over = null;
            end = null;
        }
        if (MapConroler._instance.teamDoors[0].wall_Info.longAndShort[0] == 1 && MapConroler._instance.teamDoors[0].order == true) {
            var temp = this.wall_Info;
        }
        if (MapConroler._instance.teamDoors[0].order == false) {
            //上来是开满的
            if (MapConroler._instance.teamDoors[0].longAndShort[0] > MapConroler._instance.teamDoors[0].longAndShort[1]) {
                // console.log("是全开的", this.wall_Info.num, MapConroler._instance.teamDoors[0].longAndShort);
                start = MapConroler._instance.teamDoors[0];
                over = null;
                end = null;
            } else {    //上来是缺口的
                // console.log("是缺口的", this.wall_Info.num, MapConroler._instance.teamDoors[0].longAndShort);
                start = MapConroler._instance.teamDoors[1];
                over = MapConroler._instance.teamDoors[0];
                if (MapConroler._instance.teamDoors.length == 3) {
                    if (MapConroler._instance.teamDoors[0].longAndShort[0] == 2) {
                        start = MapConroler._instance.teamDoors[1];
                        over = MapConroler._instance.teamDoors[0];
                    }
                    else if (MapConroler._instance.teamDoors[0].longAndShort[0] == 1) {
                        start = MapConroler._instance.teamDoors[2];
                        over = MapConroler._instance.teamDoors[1];
                        end = MapConroler._instance.teamDoors[0];

                    }
                }
            }
        }
        // console.log("数组长度：", MapConroler._instance.teamDoors.length);
        start.runLongAndShort(0);
        if (end == null) {
            if (over) over.runLongAndShort(2);
        }
        else {
            if (over) over.runLongAndShort(1);
            if (end) end.runLongAndShort(3);

        }

        //下一帧执行
    }
    //长短门改变变化
    runLongAndShort(type) {
        // console.log(this.node.parent.name);
        //顺向的， 在第一个。
        // console.log("执行:", this.longAndShort[0], this.longAndShort[1]);
        this.length = this.longAndShort[0];
        if (type == 0) {
            // this.length = this.longAndShort[0];
            // console.log(this.wall_Info.num, "实际长度:", this.length, "本身长度:", this.wall_Info.length);
            if (this.wall_Info.length == this.length) {
                if (this.teamDoors[1].node.getComponent(cc.Sprite)) this.teamDoors[1].node.getComponent(cc.Sprite).spriteFrame = null;
                if (this.teamDoors[1].node.getChildByName("icon")) this.teamDoors[1].node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = null;
                this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = null;
            }
            this.initColor(this.longAndShort[0]);

        }
        else if (type == 1) {
            // console.log(this.wall_Info.num, "隐身", "实际长度:", this.length, "本身长度:", this.wall_Info.length);
            this.node.getComponent(cc.Sprite).spriteFrame = null;
            this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = null;
            this.node.parent.getChildByName("length").active = true;
        }
        else if (type == 2 || type == 3) {
            // console.log(this.wall_Info.num, "隐身", "实际长度:", this.length, "本身长度:", this.wall_Info.length);
            this.node.getComponent(cc.Sprite).spriteFrame = null;
            let name = "";
            if (this.node.parent.name == "down" || this.node.parent.name == "up") name = "heng";
            else name = "shu";
            name = name + (2 * (type - 1));

            if (this.node.parent.name == "down" && this.teamDoors[0].order == false) {
                if (name == "heng2") name = "heng1";
                else if (name == "heng4") name = "heng3";
            }
            else if (this.node.parent.name == "up" && this.teamDoors[0].order == false) {
                if (name == "heng2") name = "heng1";
                else if (name == "heng4") name = "heng3";
            }
            else if (this.node.parent.name == "left" && this.teamDoors[0].order == true) {
                if (name == "shu2") name = "shu1";
                else if (name == "shu4") name = "shu3";
            }
            else if (this.node.parent.name == "right" && this.teamDoors[0].order == true) {
                if (name == "shu2") name = "shu1";
                else if (name == "shu4") name = "shu3";
            }

            // console.log("墻體名字:", name, this.node.name, this.node.parent.name, this.teamDoors[0].order);

            var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
            if (this.node.getComponent(cc.Sprite)) this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            // this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = spriteFrame;
            // if (this.node.getChildByName("icon")) this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;

            this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = null;
            this.node.parent.getChildByName("length").active = true;
        }
        if (type == 0) {
            setTimeout(() => {
                for (let k = 0; k < this.teamDoors.length; k++) {
                    this.teamDoors[k].longAndShort.reverse();
                    // console.log("第" + k + "个门", this.teamDoors[k].longAndShort[0], this.teamDoors[k].longAndShort[1]);
                }
            }, 1);
        }


    }

    //改变长短门变化状态
    changeLongAndShort() {
        for (let i = 0; i < this.teamDoors.length; i++) {
            this.teamDoors[i].node.parent.getChildByName("length").active = false;
        }
        let start = this.teamDoors[0];

        let over = this.teamDoors.length != 3 ? this.teamDoors[1] : this.teamDoors[2];
        let end = null;
        if (this.teamDoors[0].order == true) {
            if (this.teamDoors[0].length == 3 && this.teamDoors[0].longAndShort[0] == 1) {
                end = this.teamDoors[1];
            }
            if (this.teamDoors[0].length == 2 && this.teamDoors[0].longAndShort[0] == 1) {
                // end = this.teamDoors[1];
                end = null;
            }
            if (this.teamDoors[0].longAndShort[0] == this.teamDoors[0].wall_Info.length) {
                over = null;
                end = null;
            }
        }
        else {
            end = null;
            //上来是开满的
            if (this.teamDoors[this.teamDoors.length - 1].longAndShort[0] > this.teamDoors[this.teamDoors.length - 1].longAndShort[1]) {
                // console.log("是全开的", this.wall_Info.num, MapConroler._instance.teamDoors[0].longAndShort);
                start = this.teamDoors[0];
                this.teamDoors[1].node.getComponent(cc.Sprite).spriteFrame = null;
                this.teamDoors[1].node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = null;
                if (this.teamDoors.length > 2) {
                    this.teamDoors[2].node.getComponent(cc.Sprite).spriteFrame = null;
                    this.teamDoors[2].node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = null;
                }
                over = null;
                end = null;
            } else {    //上来是缺口的
                var temp1 = this.teamDoors[this.teamDoors.length - 1].longAndShort[0];
                var temp2 = this.teamDoors[this.teamDoors.length - 1].longAndShort[1];
                // console.log("是缺口的", this.wall_Info.num, temp1, temp2);
                start = this.teamDoors[1];
                over = this.teamDoors[0];
                if (this.teamDoors.length == 3) {
                    if (this.teamDoors[0].longAndShort[0] == 2) {
                        start = this.teamDoors[1];
                        over = this.teamDoors[0];
                    }
                    else if (this.teamDoors[0].longAndShort[0] == 1) {
                        start = this.teamDoors[2];
                        over = this.teamDoors[1];
                        end = this.teamDoors[0];
                    }
                }
            }
        }
        // console.log("数组长度：", MapConroler._instance.teamDoors.length);

        start.runLongAndShort(0);
        if (end == null) {
            if (over) over.runLongAndShort(2);
        }
        else {
            if (over) over.runLongAndShort(1);
            if (end) end.runLongAndShort(3);

        }
    }


    // update (dt) {}
}
