ManageBuildings.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. var reGameStates = require('GameStates');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. //MyMapNode
  6. //建筑物的节点
  7. BuildingsParent: {
  8. default: null,
  9. type: cc.Node,
  10. },
  11. //神农专用农田
  12. Labour_205_Holy_Farmland: {
  13. default: null,
  14. type: cc.Prefab,
  15. serializable: true,
  16. },
  17. Labour_206_Holy_Animal: {
  18. default: null,
  19. type: cc.Prefab,
  20. serializable: true,
  21. },
  22. animalSpriteFrame: {
  23. default: [],
  24. type: [cc.SpriteFrame]
  25. },
  26. animalPanelSpriteFrame: {
  27. default: [],
  28. type: [cc.SpriteFrame]
  29. }
  30. },
  31. // start() {
  32. // },
  33. /**
  34. * GameData里面处理的生成数据流程,生成建筑物
  35. */
  36. InitBuildings() {
  37. /**
  38. * 注释 spawnConfigLand 不初始化固定土地
  39. */
  40. //第一步初始化土地的网络数据
  41. this.spawnConfigLand(GlobalD.UserLeaseLand, true);
  42. },
  43. //生成房子的默认值
  44. SpawnBuildingDefaultValue(buildingsInfo) {
  45. buildingsInfo.InitPos = false;
  46. buildingsInfo.InitWorkBuildingInfo = false;
  47. //激活状态,可运营
  48. buildingsInfo.buildInfo.isItActive = true;
  49. //设置销售状态
  50. buildingsInfo.buildInfo.isItSaleable = true;
  51. if (reGameStates.BuildType.Farmland == buildingsInfo.buildInfo.buildType) {
  52. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetFoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetFoodTradeState();
  53. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable);
  54. }
  55. if (reGameStates.BuildType.MiningPit == buildingsInfo.buildInfo.buildType) {
  56. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetMineralTradeState() > 0 ? true : false;// GlobalD.GameData.GetMineralTradeState();
  57. }
  58. if (reGameStates.BuildType.TimberYard == buildingsInfo.buildInfo.buildType) {
  59. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetWoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetWoodTradeState();
  60. }
  61. //设置一下,可查看信息
  62. if (reGameStates.BuildType.Housing !== buildingsInfo.buildInfo.buildType
  63. && reGameStates.BuildType.Special !== buildingsInfo.buildInfo.buildType
  64. /** 神农小镇固定土地关闭 基本的房屋信息,另外设置其他信息 */
  65. && reGameStates.BuildType.HolyFarmland !== buildingsInfo.buildInfo.buildType
  66. ) {
  67. buildingsInfo.node.getComponent('buildingsTouch').isShowBuildingInfo = true;
  68. }
  69. },
  70. /**
  71. * 按钮调用
  72. * 回到用户自己的农场
  73. */
  74. onBackSelfFarmland() {
  75. //刷新一下自己的用户信息
  76. GlobalD.GameData.getSelfUserLandList(() => {
  77. this.spawnConfigLand(GlobalD.UserLeaseLand, true);
  78. //显示底部菜单栏
  79. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  80. //收起其他用户场景后菜单操作
  81. GlobalD.game._ManageUIScript.onOtherFarmerView(false);
  82. })
  83. //更新自己的狗
  84. let _dogContainerScript = cc.find("Canvas/DogContainer").getComponent("DogContainer")
  85. _dogContainerScript.onGetSelfList();
  86. GlobalD.GameData.onSwitchAddressBg(GlobalD.Dapp.UserInfo.agent_level);
  87. },
  88. /**
  89. * 初始化后台配置的土地数据
  90. * 切换时候需要重置其他用户数据
  91. * @returns
  92. */
  93. spawnConfigLand(_userLeaseLand, bSelf) {
  94. if (GlobalD.ConfigLand == null) {
  95. console.error("GlobalD.ConfigLand 未初始化设置!");
  96. return;
  97. }
  98. if (_userLeaseLand == null) {
  99. console.error("_userLeaseLand 未初始化设置!");
  100. return;
  101. }
  102. let _configLand = GlobalD.ConfigLand;
  103. if (bSelf) {
  104. let _bInitFarmland = false;
  105. let _landBuildingsInfo = this.BuildingsParent.children;
  106. for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
  107. let resetLandInfo = _landBuildingsInfo[i];
  108. if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
  109. let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
  110. //如果存在开锁的土地,重置状态
  111. if (!leaseFarmlandInfoScript.onGetUnlockLandState()) {
  112. leaseFarmlandInfoScript.onLockLand();
  113. }
  114. leaseFarmlandInfoScript.onSwitchEnvBgFromLevel(0);
  115. for (let j = 0; j < _userLeaseLand.length; j++) {
  116. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  117. if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) {
  118. // console.log(leaseFarmlandInfoScript.initConfigLandId);
  119. let _leaseLandInfo = _userLeaseLand[j];
  120. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  121. }
  122. }
  123. _bInitFarmland = true;
  124. } else if (resetLandInfo.name == "Labour_206_Holy_Animal") {
  125. //牧场
  126. let leaseAnimalInfoScript = resetLandInfo.getComponent("LeaseAnimalInfo");
  127. //如果存在开锁的土地,重置状态
  128. if (!leaseAnimalInfoScript.onGetUnlockLandState()) {
  129. leaseAnimalInfoScript.onLockLand();
  130. }
  131. for (let j = 0; j < _userLeaseLand.length; j++) {
  132. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  133. if (_userLeaseLand[j].configLandId == leaseAnimalInfoScript.initConfigLandId) {
  134. let _leaseLandInfo = _userLeaseLand[j];
  135. leaseAnimalInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  136. }
  137. }
  138. }
  139. }
  140. if (_bInitFarmland) {
  141. console.log("已经初始化过土地数据!");
  142. return;
  143. }
  144. //获取tiled的配置点
  145. let _tiledPosArray = GlobalD.TiledMap.onGetLandList();
  146. let _tiledPosArrayAni = GlobalD.TiledMap.onGetBreedList();
  147. // console.log("_userLeaseLand", _userLeaseLand);
  148. // console.log("_tiledPosArray:", _tiledPosArray);
  149. // console.log("_tiledPosArrayAni", _tiledPosArrayAni);
  150. // console.log("_configLand:", _configLand);
  151. // if (_tiledPosArray.length != _configLand.length) {
  152. // console.error("服务器土地id配置对应地图配置错误!")
  153. // return;
  154. // }
  155. //此时是第一次生成, 目前_configLand 签名24块地就是种植相关,后面的是牧场区域
  156. for (let i = 0; i < 24; i++) {
  157. let _index = _configLand[i].id - 1;
  158. let _spawnPos = _tiledPosArray[_index];
  159. //生成默认土地
  160. //农田
  161. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_205_Holy_Farmland');
  162. housingTemp_farmland.parent = this.BuildingsParent;
  163. //cc.v2(_configLand[i].posX, _configLand[i].posY)
  164. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(_spawnPos);
  165. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  166. let leaseFarmlandInfoScript = housingTemp_farmland.getComponent("LeaseFarmlandInfo");
  167. //记录一下第一次初始化时候的id
  168. leaseFarmlandInfoScript.initConfigLandId = _configLand[i].id;
  169. // console.log(housingTemp_farmland.name +" = "+ leaseFarmlandInfoScript.initConfigLandId);
  170. //记录config的土地信息
  171. leaseFarmlandInfoScript.setConfigLandInfo(_configLand[i]);
  172. for (let j = 0; j < _userLeaseLand.length; j++) {
  173. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  174. if (_userLeaseLand[j].configLandId == _configLand[i].id) {
  175. let _leaseLandInfo = _userLeaseLand[j];
  176. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  177. }
  178. }
  179. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  180. //id用时间戳来记录
  181. buildingsInfo_farmland.buildInfo.id = _configLand[i].id;//对应的id
  182. /**
  183. * 服务器设置的农田 独立于其他游戏数据
  184. * 这步设置之后,addBuilding 时候不存储到 GameData_buildings
  185. */
  186. buildingsInfo_farmland.InitPosFromStore = true;
  187. //设置tile的最底下的坐标,就是起始坐标
  188. buildingsInfo_farmland.buildInfo.startTilePos = _spawnPos;
  189. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  190. //起始坐标,占位范围,是否占位
  191. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  192. //添加占位信息
  193. GlobalD.game.addBuilding(buildingsInfo_farmland);
  194. }
  195. //除去24块种植区域,后面是牧场区域,用配置表对应
  196. for (let i = 0; i < 12; i++) {
  197. let _id = _tiledPosArrayAni[i].id;
  198. let _spawnPos = new cc.Vec2(_tiledPosArrayAni[i].InitX, _tiledPosArrayAni[i].InitY);
  199. //生成默认养殖场
  200. let housingTemp_animal = this.onGetHousingPrefabFromName('Labour_206_Holy_Animal');
  201. housingTemp_animal.parent = this.BuildingsParent;
  202. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(_spawnPos);
  203. housingTemp_animal.setPosition(endPos.x, endPos.y);
  204. let _configLandTemp = null;
  205. let _brandFarm = housingTemp_animal.getChildByName("midSliderContainer").getChildByName("brand_farm");
  206. let _farmTitle = housingTemp_animal.getChildByName("infoBox").getChildByName("sliderContainer").getChildByName("farmTitle");
  207. let _envBg = housingTemp_animal.getChildByName("EnvBg");
  208. //根据名字切换贴图
  209. switch (_tiledPosArrayAni[i].name) {
  210. //牧场区
  211. case 'pasture_01':
  212. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[6];
  213. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[0];
  214. _configLandTemp = _configLand[24];
  215. _farmTitle.getComponent(cc.Label).string = "牧场1区";
  216. break;
  217. case 'pasture_02':
  218. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[7];
  219. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[0];
  220. _configLandTemp = _configLand[25];
  221. _farmTitle.getComponent(cc.Label).string = "牧场2区";
  222. break;
  223. case 'pasture_03':
  224. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[8];
  225. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[0];
  226. _configLandTemp = _configLand[26];
  227. _farmTitle.getComponent(cc.Label).string = "牧场3区";
  228. break;
  229. //养殖区
  230. case 'farm_01':
  231. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[0];
  232. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[1];
  233. _configLandTemp = _configLand[27];
  234. _farmTitle.getComponent(cc.Label).string = "农场1区";
  235. break;
  236. case 'farm_02':
  237. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[1];
  238. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[1];
  239. _configLandTemp = _configLand[28];
  240. _farmTitle.getComponent(cc.Label).string = "农场2区";
  241. break;
  242. case 'farm_03':
  243. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[2];
  244. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[1];
  245. _configLandTemp = _configLand[29];
  246. _farmTitle.getComponent(cc.Label).string = "农场3区";
  247. break;
  248. //淡水区
  249. case 'freshwater_01':
  250. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[3];
  251. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[2];
  252. _configLandTemp = _configLand[30];
  253. _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13);
  254. _farmTitle.getComponent(cc.Label).string = "淡水1区";
  255. break;
  256. case 'freshwater_02':
  257. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[4];
  258. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[2];
  259. _configLandTemp = _configLand[31];
  260. _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13);
  261. _farmTitle.getComponent(cc.Label).string = "淡水2区";
  262. break;
  263. case 'freshwater_03':
  264. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[5];
  265. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[2];
  266. _configLandTemp = _configLand[32];
  267. _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13);
  268. _farmTitle.getComponent(cc.Label).string = "淡水3区";
  269. break;
  270. //海水区
  271. case 'seawater_01':
  272. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[9];
  273. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[3];
  274. _configLandTemp = _configLand[33];
  275. _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13);
  276. _farmTitle.getComponent(cc.Label).string = "海水1区";
  277. break;
  278. case 'seawater_02':
  279. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[10];
  280. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[3];
  281. _configLandTemp = _configLand[34];
  282. _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13);
  283. _farmTitle.getComponent(cc.Label).string = "海水2区";
  284. break;
  285. case 'seawater_03':
  286. _envBg.getComponent(cc.Sprite).spriteFrame = this.animalSpriteFrame[11];
  287. _brandFarm.getComponent(cc.Sprite).spriteFrame = this.animalPanelSpriteFrame[3];
  288. _configLandTemp = _configLand[35];
  289. _brandFarm.setPosition(_brandFarm.getPosition(cc.Vec2).x, 13);
  290. _farmTitle.getComponent(cc.Label).string = "海水3区";
  291. break;
  292. }
  293. if (_configLandTemp == null) {
  294. console.error("_configLandTemp 错误!");
  295. return;
  296. };
  297. // console.log("_configLandTemp:", _configLandTemp);
  298. //牧场设置
  299. let leaseAnimalInfoScript = housingTemp_animal.getComponent("LeaseAnimalInfo");
  300. //记录一下第一次初始化时候的id
  301. leaseAnimalInfoScript.initConfigLandId = _configLandTemp.id;
  302. leaseAnimalInfoScript.setConfigLandInfo(_configLandTemp);
  303. for (let j = 0; j < _userLeaseLand.length; j++) {
  304. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  305. if (_userLeaseLand[j].configLandId == _configLandTemp.id) {
  306. let _leaseLandInfo = _userLeaseLand[j];
  307. leaseAnimalInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  308. }
  309. }
  310. let buildingsInfo_farmland = housingTemp_animal.getComponent("buildingsInfo");
  311. //id用时间戳来记录
  312. buildingsInfo_farmland.buildInfo.id = _id;//对应的id
  313. buildingsInfo_farmland.InitPosFromStore = true;
  314. buildingsInfo_farmland.buildInfo.startTilePos = _spawnPos;
  315. buildingsInfo_farmland.buildInfo.occupyArea = new cc.Vec2(_tiledPosArrayAni[i].tiledX, _tiledPosArrayAni[i].tiledY);
  316. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  317. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  318. GlobalD.game.addBuilding(buildingsInfo_farmland);
  319. }
  320. // console.log(this.BuildingsParent.children);
  321. } else {
  322. //访问其他用户时候,已经有土地了
  323. //重置土地信息
  324. let _landBuildingsInfo = this.BuildingsParent.children;
  325. for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
  326. let resetLandInfo = _landBuildingsInfo[i];
  327. if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
  328. let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
  329. //如果存在开锁的土地,重置状态
  330. if (!leaseFarmlandInfoScript.onGetUnlockLandState()) {
  331. leaseFarmlandInfoScript.onLockLand();
  332. }
  333. //全部设置成普通的土地
  334. leaseFarmlandInfoScript.onSwitchEnvBgFromLevel(0);
  335. //访问其他农场时候,全部不显示待租按钮
  336. leaseFarmlandInfoScript.onUnlockLand();
  337. for (let j = 0; j < _userLeaseLand.length; j++) {
  338. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  339. if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) {
  340. let _leaseLandInfo = _userLeaseLand[j];
  341. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  342. }
  343. }
  344. } else if (resetLandInfo.name == "Labour_206_Holy_Animal") {
  345. //牧场
  346. let leaseAnimalInfoScript = resetLandInfo.getComponent("LeaseAnimalInfo");
  347. //如果存在开锁的土地,重置状态
  348. if (!leaseAnimalInfoScript.onGetUnlockLandState()) {
  349. leaseAnimalInfoScript.onLockLand();
  350. }
  351. //访问其他农场时候,全部不显示待租按钮
  352. leaseAnimalInfoScript.onUnlockLand();
  353. for (let j = 0; j < _userLeaseLand.length; j++) {
  354. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  355. if (_userLeaseLand[j].configLandId == leaseAnimalInfoScript.initConfigLandId) {
  356. let _leaseLandInfo = _userLeaseLand[j];
  357. leaseAnimalInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  358. }
  359. }
  360. }
  361. }
  362. // console.log("other:", this.BuildingsParent.children);
  363. }
  364. },
  365. onGetHousingPrefabFromName(HouseName) {
  366. let housing = null;
  367. //神农专用农田
  368. if (HouseName == 'Labour_205_Holy_Farmland') {
  369. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  370. }
  371. else if (HouseName == 'Labour_206_Holy_Animal') {
  372. housing = cc.instantiate(this.Labour_206_Holy_Animal);
  373. }
  374. return housing;
  375. },
  376. //获取预制的名字
  377. onGetPrefabsBuildingName(index) {
  378. //content button 设置的index
  379. let buildingName = '';
  380. //神农专用农田
  381. if ('205' == index) {
  382. buildingName = 'Labour_205_Holy_Farmland';
  383. }
  384. else if ('206' == index) {
  385. housing = 'Labour_206_Holy_Animal';
  386. }
  387. return buildingName;
  388. }
  389. });