ManageBuildings.js 32 KB

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