import CollisionDetection from "./CollisionDetection";
import MapConroler from "./Map";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Block extends cc.Component {

    // 新增缓存变量
    private selfBoxColliders: cc.BoxCollider[] = [];
    private allBoxColliders: cc.BoxCollider[] = [];
    // @property({
    //     tooltip: '碰撞形状，None就是无敌，不参与碰撞',
    //     type: cc.Enum(BlockType),
    //     // default: BlockType.Nomal,
    //     displayName: '碰撞形状'
    // })




    // LIFE-CYCLE CALLBACKS:
    // @property(cc.SpriteAtlas)
    // UI: cc.SpriteAtlas = null;
    private initialTouchOffset: cc.Vec2 = null;
    private offsetTolerance = 100; // 偏移容忍度; 

    allBlocks: any;       //所有的方块,用于计算posX,posY消除
    touchPoint: cc.Vec2 = null;     //触摸点
    isTouch: boolean = false;       //是否触摸
    posX: number = 0;               //地图块的X坐标
    posY: number = 0;               //地图块的Y坐标
    moveLeft: boolean = true;      //是否可以左移;
    moveRight: boolean = true;     //是否可以右移;
    moveUp: boolean = true;        //是否可以上移;
    moveDown: boolean = true;      //是否可以下移;
    moveCorner: number = 0;        //是否碰撞角落
    moveY: number = 0;          //是否可以上下移动;
    moveX: number = 0;          //是否可以左右移动;
    touchPointX: number = 0;        //触摸点X坐标;
    touchPointY: number = 0;        //触摸点Y坐标;
    blockId: number = 0;            //方块ID;
    stacking: cc.Vec2;        //叠加方块
    level: number = 0;            //叠加方块层数;
    pz: boolean = false;
    collider: any;
    block_Info: any;
    _touchListener: any;
    relative_Position: cc.Vec2;  //点击和方块相对位置
    private _eventManager: any;
    hit: cc.Node;



    onLoad() {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        // this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);  
        // this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);  
        // this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);  
        this.pz = false;
        this.stacking = cc.v2(0, 0);

    }


    start() {

    }

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

    changeState() {
        if (this.block_Info.barriersLock != null && this.block_Info.barriersLock != undefined) {
            if (this.block_Info.barriersLock == true) {
                this.block_Info.barriersLock = false;
                this.node.active = true;
            }
            else {
                this.block_Info.barriersLock = true;
                if (this.node)
                    this.node.active = false;
            }
        }
    }

    init(block_Info, posX, posY, node) {
        this.block_Info = this.jsonDeepClone(block_Info);
        this.changeState();
        let mapInfo = MapConroler._instance.mapInfo;
        //console.log("block_Info", this.block_Info);
        for (let i = 0; i < mapInfo.length; i++) {
            let blockRect = mapInfo[i].getBoundingBox();
            // 使用 cc.Intersection.pointInRect 方法判断点是否在矩形范围内
            let point = cc.v2(this.node.position.x - 5, this.node.position.y + 10)
            if (blockRect.contains(point)) {
                this.posX = mapInfo[i].getComponent("MapBlock").posX;
                this.posY = mapInfo[i].getComponent("MapBlock").posY;
                this.level = 50 + this.posX - this.posY * 3;
                this.node.zIndex = this.level;
                this.node.x = mapInfo[i].x + 65;
                this.node.y = mapInfo[i].y - 60;
                i = 10000;
                break;
            }
        }

    }

}


