function MapCtrl(){
    const _inst = this;
    this.awake = function (){
        this.listMap = [];
        this.currFlag = "11";
        /*this.sX = -38; this.sZ = -92;
        this.eX = 32; this.eZ = 87;
        this.xDis = Math.abs(this.eX-this.sX);//宽
        this.zDis = Math.abs(this.eZ-this.sZ);//长*/
    }
    this.addMapPos = function (id, startPos, endPos){
        let xMin = startPos.x;
        let xMax = endPos.x;
        if(xMin > endPos.x){
            xMax = xMin;
            xMin = endPos.x;
        }
        let zMin = startPos.z;
        let zMax = endPos.z;
        if(zMin > endPos.z){
            zMax = zMin;
            zMin = endPos.z;
        }
        this.listMap.push({id:id, xMin:xMin, xMax:xMax, zMin:zMin, zMax:zMax,startPos:startPos, endPos:endPos, xDis: Math.abs(endPos.x-startPos.x), zDis:Math.abs(endPos.z-startPos.z), map:[]});
    }
    this.attachMap = function (id, map, arrow, bkOffset, offset={w:0, h:0}, hasClick = false){
        let cw = parseInt(map.style.width.substr(0, map.style.width.length-2));
        let ch = parseInt(map.style.height.substr(0, map.style.height.length-2));
        let width = Math.round(cw)-bkOffset.bw;
        let height = Math.round(ch)-bkOffset.bh;

        var data = this.getDataByID(id);

        let xH = height/data.xDis;
        let zW = width/data.zDis;

        data.map.push({xH:xH, height:height, zW:zW, width:width, offset:offset,arrow:arrow});

        if(hasClick){
            map.onclick = function (ev){
                let left = ev.clientX-map.offsetLeft;
                let top = ev.clientY-map.offsetTop;

                let posX = (top-height-offset.h)/xH+data.startPos.x
                let posZ = (left-offset.w)/zW + data.startPos.z;


                if(posX < data.xMin){
                    posX = data.xMin;
                }
                if(posX > data.xMax){
                    posX = data.xMax;
                }
                if(posZ < data.zMin){
                    posZ = data.zMin;
                }
                if(posZ > data.zMax){
                    posZ = data.zMax;
                }
                //暂停漫游
                camCtrl.freeCamera.position.x = posX;
                camCtrl.freeCamera.position.z = posZ;
            }
        }
    }
    this.getDataByID = function (id){
        for(let i = 0; i < this.listMap.length; i++){
            if(this.listMap[i].id == id){
                return this.listMap[i];
            }
        }
    }
    this.update = function (camera){
        let pos = camera.position.clone();
        let ry = camera.rotation.y ;
        if(camera.parent){
            pos = pos.add(camera.parent.position);
            if(camera.parent.rotationQuaternion){
                ry += camera.parent.rotationQuaternion.toEulerAngles().y;
            }
        }
        this.listMap.forEach((item)=>{
            if(item.id.match(_inst.currFlag)){
                item.map.forEach((map)=>{
                    map.arrow.style.transform = "rotate("+(ToolMgr.Radian2Angle(ry)+90)+"deg)";
                    let top = (pos.x - item.startPos.x)*map.xH;
                    //item.arrow.style.top = (item.height-top+item.offset.h)*(1+item.scale)+"px";
                    map.arrow.style.top = (map.height+top+map.offset.h)+"px";
                    let left = (pos.z - item.startPos.z)*map.zW;
                    //item.arrow.style.left = (item.width-left+item.offset.w)*(1+item.scale)+"px";
                    if (ModelConfig.type == 2) { // 古代
                        map.arrow.style.left = (left+map.offset.w)+"px";
                    } else if (ModelConfig.type == 1) { // 现代
                        map.arrow.style.left = (map.width+left+map.offset.w)+"px";
                    }
                })

            }

        })
    }
}
