ManageBuildings.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. * 定义一个场景预制对象
  197. */
  198. DressUpArray: {
  199. default: [],
  200. type: [cc.Prefab]
  201. }
  202. },
  203. onBuildHouse(index) {
  204. // cc.log(index);
  205. let mainCamera = GlobalD.game.MainCamera;
  206. let housing = null;
  207. //content button 设置的index
  208. //农舍
  209. if ('101' == index) {
  210. housing = cc.instantiate(this.Env_101_house_low);
  211. }
  212. //单元楼
  213. else if ('102' == index) {
  214. housing = cc.instantiate(this.Env_102_house_mid);
  215. }
  216. //别墅
  217. else if ('103' == index) {
  218. housing = cc.instantiate(this.Env_103_house_high);
  219. }
  220. else if ('104' == index) {
  221. housing = cc.instantiate(this.Env_104_BlueCastle);
  222. }
  223. else if ('105' == index) {
  224. housing = cc.instantiate(this.Env_105_PinkCastle);
  225. }
  226. //农田
  227. else if ('201' == index) {
  228. housing = cc.instantiate(this.Labour_201_Farmland);
  229. }
  230. //伐木场
  231. else if ('202' == index) {
  232. housing = cc.instantiate(this.Labour_202_TimberYard);
  233. }
  234. //矿坑
  235. else if ('203' == index) {
  236. housing = cc.instantiate(this.Labour_203_MiningPit);
  237. }
  238. //工厂
  239. else if ('204' == index) {
  240. housing = cc.instantiate(this.Labour_204_Factory);
  241. }
  242. //神农专用农田
  243. else if ('205' == index) {
  244. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  245. }
  246. //冷饮摊
  247. else if ('30101' == index) {
  248. housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
  249. }
  250. //贩卖机
  251. else if ('30102' == index) {
  252. housing = cc.instantiate(this.Shops_30102_SalesMachine);
  253. }
  254. //面包房
  255. else if ('30103' == index) {
  256. housing = cc.instantiate(this.Shops_30103_Bakery);
  257. }
  258. //早餐车
  259. else if ('30104' == index) {
  260. housing = cc.instantiate(this.Shops_30104_BreakfastCar);
  261. }
  262. //饮茶店
  263. else if ('30201' == index) {
  264. housing = cc.instantiate(this.Shops_30201_TeaShop);
  265. }
  266. //点心店
  267. else if ('30202' == index) {
  268. housing = cc.instantiate(this.Shops_30202_confectaurant);
  269. }
  270. //美食店
  271. else if ('30203' == index) {
  272. housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
  273. }
  274. //西餐厅
  275. else if ('30204' == index) {
  276. housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
  277. }
  278. //花店
  279. else if ('30301' == index) {
  280. housing = cc.instantiate(this.Shops_30301_Florist);
  281. }
  282. //美发店
  283. else if ('30302' == index) {
  284. housing = cc.instantiate(this.Shops_30302_HairSalon);
  285. }
  286. //洋装店
  287. else if ('30303' == index) {
  288. housing = cc.instantiate(this.Shops_30303_DressShop);
  289. }
  290. //珠宝店
  291. else if ('30304' == index) {
  292. housing = cc.instantiate(this.Shops_30304_JewelryStore);
  293. }
  294. //电影院
  295. else if ('30305' == index) {
  296. housing = cc.instantiate(this.Shops_30305_Cinema);
  297. }
  298. //路灯
  299. else if ('401' == index) {
  300. housing = cc.instantiate(this.Spe_401_StreetLamp);
  301. }
  302. //绿化带
  303. else if ('402' == index) {
  304. housing = cc.instantiate(this.Spe_402_GreenBelt);
  305. }
  306. //花坛
  307. else if ('403' == index) {
  308. housing = cc.instantiate(this.Spe_403_FlowerBed);
  309. }
  310. //喷泉
  311. else if ('404' == index) {
  312. housing = cc.instantiate(this.Spe_404_Pool);
  313. }
  314. //警察局
  315. else if ('405' == index) {
  316. housing = cc.instantiate(this.Spe_405_PoliceOffice);
  317. }
  318. //游乐场
  319. else if ('406' == index) {
  320. housing = cc.instantiate(this.Spe_406_Playground);
  321. }
  322. housing.parent = this.BuildingsParent;
  323. let tempPos = housing.parent.convertToNodeSpace(mainCamera.node);
  324. let CanvasPos = GlobalD.game.Canvas.position;
  325. housing.setPosition(tempPos.x + CanvasPos.x, tempPos.y + CanvasPos.y);
  326. let buildingsInfo = housing.getComponent("buildingsInfo");
  327. //id用时间戳来记录
  328. buildingsInfo.buildInfo.id = new Date().getTime();//+= Math.random() * 1000000;
  329. this.SpawnBuildingDefaultValue(buildingsInfo);
  330. let buildingsTouch = housing.getComponent("buildingsTouch");
  331. // buildingsTouch.buildInfo = buildingsInfo.buildInfo;
  332. buildingsTouch.onEditorStatus(true);
  333. // housing.active = true;
  334. //收起菜单
  335. GlobalD.game._ManageUIScript.onHideMenu();
  336. //收起底部菜单栏
  337. GlobalD.game._ManageUIScript.onBottomMenuView(false);
  338. },
  339. start() {
  340. },
  341. //如果存储到有房子数据的话,把地图上的房屋都隐藏起来,或者删除
  342. //todo...
  343. onHideInitPosBuildings() {
  344. //删除对应层的子节点
  345. let tempNode = this.BuildingsParent.children;
  346. let length = tempNode.length;
  347. for (let i = length - 1; i >= 0; i--) {
  348. tempNode[i].active = false;
  349. }
  350. },
  351. /**
  352. * GameData里面处理的生成数据流程,生成建筑物
  353. */
  354. InitBuildings() {
  355. //如果有存储的数据,隐藏场景房屋节点
  356. // this.onHideInitPosBuildings();
  357. /**
  358. * 注释 spawnConfigLand 不初始化固定土地
  359. */
  360. //第一步初始化土地的网络数据
  361. this.spawnConfigLand(GlobalD.UserLeaseLand, true);
  362. /**
  363. * todo 之类初始化场景障碍物操作,如果不需要直接注释即可。
  364. * 村长和镇长
  365. * 初始化障碍物场景,装饰
  366. */
  367. // if(0 != GlobalD.Dapp.UserInfo.agent_level){
  368. // this.spawnDressUp();
  369. // }
  370. //先初始化网络数据
  371. this.SpawnHouse();
  372. // cc.log('初始化场景建筑');
  373. let BuildingStateArray = GlobalD.GameData.GetBuildingStateArray();
  374. for (let i = 0; i < BuildingStateArray.length; i++) {
  375. if (BuildingStateArray[i] == 0) {
  376. if (this.BuildingArray[i])
  377. this.BuildingArray[i].active = false;
  378. }
  379. else {
  380. if (this.BuildingArray[i])
  381. this.BuildingArray[i].active = true;
  382. }
  383. }
  384. let BuildingNumArray = GlobalD.GameData.GetBuildingNumArray();
  385. // cc.log('BuildingNumArray',BuildingNumArray)
  386. for (let i = 3; i < this.BuildingArray.length; i++) {
  387. // cc.log('this.BuildingArray[' + i + ']=' + this.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string);
  388. if (this.BuildingArray[i])
  389. this.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i];
  390. }
  391. },
  392. //生成房子的默认值
  393. SpawnBuildingDefaultValue(buildingsInfo) {
  394. buildingsInfo.InitPos = false;
  395. buildingsInfo.InitWorkBuildingInfo = false;
  396. //激活状态,可运营
  397. buildingsInfo.buildInfo.isItActive = true;
  398. //设置销售状态
  399. buildingsInfo.buildInfo.isItSaleable = true;
  400. if (reGameStates.BuildType.Farmland == buildingsInfo.buildInfo.buildType) {
  401. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetFoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetFoodTradeState();
  402. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable);
  403. }
  404. if (reGameStates.BuildType.MiningPit == buildingsInfo.buildInfo.buildType) {
  405. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetMineralTradeState() > 0 ? true : false;// GlobalD.GameData.GetMineralTradeState();
  406. }
  407. if (reGameStates.BuildType.TimberYard == buildingsInfo.buildInfo.buildType) {
  408. buildingsInfo.buildInfo.isItSaleable = GlobalD.GameData.GetWoodTradeState() > 0 ? true : false;// GlobalD.GameData.GetWoodTradeState();
  409. }
  410. //设置一下,可查看信息
  411. if (reGameStates.BuildType.Housing !== buildingsInfo.buildInfo.buildType
  412. && reGameStates.BuildType.Special !== buildingsInfo.buildInfo.buildType
  413. /** 神农小镇固定土地关闭 基本的房屋信息,另外设置其他信息 */
  414. && reGameStates.BuildType.HolyFarmland !== buildingsInfo.buildInfo.buildType
  415. ) {
  416. buildingsInfo.node.getComponent('buildingsTouch').isShowBuildingInfo = true;
  417. }
  418. //设置消耗的体力值为工人的最低值 20
  419. //过高的话,会导致工人不工作
  420. // buildingsInfo.buildInfo.totalConsumption = 20;
  421. // cc.log('初始化房子的信息:', buildingsInfo.buildInfo.buildingName, buildingsInfo.buildInfo.isItSaleable)
  422. },
  423. //初始化场景的两个建筑
  424. SpawnDefaultHouse() {
  425. cc.log('初始化默认房子');
  426. //房子
  427. let housingTemp_house = this.onGetHousingPrefabFromName('Env_101_house_low');
  428. housingTemp_house.parent = this.BuildingsParent;
  429. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(24, 23));
  430. housingTemp_house.setPosition(endPos.x, endPos.y);
  431. let buildingsInfo_house = housingTemp_house.getComponent("buildingsInfo");
  432. //id用时间戳来记录
  433. buildingsInfo_house.buildInfo.id = 101;//对应的id
  434. //设置tile的最底下的坐标,就是起始坐标
  435. buildingsInfo_house.buildInfo.startTilePos = cc.v2(24, 23);
  436. this.SpawnBuildingDefaultValue(buildingsInfo_house);
  437. //起始坐标,占位范围,是否占位
  438. GlobalD.game.addBuildTiled(buildingsInfo_house.buildInfo.id, buildingsInfo_house.buildInfo.startTilePos, buildingsInfo_house.buildInfo.occupyArea);
  439. //添加占位信息
  440. GlobalD.game.addBuilding(buildingsInfo_house);
  441. //农田
  442. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_201_Farmland');
  443. housingTemp_farmland.parent = this.BuildingsParent;
  444. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(26, 24));
  445. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  446. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  447. //id用时间戳来记录
  448. buildingsInfo_farmland.buildInfo.id = 201;//对应的id
  449. //设置tile的最底下的坐标,就是起始坐标
  450. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(26, 24);
  451. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  452. //起始坐标,占位范围,是否占位
  453. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  454. //添加占位信息
  455. GlobalD.game.addBuilding(buildingsInfo_farmland);
  456. },
  457. /**
  458. * 按钮调用
  459. * 回到用户自己的农场
  460. */
  461. onBackSelfFarmland() {
  462. //刷新一下自己的用户信息
  463. GlobalD.GameData.getSelfUserLandList(() => {
  464. this.spawnConfigLand(GlobalD.UserLeaseLand, true);
  465. //显示底部菜单栏
  466. GlobalD.game._ManageUIScript.onBottomMenuView(true);
  467. //收起其他用户场景后菜单操作
  468. GlobalD.game._ManageUIScript.onOtherFarmerView(false);
  469. })
  470. },
  471. /**
  472. * 初始化后台配置的土地数据
  473. * 切换时候需要重置其他用户数据
  474. * @returns
  475. */
  476. spawnConfigLand(_userLeaseLand, bSelf) {
  477. if (GlobalD.ConfigLand == null) {
  478. console.error("GlobalD.ConfigLand 未初始化设置!");
  479. return;
  480. }
  481. if (_userLeaseLand == null) {
  482. console.error("_userLeaseLand 未初始化设置!");
  483. return;
  484. }
  485. let _configLand = GlobalD.ConfigLand;
  486. if (bSelf) {
  487. let _bInitFarmland = false;
  488. let _landBuildingsInfo = this.BuildingsParent.children;
  489. for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
  490. let resetLandInfo = _landBuildingsInfo[i];
  491. if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
  492. let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
  493. //如果存在开锁的土地,重置状态
  494. if (!leaseFarmlandInfoScript.onGetUnlockLandState()) {
  495. leaseFarmlandInfoScript.onLockLand();
  496. }
  497. for (let j = 0; j < _userLeaseLand.length; j++) {
  498. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  499. if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) {
  500. // console.log(leaseFarmlandInfoScript.initConfigLandId);
  501. let _leaseLandInfo = _userLeaseLand[j];
  502. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  503. }
  504. }
  505. _bInitFarmland = true;
  506. }
  507. }
  508. if (_bInitFarmland) {
  509. console.log("已经初始化过土地数据!");
  510. return;
  511. }
  512. //此时是第一次生成
  513. for (let i = 0; i < _configLand.length; i++) {
  514. //生成默认土地
  515. //农田
  516. let housingTemp_farmland = this.onGetHousingPrefabFromName('Labour_205_Holy_Farmland');
  517. housingTemp_farmland.parent = this.BuildingsParent;
  518. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(cc.v2(_configLand[i].posX, _configLand[i].posY));
  519. housingTemp_farmland.setPosition(endPos.x, endPos.y);
  520. let leaseFarmlandInfoScript = housingTemp_farmland.getComponent("LeaseFarmlandInfo");
  521. //记录一下第一次初始化时候的id
  522. leaseFarmlandInfoScript.initConfigLandId = _configLand[i].id;
  523. // console.log(housingTemp_farmland.name +" = "+ leaseFarmlandInfoScript.initConfigLandId);
  524. //记录config的土地信息
  525. leaseFarmlandInfoScript.setConfigLandInfo(_configLand[i]);
  526. for (let j = 0; j < _userLeaseLand.length; j++) {
  527. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  528. if (_userLeaseLand[j].configLandId == _configLand[i].id) {
  529. let _leaseLandInfo = _userLeaseLand[j];
  530. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  531. }
  532. }
  533. let buildingsInfo_farmland = housingTemp_farmland.getComponent("buildingsInfo");
  534. //id用时间戳来记录
  535. buildingsInfo_farmland.buildInfo.id = _configLand[i].id;//对应的id
  536. /**
  537. * 服务器设置的农田 独立于其他游戏数据
  538. * 这步设置之后,addBuilding 时候不存储到 GameData_buildings
  539. */
  540. buildingsInfo_farmland.InitPosFromStore = true;
  541. //设置tile的最底下的坐标,就是起始坐标
  542. buildingsInfo_farmland.buildInfo.startTilePos = cc.v2(_configLand[i].posX, _configLand[i].posY);
  543. this.SpawnBuildingDefaultValue(buildingsInfo_farmland);
  544. //起始坐标,占位范围,是否占位
  545. GlobalD.game.addBuildTiled(buildingsInfo_farmland.buildInfo.id, buildingsInfo_farmland.buildInfo.startTilePos, buildingsInfo_farmland.buildInfo.occupyArea);
  546. //添加占位信息
  547. GlobalD.game.addBuilding(buildingsInfo_farmland);
  548. }
  549. // console.log(this.BuildingsParent.children);
  550. } else {
  551. //访问其他用户时候,已经有土地了
  552. //重置土地信息
  553. let _landBuildingsInfo = this.BuildingsParent.children;
  554. for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
  555. let resetLandInfo = _landBuildingsInfo[i];
  556. if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
  557. let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
  558. //如果存在开锁的土地,重置状态
  559. if (!leaseFarmlandInfoScript.onGetUnlockLandState()) {
  560. leaseFarmlandInfoScript.onLockLand();
  561. }
  562. //访问其他农场时候,全部不显示待租按钮
  563. leaseFarmlandInfoScript.onUnlockLand();
  564. for (let j = 0; j < _userLeaseLand.length; j++) {
  565. //记录已经租赁的信息(租赁里面包含了seedInfo,如果有种植的话,对应的种植信息会有)
  566. if (_userLeaseLand[j].configLandId == leaseFarmlandInfoScript.initConfigLandId) {
  567. let _leaseLandInfo = _userLeaseLand[j];
  568. leaseFarmlandInfoScript.setLeaseLandInfo(_leaseLandInfo, bSelf);
  569. }
  570. }
  571. }
  572. }
  573. // console.log("other:", this.BuildingsParent.children);
  574. }
  575. },
  576. /**
  577. * 初始化障碍物
  578. * 生成的预制信息,需要在 DressUpArray 添加对应的预制件,才可以添加
  579. */
  580. spawnDressUp() {
  581. //村长和镇长的区别,樱花树区别
  582. let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
  583. let _cherryTree = 2 === agent_level ? "Dress_505_SmallTree02" : "Dress_505_SmallTree01";
  584. let _temp = [
  585. //左边第一部分 ----start----
  586. {
  587. spawnName: "Dress_502_ChrisTree",
  588. startPos: cc.v2(14, 20)
  589. },
  590. //拱门
  591. {
  592. spawnName: "Dress_505_MainDoor01",
  593. startPos: cc.v2(18, 17)
  594. },
  595. {
  596. spawnName: "Dress_503_RailingGrassX",
  597. startPos: cc.v2(9, 20)
  598. },
  599. {
  600. spawnName: "Dress_503_RailingGrassX",
  601. startPos: cc.v2(7, 20)
  602. },
  603. //-----
  604. {
  605. spawnName: "Dress_505_FlowerBed",
  606. startPos: cc.v2(8, 22)
  607. },
  608. {
  609. spawnName: "Dress_505_FlowerBed",
  610. startPos: cc.v2(8, 23)
  611. },
  612. //喷泉
  613. {
  614. spawnName: "Dress_502_Fountain",
  615. startPos: cc.v2(10, 23)
  616. },
  617. {
  618. spawnName: "Dress_505_FlowerBed",
  619. startPos: cc.v2(11, 22)
  620. },
  621. {
  622. spawnName: "Dress_505_FlowerBed",
  623. startPos: cc.v2(11, 23)
  624. },
  625. {
  626. spawnName: "Dress_505_ChrisSanta",
  627. startPos: cc.v2(10, 25)
  628. },
  629. //-----
  630. //左边第一部分 ----end----
  631. //主路放两个灯和树
  632. {
  633. spawnName: "Dress_505_Streetlight",
  634. startPos: cc.v2(15, 29)
  635. },
  636. {
  637. spawnName: "Dress_505_Streetlight",
  638. startPos: cc.v2(18, 29)
  639. },
  640. {
  641. spawnName: "Dress_505_SmallTree",
  642. startPos: cc.v2(15, 30)
  643. },
  644. {
  645. spawnName: "Dress_505_SmallTree",
  646. startPos: cc.v2(18, 30)
  647. }
  648. ];
  649. //左边小麦区域
  650. // for (let x = 0; x < 3; x++) {
  651. // for (let y = 0; y < 2; y++) {
  652. // _temp.push({
  653. // spawnName: "Dress_502_Rice",
  654. // startPos: cc.v2(4 - x * 2, 30 - y * 2)
  655. // })
  656. // }
  657. // }
  658. //1倍土地部分木质花圃
  659. // for (let x = 0; x < 6; x++) {
  660. // for (let y = 0; y < 3; y++) {
  661. // _temp.push({
  662. // spawnName: "Dress_503_WhiteWoodRailingX",
  663. // startPos: cc.v2(18 + x, 14 - y * 3)
  664. // })
  665. // }
  666. // }
  667. //生成一个未盛开的樱花围绕,间隔1个空格 ---start ,镇长换成盛开的樱花
  668. // for (let x = 0; x < 3; x++) {
  669. // _temp.push({
  670. // spawnName: _cherryTree,
  671. // startPos: cc.v2(18 + x * 2, 17)
  672. // })
  673. // _temp.push({
  674. // spawnName: _cherryTree,
  675. // startPos: cc.v2(18 + x * 2, 5)
  676. // })
  677. // }
  678. // for (let y = 0; y < 7; y++) {
  679. // _temp.push({
  680. // spawnName: _cherryTree,
  681. // startPos: cc.v2(24, 17 - y * 2)
  682. // })
  683. // }
  684. //多倍区域
  685. // for (let x = 0; x < 3; x++) {
  686. // _temp.push({
  687. // spawnName: _cherryTree,
  688. // startPos: cc.v2(15 - x * 2, 14)
  689. // })
  690. // _temp.push({
  691. // spawnName: _cherryTree,
  692. // startPos: cc.v2(15 - x * 2, 2)
  693. // })
  694. // }
  695. //生成一个未盛开的樱花围绕,间隔1个空格 --- end
  696. //多倍土地部分木质花圃
  697. // for (let x = 0; x < 3; x++) {
  698. // for (let y = 0; y < 3; y++) {
  699. // _temp.push({
  700. // spawnName: "Dress_503_RailingGrassX",
  701. // startPos: cc.v2(15 - x * 2, 11 - y * 3)
  702. // })
  703. // }
  704. // }
  705. //生成番茄区域
  706. // for (let x = 0; x < 3; x++) {
  707. // for (let y = 0; y < 2; y++) {
  708. // _temp.push({
  709. // spawnName: "Dress_502_Tomato",
  710. // startPos: cc.v2(26 + x * 2, 30 - y * 2)
  711. // })
  712. // }
  713. // }
  714. //----中上别墅区域--start
  715. //生成中上三个别墅和三个树木
  716. for (let x = 0; x < 3; x++) {
  717. _temp.push({
  718. spawnName: "Dress_505_SmallTree",
  719. startPos: cc.v2(7 - x * 2, 6)
  720. })
  721. _temp.push({
  722. spawnName: "Dress_502_Villa",
  723. startPos: cc.v2(8 - x * 2, 8)
  724. })
  725. }
  726. //雪橇
  727. _temp.push({
  728. spawnName: "Dress_505_ChrisCar02",
  729. startPos: cc.v2(8, 10)
  730. })
  731. //圣诞门和栏杆
  732. for (let x = 0; x < 3; x++) {
  733. _temp.push({
  734. spawnName: "Dress_503_WhiteWoodRailingX",
  735. startPos: cc.v2(8 - x, 11)
  736. })
  737. }
  738. _temp.push({
  739. spawnName: "Dress_503_ChrisDoorX",
  740. startPos: cc.v2(5, 11)
  741. })
  742. for (let x = 0; x < 3; x++) {
  743. _temp.push({
  744. spawnName: "Dress_503_WhiteWoodRailingX",
  745. startPos: cc.v2(4 - x, 11)
  746. })
  747. }
  748. //门外区域
  749. _temp.push({
  750. spawnName: "Dress_502_florist",
  751. startPos: cc.v2(4, 15)
  752. })
  753. _temp.push({
  754. spawnName: "Dress_505_ChrisCar01",
  755. startPos: cc.v2(6, 16)
  756. })
  757. _temp.push({
  758. spawnName: "Dress_505_PondDuck",
  759. startPos: cc.v2(6, 14)
  760. })
  761. for (let y = 0; y < 3; y++) {
  762. _temp.push({
  763. spawnName: "Dress_504_ChrisRailingY",
  764. startPos: cc.v2(8, 16 - y)
  765. })
  766. }
  767. //樱花路
  768. for (let x = 0; x < 5; x++) {
  769. _temp.push({
  770. spawnName: _cherryTree,
  771. startPos: cc.v2(10 - x * 2, 17)
  772. })
  773. _temp.push({
  774. spawnName: _cherryTree,
  775. startPos: cc.v2(10 - x * 2, 19)
  776. })
  777. }
  778. //------end
  779. //----右边区域--start
  780. _temp.push({
  781. spawnName: "Dress_505_Pond",
  782. startPos: cc.v2(20, 3)
  783. })
  784. _temp.push({
  785. spawnName: "Dress_504_RailingGrassY",
  786. startPos: cc.v2(21, 3)
  787. })
  788. _temp.push({
  789. spawnName: "Dress_502_FruitTreeAndStool",
  790. startPos: cc.v2(23, 3)
  791. })
  792. //---- end
  793. for (let i = 0; i < _temp.length; i++) {
  794. for (let j = 0; j < this.DressUpArray.length; j++) {
  795. let _dressUpTemp = this.DressUpArray[j];
  796. if (_dressUpTemp.name == _temp[i].spawnName) {
  797. let prefab = cc.instantiate(_dressUpTemp);
  798. prefab.parent = this.BuildingsParent;
  799. let tipNode = prefab.getChildByName("Tip");
  800. if (tipNode) {
  801. tipNode.destroy();
  802. }
  803. let buildingsInfo = prefab.getComponent("buildingsInfo");
  804. //id用时间戳来记录
  805. buildingsInfo.buildInfo.id = new Date().getTime();//对应的id
  806. buildingsInfo.buildInfo.isItActive = false;
  807. //设置tile的最底下的坐标,就是起始坐标
  808. buildingsInfo.buildInfo.startTilePos = _temp[i].startPos;
  809. buildingsInfo.InitPos = false;
  810. buildingsInfo.InitStartPos = _temp[i].startPos;
  811. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(_temp[i].startPos);
  812. prefab.setPosition(endPos.x, endPos.y);
  813. //起始坐标,占位范围,是否占位
  814. GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
  815. //添加占位信息
  816. GlobalD.game.addBuilding(buildingsInfo);
  817. // console.log(_dressUpTemp.name);
  818. //跳出里面一层循环
  819. break;
  820. }
  821. }
  822. }
  823. },
  824. //初始化场景建筑
  825. SpawnHouse: function () {
  826. let BuildingJSON = Object.assign([], GlobalD.GameData.GameData_buildings);
  827. //拿到数据后,清空 GlobalD.GameData.GameData_buildings 数据,下面再添加上去
  828. GlobalD.GameData.GameData_buildings.length = 0;
  829. // cc.log('BuildingJSON', BuildingJSON);
  830. // if (BuildingJSON.length == 0) {
  831. // //初始化默认房子
  832. // this.SpawnDefaultHouse();
  833. // return
  834. // };
  835. for (let i = 0; i < BuildingJSON.length; i++) {
  836. let HouseName = BuildingJSON[i].buildingNodeName;
  837. let housingTemp = this.onGetHousingPrefabFromName(HouseName);
  838. if (!housingTemp)
  839. cc.error('housingTemp', HouseName, housingTemp);
  840. //预制为空
  841. if (!housingTemp) continue;
  842. let buildingsInfo = housingTemp.getComponent("buildingsInfo");
  843. /**
  844. * 处理数据库数据冲突问题
  845. * 这里处理判断初始化位置是否被占用,占用了回收到仓库背包系统
  846. * BuildingJSON 看做旧的数据, 拿到旧数据 BuildingJSON[i].startTilePos 起始位置 ,生成对应预制的大小occupyArea
  847. * 来判断占位信息
  848. */
  849. // //全部回收
  850. // // 背包添加对应的建筑数量
  851. // GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
  852. // // 删除上面生成的预制节点
  853. // housingTemp.destroy();
  854. // continue;
  855. let _canNotBuild = GlobalD.game.areTheraOverlappingAreas(BuildingJSON[i].startTilePos, buildingsInfo.buildInfo.occupyArea, 1);
  856. if (_canNotBuild) {
  857. // 背包添加对应的建筑数量
  858. GlobalD.game.PlusBuilding(buildingsInfo.buildInfo.buildingName);
  859. // 删除上面生成的预制节点
  860. housingTemp.destroy();
  861. //跳过生成
  862. continue;
  863. }
  864. housingTemp.parent = this.BuildingsParent;
  865. var endPos = GlobalD.TiledMap._getTheMiddleLocationFromtilePos(BuildingJSON[i].startTilePos);
  866. housingTemp.setPosition(endPos.x, endPos.y);
  867. //id用时间戳来记录
  868. buildingsInfo.buildInfo.id = BuildingJSON[i].buildingId;//对应的id
  869. //设置tile的最底下的坐标,就是起始坐标
  870. buildingsInfo.buildInfo.startTilePos = BuildingJSON[i].startTilePos;
  871. //初始化位置的坐标
  872. buildingsInfo.InitStartPos = BuildingJSON[i].startTilePos;
  873. this.SpawnBuildingDefaultValue(buildingsInfo);
  874. //目前应该只有商店,后面根据RunningCost来判断 todo.....
  875. // if (reGameStates.BuildType.Shop == buildingsInfo.buildInfo.buildType)
  876. {
  877. // buildingsInfo.buildInfo.isItSaleable = Number(BuildingJSON[i].isItSaleable) > 0 ? true : false;
  878. buildingsInfo.buildInfo.isItStopOperation = Number(BuildingJSON[i].isItStopOperation) > 0 ? true : false;
  879. //如果不可以运营的,加上牌子
  880. if (buildingsInfo.buildInfo.isItStopOperation) {
  881. GlobalD.game.onAddBuildingTipView(buildingsInfo);
  882. }
  883. }
  884. //建筑物的加成值
  885. buildingsInfo.buildInfo.consumeStrengthAddValue = Number(BuildingJSON[i].consumeStrengthAddValue);
  886. buildingsInfo.buildInfo.goodsPriceAddValue = Number(BuildingJSON[i].goodsPriceAddValue);
  887. /**
  888. * 判断是否占位的时候,需要进一步处理。放回背包,然后背包物品加上对应的数量
  889. */
  890. //起始坐标,占位范围,是否占位
  891. GlobalD.game.addBuildTiled(buildingsInfo.buildInfo.id, buildingsInfo.buildInfo.startTilePos, buildingsInfo.buildInfo.occupyArea);
  892. //添加占位信息
  893. GlobalD.game.addBuilding(buildingsInfo);
  894. }
  895. // console.log("GlobalD.GameData.GameData_buildings:", GlobalD.GameData.GameData_buildings);
  896. },
  897. onGetHousingPrefabFromName(HouseName) {
  898. let housing = null;
  899. //住宿地方
  900. if (HouseName == 'Env_101_house_low') {
  901. housing = cc.instantiate(this.Env_101_house_low);
  902. }
  903. else if (HouseName === 'Env_102_house_mid') {
  904. housing = cc.instantiate(this.Env_102_house_mid);
  905. }
  906. else if (HouseName == 'Env_103_house_high') {
  907. housing = cc.instantiate(this.Env_103_house_high);
  908. }
  909. else if (HouseName == 'Env_104_BlueCastle') {
  910. housing = cc.instantiate(this.Env_104_BlueCastle);
  911. }
  912. else if (HouseName == 'Env_105_PinkCastle') {
  913. housing = cc.instantiate(this.Env_105_PinkCastle);
  914. }
  915. //劳动地方
  916. else if (HouseName == 'Labour_201_Farmland') {
  917. housing = cc.instantiate(this.Labour_201_Farmland);
  918. }
  919. else if (HouseName == 'Labour_202_TimberYard') {
  920. housing = cc.instantiate(this.Labour_202_TimberYard);
  921. }
  922. else if (HouseName == 'Labour_203_MiningPit') {
  923. housing = cc.instantiate(this.Labour_203_MiningPit);
  924. }
  925. else if (HouseName == 'Labour_204_Factory') {
  926. housing = cc.instantiate(this.Labour_204_Factory);
  927. }
  928. //神农专用农田
  929. else if (HouseName == 'Labour_205_Holy_Farmland') {
  930. housing = cc.instantiate(this.Labour_205_Holy_Farmland);
  931. }
  932. //特殊建筑
  933. else if (HouseName == 'Spe_401_StreetLamp') {
  934. housing = cc.instantiate(this.Spe_401_StreetLamp);
  935. }
  936. else if (HouseName == 'Spe_402_GreenBelt') {
  937. housing = cc.instantiate(this.Spe_402_GreenBelt);
  938. }
  939. else if (HouseName == 'Spe_403_FlowerBed') {
  940. housing = cc.instantiate(this.Spe_403_FlowerBed);
  941. }
  942. else if (HouseName == 'Spe_404_Pool') {
  943. housing = cc.instantiate(this.Spe_404_Pool);
  944. }
  945. else if (HouseName == 'Spe_405_PoliceOffice') {
  946. housing = cc.instantiate(this.Spe_405_PoliceOffice);
  947. }
  948. else if (HouseName == 'Spe_406_Playground') {
  949. housing = cc.instantiate(this.Spe_406_Playground);
  950. }
  951. //商店类型
  952. else if (HouseName == 'Shops_30101_ColdDrinkStall') {
  953. housing = cc.instantiate(this.Shops_30101_ColdDrinkStall);
  954. }
  955. else if (HouseName == 'Shops_30102_SalesMachine') {
  956. housing = cc.instantiate(this.Shops_30102_SalesMachine);
  957. }
  958. else if (HouseName == 'Shops_30103_Bakery') {
  959. housing = cc.instantiate(this.Shops_30103_Bakery);
  960. }
  961. else if (HouseName == 'Shops_30104_BreakfastCar') {
  962. housing = cc.instantiate(this.Shops_30104_BreakfastCar);
  963. }
  964. else if (HouseName == 'Shops_30201_TeaShop') {
  965. housing = cc.instantiate(this.Shops_30201_TeaShop);
  966. }
  967. else if (HouseName == 'Shops_30202_confectaurant') {
  968. housing = cc.instantiate(this.Shops_30202_confectaurant);
  969. }
  970. else if (HouseName == 'Shops_30203_GourmetRestaurant') {
  971. housing = cc.instantiate(this.Shops_30203_GourmetRestaurant);
  972. }
  973. else if (HouseName == 'Shops_30204_WesternRestaurant') {
  974. housing = cc.instantiate(this.Shops_30204_WesternRestaurant);
  975. }
  976. else if (HouseName == 'Shops_30301_Florist') {
  977. housing = cc.instantiate(this.Shops_30301_Florist);
  978. }
  979. else if (HouseName == 'Shops_30302_HairSalon') {
  980. housing = cc.instantiate(this.Shops_30302_HairSalon);
  981. }
  982. else if (HouseName == 'Shops_30303_DressShop') {
  983. housing = cc.instantiate(this.Shops_30303_DressShop);
  984. }
  985. else if (HouseName == 'Shops_30304_JewelryStore') {
  986. housing = cc.instantiate(this.Shops_30304_JewelryStore);
  987. }
  988. else if (HouseName == 'Shops_30305_Cinema') {
  989. housing = cc.instantiate(this.Shops_30305_Cinema);
  990. }
  991. return housing;
  992. },
  993. //获取预制的名字
  994. onGetPrefabsBuildingName(index) {
  995. //content button 设置的index
  996. let buildingName = '';
  997. //农舍
  998. if ('101' == index) {
  999. buildingName = 'Env_101_house_low';
  1000. }
  1001. //单元楼
  1002. else if ('102' == index) {
  1003. buildingName = 'Env_102_house_mid';
  1004. }
  1005. //别墅
  1006. else if ('103' == index) {
  1007. buildingName = 'Env_103_house_high';
  1008. }
  1009. //蓝色城堡
  1010. else if ('104' == index) {
  1011. buildingName = 'Env_104_BlueCastle';
  1012. }
  1013. //粉色城堡
  1014. else if ('105' == index) {
  1015. buildingName = 'Env_105_PinkCastle';
  1016. }
  1017. //农田
  1018. else if ('201' == index) {
  1019. buildingName = 'Labour_201_Farmland';
  1020. }
  1021. //伐木场
  1022. else if ('202' == index) {
  1023. buildingName = 'Labour_202_TimberYard';
  1024. }
  1025. //矿坑
  1026. else if ('203' == index) {
  1027. buildingName = 'Labour_203_MiningPit';
  1028. }
  1029. //工厂
  1030. else if ('204' == index) {
  1031. buildingName = 'Labour_204_Factory';
  1032. }
  1033. //神农专用农田
  1034. else if ('205' == index) {
  1035. buildingName = 'Labour_205_Holy_Farmland';
  1036. }
  1037. //冷饮摊
  1038. else if ('30101' == index) {
  1039. buildingName = 'Shops_30101_ColdDrinkStall';
  1040. }
  1041. //贩卖机
  1042. else if ('30102' == index) {
  1043. buildingName = 'Shops_30102_SalesMachine';
  1044. }
  1045. //面包房
  1046. else if ('30103' == index) {
  1047. buildingName = 'Shops_30103_Bakery';
  1048. }
  1049. //早餐车
  1050. else if ('30104' == index) {
  1051. buildingName = 'Shops_30104_BreakfastCar';
  1052. }
  1053. //饮茶店
  1054. else if ('30201' == index) {
  1055. buildingName = 'Shops_30201_TeaShop';
  1056. }
  1057. //点心店
  1058. else if ('30202' == index) {
  1059. buildingName = 'Shops_30202_confectaurant';
  1060. }
  1061. //美食店
  1062. else if ('30203' == index) {
  1063. buildingName = 'Shops_30203_GourmetRestaurant';
  1064. }
  1065. //西餐厅
  1066. else if ('30204' == index) {
  1067. buildingName = 'Shops_30204_WesternRestaurant';
  1068. }
  1069. //花店
  1070. else if ('30301' == index) {
  1071. buildingName = 'Shops_30301_Florist';
  1072. }
  1073. //美发店
  1074. else if ('30302' == index) {
  1075. buildingName = 'Shops_30302_HairSalon';
  1076. }
  1077. //洋装店
  1078. else if ('30303' == index) {
  1079. buildingName = 'Shops_30303_DressShop';
  1080. }
  1081. //珠宝店
  1082. else if ('30304' == index) {
  1083. buildingName = 'Shops_30304_JewelryStore';
  1084. }
  1085. //电影院
  1086. else if ('30305' == index) {
  1087. buildingName = 'Shops_30305_Cinema';
  1088. }
  1089. //路灯
  1090. else if ('401' == index) {
  1091. buildingName = 'Spe_401_StreetLamp';
  1092. }
  1093. //绿化带
  1094. else if ('402' == index) {
  1095. buildingName = 'Spe_402_GreenBelt';
  1096. }
  1097. //花坛
  1098. else if ('403' == index) {
  1099. buildingName = 'Spe_403_FlowerBed';
  1100. }
  1101. //喷泉
  1102. else if ('404' == index) {
  1103. buildingName = 'Spe_404_Pool';
  1104. }
  1105. //警察局
  1106. else if ('405' == index) {
  1107. buildingName = 'Spe_405_PoliceOffice';
  1108. }
  1109. //游乐场
  1110. else if ('406' == index) {
  1111. buildingName = 'Spe_406_Playground';
  1112. }
  1113. return buildingName;
  1114. },
  1115. Grant() {
  1116. },
  1117. Sale() {
  1118. },
  1119. Exchange() {
  1120. }
  1121. });