var reGameStates = require('GameStates');
import date from "../Unit/date.js"
cc.Class({
    extends: cc.Component,

    properties: {
        //MyMapNode
        //建筑物的节点
        BuildingsParent: {
            default: null,
            type: cc.Node,
        },
        //农舍
        Env_101_house_low: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //单元楼
        Env_102_house_mid: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //别墅
        Env_103_house_high: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //蓝色城堡
        Env_104_BlueCastle: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //粉色城堡
        Env_105_PinkCastle: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //农田
        Labour_201_Farmland: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //伐木场
        Labour_202_TimberYard: {

            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //矿坑
        Labour_203_MiningPit: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //工厂
        Labour_204_Factory: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //神农专用农田
        Labour_205_Holy_Farmland: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //一级
        // 冷饮摊
        Shops_30101_ColdDrinkStall: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //贩卖机
        Shops_30102_SalesMachine: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //面包房
        Shops_30103_Bakery: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //早餐车
        Shops_30104_BreakfastCar: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //二级
        //饮茶店
        Shops_30201_TeaShop: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //点心店
        Shops_30202_confectaurant: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //美食店
        Shops_30203_GourmetRestaurant: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //西餐厅
        Shops_30204_WesternRestaurant: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //三级
        //花店
        Shops_30301_Florist: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //美发店
        Shops_30302_HairSalon: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //洋装店
        Shops_30303_DressShop: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //珠宝店
        Shops_30304_JewelryStore: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //电影院
        Shops_30305_Cinema: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //特殊建筑
        //路灯
        Spe_401_StreetLamp: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //绿化带
        Spe_402_GreenBelt: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //花坛
        Spe_403_FlowerBed: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //喷泉
        Spe_404_Pool: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //警察局
        Spe_405_PoliceOffice: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        //游乐场
        Spe_406_Playground: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        BuildingArray: {
            default: [],
            type: [cc.Node],
            serializable: true,
        },

        /**
         * 定义一个场景预制对象
         */
        DressUpArray: {
            default: [],
            type: [cc.Prefab]
        }
    },
    onBuildHouse(index) {
        // cc.log(index);

        let mainCamera = GlobalD.game.MainCamera;
        let housing = null;
        //content button 设置的index
        //农舍
        if ('101' == index) {
            housing = cc.instantiate(this.Env_101_house_low);
        }
        //单元楼
        else if ('102' == index) {
            housing = cc.instantiate(this.Env_102_house_mid);
        }
        //别墅
        else if ('103' == index) {
            housing = cc.instantiate(this.Env_103_house_high);
        }
        else if ('104' == index) {
            housing = cc.instantiate(this.Env_104_BlueCastle);
        }
        else if ('105' == index) {
            housing = cc.instantiate(this.Env_105_PinkCastle);
        }
        //农田
        else if ('201' == index) {
            housing = cc.instantiate(this.Labour_201_Farmland);
        }
        //伐木场
        else if ('202' == index) {
            housing = cc.instantiate(this.Labour_202_TimberYard);
        }
        //矿坑
        else if ('203' == index) {
            housing = cc.instantiate(this.Labour_203_MiningPit);
        }
        //工厂
        else if ('204' == index) {
            housing = cc.instantiate(this.Labour_204_Factory);
        }
        //神农专用农田
        else if ('205' == index) {
            housing = cc.instantiate(this.Labour_205_Holy_Farmland);
        }
        //冷饮摊
        else if ('30101' == index) {
            housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
        }
        //贩卖机
        else if ('30102' == index) {
            housing = cc.instantiate(this.Shops_30102_SalesMachine);
        }
        //面包房
        else if ('30103' == index) {
            housing = cc.instantiate(this.Shops_30103_Bakery);
        }
        //早餐车
        else if ('30104' == index) {
            housing = cc.instantiate(this.Shops_30104_BreakfastCar);
        }
        //饮茶店
        else if ('30201' == index) {
            housing = cc.instantiate(this.Shops_30201_TeaShop);
        }
        //点心店
        else if ('30202' == index) {
            housing = cc.instantiate(this.Shops_30202_confectaurant);
        }
        //美食店
        else if ('30203' == index) {
            housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
        }
        //西餐厅
        else if ('30204' == index) {
            housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
        }
        //花店
        else if ('30301' == index) {
            housing = cc.instantiate(this.Shops_30301_Florist);
        }
        //美发店
        else if ('30302' == index) {
            housing = cc.instantiate(this.Shops_30302_HairSalon);
        }
        //洋装店
        else if ('30303' == index) {
            housing = cc.instantiate(this.Shops_30303_DressShop);
        }
        //珠宝店
        else if ('30304' == index) {
            housing = cc.instantiate(this.Shops_30304_JewelryStore);
        }
        //电影院
        else if ('30305' == index) {
            housing = cc.instantiate(this.Shops_30305_Cinema);
        }
        //路灯
        else if ('401' == index) {
            housing = cc.instantiate(this.Spe_401_StreetLamp);
        }
        //绿化带
        else if ('402' == index) {
            housing = cc.instantiate(this.Spe_402_GreenBelt);
        }
        //花坛
        else if ('403' == index) {
            housing = cc.instantiate(this.Spe_403_FlowerBed);
        }
        //喷泉
        else if ('404' == index) {
            housing = cc.instantiate(this.Spe_404_Pool);
        }
        //警察局
        else if ('405' == index) {
            housing = cc.instantiate(this.Spe_405_PoliceOffice);
        }
        //游乐场
        else if ('406' == index) {
            housing = cc.instantiate(this.Spe_406_Playground);
        }
        housing.parent = this.BuildingsParent;
        let tempPos = housing.parent.convertToNodeSpace(mainCamera.node);
        let CanvasPos = GlobalD.game.Canvas.position;
        housing.setPosition(tempPos.x + CanvasPos.x, tempPos.y + CanvasPos.y);

        let buildingsInfo = housing.getComponent("buildingsInfo");
        //id用时间戳来记录
        buildingsInfo.buildInfo.id = new Date().getTime();//+= Math.random() * 1000000;

        this.SpawnBuildingDefaultValue(buildingsInfo);

        let buildingsTouch = housing.getComponent("buildingsTouch");
        // buildingsTouch.buildInfo = buildingsInfo.buildInfo;
        buildingsTouch.onEditorStatus(true);
        // housing.active = true;

        //收起菜单
        GlobalD.game._ManageUIScript.onHideMenu();
        //收起底部菜单栏
        GlobalD.game._ManageUIScript.onBottomMenuView(false);
    },
    start() {

    },

    //如果存储到有房子数据的话，把地图上的房屋都隐藏起来,或者删除
    //todo...
    onHideInitPosBuildings() {

        //删除对应层的子节点
        let tempNode = this.BuildingsParent.children;
        let length = tempNode.length;
        for (let i = length - 1; i >= 0; i--) {
            tempNode[i].active = false;
        }
    },

    /**
     * GameData里面处理的生成数据流程，生成建筑物
     */
    InitBuildings() {
        //如果有存储的数据，隐藏场景房屋节点
        // this.onHideInitPosBuildings();
        /**
         * 注释 spawnConfigLand 不初始化固定土地
         */
        //第一步初始化土地的网络数据
        this.spawnConfigLand(GlobalD.UserLeaseLand, true);
        /**
         * todo 之类初始化场景障碍物操作，如果不需要直接注释即可。
         * 村长和镇长
         * 初始化障碍物场景，装饰
         */
        // if(0 != GlobalD.Dapp.UserInfo.agent_level){
        //     this.spawnDressUp();
        // }
        //先初始化网络数据
        this.SpawnHouse();
        // cc.log('初始化场景建筑');
        let BuildingStateArray = GlobalD.GameData.GetBuildingStateArray();
        for (let i = 0; i < BuildingStateArray.length; i++) {
            if (BuildingStateArray[i] == 0) {
                if (this.BuildingArray[i])
                    this.BuildingArray[i].active = false;
            }
            else {
                if (this.BuildingArray[i])
                    this.BuildingArray[i].active = true;
            }
        }
        let BuildingNumArray = GlobalD.GameData.GetBuildingNumArray();
        // cc.log('BuildingNumArray',BuildingNumArray)
        for (let i = 3; i < this.BuildingArray.length; i++) {
            // cc.log('this.BuildingArray[' + i + ']=' + this.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string);
            if (this.BuildingArray[i])
                this.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i];
        }


    },
    //生成房子的默认值
    SpawnBuildingDefaultValue(buildingsInfo) {
        buildingsInfo.InitPos = false;
        buildingsInfo.InitWorkBuildingInfo = false;
        //激活状态，可运营
        buildingsInfo.buildInfo.isItActive = true;
        //设置销售状态

        buildingsInfo.buildInfo.isItSaleable = true;

        if (reGameStates.BuildType.Farmland == buildingsInfo.buildInfo.buildType) {
            buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetFoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetFoodTradeState();

            // cc.log('初始化房子的信息：', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable);
        }
        if (reGameStates.BuildType.MiningPit == buildingsInfo.buildInfo.buildType) {
            buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetMineralTradeState() > 0 ? true : false;// GlobalD.GameData.GetMineralTradeState();
        }
        if (reGameStates.BuildType.TimberYard == buildingsInfo.buildInfo.buildType) {
            buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetWoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetWoodTradeState();
        }


        //设置一下，可查看信息
        if (reGameStates.BuildType.Housing !== buildingsInfo.buildInfo.buildType
            && reGameStates.BuildType.Special !== buildingsInfo.buildInfo.buildType
            /** 神农小镇固定土地关闭 基本的房屋信息，另外设置其他信息 */
            && reGameStates.BuildType.HolyFarmland !== buildingsInfo.buildInfo.buildType
        ) {

            buildingsInfo.node.getComponent('buildingsTouch').isShowBuildingInfo = true;
        }

        //设置消耗的体力值为工人的最低值 20
        //过高的话，会导致工人不工作
        // buildingsInfo.buildInfo.totalConsumption = 20;

        // cc.log('初始化房子的信息：', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable)
    },

    //初始化场景的两个建筑
    SpawnDefaultHouse() {
        cc.log('初始化默认房子');
        //房子
        let housingTemp_house = this.onGetHousingPrefabFromName('Env_101_house_low');
        housingTemp_house.parent = this.BuildingsParent;
        var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(24, 23));
        housingTemp_house.setPosition(endPos.x, endPos.y);

        let buildingsInfo_house = housingTemp_house.getComponent("buildingsInfo");
        //id用时间戳来记录
        buildingsInfo_house.buildInfo.id = 101;//对应的id
        //设置tile的最底下的坐标，就是起始坐标
        buildingsInfo_house.buildInfo.startTilePos = cc.v2(24, 23);

        this.SpawnBuildingDefaultValue(buildingsInfo_house);

        //起始坐标，占位范围，是否占位
        GlobalD.game.addBuildTiled(buildingsInfo_house.buildInfo.id, buildingsInfo_house.buildInfo.startTilePos, buildingsInfo_house.buildInfo.occupyArea);
        //添加占位信息
        GlobalD.game.addBuilding(buildingsInfo_house);


        //农田
        let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_201_Farmland');
        housingTemp_farmland.parent = this.BuildingsParent;
        var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(26, 24));
        housingTemp_farmland.setPosition(endPos.x, endPos.y);

        let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
        //id用时间戳来记录
        buildingsInfo_farmland.buildInfo.id = 201;//对应的id
        //设置tile的最底下的坐标，就是起始坐标
        buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(26, 24);

        this.SpawnBuildingDefaultValue(buildingsInfo_farmland);

        //起始坐标，占位范围，是否占位
        GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
        //添加占位信息
        GlobalD.game.addBuilding(buildingsInfo_farmland);

    },

    /**
     * 按钮调用
     * 回到用户自己的农场
     */
    onBackSelfFarmland() {
        //刷新一下自己的用户信息
        GlobalD.GameData.getSelfUserLandList(() => {
            this.spawnConfigLand(GlobalD.UserLeaseLand, true);

            //显示底部菜单栏
            GlobalD.game._ManageUIScript.onBottomMenuView(true);
            //收起其他用户场景后菜单操作
            GlobalD.game._ManageUIScript.onOtherFarmerView(false);
        })
    },

    /**
     * 初始化后台配置的土地数据
     * 切换时候需要重置其他用户数据
     * @returns 
     */
    spawnConfigLand(_userLeaseLand, bSelf) {

        if (GlobalD.ConfigLand == null) {
            console.error("GlobalD.ConfigLand 未初始化设置！");
            return;
        }
        if (_userLeaseLand == null) {
            console.error("_userLeaseLand 未初始化设置！");
            return;
        }
        let _configLand = GlobalD.ConfigLand;
        if (bSelf) {
            let _bInitFarmland = false;
            let _landBuildingsInfo = this.BuildingsParent.children;
            for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
                let resetLandInfo = _landBuildingsInfo[i];
                if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
                    let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
                    //如果存在开锁的土地，重置状态
                    if (!leaseFarmlandInfoScript.onGetUnlockLandState()) {
                        leaseFarmlandInfoScript.onLockLand();
                    }
                    for (let j = 0; j < _userLeaseLand.length; j++) {
                        //记录已经租赁的信息(租赁里面包含了seedInfo，如果有种植的话，对应的种植信息会有)
                        if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) {
                            // console.log(leaseFarmlandInfoScript.initConfigLandId);
                            let _leaseLandInfo = _userLeaseLand[j];
                            leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
                        }
                    }

                    _bInitFarmland = true;
                }

            }
            if (_bInitFarmland) {
                console.log("已经初始化过土地数据！");
                return;
            }
            //此时是第一次生成
            for (let i = 0; i < _configLand.length; i++) {
                //生成默认土地
                //农田
                let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_205_Holy_Farmland');
                housingTemp_farmland.parent = this.BuildingsParent;
                var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(_configLand[i].posX, _configLand[i].posY));
                housingTemp_farmland.setPosition(endPos.x, endPos.y);
                let leaseFarmlandInfoScript = housingTemp_farmland.getComponent("LeaseFarmlandInfo");
                //记录一下第一次初始化时候的id
                leaseFarmlandInfoScript.initConfigLandId = _configLand[i].id;
                // console.log(housingTemp_farmland.name +" = "+ leaseFarmlandInfoScript.initConfigLandId);
                //记录config的土地信息
                leaseFarmlandInfoScript.setConfigLandInfo(_configLand[i]);
                for (let j = 0; j < _userLeaseLand.length; j++) {
                    //记录已经租赁的信息(租赁里面包含了seedInfo，如果有种植的话，对应的种植信息会有)
                    if (_userLeaseLand[j].configLandId == _configLand[i].id) {
                        let _leaseLandInfo = _userLeaseLand[j];
                        leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
                    }
                }
                let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
                //id用时间戳来记录
                buildingsInfo_farmland.buildInfo.id = _configLand[i].id;//对应的id
                /**
                 * 服务器设置的农田 独立于其他游戏数据
                 * 这步设置之后，addBuilding 时候不存储到 GameData_buildings
                 */
                buildingsInfo_farmland.InitPosFromStore = true;
                //设置tile的最底下的坐标，就是起始坐标
                buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(_configLand[i].posX, _configLand[i].posY);
                this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
                //起始坐标，占位范围，是否占位
                GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
                //添加占位信息
                GlobalD.game.addBuilding(buildingsInfo_farmland);

            }

            // console.log(this.BuildingsParent.children);
        } else {
            //访问其他用户时候，已经有土地了
            //重置土地信息
            let _landBuildingsInfo = this.BuildingsParent.children;
            for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
                let resetLandInfo = _landBuildingsInfo[i];
                if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
                    let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
                    //如果存在开锁的土地，重置状态
                    if (!leaseFarmlandInfoScript.onGetUnlockLandState()) {
                        leaseFarmlandInfoScript.onLockLand();
                    }
                    //访问其他农场时候，全部不显示待租按钮
                    leaseFarmlandInfoScript.onUnlockLand();
                    for (let j = 0; j < _userLeaseLand.length; j++) {
                        //记录已经租赁的信息(租赁里面包含了seedInfo，如果有种植的话，对应的种植信息会有)
                        if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) {
                            let _leaseLandInfo = _userLeaseLand[j];
                            leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
                        }
                    }
                }

            }
            // console.log("other:", this.BuildingsParent.children);
        }

    },



    /**
     * 初始化障碍物
     * 生成的预制信息，需要在 DressUpArray 添加对应的预制件，才可以添加
     */
    spawnDressUp() {
        //村长和镇长的区别，樱花树区别

        let agent_level = GlobalD.Dapp.UserInfo.agent_level;  // 0普通用户，1 村长身份，2 镇长身份
        let _cherryTree = 2 === agent_level ? "Dress_505_SmallTree02" : "Dress_505_SmallTree01";
        let _temp = [
            //左边第一部分 ----start----
            {
                spawnName: "Dress_502_ChrisTree",
                startPos: cc.v2(14, 20)
            },
            //拱门
            {
                spawnName: "Dress_505_MainDoor01",
                startPos: cc.v2(18, 17)
            },
            {
                spawnName: "Dress_503_RailingGrassX",
                startPos: cc.v2(9, 20)
            },
            {
                spawnName: "Dress_503_RailingGrassX",
                startPos: cc.v2(7, 20)
            },
            //-----
            {
                spawnName: "Dress_505_FlowerBed",
                startPos: cc.v2(8, 22)
            },
            {
                spawnName: "Dress_505_FlowerBed",
                startPos: cc.v2(8, 23)
            },
            //喷泉
            {
                spawnName: "Dress_502_Fountain",
                startPos: cc.v2(10, 23)
            },
            {
                spawnName: "Dress_505_FlowerBed",
                startPos: cc.v2(11, 22)
            },
            {
                spawnName: "Dress_505_FlowerBed",
                startPos: cc.v2(11, 23)
            },
            {
                spawnName: "Dress_505_ChrisSanta",
                startPos: cc.v2(10, 25)
            },
            //-----


            //左边第一部分 ----end----

            //主路放两个灯和树
            {
                spawnName: "Dress_505_Streetlight",
                startPos: cc.v2(15, 29)
            },
            {
                spawnName: "Dress_505_Streetlight",
                startPos: cc.v2(18, 29)
            },
            {
                spawnName: "Dress_505_SmallTree",
                startPos: cc.v2(15, 30)
            },
            {
                spawnName: "Dress_505_SmallTree",
                startPos: cc.v2(18, 30)
            }


        ];
        //左边小麦区域
        // for (let x = 0; x < 3; x++) {
        //     for (let y = 0; y < 2; y++) {
        //         _temp.push({
        //             spawnName: "Dress_502_Rice",
        //             startPos: cc.v2(4 - x * 2, 30 - y * 2)
        //         })
        //     }
        // }


        //1倍土地部分木质花圃
        // for (let x = 0; x < 6; x++) {
        //     for (let y = 0; y < 3; y++) {
        //         _temp.push({
        //             spawnName: "Dress_503_WhiteWoodRailingX",
        //             startPos: cc.v2(18 + x, 14 - y * 3)
        //         })
        //     }
        // }
        //生成一个未盛开的樱花围绕，间隔1个空格 ---start  ，镇长换成盛开的樱花
        // for (let x = 0; x < 3; x++) {
        //     _temp.push({
        //         spawnName: _cherryTree,
        //         startPos: cc.v2(18 + x * 2, 17)
        //     })
        //     _temp.push({
        //         spawnName: _cherryTree,
        //         startPos: cc.v2(18 + x * 2, 5)
        //     })
        // }
        // for (let y = 0; y < 7; y++) {
        //     _temp.push({
        //         spawnName: _cherryTree,
        //         startPos: cc.v2(24, 17 - y * 2)
        //     })

        // }
        //多倍区域
        // for (let x = 0; x < 3; x++) {
        //     _temp.push({
        //         spawnName: _cherryTree,
        //         startPos: cc.v2(15 - x * 2, 14)
        //     })
        //     _temp.push({
        //         spawnName: _cherryTree,
        //         startPos: cc.v2(15 - x * 2, 2)
        //     })
        // }

        //生成一个未盛开的樱花围绕，间隔1个空格 --- end


        //多倍土地部分木质花圃
        // for (let x = 0; x < 3; x++) {
        //     for (let y = 0; y < 3; y++) {
        //         _temp.push({
        //             spawnName: "Dress_503_RailingGrassX",
        //             startPos: cc.v2(15 - x * 2, 11 - y * 3)
        //         })
        //     }
        // }

        //生成番茄区域
        // for (let x = 0; x < 3; x++) {
        //     for (let y = 0; y < 2; y++) {
        //         _temp.push({
        //             spawnName: "Dress_502_Tomato",
        //             startPos: cc.v2(26 + x * 2, 30 - y * 2)
        //         })
        //     }
        // }


        //----中上别墅区域--start
        //生成中上三个别墅和三个树木
        for (let x = 0; x < 3; x++) {
            _temp.push({
                spawnName: "Dress_505_SmallTree",
                startPos: cc.v2(7 - x * 2, 6)
            })
            _temp.push({
                spawnName: "Dress_502_Villa",
                startPos: cc.v2(8 - x * 2, 8)
            })
        }
        //雪橇
        _temp.push({
            spawnName: "Dress_505_ChrisCar02",
            startPos: cc.v2(8, 10)
        })
        //圣诞门和栏杆
        for (let x = 0; x < 3; x++) {
            _temp.push({
                spawnName: "Dress_503_WhiteWoodRailingX",
                startPos: cc.v2(8 - x, 11)
            })
        }
        _temp.push({
            spawnName: "Dress_503_ChrisDoorX",
            startPos: cc.v2(5, 11)
        })
        for (let x = 0; x < 3; x++) {
            _temp.push({
                spawnName: "Dress_503_WhiteWoodRailingX",
                startPos: cc.v2(4 - x, 11)
            })
        }
        //门外区域
        _temp.push({
            spawnName: "Dress_502_florist",
            startPos: cc.v2(4, 15)
        })

        _temp.push({
            spawnName: "Dress_505_ChrisCar01",
            startPos: cc.v2(6, 16)
        })

        _temp.push({
            spawnName: "Dress_505_PondDuck",
            startPos: cc.v2(6, 14)
        })

        for (let y = 0; y < 3; y++) {
            _temp.push({
                spawnName: "Dress_504_ChrisRailingY",
                startPos: cc.v2(8, 16 - y)
            })
        }

        //樱花路
        for (let x = 0; x < 5; x++) {
            _temp.push({
                spawnName: _cherryTree,
                startPos: cc.v2(10 - x * 2, 17)
            })
            _temp.push({
                spawnName: _cherryTree,
                startPos: cc.v2(10 - x * 2, 19)
            })
        }

        //------end


        //----右边区域--start
        _temp.push({
            spawnName: "Dress_505_Pond",
            startPos: cc.v2(20, 3)
        })
        _temp.push({
            spawnName: "Dress_504_RailingGrassY",
            startPos: cc.v2(21, 3)
        })
        _temp.push({
            spawnName: "Dress_502_FruitTreeAndStool",
            startPos: cc.v2(23, 3)
        })


        //---- end


        for (let i = 0; i < _temp.length; i++) {
            for (let j = 0; j < this.DressUpArray.length; j++) {
                let _dressUpTemp = this.DressUpArray[j];
                if (_dressUpTemp.name == _temp[i].spawnName) {
                    let prefab = cc.instantiate(_dressUpTemp);
                    prefab.parent = this.BuildingsParent;
                    let tipNode = prefab.getChildByName("Tip");
                    if (tipNode) {
                        tipNode.destroy();
                    }
                    let buildingsInfo = prefab.getComponent("buildingsInfo");
                    //id用时间戳来记录
                    buildingsInfo.buildInfo.id = new Date().getTime();//对应的id
                    buildingsInfo.buildInfo.isItActive = false;
                    //设置tile的最底下的坐标，就是起始坐标
                    buildingsInfo.buildInfo.startTilePos = _temp[i].startPos;
                    buildingsInfo.InitPos = false;
                    buildingsInfo.InitStartPos = _temp[i].startPos;
                    var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(_temp[i].startPos);
                    prefab.setPosition(endPos.x, endPos.y);
                    //起始坐标，占位范围，是否占位
                    GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
                    //添加占位信息
                    GlobalD.game.addBuilding(buildingsInfo);

                    // console.log(_dressUpTemp.name);
                    //跳出里面一层循环
                    break;
                }
            }

        }

    },

    //初始化场景建筑
    SpawnHouse: function () {
        let BuildingJSON = Object.assign([], GlobalD.GameData.GameData_buildings);
        //拿到数据后，清空 GlobalD.GameData.GameData_buildings 数据，下面再添加上去
        GlobalD.GameData.GameData_buildings.length = 0;
        // cc.log('BuildingJSON', BuildingJSON);
        // if (BuildingJSON.length == 0) {
        //     //初始化默认房子
        //     this.SpawnDefaultHouse();
        //     return
        // };

        for (let i = 0; i < BuildingJSON.length; i++) {

            let HouseName = BuildingJSON[i].buildingNodeName;
            let housingTemp = this.onGetHousingPrefabFromName(HouseName);
            if (!housingTemp)
                cc.error('housingTemp', HouseName, housingTemp);

            //预制为空
            if (!housingTemp) continue;
            let buildingsInfo = housingTemp.getComponent("buildingsInfo");

            /**
            * 处理数据库数据冲突问题
            * 这里处理判断初始化位置是否被占用,占用了回收到仓库背包系统
            * BuildingJSON 看做旧的数据, 拿到旧数据 BuildingJSON[i].startTilePos 起始位置 ,生成对应预制的大小occupyArea
            * 来判断占位信息
            */
            // //全部回收
            // // 背包添加对应的建筑数量
            // GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
            // // 删除上面生成的预制节点
            // housingTemp.destroy();
            // continue;
            let _canNotBuild = GlobalD.game.areTheraOverlappingAreas(BuildingJSON[i].startTilePos, buildingsInfo.buildInfo.occupyArea, 1);
            if (_canNotBuild) {
                // 背包添加对应的建筑数量
                GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
                // 删除上面生成的预制节点
                housingTemp.destroy();
                //跳过生成
                continue;
            }

            housingTemp.parent = this.BuildingsParent;
            var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(BuildingJSON[i].startTilePos);
            housingTemp.setPosition(endPos.x, endPos.y);
            //id用时间戳来记录
            buildingsInfo.buildInfo.id = BuildingJSON[i].buildingId;//对应的id
            //设置tile的最底下的坐标，就是起始坐标
            buildingsInfo.buildInfo.startTilePos = BuildingJSON[i].startTilePos;
            //初始化位置的坐标
            buildingsInfo.InitStartPos = BuildingJSON[i].startTilePos;

            this.SpawnBuildingDefaultValue(buildingsInfo);
            //目前应该只有商店,后面根据RunningCost来判断  todo.....
            // if (reGameStates.BuildType.Shop == buildingsInfo.buildInfo.buildType) 
            {
                // buildingsInfo.buildInfo.isItSaleable = Number(BuildingJSON[i].isItSaleable) > 0 ? true : false;
                buildingsInfo.buildInfo.isItStopOperation = Number(BuildingJSON[i].isItStopOperation) > 0 ? true : false;
                //如果不可以运营的，加上牌子
                if (buildingsInfo.buildInfo.isItStopOperation) {
                    GlobalD.game.onAddBuildingTipView(buildingsInfo);
                }
            }
            //建筑物的加成值
            buildingsInfo.buildInfo.consumeStrengthAddValue = Number(BuildingJSON[i].consumeStrengthAddValue);
            buildingsInfo.buildInfo.goodsPriceAddValue = Number(BuildingJSON[i].goodsPriceAddValue);
            /**
             * 判断是否占位的时候，需要进一步处理。放回背包，然后背包物品加上对应的数量
             */
            //起始坐标，占位范围，是否占位
            GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
            //添加占位信息
            GlobalD.game.addBuilding(buildingsInfo);
        }

        // console.log("GlobalD.GameData.GameData_buildings:", GlobalD.GameData.GameData_buildings);
    },
    onGetHousingPrefabFromName(HouseName) {
        let housing = null;
        //住宿地方
        if (HouseName == 'Env_101_house_low') {
            housing = cc.instantiate(this.Env_101_house_low);
        }
        else if (HouseName === 'Env_102_house_mid') {
            housing = cc.instantiate(this.Env_102_house_mid);
        }
        else if (HouseName == 'Env_103_house_high') {
            housing = cc.instantiate(this.Env_103_house_high);
        }
        else if (HouseName == 'Env_104_BlueCastle') {
            housing = cc.instantiate(this.Env_104_BlueCastle);
        }
        else if (HouseName == 'Env_105_PinkCastle') {
            housing = cc.instantiate(this.Env_105_PinkCastle);
        }
        //劳动地方
        else if (HouseName == 'Labour_201_Farmland') {
            housing = cc.instantiate(this.Labour_201_Farmland);
        }
        else if (HouseName == 'Labour_202_TimberYard') {
            housing = cc.instantiate(this.Labour_202_TimberYard);
        }
        else if (HouseName == 'Labour_203_MiningPit') {
            housing = cc.instantiate(this.Labour_203_MiningPit);
        }
        else if (HouseName == 'Labour_204_Factory') {
            housing = cc.instantiate(this.Labour_204_Factory);
        }
        //神农专用农田
        else if (HouseName == 'Labour_205_Holy_Farmland') {
            housing = cc.instantiate(this.Labour_205_Holy_Farmland);
        }
        //特殊建筑
        else if (HouseName == 'Spe_401_StreetLamp') {
            housing = cc.instantiate(this.Spe_401_StreetLamp);
        }
        else if (HouseName == 'Spe_402_GreenBelt') {
            housing = cc.instantiate(this.Spe_402_GreenBelt);
        }
        else if (HouseName == 'Spe_403_FlowerBed') {
            housing = cc.instantiate(this.Spe_403_FlowerBed);
        }
        else if (HouseName == 'Spe_404_Pool') {
            housing = cc.instantiate(this.Spe_404_Pool);
        }
        else if (HouseName == 'Spe_405_PoliceOffice') {
            housing = cc.instantiate(this.Spe_405_PoliceOffice);
        }
        else if (HouseName == 'Spe_406_Playground') {
            housing = cc.instantiate(this.Spe_406_Playground);
        }
        //商店类型
        else if (HouseName == 'Shops_30101_ColdDrinkStall') {
            housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
        }
        else if (HouseName == 'Shops_30102_SalesMachine') {
            housing = cc.instantiate(this.Shops_30102_SalesMachine);
        }
        else if (HouseName == 'Shops_30103_Bakery') {
            housing = cc.instantiate(this.Shops_30103_Bakery);
        }
        else if (HouseName == 'Shops_30104_BreakfastCar') {
            housing = cc.instantiate(this.Shops_30104_BreakfastCar);
        }
        else if (HouseName == 'Shops_30201_TeaShop') {
            housing = cc.instantiate(this.Shops_30201_TeaShop);
        }
        else if (HouseName == 'Shops_30202_confectaurant') {
            housing = cc.instantiate(this.Shops_30202_confectaurant);
        }
        else if (HouseName == 'Shops_30203_GourmetRestaurant') {
            housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
        }
        else if (HouseName == 'Shops_30204_WesternRestaurant') {
            housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
        }
        else if (HouseName == 'Shops_30301_Florist') {
            housing = cc.instantiate(this.Shops_30301_Florist);
        }
        else if (HouseName == 'Shops_30302_HairSalon') {
            housing = cc.instantiate(this.Shops_30302_HairSalon);
        }
        else if (HouseName == 'Shops_30303_DressShop') {
            housing = cc.instantiate(this.Shops_30303_DressShop);
        }
        else if (HouseName == 'Shops_30304_JewelryStore') {
            housing = cc.instantiate(this.Shops_30304_JewelryStore);
        }
        else if (HouseName == 'Shops_30305_Cinema') {
            housing = cc.instantiate(this.Shops_30305_Cinema);
        }
        return housing;
    },


    //获取预制的名字
    onGetPrefabsBuildingName(index) {
        //content button 设置的index
        let buildingName = '';
        //农舍
        if ('101' == index) {
            buildingName = 'Env_101_house_low';
        }
        //单元楼
        else if ('102' == index) {
            buildingName = 'Env_102_house_mid';
        }
        //别墅
        else if ('103' == index) {
            buildingName = 'Env_103_house_high';
        }
        //蓝色城堡
        else if ('104' == index) {
            buildingName = 'Env_104_BlueCastle';
        }
        //粉色城堡
        else if ('105' == index) {
            buildingName = 'Env_105_PinkCastle';
        }
        //农田
        else if ('201' == index) {
            buildingName = 'Labour_201_Farmland';
        }
        //伐木场
        else if ('202' == index) {
            buildingName = 'Labour_202_TimberYard';
        }
        //矿坑
        else if ('203' == index) {
            buildingName = 'Labour_203_MiningPit';
        }
        //工厂
        else if ('204' == index) {
            buildingName = 'Labour_204_Factory';
        }
        //神农专用农田
        else if ('205' == index) {
            buildingName = 'Labour_205_Holy_Farmland';
        }
        //冷饮摊
        else if ('30101' == index) {
            buildingName = 'Shops_30101_ColdDrinkStall';
        }
        //贩卖机
        else if ('30102' == index) {
            buildingName = 'Shops_30102_SalesMachine';
        }
        //面包房
        else if ('30103' == index) {
            buildingName = 'Shops_30103_Bakery';
        }
        //早餐车
        else if ('30104' == index) {
            buildingName = 'Shops_30104_BreakfastCar';
        }
        //饮茶店
        else if ('30201' == index) {
            buildingName = 'Shops_30201_TeaShop';
        }
        //点心店
        else if ('30202' == index) {
            buildingName = 'Shops_30202_confectaurant';
        }
        //美食店
        else if ('30203' == index) {
            buildingName = 'Shops_30203_GourmetRestaurant';
        }
        //西餐厅
        else if ('30204' == index) {
            buildingName = 'Shops_30204_WesternRestaurant';
        }
        //花店
        else if ('30301' == index) {
            buildingName = 'Shops_30301_Florist';
        }
        //美发店
        else if ('30302' == index) {
            buildingName = 'Shops_30302_HairSalon';
        }
        //洋装店
        else if ('30303' == index) {
            buildingName = 'Shops_30303_DressShop';
        }
        //珠宝店
        else if ('30304' == index) {
            buildingName = 'Shops_30304_JewelryStore';
        }
        //电影院
        else if ('30305' == index) {
            buildingName = 'Shops_30305_Cinema';
        }
        //路灯
        else if ('401' == index) {
            buildingName = 'Spe_401_StreetLamp';
        }
        //绿化带
        else if ('402' == index) {
            buildingName = 'Spe_402_GreenBelt';
        }
        //花坛
        else if ('403' == index) {
            buildingName = 'Spe_403_FlowerBed';
        }
        //喷泉
        else if ('404' == index) {
            buildingName = 'Spe_404_Pool';
        }
        //警察局
        else if ('405' == index) {
            buildingName = 'Spe_405_PoliceOffice';
        }
        //游乐场
        else if ('406' == index) {
            buildingName = 'Spe_406_Playground';
        }
        return buildingName;
    },
    Grant() {

    },
    Sale() {

    },
    Exchange() {

    }
});
