ManageBuildings.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. var reGameStates = require('GameStates');
  2. import date from "../Unit/date.js"
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. //MyMapNode
  7. //建筑物的节点
  8. BuildingsParent: {
  9. default: null,
  10. type: cc.Node,
  11. },
  12. //农舍
  13. Env_101_house_low: {
  14. default: null,
  15. type: cc.Prefab,
  16. serializable: true,
  17. },
  18. //单元楼
  19. Env_102_house_mid: {
  20. default: null,
  21. type: cc.Prefab,
  22. serializable: true,
  23. },
  24. //别墅
  25. Env_103_house_high: {
  26. default: null,
  27. type: cc.Prefab,
  28. serializable: true,
  29. },
  30. //蓝色城堡
  31. Env_104_BlueCastle: {
  32. default: null,
  33. type: cc.Prefab,
  34. serializable: true,
  35. },
  36. //粉色城堡
  37. Env_105_PinkCastle: {
  38. default: null,
  39. type: cc.Prefab,
  40. serializable: true,
  41. },
  42. //农田
  43. Labour_201_Farmland: {
  44. default: null,
  45. type: cc.Prefab,
  46. serializable: true,
  47. },
  48. //伐木场
  49. Labour_202_TimberYard: {
  50. default: null,
  51. type: cc.Prefab,
  52. serializable: true,
  53. },
  54. //矿坑
  55. Labour_203_MiningPit: {
  56. default: null,
  57. type: cc.Prefab,
  58. serializable: true,
  59. },
  60. //工厂
  61. Labour_204_Factory: {
  62. default: null,
  63. type: cc.Prefab,
  64. serializable: true,
  65. },
  66. //神农专用农田
  67. Labour_205_Holy_Farmland: {
  68. default: null,
  69. type: cc.Prefab,
  70. serializable: true,
  71. },
  72. //一级
  73. // 冷饮摊
  74. Shops_30101_ColdDrinkStall: {
  75. default: null,
  76. type: cc.Prefab,
  77. serializable: true,
  78. },
  79. //贩卖机
  80. Shops_30102_SalesMachine: {
  81. default: null,
  82. type: cc.Prefab,
  83. serializable: true,
  84. },
  85. //面包房
  86. Shops_30103_Bakery: {
  87. default: null,
  88. type: cc.Prefab,
  89. serializable: true,
  90. },
  91. //早餐车
  92. Shops_30104_BreakfastCar: {
  93. default: null,
  94. type: cc.Prefab,
  95. serializable: true,
  96. },
  97. //二级
  98. //饮茶店
  99. Shops_30201_TeaShop: {
  100. default: null,
  101. type: cc.Prefab,
  102. serializable: true,
  103. },
  104. //点心店
  105. Shops_30202_confectaurant: {
  106. default: null,
  107. type: cc.Prefab,
  108. serializable: true,
  109. },
  110. //美食店
  111. Shops_30203_GourmetRestaurant: {
  112. default: null,
  113. type: cc.Prefab,
  114. serializable: true,
  115. },
  116. //西餐厅
  117. Shops_30204_WesternRestaurant: {
  118. default: null,
  119. type: cc.Prefab,
  120. serializable: true,
  121. },
  122. //三级
  123. //花店
  124. Shops_30301_Florist: {
  125. default: null,
  126. type: cc.Prefab,
  127. serializable: true,
  128. },
  129. //美发店
  130. Shops_30302_HairSalon: {
  131. default: null,
  132. type: cc.Prefab,
  133. serializable: true,
  134. },
  135. //洋装店
  136. Shops_30303_DressShop: {
  137. default: null,
  138. type: cc.Prefab,
  139. serializable: true,
  140. },
  141. //珠宝店
  142. Shops_30304_JewelryStore: {
  143. default: null,
  144. type: cc.Prefab,
  145. serializable: true,
  146. },
  147. //电影院
  148. Shops_30305_Cinema: {
  149. default: null,
  150. type: cc.Prefab,
  151. serializable: true,
  152. },
  153. //特殊建筑
  154. //路灯
  155. Spe_401_StreetLamp: {
  156. default: null,
  157. type: cc.Prefab,
  158. serializable: true,
  159. },
  160. //绿化带
  161. Spe_402_GreenBelt: {
  162. default: null,
  163. type: cc.Prefab,
  164. serializable: true,
  165. },
  166. //花坛
  167. Spe_403_FlowerBed: {
  168. default: null,
  169. type: cc.Prefab,
  170. serializable: true,
  171. },
  172. //喷泉
  173. Spe_404_Pool: {
  174. default: null,
  175. type: cc.Prefab,
  176. serializable: true,
  177. },
  178. //警察局
  179. Spe_405_PoliceOffice: {
  180. default: null,
  181. type: cc.Prefab,
  182. serializable: true,
  183. },
  184. //游乐场
  185. Spe_406_Playground: {
  186. default: null,
  187. type: cc.Prefab,
  188. serializable: true,
  189. },
  190. BuildingArray: {
  191. default: [],
  192. type: [cc.Node],
  193. serializable: true,
  194. },
  195. },
  196. onBuildHouse(index) {
  197. // cc.log(index);
  198. let mainCamera = GlobalD.game.MainCamera;
  199. let housing = null;
  200. //content button 设置的index
  201. //农舍
  202. if ('101' == index) {
  203. housing = cc.instantiate(this.Env_101_house_low);
  204. }
  205. //单元楼
  206. else if ('102' == index) {
  207. housing = cc.instantiate(this.Env_102_house_mid);
  208. }
  209. //别墅
  210. else if ('103' == index) {
  211. housing = cc.instantiate(this.Env_103_house_high);
  212. }
  213. else if ('104' == index) {
  214. housing = cc.instantiate(this.Env_104_BlueCastle);
  215. }
  216. else if ('105' == index) {
  217. housing = cc.instantiate(this.Env_105_PinkCastle);
  218. }
  219. //农田
  220. else if ('201' == index) {
  221. housing = cc.instantiate(this.Labour_201_Farmland);
  222. }
  223. //伐木场
  224. else if ('202' == index) {
  225. housing = cc.instantiate(this.Labour_202_TimberYard);
  226. }
  227. //矿坑
  228. else if ('203' == index) {
  229. housing = cc.instantiate(this.Labour_203_MiningPit);
  230. }
  231. //工厂
  232. else if ('204' == index) {
  233. housing = cc.instantiate(this.Labour_204_Factory);
  234. }
  235. //神农专用农田
  236. else if ('205' == index) {
  237. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  238. }
  239. //冷饮摊
  240. else if ('30101' == index) {
  241. housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
  242. }
  243. //贩卖机
  244. else if ('30102' == index) {
  245. housing = cc.instantiate(this.Shops_30102_SalesMachine);
  246. }
  247. //面包房
  248. else if ('30103' == index) {
  249. housing = cc.instantiate(this.Shops_30103_Bakery);
  250. }
  251. //早餐车
  252. else if ('30104' == index) {
  253. housing = cc.instantiate(this.Shops_30104_BreakfastCar);
  254. }
  255. //饮茶店
  256. else if ('30201' == index) {
  257. housing = cc.instantiate(this.Shops_30201_TeaShop);
  258. }
  259. //点心店
  260. else if ('30202' == index) {
  261. housing = cc.instantiate(this.Shops_30202_confectaurant);
  262. }
  263. //美食店
  264. else if ('30203' == index) {
  265. housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
  266. }
  267. //西餐厅
  268. else if ('30204' == index) {
  269. housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
  270. }
  271. //花店
  272. else if ('30301' == index) {
  273. housing = cc.instantiate(this.Shops_30301_Florist);
  274. }
  275. //美发店
  276. else if ('30302' == index) {
  277. housing = cc.instantiate(this.Shops_30302_HairSalon);
  278. }
  279. //洋装店
  280. else if ('30303' == index) {
  281. housing = cc.instantiate(this.Shops_30303_DressShop);
  282. }
  283. //珠宝店
  284. else if ('30304' == index) {
  285. housing = cc.instantiate(this.Shops_30304_JewelryStore);
  286. }
  287. //电影院
  288. else if ('30305' == index) {
  289. housing = cc.instantiate(this.Shops_30305_Cinema);
  290. }
  291. //路灯
  292. else if ('401' == index) {
  293. housing = cc.instantiate(this.Spe_401_StreetLamp);
  294. }
  295. //绿化带
  296. else if ('402' == index) {
  297. housing = cc.instantiate(this.Spe_402_GreenBelt);
  298. }
  299. //花坛
  300. else if ('403' == index) {
  301. housing = cc.instantiate(this.Spe_403_FlowerBed);
  302. }
  303. //喷泉
  304. else if ('404' == index) {
  305. housing = cc.instantiate(this.Spe_404_Pool);
  306. }
  307. //警察局
  308. else if ('405' == index) {
  309. housing = cc.instantiate(this.Spe_405_PoliceOffice);
  310. }
  311. //游乐场
  312. else if ('406' == index) {
  313. housing = cc.instantiate(this.Spe_406_Playground);
  314. }
  315. housing.parent = this.BuildingsParent;
  316. let tempPos = housing.parent.convertToNodeSpace(mainCamera.node);
  317. let CanvasPos = GlobalD.game.Canvas.position;
  318. housing.setPosition(tempPos.x + CanvasPos.x, tempPos.y + CanvasPos.y);
  319. let buildingsInfo = housing.getComponent("buildingsInfo");
  320. //id用时间戳来记录
  321. buildingsInfo.buildInfo.id = new Date().getTime();//+= Math.random() * 1000000;
  322. this.SpawnBuildingDefaultValue(buildingsInfo);
  323. let buildingsTouch = housing.getComponent("buildingsTouch");
  324. // buildingsTouch.buildInfo = buildingsInfo.buildInfo;
  325. buildingsTouch.onEditorStatus(true);
  326. // housing.active = true;
  327. //收起菜单
  328. GlobalD.game._ManageUIScript.onHideMenu();
  329. //收起底部菜单栏
  330. GlobalD.game._ManageUIScript.onBottomMenuView(false);
  331. },
  332. start() {
  333. },
  334. //如果存储到有房子数据的话,把地图上的房屋都隐藏起来,或者删除
  335. //todo...
  336. onHideInitPosBuildings() {
  337. //删除对应层的子节点
  338. let tempNode = this.BuildingsParent.children;
  339. let length = tempNode.length;
  340. for (let i = length - 1; i >= 0; i--) {
  341. tempNode[i].active = false;
  342. }
  343. },
  344. /**
  345. * GameData里面处理的生成数据流程,生成建筑物
  346. */
  347. InitBuildings() {
  348. //如果有存储的数据,隐藏场景房屋节点
  349. // this.onHideInitPosBuildings();
  350. /**
  351. * 注释 spawnConfigLand 不初始化固定土地
  352. */
  353. //第一步初始化土地的网络数据
  354. this.spawnConfigLand();
  355. //先初始化网络数据
  356. this.SpawnHouse();
  357. // cc.log('初始化场景建筑');
  358. let BuildingStateArray = GlobalD.GameData.GetBuildingStateArray();
  359. for (let i = 0; i < BuildingStateArray.length; i++) {
  360. if (BuildingStateArray[i] == 0) {
  361. this.BuildingArray[i].active = false;
  362. }
  363. else {
  364. this.BuildingArray[i].active = true;
  365. }
  366. }
  367. let BuildingNumArray = GlobalD.GameData.GetBuildingNumArray();
  368. // cc.log('BuildingNumArray',BuildingNumArray)
  369. for (let i = 3; i < this.BuildingArray.length; i++) {
  370. // cc.log('this.BuildingArray['+i+']='+this.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string);
  371. this.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i];
  372. }
  373. //初始化两个默认的建筑
  374. //
  375. },
  376. //生成房子的默认值
  377. SpawnBuildingDefaultValue(buildingsInfo) {
  378. buildingsInfo.InitPos = false;
  379. buildingsInfo.InitWorkBuildingInfo = false;
  380. //激活状态,可运营
  381. buildingsInfo.buildInfo.isItActive = true;
  382. //设置销售状态
  383. buildingsInfo.buildInfo.isItSaleable = true;
  384. if (reGameStates.BuildType.Farmland == buildingsInfo.buildInfo.buildType) {
  385. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetFoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetFoodTradeState();
  386. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable);
  387. }
  388. if (reGameStates.BuildType.MiningPit == buildingsInfo.buildInfo.buildType) {
  389. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetMineralTradeState() > 0 ? true : false;// GlobalD.GameData.GetMineralTradeState();
  390. }
  391. if (reGameStates.BuildType.TimberYard == buildingsInfo.buildInfo.buildType) {
  392. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetWoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetWoodTradeState();
  393. }
  394. //设置一下,可查看信息
  395. if (reGameStates.BuildType.Housing !== buildingsInfo.buildInfo.buildType
  396. && reGameStates.BuildType.Special !== buildingsInfo.buildInfo.buildType
  397. /** 神农小镇固定土地关闭 基本的房屋信息,另外设置其他信息 */
  398. && reGameStates.BuildType.HolyFarmland !== buildingsInfo.buildInfo.buildType
  399. ) {
  400. buildingsInfo.node.getComponent('buildingsTouch').isShowBuildingInfo = true;
  401. }
  402. //设置消耗的体力值为工人的最低值 20
  403. //过高的话,会导致工人不工作
  404. // buildingsInfo.buildInfo.totalConsumption = 20;
  405. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable)
  406. },
  407. //初始化场景的两个建筑
  408. SpawnDefaultHouse() {
  409. cc.log('初始化默认房子');
  410. //房子
  411. let housingTemp_house = this.onGetHousingPrefabFromName('Env_101_house_low');
  412. housingTemp_house.parent = this.BuildingsParent;
  413. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(24, 23));
  414. housingTemp_house.setPosition(endPos.x, endPos.y);
  415. let buildingsInfo_house = housingTemp_house.getComponent("buildingsInfo");
  416. //id用时间戳来记录
  417. buildingsInfo_house.buildInfo.id = 101;//对应的id
  418. //设置tile的最底下的坐标,就是起始坐标
  419. buildingsInfo_house.buildInfo.startTilePos = cc.v2(24, 23);
  420. this.SpawnBuildingDefaultValue(buildingsInfo_house);
  421. //起始坐标,占位范围,是否占位
  422. GlobalD.game.addBuildTiled(buildingsInfo_house.buildInfo.id, buildingsInfo_house.buildInfo.startTilePos, buildingsInfo_house.buildInfo.occupyArea);
  423. //添加占位信息
  424. GlobalD.game.addBuilding(buildingsInfo_house);
  425. //农田
  426. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_201_Farmland');
  427. housingTemp_farmland.parent = this.BuildingsParent;
  428. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(26, 24));
  429. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  430. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  431. //id用时间戳来记录
  432. buildingsInfo_farmland.buildInfo.id = 201;//对应的id
  433. //设置tile的最底下的坐标,就是起始坐标
  434. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(26, 24);
  435. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  436. //起始坐标,占位范围,是否占位
  437. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  438. //添加占位信息
  439. GlobalD.game.addBuilding(buildingsInfo_farmland);
  440. },
  441. //初始化后台配置的土地数据
  442. spawnConfigLand() {
  443. if (GlobalD.ConfigLand == null) {
  444. console.error("GlobalD.ConfigLand 未初始化设置!");
  445. return;
  446. }
  447. if (GlobalD.UserLeaseLand == null) {
  448. console.error("GlobalD.UserLeaseLand 未初始化设置!");
  449. return;
  450. }
  451. let _configLand = GlobalD.ConfigLand;
  452. let _userLeaseLand = GlobalD.UserLeaseLand;
  453. console.log(_configLand, _userLeaseLand);
  454. // console.log(this.node.getComponent("ManageUI"));
  455. let _buildingView = this.node.getComponent("ManageUI").BuildView.getComponent('BuildingView');
  456. for (let i = 0; i < _configLand.length; i++) {
  457. //生成默认土地
  458. //农田
  459. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_205_Holy_Farmland');
  460. housingTemp_farmland.parent = this.BuildingsParent;
  461. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(_configLand[i].posX, _configLand[i].posY));
  462. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  463. for (let j = 0; j < _userLeaseLand.length; j++) {
  464. let leaseFarmlandInfoScript = housingTemp_farmland.getComponent("LeaseFarmlandInfo");
  465. //记录config的土地信息
  466. leaseFarmlandInfoScript.setConfigLandInfo(_configLand[i]);
  467. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  468. if (_userLeaseLand[j].configLandId == _configLand[i].id) {
  469. let _leaseLandInfo = _userLeaseLand[j];
  470. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo);
  471. //todo 后续看看如何刷新,现在走的是开始刷新一次
  472. if (_leaseLandInfo.isPlant === 1) {
  473. let _workingBuilding = housingTemp_farmland.getComponent("WorkingBuilding");
  474. let _spriteFrame;
  475. switch (_leaseLandInfo.seedInfo.picture) {
  476. case "Cabbage":
  477. _spriteFrame = _buildingView.fruitSpriteFrame[0];
  478. break;
  479. case "Potato":
  480. _spriteFrame = _buildingView.fruitSpriteFrame[1];
  481. break;
  482. case "Carrot":
  483. _spriteFrame = _buildingView.fruitSpriteFrame[2];
  484. break;
  485. case "Broccoli":
  486. _spriteFrame = _buildingView.fruitSpriteFrame[3];
  487. break;
  488. case "Tomato":
  489. _spriteFrame = _buildingView.fruitSpriteFrame[4];
  490. break;
  491. case "Squash":
  492. _spriteFrame = _buildingView.fruitSpriteFrame[5];
  493. break;
  494. case "Eggplant":
  495. _spriteFrame = _buildingView.fruitSpriteFrame[6];
  496. break;
  497. case "Pepper":
  498. _spriteFrame = _buildingView.fruitSpriteFrame[7];
  499. break;
  500. default:
  501. break;
  502. }
  503. //拿到土地的成熟期来判断
  504. let _currentDay = date.datedifference(date.formatTime(new Date()), _leaseLandInfo.plantStart);
  505. let _ratio = _currentDay / _leaseLandInfo.seedInfo.maturity;
  506. if (_ratio <= 0.3) {
  507. _workingBuilding.onSetGrow(1, _spriteFrame);
  508. //测试,后面删除
  509. // leaseFarmlandInfoScript.onShowHarvest();
  510. } else if (_ratio <= 0.7) {
  511. _workingBuilding.onSetGrow(2, _spriteFrame);
  512. } else {
  513. //成熟阶段
  514. _workingBuilding.onSetGrow(3, _spriteFrame);
  515. if (_currentDay >= _leaseLandInfo.seedInfo.maturity) {
  516. //todo,如果时间小的,走收获,收获成果实
  517. console.log("土地:", _userLeaseLand[j].configLandId, "已经成熟,可以操作收取果实了!!", _currentDay);
  518. //todo 土地可收获状态
  519. leaseFarmlandInfoScript.onShowHarvest();
  520. }
  521. }
  522. }
  523. }
  524. }
  525. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  526. //id用时间戳来记录
  527. buildingsInfo_farmland.buildInfo.id = _configLand[i].id;//对应的id
  528. /**
  529. * 服务器设置的农田 独立于其他游戏数据
  530. * 这步设置之后,addBuilding 时候不存储到 GameData_buildings
  531. */
  532. buildingsInfo_farmland.InitPosFromStore = true;
  533. //设置tile的最底下的坐标,就是起始坐标
  534. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(_configLand[i].posX, _configLand[i].posY);
  535. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  536. //起始坐标,占位范围,是否占位
  537. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  538. //添加占位信息
  539. GlobalD.game.addBuilding(buildingsInfo_farmland);
  540. }
  541. },
  542. //初始化场景建筑
  543. SpawnHouse: function () {
  544. let BuildingJSON = Object.assign([], GlobalD.GameData.GameData_buildings);
  545. //拿到数据后,清空 GlobalD.GameData.GameData_buildings 数据,下面再添加上去
  546. GlobalD.GameData.GameData_buildings.length = 0;
  547. cc.log('BuildingJSON', BuildingJSON);
  548. // if (BuildingJSON.length == 0) {
  549. // //初始化默认房子
  550. // this.SpawnDefaultHouse();
  551. // return
  552. // };
  553. for (let i = 0; i < BuildingJSON.length; i++) {
  554. let HouseName = BuildingJSON[i].buildingNodeName;
  555. let housingTemp = this.onGetHousingPrefabFromName(HouseName);
  556. if (!housingTemp)
  557. cc.error('housingTemp', HouseName, housingTemp);
  558. //预制为空
  559. if (!housingTemp) return;
  560. let buildingsInfo = housingTemp.getComponent("buildingsInfo");
  561. /**
  562. * 处理数据库数据冲突问题
  563. * 这里处理判断初始化位置是否被占用,占用了回收到仓库背包系统
  564. * BuildingJSON 看做旧的数据, 拿到旧数据 BuildingJSON[i].startTilePos 起始位置 ,生成对应预制的大小occupyArea
  565. * 来判断占位信息
  566. */
  567. let _canNotBuild = GlobalD.game.areTheraOverlappingAreas(BuildingJSON[i].startTilePos, buildingsInfo.buildInfo.occupyArea, 1);
  568. if (_canNotBuild) {
  569. // 背包添加对应的建筑数量
  570. GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
  571. // 删除上面生成的预制节点
  572. housingTemp.destroy();
  573. //跳过生成
  574. continue;
  575. }
  576. housingTemp.parent = this.BuildingsParent;
  577. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(BuildingJSON[i].startTilePos);
  578. housingTemp.setPosition(endPos.x, endPos.y);
  579. //id用时间戳来记录
  580. buildingsInfo.buildInfo.id = BuildingJSON[i].buildingId;//对应的id
  581. //设置tile的最底下的坐标,就是起始坐标
  582. buildingsInfo.buildInfo.startTilePos = BuildingJSON[i].startTilePos;
  583. //初始化位置的坐标
  584. buildingsInfo.InitStartPos = BuildingJSON[i].startTilePos;
  585. this.SpawnBuildingDefaultValue(buildingsInfo);
  586. //目前应该只有商店,后面根据RunningCost来判断 todo.....
  587. // if (reGameStates.BuildType.Shop == buildingsInfo.buildInfo.buildType)
  588. {
  589. // buildingsInfo.buildInfo.isItSaleable = Number(BuildingJSON[i].isItSaleable) > 0 ? true : false;
  590. buildingsInfo.buildInfo.isItStopOperation = Number(BuildingJSON[i].isItStopOperation) > 0 ? true : false;
  591. //如果不可以运营的,加上牌子
  592. if (buildingsInfo.buildInfo.isItStopOperation) {
  593. GlobalD.game.onAddBuildingTipView(buildingsInfo);
  594. }
  595. }
  596. //建筑物的加成值
  597. buildingsInfo.buildInfo.consumeStrengthAddValue = Number(BuildingJSON[i].consumeStrengthAddValue);
  598. buildingsInfo.buildInfo.goodsPriceAddValue = Number(BuildingJSON[i].goodsPriceAddValue);
  599. /**
  600. * 判断是否占位的时候,需要进一步处理。放回背包,然后背包物品加上对应的数量
  601. */
  602. //起始坐标,占位范围,是否占位
  603. GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
  604. //添加占位信息
  605. GlobalD.game.addBuilding(buildingsInfo);
  606. }
  607. console.log("GlobalD.GameData.GameData_buildings:", GlobalD.GameData.GameData_buildings);
  608. },
  609. onGetHousingPrefabFromName(HouseName) {
  610. let housing = null;
  611. //住宿地方
  612. if (HouseName == 'Env_101_house_low') {
  613. housing = cc.instantiate(this.Env_101_house_low);
  614. }
  615. else if (HouseName === 'Env_102_house_mid') {
  616. housing = cc.instantiate(this.Env_102_house_mid);
  617. }
  618. else if (HouseName == 'Env_103_house_high') {
  619. housing = cc.instantiate(this.Env_103_house_high);
  620. }
  621. else if (HouseName == 'Env_104_BlueCastle') {
  622. housing = cc.instantiate(this.Env_104_BlueCastle);
  623. }
  624. else if (HouseName == 'Env_105_PinkCastle') {
  625. housing = cc.instantiate(this.Env_105_PinkCastle);
  626. }
  627. //劳动地方
  628. else if (HouseName == 'Labour_201_Farmland') {
  629. housing = cc.instantiate(this.Labour_201_Farmland);
  630. }
  631. else if (HouseName == 'Labour_202_TimberYard') {
  632. housing = cc.instantiate(this.Labour_202_TimberYard);
  633. }
  634. else if (HouseName == 'Labour_203_MiningPit') {
  635. housing = cc.instantiate(this.Labour_203_MiningPit);
  636. }
  637. else if (HouseName == 'Labour_204_Factory') {
  638. housing = cc.instantiate(this.Labour_204_Factory);
  639. }
  640. //神农专用农田
  641. else if (HouseName == 'Labour_205_Holy_Farmland') {
  642. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  643. }
  644. //特殊建筑
  645. else if (HouseName == 'Spe_401_StreetLamp') {
  646. housing = cc.instantiate(this.Spe_401_StreetLamp);
  647. }
  648. else if (HouseName == 'Spe_402_GreenBelt') {
  649. housing = cc.instantiate(this.Spe_402_GreenBelt);
  650. }
  651. else if (HouseName == 'Spe_403_FlowerBed') {
  652. housing = cc.instantiate(this.Spe_403_FlowerBed);
  653. }
  654. else if (HouseName == 'Spe_404_Pool') {
  655. housing = cc.instantiate(this.Spe_404_Pool);
  656. }
  657. else if (HouseName == 'Spe_405_PoliceOffice') {
  658. housing = cc.instantiate(this.Spe_405_PoliceOffice);
  659. }
  660. else if (HouseName == 'Spe_406_Playground') {
  661. housing = cc.instantiate(this.Spe_406_Playground);
  662. }
  663. //商店类型
  664. else if (HouseName == 'Shops_30101_ColdDrinkStall') {
  665. housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
  666. }
  667. else if (HouseName == 'Shops_30102_SalesMachine') {
  668. housing = cc.instantiate(this.Shops_30102_SalesMachine);
  669. }
  670. else if (HouseName == 'Shops_30103_Bakery') {
  671. housing = cc.instantiate(this.Shops_30103_Bakery);
  672. }
  673. else if (HouseName == 'Shops_30104_BreakfastCar') {
  674. housing = cc.instantiate(this.Shops_30104_BreakfastCar);
  675. }
  676. else if (HouseName == 'Shops_30201_TeaShop') {
  677. housing = cc.instantiate(this.Shops_30201_TeaShop);
  678. }
  679. else if (HouseName == 'Shops_30202_confectaurant') {
  680. housing = cc.instantiate(this.Shops_30202_confectaurant);
  681. }
  682. else if (HouseName == 'Shops_30203_GourmetRestaurant') {
  683. housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
  684. }
  685. else if (HouseName == 'Shops_30204_WesternRestaurant') {
  686. housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
  687. }
  688. else if (HouseName == 'Shops_30301_Florist') {
  689. housing = cc.instantiate(this.Shops_30301_Florist);
  690. }
  691. else if (HouseName == 'Shops_30302_HairSalon') {
  692. housing = cc.instantiate(this.Shops_30302_HairSalon);
  693. }
  694. else if (HouseName == 'Shops_30303_DressShop') {
  695. housing = cc.instantiate(this.Shops_30303_DressShop);
  696. }
  697. else if (HouseName == 'Shops_30304_JewelryStore') {
  698. housing = cc.instantiate(this.Shops_30304_JewelryStore);
  699. }
  700. else if (HouseName == 'Shops_30305_Cinema') {
  701. housing = cc.instantiate(this.Shops_30305_Cinema);
  702. }
  703. return housing;
  704. },
  705. //获取预制的名字
  706. onGetPrefabsBuildingName(index) {
  707. //content button 设置的index
  708. let buildingName = '';
  709. //农舍
  710. if ('101' == index) {
  711. buildingName = 'Env_101_house_low';
  712. }
  713. //单元楼
  714. else if ('102' == index) {
  715. buildingName = 'Env_102_house_mid';
  716. }
  717. //别墅
  718. else if ('103' == index) {
  719. buildingName = 'Env_103_house_high';
  720. }
  721. //蓝色城堡
  722. else if ('104' == index) {
  723. buildingName = 'Env_104_BlueCastle';
  724. }
  725. //粉色城堡
  726. else if ('105' == index) {
  727. buildingName = 'Env_105_PinkCastle';
  728. }
  729. //农田
  730. else if ('201' == index) {
  731. buildingName = 'Labour_201_Farmland';
  732. }
  733. //伐木场
  734. else if ('202' == index) {
  735. buildingName = 'Labour_202_TimberYard';
  736. }
  737. //矿坑
  738. else if ('203' == index) {
  739. buildingName = 'Labour_203_MiningPit';
  740. }
  741. //工厂
  742. else if ('204' == index) {
  743. buildingName = 'Labour_204_Factory';
  744. }
  745. //神农专用农田
  746. else if ('205' == index) {
  747. buildingName = 'Labour_205_Holy_Farmland';
  748. }
  749. //冷饮摊
  750. else if ('30101' == index) {
  751. buildingName = 'Shops_30101_ColdDrinkStall';
  752. }
  753. //贩卖机
  754. else if ('30102' == index) {
  755. buildingName = 'Shops_30102_SalesMachine';
  756. }
  757. //面包房
  758. else if ('30103' == index) {
  759. buildingName = 'Shops_30103_Bakery';
  760. }
  761. //早餐车
  762. else if ('30104' == index) {
  763. buildingName = 'Shops_30104_BreakfastCar';
  764. }
  765. //饮茶店
  766. else if ('30201' == index) {
  767. buildingName = 'Shops_30201_TeaShop';
  768. }
  769. //点心店
  770. else if ('30202' == index) {
  771. buildingName = 'Shops_30202_confectaurant';
  772. }
  773. //美食店
  774. else if ('30203' == index) {
  775. buildingName = 'Shops_30203_GourmetRestaurant';
  776. }
  777. //西餐厅
  778. else if ('30204' == index) {
  779. buildingName = 'Shops_30204_WesternRestaurant';
  780. }
  781. //花店
  782. else if ('30301' == index) {
  783. buildingName = 'Shops_30301_Florist';
  784. }
  785. //美发店
  786. else if ('30302' == index) {
  787. buildingName = 'Shops_30302_HairSalon';
  788. }
  789. //洋装店
  790. else if ('30303' == index) {
  791. buildingName = 'Shops_30303_DressShop';
  792. }
  793. //珠宝店
  794. else if ('30304' == index) {
  795. buildingName = 'Shops_30304_JewelryStore';
  796. }
  797. //电影院
  798. else if ('30305' == index) {
  799. buildingName = 'Shops_30305_Cinema';
  800. }
  801. //路灯
  802. else if ('401' == index) {
  803. buildingName = 'Spe_401_StreetLamp';
  804. }
  805. //绿化带
  806. else if ('402' == index) {
  807. buildingName = 'Spe_402_GreenBelt';
  808. }
  809. //花坛
  810. else if ('403' == index) {
  811. buildingName = 'Spe_403_FlowerBed';
  812. }
  813. //喷泉
  814. else if ('404' == index) {
  815. buildingName = 'Spe_404_Pool';
  816. }
  817. //警察局
  818. else if ('405' == index) {
  819. buildingName = 'Spe_405_PoliceOffice';
  820. }
  821. //游乐场
  822. else if ('406' == index) {
  823. buildingName = 'Spe_406_Playground';
  824. }
  825. return buildingName;
  826. },
  827. Grant() {
  828. },
  829. Sale() {
  830. }
  831. });