var reGameStates = require('GameStates'); cc.Class({ extends: cc.Component, properties: { //MyMapNode //建筑物的节点 BuildingsParent: { default: null, type: cc.Node, }, //神农专用农田 Labour_205_Holy_Farmland: { default: null, type: cc.Prefab, serializable: true, }, Labour_206_Holy_Animal: { default: null, type: cc.Prefab, serializable: true, }, animalSpriteFrame: { default: [], type: [cc.SpriteFrame] }, animalPanelSpriteFrame: { default: [], type: [cc.SpriteFrame] }, isBackLimit: false }, // start() { // }, /** * GameData里面处理的生成数据流程,生成建筑物 */ InitBuildings() { /** * 注释 spawnConfigLand 不初始化固定土地 */ //第一步初始化土地的网络数据 this.spawnConfigLand(GlobalD.UserLeaseLand, true); }, //生成房子的默认值 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; } }, /** * 按钮调用 * 回到用户自己的农场 */ onBackSelfFarmland() { if (this.isBackLimit) return; this.isBackLimit = true; GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "返回农场中..", 10); //刷新一下自己的用户信息 GlobalD.GameData.getSelfUserLandList(() => { this.isBackLimit = false; this.spawnConfigLand(GlobalD.UserLeaseLand, true); //显示底部菜单栏 GlobalD.game._ManageUIScript.onBottomMenuView(true); //收起其他用户场景后菜单操作 GlobalD.game._ManageUIScript.onOtherFarmerView(false); GlobalD.GameData.hideToast(); }) //更新自己的狗 let _dogContainerScript = cc.find("Canvas/DogContainer").getComponent("DogContainer") _dogContainerScript.onGetSelfList(); GlobalD.GameData.onSwitchAddressBg(GlobalD.Dapp.UserInfo.agent_level); }, /** * 初始化后台配置的土地数据 * 切换时候需要重置其他用户数据 * @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(); } leaseFarmlandInfoScript.onSwitchEnvBgFromLevel(0); 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; } else if (resetLandInfo.name == "Labour_206_Holy_Animal") { //牧场 let leaseAnimalInfoScript = resetLandInfo.getComponent("LeaseAnimalInfo"); //如果存在开锁的土地,重置状态 if (!leaseAnimalInfoScript.onGetUnlockLandState()) { leaseAnimalInfoScript.onLockLand(); } for (let j = 0; j < _userLeaseLand.length; j++) { //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有) if (_userLeaseLand[j].configLandId == leaseAnimalInfoScript.initConfigLandId) { let _leaseLandInfo = _userLeaseLand[j]; this.setAnimalHousing(resetLandInfo, leaseAnimalInfoScript.titleName); leaseAnimalInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf); } } } } if (_bInitFarmland) { console.log("已经初始化过土地数据!"); return; } //获取tiled的配置点 let _tiledPosArray = GlobalD.TiledMap.onGetLandList(); let _tiledPosArrayAni = GlobalD.TiledMap.onGetBreedList(); // console.log("_userLeaseLand", _userLeaseLand); // console.log("_tiledPosArray:", _tiledPosArray); // console.log("_tiledPosArrayAni", _tiledPosArrayAni); // console.log("_configLand:", _configLand); // if (_tiledPosArray.length != _configLand.length) { // console.error("服务器土地id配置对应地图配置错误!") // return; // } //此时是第一次生成, 目前_configLand 签名24块地就是种植相关,后面的是牧场区域 for (let i = 0; i < 24; i++) { let _index = _configLand[i].id - 1; let _spawnPos = _tiledPosArray[_index]; //生成默认土地 //农田 let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_205_Holy_Farmland'); housingTemp_farmland.parent = this.BuildingsParent; //cc.v2(_configLand[i].posX, _configLand[i].posY) var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(_spawnPos); 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 = _spawnPos; this.SpawnBuildingDefaultValue(buildingsInfo_farmland); //起始坐标,占位范围,是否占位 GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea); //添加占位信息 GlobalD.game.addBuilding(buildingsInfo_farmland); } //除去24块种植区域,后面是牧场区域,用配置表对应 for (let i = 0; i < 12; i++) { let _id = _tiledPosArrayAni[i].id; let _spawnPos = new cc.Vec2(_tiledPosArrayAni[i].InitX, _tiledPosArrayAni[i].InitY); //生成默认养殖场 let housingTemp_animal = this.onGetHousingPrefabFromName('Labour_206_Holy_Animal'); housingTemp_animal.parent = this.BuildingsParent; var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(_spawnPos); housingTemp_animal.setPosition(endPos.x, endPos.y); //修改土地sprite let { _configLandTemp = null } = this.setAnimalHousing(housingTemp_animal, _tiledPosArrayAni[i].name); if (_configLandTemp == null) { console.error("_configLandTemp 错误!"); return; }; // console.log("_configLandTemp:", _configLandTemp); //牧场设置 let leaseAnimalInfoScript = housingTemp_animal.getComponent("LeaseAnimalInfo"); //记录一下第一次初始化时候的id leaseAnimalInfoScript.initConfigLandId = _configLandTemp.id; leaseAnimalInfoScript.titleName = _tiledPosArrayAni[i].name; leaseAnimalInfoScript.setConfigLandInfo(_configLandTemp); for (let j = 0; j < _userLeaseLand.length; j++) { //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有) if (_userLeaseLand[j].configLandId == _configLandTemp.id) { let _leaseLandInfo = _userLeaseLand[j]; leaseAnimalInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf); } } let buildingsInfo_farmland = housingTemp_animal.getComponent("buildingsInfo"); //id用时间戳来记录 buildingsInfo_farmland.buildInfo.id = _id;//对应的id buildingsInfo_farmland.InitPosFromStore = true; buildingsInfo_farmland.buildInfo.startTilePos = _spawnPos; buildingsInfo_farmland.buildInfo.occupyArea = new cc.Vec2(_tiledPosArrayAni[i].tiledX, _tiledPosArrayAni[i].tiledY); 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.onSwitchEnvBgFromLevel(0); for (let j = 0; j < _userLeaseLand.length; j++) { //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有) if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) { let _leaseLandInfo = _userLeaseLand[j]; leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf); } else { //访问其他农场时候,全部不显示待租按钮 leaseFarmlandInfoScript.onUnlockLand(); } } } else if (resetLandInfo.name == "Labour_206_Holy_Animal") { //牧场 let leaseAnimalInfoScript = resetLandInfo.getComponent("LeaseAnimalInfo"); //如果存在开锁的土地,重置状态 if (!leaseAnimalInfoScript.onGetUnlockLandState()) { leaseAnimalInfoScript.onLockLand(); } for (let j = 0; j < _userLeaseLand.length; j++) { //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有) if (_userLeaseLand[j].configLandId == leaseAnimalInfoScript.initConfigLandId) { let _leaseLandInfo = _userLeaseLand[j]; // console.log("*** _leaseLandInfo =", _leaseLandInfo) this.setAnimalHousing(resetLandInfo, leaseAnimalInfoScript.titleName); leaseAnimalInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf); } else { //访问其他农场时候,全部不显示待租按钮 leaseAnimalInfoScript.onUnlockLand(); } } } } // console.log("other:", this.BuildingsParent.children); } }, onGetHousingPrefabFromName(HouseName) { let housing = null; //神农专用农田 if (HouseName == 'Labour_205_Holy_Farmland') { housing = cc.instantiate(this.Labour_205_Holy_Farmland); } else if (HouseName == 'Labour_206_Holy_Animal') { housing = cc.instantiate(this.Labour_206_Holy_Animal); } return housing; }, //获取预制的名字 onGetPrefabsBuildingName(index) { //content button 设置的index let buildingName = ''; //神农专用农田 if ('205' == index) { buildingName = 'Labour_205_Holy_Farmland'; } else if ('206' == index) { housing = 'Labour_206_Holy_Animal'; } return buildingName; }, setAnimalHousing(housingTemp_animal, _titleName) { let _configLand = GlobalD.ConfigLand; let _configLandTemp = null; let _brandFarm = housingTemp_animal.getChildByName("midSliderContainer").getChildByName("brand_farm"); let _farmTitle = housingTemp_animal.getChildByName("infoBox").getChildByName("sliderContainer").getChildByName("farmTitle"); let _envBg = housingTemp_animal.getChildByName("EnvBg"); //根据名字切换贴图 switch (_titleName) { //牧场区 case 'pasture_01': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[6]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[0]; _configLandTemp = _configLand[24]; _farmTitle.getComponent(cc.Label).string = "牧场1区"; break; case 'pasture_02': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[7]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[0]; _configLandTemp = _configLand[25]; _farmTitle.getComponent(cc.Label).string = "牧场2区"; break; case 'pasture_03': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[8]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[0]; _configLandTemp = _configLand[26]; _farmTitle.getComponent(cc.Label).string = "牧场3区"; break; //养殖区 case 'farm_01': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[0]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[1]; _configLandTemp = _configLand[27]; _farmTitle.getComponent(cc.Label).string = "农场1区"; break; case 'farm_02': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[1]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[1]; _configLandTemp = _configLand[28]; _farmTitle.getComponent(cc.Label).string = "农场2区"; break; case 'farm_03': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[2]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[1]; _configLandTemp = _configLand[29]; _farmTitle.getComponent(cc.Label).string = "农场3区"; break; //淡水区 case 'freshwater_01': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[3]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[2]; _configLandTemp = _configLand[30]; _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13); _farmTitle.getComponent(cc.Label).string = "淡水1区"; break; case 'freshwater_02': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[4]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[2]; _configLandTemp = _configLand[31]; _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13); _farmTitle.getComponent(cc.Label).string = "淡水2区"; break; case 'freshwater_03': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[5]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[2]; _configLandTemp = _configLand[32]; _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13); _farmTitle.getComponent(cc.Label).string = "淡水3区"; break; //海水区 case 'seawater_01': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[9]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[3]; _configLandTemp = _configLand[33]; _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13); _farmTitle.getComponent(cc.Label).string = "海水1区"; break; case 'seawater_02': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[10]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[3]; _configLandTemp = _configLand[34]; _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13); _farmTitle.getComponent(cc.Label).string = "海水2区"; break; case 'seawater_03': _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[11]; _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[3]; _configLandTemp = _configLand[35]; _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13); _farmTitle.getComponent(cc.Label).string = "海水3区"; break; } return { _configLandTemp }; } });