ManageBuildings.js 22 KB

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