ManageBuildings.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 _plantStart = Date.parse(_leaseLandInfo.plantStart.replace(/-/g, '/'))
  506. // let _currentDay = _plantStart.getTime() - new Date().getTime(); //date.datedifference(date.formatTime(new Date()), _leaseLandInfo.plantStart);
  507. //种植日期加成熟期
  508. let _endMilli = date.addDayTranMillisecond(_leaseLandInfo.plantStart, _leaseLandInfo.seedInfo.maturity);
  509. let _currentMilli = new Date().getTime();
  510. let _ratio = _currentMilli / _endMilli;
  511. if (_ratio <= 0.3) {
  512. _workingBuilding.onSetGrow(1, _spriteFrame);
  513. //测试,后面删除
  514. // leaseFarmlandInfoScript.onShowHarvest();
  515. } else if (_ratio < 1) {
  516. _workingBuilding.onSetGrow(2, _spriteFrame);
  517. } else {
  518. //成熟阶段
  519. _workingBuilding.onSetGrow(3, _spriteFrame);
  520. //todo,如果时间小的,走收获,收获成果实
  521. // console.log("土地:", _userLeaseLand[j].configLandId, "已经成熟,可以操作收取果实了!!");
  522. //todo 土地可收获状态
  523. leaseFarmlandInfoScript.onShowHarvest();
  524. }
  525. }
  526. }
  527. }
  528. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  529. //id用时间戳来记录
  530. buildingsInfo_farmland.buildInfo.id = _configLand[i].id;//对应的id
  531. /**
  532. * 服务器设置的农田 独立于其他游戏数据
  533. * 这步设置之后,addBuilding 时候不存储到 GameData_buildings
  534. */
  535. buildingsInfo_farmland.InitPosFromStore = true;
  536. //设置tile的最底下的坐标,就是起始坐标
  537. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(_configLand[i].posX, _configLand[i].posY);
  538. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  539. //起始坐标,占位范围,是否占位
  540. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  541. //添加占位信息
  542. GlobalD.game.addBuilding(buildingsInfo_farmland);
  543. }
  544. },
  545. //初始化场景建筑
  546. SpawnHouse: function () {
  547. let BuildingJSON = Object.assign([], GlobalD.GameData.GameData_buildings);
  548. //拿到数据后,清空 GlobalD.GameData.GameData_buildings 数据,下面再添加上去
  549. GlobalD.GameData.GameData_buildings.length = 0;
  550. // cc.log('BuildingJSON', BuildingJSON);
  551. // if (BuildingJSON.length == 0) {
  552. // //初始化默认房子
  553. // this.SpawnDefaultHouse();
  554. // return
  555. // };
  556. for (let i = 0; i < BuildingJSON.length; i++) {
  557. let HouseName = BuildingJSON[i].buildingNodeName;
  558. let housingTemp = this.onGetHousingPrefabFromName(HouseName);
  559. if (!housingTemp)
  560. cc.error('housingTemp', HouseName, housingTemp);
  561. //预制为空
  562. if (!housingTemp) return;
  563. let buildingsInfo = housingTemp.getComponent("buildingsInfo");
  564. /**
  565. * 处理数据库数据冲突问题
  566. * 这里处理判断初始化位置是否被占用,占用了回收到仓库背包系统
  567. * BuildingJSON 看做旧的数据, 拿到旧数据 BuildingJSON[i].startTilePos 起始位置 ,生成对应预制的大小occupyArea
  568. * 来判断占位信息
  569. */
  570. let _canNotBuild = GlobalD.game.areTheraOverlappingAreas(BuildingJSON[i].startTilePos, buildingsInfo.buildInfo.occupyArea, 1);
  571. if (_canNotBuild) {
  572. // 背包添加对应的建筑数量
  573. GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
  574. // 删除上面生成的预制节点
  575. housingTemp.destroy();
  576. //跳过生成
  577. continue;
  578. }
  579. housingTemp.parent = this.BuildingsParent;
  580. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(BuildingJSON[i].startTilePos);
  581. housingTemp.setPosition(endPos.x, endPos.y);
  582. //id用时间戳来记录
  583. buildingsInfo.buildInfo.id = BuildingJSON[i].buildingId;//对应的id
  584. //设置tile的最底下的坐标,就是起始坐标
  585. buildingsInfo.buildInfo.startTilePos = BuildingJSON[i].startTilePos;
  586. //初始化位置的坐标
  587. buildingsInfo.InitStartPos = BuildingJSON[i].startTilePos;
  588. this.SpawnBuildingDefaultValue(buildingsInfo);
  589. //目前应该只有商店,后面根据RunningCost来判断 todo.....
  590. // if (reGameStates.BuildType.Shop == buildingsInfo.buildInfo.buildType)
  591. {
  592. // buildingsInfo.buildInfo.isItSaleable = Number(BuildingJSON[i].isItSaleable) > 0 ? true : false;
  593. buildingsInfo.buildInfo.isItStopOperation = Number(BuildingJSON[i].isItStopOperation) > 0 ? true : false;
  594. //如果不可以运营的,加上牌子
  595. if (buildingsInfo.buildInfo.isItStopOperation) {
  596. GlobalD.game.onAddBuildingTipView(buildingsInfo);
  597. }
  598. }
  599. //建筑物的加成值
  600. buildingsInfo.buildInfo.consumeStrengthAddValue = Number(BuildingJSON[i].consumeStrengthAddValue);
  601. buildingsInfo.buildInfo.goodsPriceAddValue = Number(BuildingJSON[i].goodsPriceAddValue);
  602. /**
  603. * 判断是否占位的时候,需要进一步处理。放回背包,然后背包物品加上对应的数量
  604. */
  605. //起始坐标,占位范围,是否占位
  606. GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
  607. //添加占位信息
  608. GlobalD.game.addBuilding(buildingsInfo);
  609. }
  610. // console.log("GlobalD.GameData.GameData_buildings:", GlobalD.GameData.GameData_buildings);
  611. },
  612. onGetHousingPrefabFromName(HouseName) {
  613. let housing = null;
  614. //住宿地方
  615. if (HouseName == 'Env_101_house_low') {
  616. housing = cc.instantiate(this.Env_101_house_low);
  617. }
  618. else if (HouseName === 'Env_102_house_mid') {
  619. housing = cc.instantiate(this.Env_102_house_mid);
  620. }
  621. else if (HouseName == 'Env_103_house_high') {
  622. housing = cc.instantiate(this.Env_103_house_high);
  623. }
  624. else if (HouseName == 'Env_104_BlueCastle') {
  625. housing = cc.instantiate(this.Env_104_BlueCastle);
  626. }
  627. else if (HouseName == 'Env_105_PinkCastle') {
  628. housing = cc.instantiate(this.Env_105_PinkCastle);
  629. }
  630. //劳动地方
  631. else if (HouseName == 'Labour_201_Farmland') {
  632. housing = cc.instantiate(this.Labour_201_Farmland);
  633. }
  634. else if (HouseName == 'Labour_202_TimberYard') {
  635. housing = cc.instantiate(this.Labour_202_TimberYard);
  636. }
  637. else if (HouseName == 'Labour_203_MiningPit') {
  638. housing = cc.instantiate(this.Labour_203_MiningPit);
  639. }
  640. else if (HouseName == 'Labour_204_Factory') {
  641. housing = cc.instantiate(this.Labour_204_Factory);
  642. }
  643. //神农专用农田
  644. else if (HouseName == 'Labour_205_Holy_Farmland') {
  645. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  646. }
  647. //特殊建筑
  648. else if (HouseName == 'Spe_401_StreetLamp') {
  649. housing = cc.instantiate(this.Spe_401_StreetLamp);
  650. }
  651. else if (HouseName == 'Spe_402_GreenBelt') {
  652. housing = cc.instantiate(this.Spe_402_GreenBelt);
  653. }
  654. else if (HouseName == 'Spe_403_FlowerBed') {
  655. housing = cc.instantiate(this.Spe_403_FlowerBed);
  656. }
  657. else if (HouseName == 'Spe_404_Pool') {
  658. housing = cc.instantiate(this.Spe_404_Pool);
  659. }
  660. else if (HouseName == 'Spe_405_PoliceOffice') {
  661. housing = cc.instantiate(this.Spe_405_PoliceOffice);
  662. }
  663. else if (HouseName == 'Spe_406_Playground') {
  664. housing = cc.instantiate(this.Spe_406_Playground);
  665. }
  666. //商店类型
  667. else if (HouseName == 'Shops_30101_ColdDrinkStall') {
  668. housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
  669. }
  670. else if (HouseName == 'Shops_30102_SalesMachine') {
  671. housing = cc.instantiate(this.Shops_30102_SalesMachine);
  672. }
  673. else if (HouseName == 'Shops_30103_Bakery') {
  674. housing = cc.instantiate(this.Shops_30103_Bakery);
  675. }
  676. else if (HouseName == 'Shops_30104_BreakfastCar') {
  677. housing = cc.instantiate(this.Shops_30104_BreakfastCar);
  678. }
  679. else if (HouseName == 'Shops_30201_TeaShop') {
  680. housing = cc.instantiate(this.Shops_30201_TeaShop);
  681. }
  682. else if (HouseName == 'Shops_30202_confectaurant') {
  683. housing = cc.instantiate(this.Shops_30202_confectaurant);
  684. }
  685. else if (HouseName == 'Shops_30203_GourmetRestaurant') {
  686. housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
  687. }
  688. else if (HouseName == 'Shops_30204_WesternRestaurant') {
  689. housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
  690. }
  691. else if (HouseName == 'Shops_30301_Florist') {
  692. housing = cc.instantiate(this.Shops_30301_Florist);
  693. }
  694. else if (HouseName == 'Shops_30302_HairSalon') {
  695. housing = cc.instantiate(this.Shops_30302_HairSalon);
  696. }
  697. else if (HouseName == 'Shops_30303_DressShop') {
  698. housing = cc.instantiate(this.Shops_30303_DressShop);
  699. }
  700. else if (HouseName == 'Shops_30304_JewelryStore') {
  701. housing = cc.instantiate(this.Shops_30304_JewelryStore);
  702. }
  703. else if (HouseName == 'Shops_30305_Cinema') {
  704. housing = cc.instantiate(this.Shops_30305_Cinema);
  705. }
  706. return housing;
  707. },
  708. //获取预制的名字
  709. onGetPrefabsBuildingName(index) {
  710. //content button 设置的index
  711. let buildingName = '';
  712. //农舍
  713. if ('101' == index) {
  714. buildingName = 'Env_101_house_low';
  715. }
  716. //单元楼
  717. else if ('102' == index) {
  718. buildingName = 'Env_102_house_mid';
  719. }
  720. //别墅
  721. else if ('103' == index) {
  722. buildingName = 'Env_103_house_high';
  723. }
  724. //蓝色城堡
  725. else if ('104' == index) {
  726. buildingName = 'Env_104_BlueCastle';
  727. }
  728. //粉色城堡
  729. else if ('105' == index) {
  730. buildingName = 'Env_105_PinkCastle';
  731. }
  732. //农田
  733. else if ('201' == index) {
  734. buildingName = 'Labour_201_Farmland';
  735. }
  736. //伐木场
  737. else if ('202' == index) {
  738. buildingName = 'Labour_202_TimberYard';
  739. }
  740. //矿坑
  741. else if ('203' == index) {
  742. buildingName = 'Labour_203_MiningPit';
  743. }
  744. //工厂
  745. else if ('204' == index) {
  746. buildingName = 'Labour_204_Factory';
  747. }
  748. //神农专用农田
  749. else if ('205' == index) {
  750. buildingName = 'Labour_205_Holy_Farmland';
  751. }
  752. //冷饮摊
  753. else if ('30101' == index) {
  754. buildingName = 'Shops_30101_ColdDrinkStall';
  755. }
  756. //贩卖机
  757. else if ('30102' == index) {
  758. buildingName = 'Shops_30102_SalesMachine';
  759. }
  760. //面包房
  761. else if ('30103' == index) {
  762. buildingName = 'Shops_30103_Bakery';
  763. }
  764. //早餐车
  765. else if ('30104' == index) {
  766. buildingName = 'Shops_30104_BreakfastCar';
  767. }
  768. //饮茶店
  769. else if ('30201' == index) {
  770. buildingName = 'Shops_30201_TeaShop';
  771. }
  772. //点心店
  773. else if ('30202' == index) {
  774. buildingName = 'Shops_30202_confectaurant';
  775. }
  776. //美食店
  777. else if ('30203' == index) {
  778. buildingName = 'Shops_30203_GourmetRestaurant';
  779. }
  780. //西餐厅
  781. else if ('30204' == index) {
  782. buildingName = 'Shops_30204_WesternRestaurant';
  783. }
  784. //花店
  785. else if ('30301' == index) {
  786. buildingName = 'Shops_30301_Florist';
  787. }
  788. //美发店
  789. else if ('30302' == index) {
  790. buildingName = 'Shops_30302_HairSalon';
  791. }
  792. //洋装店
  793. else if ('30303' == index) {
  794. buildingName = 'Shops_30303_DressShop';
  795. }
  796. //珠宝店
  797. else if ('30304' == index) {
  798. buildingName = 'Shops_30304_JewelryStore';
  799. }
  800. //电影院
  801. else if ('30305' == index) {
  802. buildingName = 'Shops_30305_Cinema';
  803. }
  804. //路灯
  805. else if ('401' == index) {
  806. buildingName = 'Spe_401_StreetLamp';
  807. }
  808. //绿化带
  809. else if ('402' == index) {
  810. buildingName = 'Spe_402_GreenBelt';
  811. }
  812. //花坛
  813. else if ('403' == index) {
  814. buildingName = 'Spe_403_FlowerBed';
  815. }
  816. //喷泉
  817. else if ('404' == index) {
  818. buildingName = 'Spe_404_Pool';
  819. }
  820. //警察局
  821. else if ('405' == index) {
  822. buildingName = 'Spe_405_PoliceOffice';
  823. }
  824. //游乐场
  825. else if ('406' == index) {
  826. buildingName = 'Spe_406_Playground';
  827. }
  828. return buildingName;
  829. },
  830. Grant() {
  831. },
  832. Sale() {
  833. },
  834. Exchange() {
  835. }
  836. });