ManageBuildings.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. if (this.BuildingArray[i])
  362. this.BuildingArray[i].active = false;
  363. }
  364. else {
  365. if (this.BuildingArray[i])
  366. this.BuildingArray[i].active = true;
  367. }
  368. }
  369. let BuildingNumArray = GlobalD.GameData.GetBuildingNumArray();
  370. // cc.log('BuildingNumArray',BuildingNumArray)
  371. for (let i = 3; i < this.BuildingArray.length; i++) {
  372. // cc.log('this.BuildingArray[' + i + ']=' + this.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string);
  373. if (this.BuildingArray[i])
  374. this.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i];
  375. }
  376. },
  377. //生成房子的默认值
  378. SpawnBuildingDefaultValue(buildingsInfo) {
  379. buildingsInfo.InitPos = false;
  380. buildingsInfo.InitWorkBuildingInfo = false;
  381. //激活状态,可运营
  382. buildingsInfo.buildInfo.isItActive = true;
  383. //设置销售状态
  384. buildingsInfo.buildInfo.isItSaleable = true;
  385. if (reGameStates.BuildType.Farmland == buildingsInfo.buildInfo.buildType) {
  386. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetFoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetFoodTradeState();
  387. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable);
  388. }
  389. if (reGameStates.BuildType.MiningPit == buildingsInfo.buildInfo.buildType) {
  390. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetMineralTradeState() > 0 ? true : false;// GlobalD.GameData.GetMineralTradeState();
  391. }
  392. if (reGameStates.BuildType.TimberYard == buildingsInfo.buildInfo.buildType) {
  393. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetWoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetWoodTradeState();
  394. }
  395. //设置一下,可查看信息
  396. if (reGameStates.BuildType.Housing !== buildingsInfo.buildInfo.buildType
  397. && reGameStates.BuildType.Special !== buildingsInfo.buildInfo.buildType
  398. /** 神农小镇固定土地关闭 基本的房屋信息,另外设置其他信息 */
  399. && reGameStates.BuildType.HolyFarmland !== buildingsInfo.buildInfo.buildType
  400. ) {
  401. buildingsInfo.node.getComponent('buildingsTouch').isShowBuildingInfo = true;
  402. }
  403. //设置消耗的体力值为工人的最低值 20
  404. //过高的话,会导致工人不工作
  405. // buildingsInfo.buildInfo.totalConsumption = 20;
  406. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable)
  407. },
  408. //初始化场景的两个建筑
  409. SpawnDefaultHouse() {
  410. cc.log('初始化默认房子');
  411. //房子
  412. let housingTemp_house = this.onGetHousingPrefabFromName('Env_101_house_low');
  413. housingTemp_house.parent = this.BuildingsParent;
  414. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(24, 23));
  415. housingTemp_house.setPosition(endPos.x, endPos.y);
  416. let buildingsInfo_house = housingTemp_house.getComponent("buildingsInfo");
  417. //id用时间戳来记录
  418. buildingsInfo_house.buildInfo.id = 101;//对应的id
  419. //设置tile的最底下的坐标,就是起始坐标
  420. buildingsInfo_house.buildInfo.startTilePos = cc.v2(24, 23);
  421. this.SpawnBuildingDefaultValue(buildingsInfo_house);
  422. //起始坐标,占位范围,是否占位
  423. GlobalD.game.addBuildTiled(buildingsInfo_house.buildInfo.id, buildingsInfo_house.buildInfo.startTilePos, buildingsInfo_house.buildInfo.occupyArea);
  424. //添加占位信息
  425. GlobalD.game.addBuilding(buildingsInfo_house);
  426. //农田
  427. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_201_Farmland');
  428. housingTemp_farmland.parent = this.BuildingsParent;
  429. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(26, 24));
  430. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  431. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  432. //id用时间戳来记录
  433. buildingsInfo_farmland.buildInfo.id = 201;//对应的id
  434. //设置tile的最底下的坐标,就是起始坐标
  435. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(26, 24);
  436. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  437. //起始坐标,占位范围,是否占位
  438. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  439. //添加占位信息
  440. GlobalD.game.addBuilding(buildingsInfo_farmland);
  441. },
  442. //初始化后台配置的土地数据
  443. spawnConfigLand() {
  444. if (GlobalD.ConfigLand == null) {
  445. console.error("GlobalD.ConfigLand 未初始化设置!");
  446. return;
  447. }
  448. if (GlobalD.UserLeaseLand == null) {
  449. console.error("GlobalD.UserLeaseLand 未初始化设置!");
  450. return;
  451. }
  452. let _configLand = GlobalD.ConfigLand;
  453. let _userLeaseLand = GlobalD.UserLeaseLand;
  454. console.log(_configLand, _userLeaseLand);
  455. // console.log(this.node.getComponent("ManageUI"));
  456. let _buildingView = this.node.getComponent("ManageUI").BuildView.getComponent('BuildingView');
  457. for (let i = 0; i < _configLand.length; i++) {
  458. //生成默认土地
  459. //农田
  460. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_205_Holy_Farmland');
  461. housingTemp_farmland.parent = this.BuildingsParent;
  462. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(_configLand[i].posX, _configLand[i].posY));
  463. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  464. let leaseFarmlandInfoScript = housingTemp_farmland.getComponent("LeaseFarmlandInfo");
  465. //记录config的土地信息
  466. leaseFarmlandInfoScript.setConfigLandInfo(_configLand[i]);
  467. for (let j = 0; j < _userLeaseLand.length; j++) {
  468. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  469. if (_userLeaseLand[j].configLandId == _configLand[i].id) {
  470. let _leaseLandInfo = _userLeaseLand[j];
  471. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo);
  472. //todo 后续看看如何刷新,现在走的是开始刷新一次
  473. if (_leaseLandInfo.isPlant === 1) {
  474. let _workingBuilding = housingTemp_farmland.getComponent("WorkingBuilding");
  475. let _spriteFrame;
  476. switch (_leaseLandInfo.seedInfo.picture) {
  477. case "Cabbage":
  478. _spriteFrame = _buildingView.fruitSpriteFrame[0];
  479. break;
  480. case "Potato":
  481. _spriteFrame = _buildingView.fruitSpriteFrame[1];
  482. break;
  483. case "Carrot":
  484. _spriteFrame = _buildingView.fruitSpriteFrame[2];
  485. break;
  486. case "Broccoli":
  487. _spriteFrame = _buildingView.fruitSpriteFrame[3];
  488. break;
  489. case "Tomato":
  490. _spriteFrame = _buildingView.fruitSpriteFrame[4];
  491. break;
  492. case "Squash":
  493. _spriteFrame = _buildingView.fruitSpriteFrame[5];
  494. break;
  495. case "Eggplant":
  496. _spriteFrame = _buildingView.fruitSpriteFrame[6];
  497. break;
  498. case "Pepper":
  499. _spriteFrame = _buildingView.fruitSpriteFrame[7];
  500. break;
  501. default:
  502. break;
  503. }
  504. //拿到土地的成熟期来判断
  505. let _currentDay = date.datedifference(date.formatTime(new Date()), _leaseLandInfo.plantStart);
  506. let _ratio = _currentDay / _leaseLandInfo.seedInfo.maturity;
  507. if (_ratio <= 0.3) {
  508. _workingBuilding.onSetGrow(1, _spriteFrame);
  509. //测试,后面删除
  510. // leaseFarmlandInfoScript.onShowHarvest();
  511. } else if (_ratio <= 0.7) {
  512. _workingBuilding.onSetGrow(2, _spriteFrame);
  513. } else {
  514. //成熟阶段
  515. _workingBuilding.onSetGrow(3, _spriteFrame);
  516. if (_currentDay >= _leaseLandInfo.seedInfo.maturity) {
  517. //todo,如果时间小的,走收获,收获成果实
  518. console.log("土地:", _userLeaseLand[j].configLandId, "已经成熟,可以操作收取果实了!!", _currentDay);
  519. //todo 土地可收获状态
  520. leaseFarmlandInfoScript.onShowHarvest();
  521. }
  522. }
  523. }
  524. }
  525. }
  526. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  527. //id用时间戳来记录
  528. buildingsInfo_farmland.buildInfo.id = _configLand[i].id;//对应的id
  529. /**
  530. * 服务器设置的农田 独立于其他游戏数据
  531. * 这步设置之后,addBuilding 时候不存储到 GameData_buildings
  532. */
  533. buildingsInfo_farmland.InitPosFromStore = true;
  534. //设置tile的最底下的坐标,就是起始坐标
  535. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(_configLand[i].posX, _configLand[i].posY);
  536. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  537. //起始坐标,占位范围,是否占位
  538. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  539. //添加占位信息
  540. GlobalD.game.addBuilding(buildingsInfo_farmland);
  541. }
  542. },
  543. //初始化场景建筑
  544. SpawnHouse: function () {
  545. let BuildingJSON = Object.assign([], GlobalD.GameData.GameData_buildings);
  546. //拿到数据后,清空 GlobalD.GameData.GameData_buildings 数据,下面再添加上去
  547. GlobalD.GameData.GameData_buildings.length = 0;
  548. cc.log('BuildingJSON', BuildingJSON);
  549. // if (BuildingJSON.length == 0) {
  550. // //初始化默认房子
  551. // this.SpawnDefaultHouse();
  552. // return
  553. // };
  554. for (let i = 0; i < BuildingJSON.length; i++) {
  555. let HouseName = BuildingJSON[i].buildingNodeName;
  556. let housingTemp = this.onGetHousingPrefabFromName(HouseName);
  557. if (!housingTemp)
  558. cc.error('housingTemp', HouseName, housingTemp);
  559. //预制为空
  560. if (!housingTemp) return;
  561. let buildingsInfo = housingTemp.getComponent("buildingsInfo");
  562. /**
  563. * 处理数据库数据冲突问题
  564. * 这里处理判断初始化位置是否被占用,占用了回收到仓库背包系统
  565. * BuildingJSON 看做旧的数据, 拿到旧数据 BuildingJSON[i].startTilePos 起始位置 ,生成对应预制的大小occupyArea
  566. * 来判断占位信息
  567. */
  568. let _canNotBuild = GlobalD.game.areTheraOverlappingAreas(BuildingJSON[i].startTilePos, buildingsInfo.buildInfo.occupyArea, 1);
  569. if (_canNotBuild) {
  570. // 背包添加对应的建筑数量
  571. GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
  572. // 删除上面生成的预制节点
  573. housingTemp.destroy();
  574. //跳过生成
  575. continue;
  576. }
  577. housingTemp.parent = this.BuildingsParent;
  578. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(BuildingJSON[i].startTilePos);
  579. housingTemp.setPosition(endPos.x, endPos.y);
  580. //id用时间戳来记录
  581. buildingsInfo.buildInfo.id = BuildingJSON[i].buildingId;//对应的id
  582. //设置tile的最底下的坐标,就是起始坐标
  583. buildingsInfo.buildInfo.startTilePos = BuildingJSON[i].startTilePos;
  584. //初始化位置的坐标
  585. buildingsInfo.InitStartPos = BuildingJSON[i].startTilePos;
  586. this.SpawnBuildingDefaultValue(buildingsInfo);
  587. //目前应该只有商店,后面根据RunningCost来判断 todo.....
  588. // if (reGameStates.BuildType.Shop == buildingsInfo.buildInfo.buildType)
  589. {
  590. // buildingsInfo.buildInfo.isItSaleable = Number(BuildingJSON[i].isItSaleable) > 0 ? true : false;
  591. buildingsInfo.buildInfo.isItStopOperation = Number(BuildingJSON[i].isItStopOperation) > 0 ? true : false;
  592. //如果不可以运营的,加上牌子
  593. if (buildingsInfo.buildInfo.isItStopOperation) {
  594. GlobalD.game.onAddBuildingTipView(buildingsInfo);
  595. }
  596. }
  597. //建筑物的加成值
  598. buildingsInfo.buildInfo.consumeStrengthAddValue = Number(BuildingJSON[i].consumeStrengthAddValue);
  599. buildingsInfo.buildInfo.goodsPriceAddValue = Number(BuildingJSON[i].goodsPriceAddValue);
  600. /**
  601. * 判断是否占位的时候,需要进一步处理。放回背包,然后背包物品加上对应的数量
  602. */
  603. //起始坐标,占位范围,是否占位
  604. GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
  605. //添加占位信息
  606. GlobalD.game.addBuilding(buildingsInfo);
  607. }
  608. console.log("GlobalD.GameData.GameData_buildings:", GlobalD.GameData.GameData_buildings);
  609. },
  610. onGetHousingPrefabFromName(HouseName) {
  611. let housing = null;
  612. //住宿地方
  613. if (HouseName == 'Env_101_house_low') {
  614. housing = cc.instantiate(this.Env_101_house_low);
  615. }
  616. else if (HouseName === 'Env_102_house_mid') {
  617. housing = cc.instantiate(this.Env_102_house_mid);
  618. }
  619. else if (HouseName == 'Env_103_house_high') {
  620. housing = cc.instantiate(this.Env_103_house_high);
  621. }
  622. else if (HouseName == 'Env_104_BlueCastle') {
  623. housing = cc.instantiate(this.Env_104_BlueCastle);
  624. }
  625. else if (HouseName == 'Env_105_PinkCastle') {
  626. housing = cc.instantiate(this.Env_105_PinkCastle);
  627. }
  628. //劳动地方
  629. else if (HouseName == 'Labour_201_Farmland') {
  630. housing = cc.instantiate(this.Labour_201_Farmland);
  631. }
  632. else if (HouseName == 'Labour_202_TimberYard') {
  633. housing = cc.instantiate(this.Labour_202_TimberYard);
  634. }
  635. else if (HouseName == 'Labour_203_MiningPit') {
  636. housing = cc.instantiate(this.Labour_203_MiningPit);
  637. }
  638. else if (HouseName == 'Labour_204_Factory') {
  639. housing = cc.instantiate(this.Labour_204_Factory);
  640. }
  641. //神农专用农田
  642. else if (HouseName == 'Labour_205_Holy_Farmland') {
  643. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  644. }
  645. //特殊建筑
  646. else if (HouseName == 'Spe_401_StreetLamp') {
  647. housing = cc.instantiate(this.Spe_401_StreetLamp);
  648. }
  649. else if (HouseName == 'Spe_402_GreenBelt') {
  650. housing = cc.instantiate(this.Spe_402_GreenBelt);
  651. }
  652. else if (HouseName == 'Spe_403_FlowerBed') {
  653. housing = cc.instantiate(this.Spe_403_FlowerBed);
  654. }
  655. else if (HouseName == 'Spe_404_Pool') {
  656. housing = cc.instantiate(this.Spe_404_Pool);
  657. }
  658. else if (HouseName == 'Spe_405_PoliceOffice') {
  659. housing = cc.instantiate(this.Spe_405_PoliceOffice);
  660. }
  661. else if (HouseName == 'Spe_406_Playground') {
  662. housing = cc.instantiate(this.Spe_406_Playground);
  663. }
  664. //商店类型
  665. else if (HouseName == 'Shops_30101_ColdDrinkStall') {
  666. housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
  667. }
  668. else if (HouseName == 'Shops_30102_SalesMachine') {
  669. housing = cc.instantiate(this.Shops_30102_SalesMachine);
  670. }
  671. else if (HouseName == 'Shops_30103_Bakery') {
  672. housing = cc.instantiate(this.Shops_30103_Bakery);
  673. }
  674. else if (HouseName == 'Shops_30104_BreakfastCar') {
  675. housing = cc.instantiate(this.Shops_30104_BreakfastCar);
  676. }
  677. else if (HouseName == 'Shops_30201_TeaShop') {
  678. housing = cc.instantiate(this.Shops_30201_TeaShop);
  679. }
  680. else if (HouseName == 'Shops_30202_confectaurant') {
  681. housing = cc.instantiate(this.Shops_30202_confectaurant);
  682. }
  683. else if (HouseName == 'Shops_30203_GourmetRestaurant') {
  684. housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
  685. }
  686. else if (HouseName == 'Shops_30204_WesternRestaurant') {
  687. housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
  688. }
  689. else if (HouseName == 'Shops_30301_Florist') {
  690. housing = cc.instantiate(this.Shops_30301_Florist);
  691. }
  692. else if (HouseName == 'Shops_30302_HairSalon') {
  693. housing = cc.instantiate(this.Shops_30302_HairSalon);
  694. }
  695. else if (HouseName == 'Shops_30303_DressShop') {
  696. housing = cc.instantiate(this.Shops_30303_DressShop);
  697. }
  698. else if (HouseName == 'Shops_30304_JewelryStore') {
  699. housing = cc.instantiate(this.Shops_30304_JewelryStore);
  700. }
  701. else if (HouseName == 'Shops_30305_Cinema') {
  702. housing = cc.instantiate(this.Shops_30305_Cinema);
  703. }
  704. return housing;
  705. },
  706. //获取预制的名字
  707. onGetPrefabsBuildingName(index) {
  708. //content button 设置的index
  709. let buildingName = '';
  710. //农舍
  711. if ('101' == index) {
  712. buildingName = 'Env_101_house_low';
  713. }
  714. //单元楼
  715. else if ('102' == index) {
  716. buildingName = 'Env_102_house_mid';
  717. }
  718. //别墅
  719. else if ('103' == index) {
  720. buildingName = 'Env_103_house_high';
  721. }
  722. //蓝色城堡
  723. else if ('104' == index) {
  724. buildingName = 'Env_104_BlueCastle';
  725. }
  726. //粉色城堡
  727. else if ('105' == index) {
  728. buildingName = 'Env_105_PinkCastle';
  729. }
  730. //农田
  731. else if ('201' == index) {
  732. buildingName = 'Labour_201_Farmland';
  733. }
  734. //伐木场
  735. else if ('202' == index) {
  736. buildingName = 'Labour_202_TimberYard';
  737. }
  738. //矿坑
  739. else if ('203' == index) {
  740. buildingName = 'Labour_203_MiningPit';
  741. }
  742. //工厂
  743. else if ('204' == index) {
  744. buildingName = 'Labour_204_Factory';
  745. }
  746. //神农专用农田
  747. else if ('205' == index) {
  748. buildingName = 'Labour_205_Holy_Farmland';
  749. }
  750. //冷饮摊
  751. else if ('30101' == index) {
  752. buildingName = 'Shops_30101_ColdDrinkStall';
  753. }
  754. //贩卖机
  755. else if ('30102' == index) {
  756. buildingName = 'Shops_30102_SalesMachine';
  757. }
  758. //面包房
  759. else if ('30103' == index) {
  760. buildingName = 'Shops_30103_Bakery';
  761. }
  762. //早餐车
  763. else if ('30104' == index) {
  764. buildingName = 'Shops_30104_BreakfastCar';
  765. }
  766. //饮茶店
  767. else if ('30201' == index) {
  768. buildingName = 'Shops_30201_TeaShop';
  769. }
  770. //点心店
  771. else if ('30202' == index) {
  772. buildingName = 'Shops_30202_confectaurant';
  773. }
  774. //美食店
  775. else if ('30203' == index) {
  776. buildingName = 'Shops_30203_GourmetRestaurant';
  777. }
  778. //西餐厅
  779. else if ('30204' == index) {
  780. buildingName = 'Shops_30204_WesternRestaurant';
  781. }
  782. //花店
  783. else if ('30301' == index) {
  784. buildingName = 'Shops_30301_Florist';
  785. }
  786. //美发店
  787. else if ('30302' == index) {
  788. buildingName = 'Shops_30302_HairSalon';
  789. }
  790. //洋装店
  791. else if ('30303' == index) {
  792. buildingName = 'Shops_30303_DressShop';
  793. }
  794. //珠宝店
  795. else if ('30304' == index) {
  796. buildingName = 'Shops_30304_JewelryStore';
  797. }
  798. //电影院
  799. else if ('30305' == index) {
  800. buildingName = 'Shops_30305_Cinema';
  801. }
  802. //路灯
  803. else if ('401' == index) {
  804. buildingName = 'Spe_401_StreetLamp';
  805. }
  806. //绿化带
  807. else if ('402' == index) {
  808. buildingName = 'Spe_402_GreenBelt';
  809. }
  810. //花坛
  811. else if ('403' == index) {
  812. buildingName = 'Spe_403_FlowerBed';
  813. }
  814. //喷泉
  815. else if ('404' == index) {
  816. buildingName = 'Spe_404_Pool';
  817. }
  818. //警察局
  819. else if ('405' == index) {
  820. buildingName = 'Spe_405_PoliceOffice';
  821. }
  822. //游乐场
  823. else if ('406' == index) {
  824. buildingName = 'Spe_406_Playground';
  825. }
  826. return buildingName;
  827. },
  828. Grant() {
  829. },
  830. Sale() {
  831. },
  832. Exchange() {
  833. }
  834. });