ManageGame.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. var tiledMapUnit = require('TiledMapUnit');
  2. var reGameStates = require('GameStates');
  3. //商品信息
  4. var highwayInfo = cc.Class({
  5. name: "hightwayInfo",
  6. properties: {
  7. //公路id
  8. highwayInfoId: {
  9. default: -1,
  10. type: cc.Integer,
  11. tooltip: '公路的id.',
  12. },
  13. //公路方向类型
  14. highwayInfoType: {
  15. default: reGameStates.HighwayType.none,
  16. type: cc.Enum(reGameStates.HighwayType),
  17. },
  18. //公路约定的index值
  19. highwayInfoIndex: {
  20. default: -1,
  21. type: cc.Integer,
  22. tooltip: '公路约定的Index值.',
  23. },
  24. }
  25. });
  26. var GameManager = cc.Class({
  27. extends: cc.Component,
  28. properties: {
  29. //记录全部道路的样式和Index值
  30. AllHighwayStylesAndIndex: {
  31. default: [],
  32. type: [highwayInfo],
  33. visible: false,
  34. serializable: false,
  35. },
  36. //记录建筑的类和信息
  37. buildingsTiledMapUnit: [tiledMapUnit],
  38. //id是对应物体的index
  39. //约定占位信息Index=WIDHT*tielY+tileX,WIDTH是最大tileX坐标。
  40. OccupyArray: [],
  41. // OccupyArrayV2: [],
  42. //维护节点的渲染顺序
  43. sliblingIndexArray: [],
  44. //工人住房的数组信息
  45. //每个工人都会分配一个房子,房子用id来区分
  46. //x值为buildId,y为Index
  47. housingArray: [],
  48. //工作的建筑数组信息
  49. //农田
  50. FarmlandBuildingArray: [],
  51. //木材厂
  52. TimberYardBuildingArray: [],
  53. //矿厂
  54. MiningPitBuildingArray: [],
  55. //游客的话,对应可销售的数组
  56. //工人的话,对应可收集数组
  57. MaterialsArray: [],
  58. //工厂
  59. FactoryArray: [],
  60. //需要生产的物品,
  61. //那个商店需要的商品
  62. productCommodityArray: [],
  63. //存储空闲工人的数组
  64. //Node类型
  65. WorkerArray: [cc.Node],
  66. //int类型,保存工人初始化位置
  67. WorkerInitInfoArray: [cc.Integer],
  68. //可销售的商店
  69. shopBuildingSalesArray: [],
  70. tiledMap: {
  71. default: null,
  72. type: cc.Node,
  73. },
  74. //最大的tilex坐标
  75. // WIDHT: 50,
  76. labelPrefabs: {
  77. default: null,
  78. type: cc.Prefab,
  79. },
  80. SpawnPoint: {
  81. default: null,
  82. type: cc.Node,
  83. },
  84. //提示牌:暂停营业
  85. buildingTipViewPrefabs: {
  86. default: null,
  87. type: cc.Prefab,
  88. },
  89. //ui的节点
  90. ManageUINode: cc.Node,
  91. //获取管理UI脚本
  92. _ManageUIScript: null,
  93. //主摄像机
  94. MainCamera: cc.Camera,
  95. //当前的canvas
  96. Canvas: cc.Node,
  97. //记录当前编辑的建筑
  98. currentEditorBuilding: {
  99. default: null,
  100. type: cc.Node,
  101. visible: false,
  102. },
  103. //是否是编辑状态
  104. _ifEditingStatus: { default: false, visible: false },
  105. //当前编辑的建筑的提示区域
  106. currentEditorBuildingTipPrefab: {
  107. default: null,
  108. type: cc.Prefab,
  109. },
  110. //当前提示区域的数组
  111. EditorBuildingTipArray: {
  112. default: [],
  113. type: cc.Node,
  114. visible: false,
  115. },
  116. //当前编辑房屋影响的房子节点
  117. EditorBuildingEffectNodeArray: {
  118. default: [],
  119. type: cc.Node,
  120. visible: false,
  121. },
  122. FoodTradeState: cc.Node,
  123. WoodTradeState: cc.Node,
  124. MineralTradeState: cc.Node,
  125. AllowPrefabs: {
  126. default: null,
  127. type: cc.SpriteFrame,
  128. },
  129. NoAllowPrefabs: {
  130. default: null,
  131. type: cc.SpriteFrame,
  132. },
  133. //影响提示的预制
  134. EffectUpAndDown: cc.Prefab,
  135. //显示的库存总量,人才界面
  136. cropContentNum: { default: 0, type: cc.Integer, visible: false },
  137. woodContentNum: { default: 0, type: cc.Integer, visible: false },
  138. mineContentNum: { default: 0, type: cc.Integer, visible: false },
  139. //记录选中的建筑
  140. currentSelectLeaseLandInfo: {
  141. default: null,
  142. type: cc.Node,
  143. visible: false,
  144. },
  145. },
  146. //Enum
  147. statics: {
  148. },
  149. onLoad() {
  150. GlobalD.GameManager = GameManager;
  151. GlobalD.game = this;
  152. if (this.ManageUINode)
  153. this._ManageUIScript = this.ManageUINode.getComponent('ManageUI');
  154. //如果有提示框
  155. if (this.currentEditorBuildingTipPrefab) {
  156. this.HighTipPool = new cc.NodePool();
  157. let initCount = 10;
  158. for (let i = 0; i < initCount; ++i) {
  159. let highTipTemp = cc.instantiate(this.currentEditorBuildingTipPrefab); // 创建节点
  160. highTipTemp.active = false;
  161. this.HighTipPool.put(highTipTemp); // 通过 putInPool 接口放入对象池
  162. }
  163. }
  164. },
  165. //如果进入编辑状态,游戏暂停
  166. //可以编辑,清除建筑,道路等.
  167. //todo...
  168. onEnterEditingStatus() {
  169. this._ifEditingStatus = true;
  170. },
  171. //添加场景物体基本信息数组// //添加占位信息
  172. addBuildTiled(_buildId, _startTiledPos, _occupyingArea, isOccupy = true) {
  173. let areaX = _occupyingArea.x;
  174. let areaY = _occupyingArea.y;
  175. for (let i = 0; i < areaX; i++) {
  176. for (let j = 0; j < areaY; j++) {
  177. // let index = this.WIDHT * (_startTiledPos.y - j) + (_startTiledPos.x - i);
  178. let index = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i, _startTiledPos.y - j));
  179. let occupyTemp = cc.v2(_buildId, index);
  180. this.OccupyArray.push(occupyTemp);
  181. // let tempV2 = cc.v2(_startTiledPos.x - i, _startTiledPos.y - j);
  182. // // this.OccupyArrayV2.push(tempV2);
  183. // var pos = GlobalD.TiledMap._locationFromtilePos(tempV2);
  184. // return;
  185. // var label = cc.instantiate(this.labelPrefabs);
  186. // label.parent = this.SpawnPoint;
  187. // label.setPosition(pos);
  188. // label.getComponent(cc.Label).string = "(" + tempV2.x + ',' + tempV2.y + ')';
  189. // label.getComponent('LabelInfo').BuildId = _buildId;
  190. }
  191. }
  192. },
  193. //更新占位信息
  194. //移动后更新原来的占位信息。
  195. updateBuildOccupy(isMove, _buildId, _startTiledPos, _occupyingArea) {
  196. if (isMove) {//如果移动的话,重新设置信息
  197. //临时清空label 节点 。todo...
  198. // this.clearChildren(_buildId);
  199. //需要更新的下标
  200. let updateIndex = 0;
  201. //需要更新的数组
  202. let updateArray = [];
  203. let areaX = _occupyingArea.x;
  204. let areaY = _occupyingArea.y;
  205. for (let i = 0; i < areaX; i++) {
  206. for (let j = 0; j < areaY; j++) {
  207. // let index = this.WIDHT * (_startTiledPos.y - j) + (_startTiledPos.x - i);
  208. let index = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i, _startTiledPos.y - j));
  209. let occupyTemp = cc.v2(_buildId, index);
  210. updateArray.push(occupyTemp);
  211. // let tempV2 = cc.v2(_startTiledPos.x - i, _startTiledPos.y - j);
  212. // var pos = GlobalD.TiledMap._locationFromtilePos(tempV2);
  213. // var label = cc.instantiate(this.labelPrefabs);
  214. // label.parent = this.SpawnPoint;
  215. // label.setPosition(pos);
  216. // label.getComponent(cc.Label).string = "(" + tempV2.x + ',' + tempV2.y + ')';
  217. // label.getComponent('LabelInfo').BuildId = _buildId;
  218. }
  219. }
  220. let length = this.OccupyArray.length;
  221. if (length > 0) {
  222. for (let i = 0; i < length; i++) {
  223. if (this.OccupyArray[i].x == _buildId) {
  224. this.OccupyArray[i].y = updateArray[updateIndex].y;
  225. updateIndex++;
  226. //如果需要更新的下标遍历完全后,直接退出
  227. if (updateIndex >= updateArray.length) {
  228. // cc.log('OccupyArray 更新完毕!');
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. } else {//没有移动的话,回到原来的位置
  235. //todo..
  236. }
  237. },
  238. //添加建筑的提示牌
  239. onAddBuildingTipView(_targetBuilding) {
  240. if (this.buildingTipViewPrefabs) {
  241. if (_targetBuilding.node.getChildByName('BuildingTipView')) return;
  242. var _buildTipView = cc.instantiate(this.buildingTipViewPrefabs);
  243. _buildTipView.parent = _targetBuilding.node;
  244. _buildTipView.setPosition(cc.v2(0, 50));
  245. } else {
  246. cc.warn('请在ManageGame 的面板添加BuildingTipView预制!');
  247. }
  248. },
  249. //移除建筑的提示牌
  250. onRemoveBuildingTipView(_targetBuilding) {
  251. if (_targetBuilding.node.getChildByName('BuildingTipView'))
  252. _targetBuilding.node.getChildByName('BuildingTipView').destroy();
  253. },
  254. //根据index 获取公路节点
  255. onGetHighwayFromIndex(targetIndex) {
  256. let roadIndexArray = GlobalD.game.AllHighwayStylesAndIndex;
  257. let length = roadIndexArray.length;
  258. for (let i = 0; i < length; i++) {
  259. if (targetIndex === roadIndexArray[i].highwayInfoIndex) {
  260. return true;
  261. }
  262. }
  263. return false;
  264. },
  265. //根据建筑id 获取对应的buildingsInfo
  266. onGetBuildingsTiledMapUnitFromBuildID(targetBuildID) {
  267. let length = this.buildingsTiledMapUnit.length;
  268. for (let i = 0; i < length; i++) {
  269. // cc.log(targetBuildID, this.buildingsTiledMapUnit[i].getBuildingID);
  270. if (targetBuildID === this.buildingsTiledMapUnit[i].getBuildingID) {
  271. return this.buildingsTiledMapUnit[i].getBuildingsInfo;
  272. }
  273. }
  274. return false;
  275. },
  276. //根据index 获取对应的buildingsInfo
  277. onGetBuildingsTiledMapUnitFromIndex(targetIndex) {
  278. let length = this.buildingsTiledMapUnit.length;
  279. for (let i = 0; i < length; i++) {
  280. if (this.buildingsTiledMapUnit[i].onIndexExistInThis(targetIndex)) {
  281. if (this.buildingsTiledMapUnit[i].getBuildingsInfo)
  282. return this.buildingsTiledMapUnit[i].getBuildingsInfo;
  283. else
  284. return false;
  285. }
  286. }
  287. return false;
  288. },
  289. // Muinus(buildingName) {
  290. /**
  291. * 建筑物减1操作
  292. * @param {建筑名称} buildingName
  293. */
  294. MuinusBuilding(buildingName) {
  295. let ManageBuildings = this.ManageUINode.getComponent('ManageBuildings');
  296. if (!ManageBuildings) return;
  297. let BuildingNumArray = GlobalD.GameData.BuildingNumArray;
  298. for (let i = 0; i < ManageBuildings.BuildingArray.length; i++) {
  299. // cc.log('Name='+ManageBuildings.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string,buildingName);
  300. if (ManageBuildings.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string == buildingName) {
  301. if (BuildingNumArray[i] == 0) return;
  302. BuildingNumArray[i] = Number(BuildingNumArray[i]) - 1;
  303. ManageBuildings.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i].toString();
  304. GlobalD.GameData.SetBuildingNumArray(BuildingNumArray);
  305. GlobalD.GameData.PlusGolden(-GlobalD.GameData.AddBuildingCost);
  306. // cc.log('222222222222='+ BuildingNumArray[i]);
  307. return;
  308. }
  309. }
  310. },
  311. //钻石:购买后增加对应可建造的数据
  312. //todo 现在修改为不给购买了。给默认值
  313. AddBuildingNum(_buttonNodeName) {
  314. let ManageBuildings = this.ManageUINode.getComponent('ManageBuildings');
  315. if (!ManageBuildings) return;
  316. let BuildingNumArray = GlobalD.GameData.BuildingNumArray;
  317. for (let i = 0; i < ManageBuildings.BuildingArray.length; i++) {
  318. if (ManageBuildings.BuildingArray[i].name == _buttonNodeName) {
  319. BuildingNumArray[i] = Number(BuildingNumArray[i]) + 1;
  320. ManageBuildings.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i].toString();
  321. GlobalD.GameData.SetBuildingNumArray(BuildingNumArray);
  322. let _contentButton = ManageBuildings.BuildingArray[i].getComponent('Content_Button');
  323. //设置新的钻石价格
  324. let _DiamondNumArray = GlobalD.GameData.GetDiamondNumArray();
  325. // _DiamondNumArray[i] = Number(_DiamondNumArray[i]) * 1.5 + '';
  326. _DiamondNumArray[i] = Number(_DiamondNumArray[i]) + '';
  327. //更新对应的ui
  328. _contentButton.DiamondPrice = _DiamondNumArray[i]; //钻石费用
  329. GlobalD.GameData.SetDiamondNumArray(_DiamondNumArray);
  330. return;
  331. }
  332. }
  333. },
  334. //用类型添加场景物件
  335. addBuilding(item) {
  336. // cc.log('buildInfo=='+ item.buildInfo.buildingName);
  337. // this.MuinusBuilding(item.buildInfo.buildingName);
  338. //添加深度检测的占位信息
  339. // _startTiledPos, _occupyingArea
  340. //四周围 有通路的点都可以作为目标点
  341. //清空数组
  342. item.buildInfo._gotoPosistion = [];
  343. let _indexArray = [];
  344. let areaX = item.buildInfo.occupyArea.x;
  345. let areaY = item.buildInfo.occupyArea.y;
  346. for (let i = 0; i < areaX; i++) {
  347. for (let j = 0; j < areaY; j++) {
  348. let targetPosition = cc.v2(item.buildInfo.startTilePos.x - i, item.buildInfo.startTilePos.y - j);
  349. item.buildInfo._gotoPosistion.push(targetPosition);
  350. let buildIndex = GlobalD.TiledMap.getIndex(targetPosition);
  351. //
  352. this.sliblingIndexArray.push(cc.v2(item.node.getSiblingIndex(), buildIndex));
  353. //添加约定的数组
  354. _indexArray.push(buildIndex);
  355. }
  356. }
  357. //设置Index
  358. let _ZIndex = this.onUpdateVertexZFromZIndex(item.node, item.buildInfo.startTilePos);//GlobalD.TiledMap.getIndex(item.buildInfo.startTilePos);
  359. // cc.log(item.node.name,' == ',_ZIndex);
  360. //记录节点信息
  361. let tiledTemp = new tiledMapUnit();
  362. tiledTemp.onSetBuildingID(item.buildInfo.id);
  363. tiledTemp.onSetSiblingIndex(item.node.getSiblingIndex());
  364. tiledTemp.onSetZIndex(_ZIndex);
  365. tiledTemp.onSetStartTiledPos(item.buildInfo.startTilePos);
  366. tiledTemp.onSetOccupyingArea(areaX, areaY)
  367. tiledTemp.onSetIndexArray(_indexArray);
  368. tiledTemp.onSetBuildingsInfo(item);
  369. this.buildingsTiledMapUnit.push(tiledTemp);
  370. switch (item.buildInfo.buildType) {
  371. //如果移动或者创建的是住房
  372. case reGameStates.BuildType.Housing:
  373. this.housingArray.push(item);
  374. break;
  375. //如果是农田,采木场,矿坑,加工厂。
  376. case reGameStates.BuildType.Farmland:
  377. this.FarmlandBuildingArray.push(item);
  378. this.onSetMaterialsArray(item);
  379. break;
  380. case reGameStates.BuildType.TimberYard:
  381. this.TimberYardBuildingArray.push(item);
  382. this.onSetMaterialsArray(item);
  383. break;
  384. case reGameStates.BuildType.MiningPit:
  385. // cc.log('工作的建筑信息:', item.buildInfo);
  386. this.MiningPitBuildingArray.push(item);
  387. this.onSetMaterialsArray(item);
  388. break;
  389. case reGameStates.BuildType.Factory:
  390. this.FactoryArray.push(item);
  391. break;
  392. case reGameStates.BuildType.Shop:
  393. this.shopBuildingSalesArray.push(item);
  394. break;
  395. }
  396. if (item.buildInfo.isItActive) {
  397. let BuildingsItem = {
  398. "buildingNodeName": item.node.name,
  399. "buildingId": item.buildInfo.id,
  400. "startTilePos": item.buildInfo.startTilePos,
  401. "isDefault": item.InitPos,//如果有初始化,说明在地图上
  402. "isItSaleable": item.buildInfo.isItSaleable ? 1 : 0,//建筑当前的销售状态
  403. "isItStopOperation": item.buildInfo.isItStopOperation ? 1 : 0,//建筑当前的运营状态
  404. "consumeStrengthAddValue": item.buildInfo.consumeStrengthAddValue,//建筑体力值变化的加成
  405. "goodsPriceAddValue": item.buildInfo.goodsPriceAddValue,//商品销售的加成值
  406. };
  407. //默认建筑初始化时候,如果存在数据中,就只更新数据
  408. let isUpdateData = false;
  409. //更新建筑物存储信息
  410. let gameDataBuildings = GlobalD.GameData.GameData_buildings;
  411. for (let i = 0; i < gameDataBuildings.length; i++) {
  412. if (item.buildInfo.id == gameDataBuildings[i].buildingId) {
  413. gameDataBuildings.splice(i, 1, BuildingsItem);
  414. // cc.log('item.buildInfo.id11', item.buildInfo.id);
  415. isUpdateData = true;
  416. break;
  417. }
  418. }
  419. if (isUpdateData) return;
  420. // cc.log('item.buildInfo.id22', item.buildInfo.id);
  421. if (!item.InitPosFromStore)
  422. //添加建筑物存储信息
  423. GlobalD.GameData.GameData_buildings.push(BuildingsItem);
  424. }
  425. },
  426. PlusBuilding(buildingName) {
  427. cc.log('buildingName=' + buildingName)
  428. let ManageBuildings = this.ManageUINode.getComponent('ManageBuildings');
  429. if (!ManageBuildings) return;
  430. let BuildingNumArray = GlobalD.GameData.BuildingNumArray;
  431. for (let i = 0; i < ManageBuildings.BuildingArray.length; i++) {
  432. // cc.log('Name='+ManageBuildings.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string);
  433. if (ManageBuildings.BuildingArray[i].getChildByName('Name').getComponent(cc.Label).string == buildingName) {
  434. BuildingNumArray[i] = Number(BuildingNumArray[i]) + 1;
  435. ManageBuildings.BuildingArray[i].getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = BuildingNumArray[i].toString();
  436. GlobalD.GameData.SetBuildingNumArray(BuildingNumArray);
  437. GlobalD.GameData.PlusGolden(-GlobalD.GameData.RemoveBuildingCost);
  438. // cc.log('222222222222='+ BuildingNumArray[i]);
  439. return;
  440. }
  441. }
  442. },
  443. //移除建筑物
  444. removeBuilding(item) {
  445. // cc.log('removeBuilding',item.buildInfo.id,this.buildingsTiledMapUnit);
  446. let buildInfo = item.buildInfo;
  447. this.PlusBuilding(item.buildInfo.buildingName);
  448. //首先移除占位信息
  449. //临时清空label 节点 。todo...
  450. this.clearChildren(buildInfo.id);
  451. let removeIndex = 0;
  452. let removeArray = [];
  453. let areaX = buildInfo.occupyArea.x;
  454. let areaY = buildInfo.occupyArea.y;
  455. for (let i = 0; i < areaX; i++) {
  456. for (let j = 0; j < areaY; j++) {
  457. let buildIndex = GlobalD.TiledMap.getIndex(cc.v2(buildInfo.startTilePos.x - i, buildInfo.startTilePos.y - j));
  458. let occupyTemp = cc.v2(buildInfo.id, buildIndex);
  459. removeArray.push(occupyTemp);
  460. }
  461. }
  462. let length = this.OccupyArray.length;
  463. for (let i = length - 1; i >= 0; i--) {
  464. if (this.OccupyArray[i].x === buildInfo.id) {
  465. this.OccupyArray.splice(i, 1);
  466. removeIndex++;
  467. //如果需要更新的下标遍历完全后,直接退出
  468. if (removeIndex >= removeArray.length) {
  469. // cc.log('OccupyArray 更新完毕!');
  470. break;
  471. }
  472. }
  473. }
  474. //清空buildingsTiledMapUnit节点信息
  475. let _buildingsTiledMapLength = this.buildingsTiledMapUnit.length;
  476. for (let i = _buildingsTiledMapLength - 1; i >= 0; i--) {
  477. // cc.log(this.buildingsTiledMapUnit[i]);
  478. if (buildInfo.id === this.buildingsTiledMapUnit[i].getBuildingID) {
  479. //移除buildingsTiledMapUnit节点信息
  480. this.buildingsTiledMapUnit.splice(i, 1);
  481. }
  482. }
  483. //拆除房子后
  484. if (item.buildInfo.occupantPlayerInfo) {
  485. item.buildInfo.occupantPlayerInfo.onHideChildrenNode(true);
  486. }
  487. //区分类别后移除
  488. switch (item.buildInfo.buildType) {
  489. //如果移动或者创建的是住房
  490. case reGameStates.BuildType.Housing:
  491. // cc.log('移除房子:');
  492. for (let i = 0; i < this.housingArray.length; i++) {
  493. if (item.buildInfo.id == this.housingArray[i].buildInfo.id) {
  494. this.housingArray.splice(i, 1);
  495. }
  496. }
  497. break;
  498. //如果是农田,采木场,矿坑,加工厂。
  499. case reGameStates.BuildType.Farmland:
  500. // cc.log('移除农田工作的建筑信息:');
  501. for (let i = 0; i < this.FarmlandBuildingArray.length; i++) {
  502. if (item.buildInfo.id == this.FarmlandBuildingArray[i].buildInfo.id) {
  503. this.FarmlandBuildingArray.splice(i, 1);
  504. }
  505. }
  506. this.onUpdateMaterialsArray(item, true);
  507. break;
  508. case reGameStates.BuildType.TimberYard:
  509. // cc.log('移除木材厂工作的建筑信息:', item.buildInfo);
  510. for (let i = 0; i < this.TimberYardBuildingArray.length; i++) {
  511. if (item.buildInfo.id == this.TimberYardBuildingArray[i].buildInfo.id) {
  512. this.TimberYardBuildingArray.splice(i, 1);
  513. }
  514. }
  515. this.onUpdateMaterialsArray(item, true);
  516. break;
  517. case reGameStates.BuildType.MiningPit:
  518. // cc.log('移除矿产工作的建筑信息:');
  519. for (let i = 0; i < this.MiningPitBuildingArray.length; i++) {
  520. if (item.buildInfo.id == this.MiningPitBuildingArray[i].buildInfo.id) {
  521. this.MiningPitBuildingArray.splice(i, 1);
  522. }
  523. }
  524. this.onUpdateMaterialsArray(item, true);
  525. break;
  526. case reGameStates.BuildType.Factory:
  527. // cc.log('移除工厂');
  528. for (let i = 0; i < this.FactoryArray.length; i++) {
  529. if (item.buildInfo.id == this.FactoryArray[i].buildInfo.id) {
  530. this.FactoryArray.splice(i, 1);
  531. }
  532. }
  533. break;
  534. case reGameStates.BuildType.Shop:
  535. // cc.log('移除商店');
  536. for (let i = 0; i < this.shopBuildingSalesArray.length; i++) {
  537. if (item.buildInfo.id == this.shopBuildingSalesArray[i].buildInfo.id) {
  538. this.shopBuildingSalesArray.splice(i, 1);
  539. }
  540. }
  541. break;
  542. }
  543. //移除建筑物存储信息
  544. let gameDataBuildings = GlobalD.GameData.GameData_buildings;
  545. for (let i = 0; i < gameDataBuildings.length; i++) {
  546. if (item.buildInfo.id == gameDataBuildings[i].buildingId) {
  547. gameDataBuildings.splice(i, 1);
  548. }
  549. }
  550. },
  551. //更新建筑信息
  552. updateBuilding(item) {
  553. let _indexArray = [];
  554. //记录目标点
  555. //清空数组
  556. item.buildInfo._gotoPosistion = [];
  557. let areaX = item.buildInfo.occupyArea.x;
  558. let areaY = item.buildInfo.occupyArea.y;
  559. for (let i = 0; i < areaX; i++) {
  560. for (let j = 0; j < areaY; j++) {
  561. let targetPosition = cc.v2(item.buildInfo.startTilePos.x - i, item.buildInfo.startTilePos.y - j);
  562. item.buildInfo._gotoPosistion.push(targetPosition);
  563. let buildIndex = GlobalD.TiledMap.getIndex(targetPosition);
  564. //添加约定的数组
  565. _indexArray.push(buildIndex);
  566. }
  567. }
  568. // let buildIndex = GlobalD.TiledMap.getIndex(item.buildInfo.startTilePos);
  569. // item.node.setSiblingIndex(buildIndex);
  570. //更新Index
  571. let _ZIndex = this.onUpdateVertexZFromZIndex(item.node, item.buildInfo.startTilePos);
  572. let _buildingsTiledMapLength = this.buildingsTiledMapUnit.length;
  573. for (let i = 0; i < _buildingsTiledMapLength; i++) {
  574. if (item.buildInfo.id === this.buildingsTiledMapUnit[i].getBuildingID) {
  575. //更新buildingsTiledMapUnit节点信息
  576. this.buildingsTiledMapUnit[i].onSetSiblingIndex(item.node.getSiblingIndex());
  577. //cc.log('this.buildingsTiledMapUnit[i]',_ZIndex,item.node.getSiblingIndex());
  578. this.buildingsTiledMapUnit[i].onSetZIndex(_ZIndex);
  579. this.buildingsTiledMapUnit[i].onSetStartTiledPos(item.buildInfo.startTilePos);
  580. this.buildingsTiledMapUnit[i].onSetOccupyingArea(areaX, areaY)
  581. this.buildingsTiledMapUnit[i].onSetIndexArray(_indexArray);
  582. this.buildingsTiledMapUnit[i].onSetBuildingsInfo(item);
  583. break;
  584. }
  585. }
  586. switch (item.buildInfo.buildType) {
  587. //如果移动或者创建的是住房
  588. case reGameStates.BuildType.Housing:
  589. // cc.log(item.buildInfo);
  590. for (let i = 0; i < this.housingArray.length; i++) {
  591. if (item.buildInfo.id == this.housingArray[i].buildInfo.id) {
  592. this.housingArray.splice(i, 1, item);
  593. }
  594. }
  595. break;
  596. //如果是农田,采木场,矿坑,加工厂。
  597. case reGameStates.BuildType.Farmland:
  598. // cc.log('农田工作的建筑信息:', item.buildInfo);
  599. for (let i = 0; i < this.FarmlandBuildingArray.length; i++) {
  600. if (item.buildInfo.id == this.FarmlandBuildingArray[i].buildInfo.id) {
  601. this.FarmlandBuildingArray.splice(i, 1, item);
  602. }
  603. }
  604. this.onUpdateMaterialsArray(item);
  605. break;
  606. case reGameStates.BuildType.TimberYard:
  607. // cc.log('木材厂工作的建筑信息:', item.buildInfo);
  608. for (let i = 0; i < this.TimberYardBuildingArray.length; i++) {
  609. if (item.buildInfo.id == this.TimberYardBuildingArray[i].buildInfo.id) {
  610. this.TimberYardBuildingArray.splice(i, 1, item);
  611. }
  612. }
  613. this.onUpdateMaterialsArray(item);
  614. break;
  615. case reGameStates.BuildType.MiningPit:
  616. // cc.log('矿产工作的建筑信息:', item.buildInfo);
  617. for (let i = 0; i < this.MiningPitBuildingArray.length; i++) {
  618. if (item.buildInfo.id == this.MiningPitBuildingArray[i].buildInfo.id) {
  619. this.MiningPitBuildingArray.splice(i, 1, item);
  620. }
  621. }
  622. this.onUpdateMaterialsArray(item);
  623. break;
  624. case reGameStates.BuildType.Factory:
  625. // cc.log('工厂', item.buildInfo);
  626. for (let i = 0; i < this.FactoryArray.length; i++) {
  627. if (item.buildInfo.id == this.FactoryArray[i].buildInfo.id) {
  628. this.FactoryArray.splice(i, 1, item);
  629. }
  630. }
  631. break;
  632. case reGameStates.BuildType.Shop:
  633. // cc.log('商店', item.buildInfo);
  634. for (let i = 0; i < this.shopBuildingSalesArray.length; i++) {
  635. if (item.buildInfo.id == this.shopBuildingSalesArray[i].buildInfo.id) {
  636. this.shopBuildingSalesArray.splice(i, 1, item);
  637. }
  638. }
  639. break;
  640. }
  641. let BuildingsItem = {
  642. "buildingNodeName": item.node.name,
  643. "buildingId": item.buildInfo.id,
  644. "startTilePos": item.buildInfo.startTilePos,
  645. "isDefault": false,
  646. "isItSaleable": item.buildInfo.isItSaleable ? 1 : 0,//建筑当前的销售状态
  647. "isItStopOperation": item.buildInfo.isItStopOperation ? 1 : 0,//建筑当前的运营状态
  648. "consumeStrengthAddValue": item.buildInfo.consumeStrengthAddValue,//建筑体力值变化的加成
  649. "goodsPriceAddValue": item.buildInfo.goodsPriceAddValue,//商品销售的加成值
  650. };
  651. // cc.log('更新的建筑信息BuildingsItem:',BuildingsItem);
  652. //更新建筑物存储信息
  653. let gameDataBuildings = GlobalD.GameData.GameData_buildings;
  654. for (let i = 0; i < gameDataBuildings.length; i++) {
  655. if (item.buildInfo.id == gameDataBuildings[i].buildingId) {
  656. // isDefault 是默认值,以数组为准
  657. BuildingsItem.isDefault = gameDataBuildings[i].isDefault;
  658. gameDataBuildings.splice(i, 1, BuildingsItem);
  659. }
  660. }
  661. },
  662. //关闭所有商店
  663. onClosingAllShopArray() {
  664. for (let i = 0; i < this.shopBuildingSalesArray.length; i++) {
  665. this.shopBuildingSalesArray[i].buildInfo.isItSaleable = false;
  666. // console.log("商店信息",this.shopBuildingSalesArray[i]);
  667. this.shopBuildingSalesArray[i].node.getComponent("buildingTips").playBoard();
  668. }
  669. },
  670. //开放所有商店
  671. onOpenAllShopArray() {
  672. for (let i = 0; i < this.shopBuildingSalesArray.length; i++) {
  673. this.shopBuildingSalesArray[i].buildInfo.isItSaleable = true;
  674. this.shopBuildingSalesArray[i].node.getComponent("buildingTips").stopBoard();
  675. }
  676. },
  677. //关闭所有销售原料的地方
  678. onClosingAllSalesArray() {
  679. for (let i = 0; i < this.MaterialsArray.length; i++) {
  680. this.MaterialsArray[i].buildInfo.isItSaleable = false;
  681. }
  682. },
  683. //开放所有销售原料的地方
  684. onOpenAllSalesArray() {
  685. for (let i = 0; i < this.MaterialsArray.length; i++) {
  686. this.MaterialsArray[i].buildInfo.isItSaleable = true;
  687. }
  688. },
  689. // 设置可以销售的建筑对象
  690. onSetMaterialsArray(item) {
  691. // cc.log("onSetMaterialsArray = ",item.buildInfo.buildingName,item.buildInfo.isItSaleable);
  692. GlobalD.game.MaterialsArray.push(item);
  693. },
  694. // 更新可以销售的建筑对象
  695. //如果isRemove 为true 的话,可移除对象
  696. onUpdateMaterialsArray(item, isRemove = false) {
  697. // cc.log("onUpdateSalesMaterialsArray1",this.MaterialsArray);
  698. for (let i = 0; i < this.MaterialsArray.length; i++) {
  699. if (item.buildInfo.id == this.MaterialsArray[i].buildInfo.id) {
  700. if (isRemove) {
  701. this.MaterialsArray.splice(i, 1);
  702. } else {
  703. this.MaterialsArray.splice(i, 1, item);
  704. }
  705. break;
  706. }
  707. }
  708. // cc.log("onUpdateSalesMaterialsArray2",this.MaterialsArray);
  709. },
  710. //新建,移动,删除,旋转的时候,判断是否在数组中
  711. //_buildId 对应 Occupy数组的x值;
  712. //return Boolean
  713. doesItExistArray(_buildId) {
  714. let isHas = false;
  715. let length = this.OccupyArray.length;
  716. if (length > 0) {
  717. for (let i = 0; i < length; i++) {
  718. if (this.OccupyArray[i].x == _buildId) {
  719. isHas = true;
  720. break;
  721. }
  722. }
  723. }
  724. return isHas;
  725. },
  726. //获取ManageGame index 的占位信息
  727. getManageGameIndexArrayAt(tiledVector2) {
  728. let index = GlobalD.TiledMap.getIndex(tiledVector2);
  729. let isHas = false;
  730. let length = this.OccupyArray.length;
  731. if (length > 0) {
  732. for (let i = 0; i < length; i++) {
  733. if (index == this.OccupyArray[i].y) {
  734. isHas = true;
  735. break;
  736. }
  737. }
  738. }
  739. return isHas;
  740. },
  741. //清空对应build id 的label节点
  742. clearChildren(_buildId) {
  743. let children = this.SpawnPoint.children;
  744. let length = children.length;
  745. for (let i = 0; i < length; i++) {
  746. if (cc.isValid(children[i]) && children[i].getComponent('LabelInfo').BuildId == _buildId) {
  747. children[i].destroy();
  748. }
  749. }
  750. // cc.log(this.SpawnPoint.children);
  751. },
  752. //清除当前编辑建筑的提示框
  753. onClearSpawnEditorBuildingTip(_parentNode) {
  754. //删除对应层的子节点
  755. let tempNode = [];
  756. if (_parentNode) {
  757. tempNode = _parentNode.children
  758. } else {
  759. tempNode = this.EditorBuildingTipArray;
  760. }
  761. let length = tempNode.length;
  762. for (let i = length - 1; i >= 0; i--) {
  763. //设置回默认的提示图片
  764. tempNode[i].getComponent('TipSprite').onReset();
  765. this.HighTipPool.put(tempNode[i]);
  766. this.EditorBuildingTipArray.splice(i, 1);
  767. }
  768. // cc.log('this.EditorBuildingTipArray',this.EditorBuildingTipArray);
  769. //清空记录的数组
  770. this.EditorBuildingEffectNodeArray = [];
  771. },
  772. // 当前编辑建筑的提示框
  773. onSpawnEditorBuildingTip(_parentNode, _startTiledPos, _occupyingArea, _isHas, _buildType) {
  774. // cc.log('_parentNode:',_parentNode);
  775. this.onClearSpawnEditorBuildingTip(_parentNode);
  776. let areaX = _occupyingArea.x;
  777. let areaY = _occupyingArea.y;
  778. //设置移动边界
  779. if (GlobalD.TiledMap.DynamicMinBoundary != _occupyingArea)
  780. GlobalD.TiledMap.DynamicMinBoundary = _occupyingArea;
  781. let selfArray = [];
  782. for (let i = 0; i < areaX; i++) {
  783. for (let j = 0; j < areaY; j++) {
  784. let TipPrefabTemp = null;
  785. if (this.HighTipPool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
  786. TipPrefabTemp = this.HighTipPool.get();
  787. } else { // 如果没有空闲对象,也就是对象池中备用对象不够时,我们就用 cc.instantiate 重新创建
  788. TipPrefabTemp = cc.instantiate(this.currentEditorBuildingTipPrefab);
  789. }
  790. TipPrefabTemp.parent = _parentNode;
  791. TipPrefabTemp.active = true;
  792. let tipTiledtileTemp = TipPrefabTemp.getComponent('TiledTile');
  793. tipTiledtileTemp.setTiledTilePos(_startTiledPos.x - i, _startTiledPos.y - j);
  794. if (_isHas) {
  795. TipPrefabTemp.getComponent('TipSprite').onSetRedSpriteFrame(230);
  796. } else {
  797. TipPrefabTemp.getComponent('TipSprite').onReset();
  798. }
  799. this.EditorBuildingTipArray.push(TipPrefabTemp);
  800. selfArray.push(GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i, _startTiledPos.y - j)))
  801. }
  802. }
  803. let isOpenEffect = _buildType === reGameStates.BuildType.Special ? true : false;
  804. //特殊效果
  805. if (!isOpenEffect) return;
  806. //影响力提示
  807. for (let i = 0; i < areaX + 4; i++) {
  808. for (let j = 0; j < areaY + 4; j++) {
  809. if (GlobalD.TiledMap.getIndexArrayAt(cc.v2(_startTiledPos.x + 2 - i, _startTiledPos.y + 2 - j), selfArray))
  810. continue;
  811. let TipPrefabTemp = null;
  812. if (this.HighTipPool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
  813. TipPrefabTemp = this.HighTipPool.get();
  814. } else { // 如果没有空闲对象,也就是对象池中备用对象不够时,我们就用 cc.instantiate 重新创建
  815. TipPrefabTemp = cc.instantiate(this.currentEditorBuildingTipPrefab);
  816. }
  817. TipPrefabTemp.parent = _parentNode;
  818. TipPrefabTemp.active = true;
  819. let tipTiledtileTemp = TipPrefabTemp.getComponent('TiledTile');
  820. tipTiledtileTemp.setTiledTilePos(_startTiledPos.x + 2 - i, _startTiledPos.y + 2 - j);
  821. let _tiledPos = cc.v2(_startTiledPos.x + 2 - i, _startTiledPos.y + 2 - j)
  822. //如果不需要删除的
  823. let isPushArray = true;
  824. // if ((i == areaX + 4 - 1 || i == 0) || (j == areaY + 4 - 1 || j == 0)) {
  825. TipPrefabTemp.getComponent('TipSprite').onSetEffectGreenSpriteFrame();
  826. let buildIndex = GlobalD.TiledMap.getIndex(_tiledPos);
  827. let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
  828. if (_targetBuildingsInfo) {
  829. // cc.log('绿色区域影响力接触到的房子', _targetBuildingsInfo.buildInfo.buildingName);
  830. TipPrefabTemp.getComponent('TipSprite').onSetEffectYellowSpriteFrame();
  831. isPushArray = this._AddEditorBuildinsEffectArray(_targetBuildingsInfo);
  832. }
  833. // } else {
  834. // TipPrefabTemp.getComponent('TipSprite').onSetEffectYellowSpriteFrame();
  835. // let buildIndex = GlobalD.TiledMap.getIndex(_tiledPos);
  836. // let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
  837. // if (_targetBuildingsInfo) {
  838. // // cc.log('黄色区域影响力接触到的房子', _targetBuildingsInfo.buildInfo.buildingName);
  839. // TipPrefabTemp.getComponent('TipSprite').onSetRedSpriteFrame(180);
  840. // isPushArray = this._AddEditorBuildinsEffectArray(_targetBuildingsInfo);
  841. // }
  842. // }
  843. // if (!isPushArray) {
  844. // TipPrefabTemp.destroy();
  845. // cc.log('删除!');
  846. // }
  847. }
  848. }
  849. },
  850. //如果拆除建筑的时候,清除对应的影响力
  851. onClearBuildingsEffect(_targetBuildingsInfo) {
  852. if (_targetBuildingsInfo.buildInfo.buildType !== reGameStates.BuildType.Special) {
  853. //如果不是特殊建筑调用,返回。
  854. return;
  855. }
  856. let areaX = _targetBuildingsInfo.buildInfo.occupyArea.x;
  857. let areaY = _targetBuildingsInfo.buildInfo.occupyArea.y;
  858. let _startTiledPos = _targetBuildingsInfo.buildInfo.startTilePos;
  859. let selfArray = [];
  860. for (let i = 0; i < areaX; i++) {
  861. for (let j = 0; j < areaY; j++) {
  862. selfArray.push(GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i, _startTiledPos.y - j)))
  863. }
  864. }
  865. let _SpecialAroundBuildings = [];
  866. //影响力提示
  867. for (let i = 0; i < areaX + 4; i++) {
  868. for (let j = 0; j < areaY + 4; j++) {
  869. if (GlobalD.TiledMap.getIndexArrayAt(cc.v2(_startTiledPos.x + 2 - i, _startTiledPos.y + 2 - j), selfArray))
  870. continue;
  871. let _tiledPos = cc.v2(_startTiledPos.x + 2 - i, _startTiledPos.y + 2 - j)
  872. let effectAddValue = 0;
  873. if ((i == areaX + 4 - 1 || i == 0) || (j == areaY + 4 - 1 || j == 0)) {
  874. let buildIndex = GlobalD.TiledMap.getIndex(_tiledPos);
  875. let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
  876. if (_targetBuildingsInfo) {
  877. // cc.log('绿色区域影响力接触到的房子', _targetBuildingsInfo.buildInfo.buildingName);
  878. _SpecialAroundBuildings = this._AddTargetEffectArray(_targetBuildingsInfo, _SpecialAroundBuildings);
  879. effectAddValue = 10;//增加10点回复或者生产力
  880. }
  881. } else {
  882. let buildIndex = GlobalD.TiledMap.getIndex(_tiledPos);
  883. let _targetBuildingsInfo = GlobalD.game.onGetBuildingsTiledMapUnitFromIndex(buildIndex);
  884. if (_targetBuildingsInfo) {
  885. // cc.log('黄色区域影响力接触到的房子', _targetBuildingsInfo.buildInfo.buildingName);
  886. _SpecialAroundBuildings = this._AddTargetEffectArray(_targetBuildingsInfo, _SpecialAroundBuildings);
  887. effectAddValue = 20;//增加20点回复或者生产力
  888. }
  889. }
  890. }
  891. }
  892. // cc.log('_SpecialAroundBuildings', _SpecialAroundBuildings);
  893. //拿到对应的特殊建筑
  894. let _length = _SpecialAroundBuildings.length;
  895. for (let i = 0; i < _length; i++) {
  896. if (_SpecialAroundBuildings[i].buildInfo.buildType === reGameStates.BuildType.Shop) {
  897. //提升销售价格
  898. _SpecialAroundBuildings[i].buildInfo.goodsPriceAddValue -= _targetBuildingsInfo.buildInfo.specialSetValue;
  899. this.updateBuilding(_SpecialAroundBuildings[i]);
  900. this._showDownEffectPrefab(_SpecialAroundBuildings[i]);
  901. } else if (_SpecialAroundBuildings[i].buildInfo.buildType === reGameStates.BuildType.Special) {
  902. //特殊建筑不影响周围的特殊建筑
  903. continue;
  904. } else {
  905. //增加消耗的加成值
  906. _SpecialAroundBuildings[i].buildInfo.consumeStrengthAddValue -= _targetBuildingsInfo.buildInfo.specialSetValue;
  907. this.updateBuilding(_SpecialAroundBuildings[i]);
  908. this._showDownEffectPrefab(_SpecialAroundBuildings[i]);
  909. }
  910. }
  911. },
  912. //如果完成建筑,为对应建筑添加影响力
  913. onFinishAddEditorBuildingsEffect(_targetBuildingsInfo) {
  914. if (_targetBuildingsInfo.buildInfo.buildType !== reGameStates.BuildType.Special) {
  915. //如果不是特殊建筑调用,返回。
  916. return;
  917. }
  918. let _length = this.EditorBuildingEffectNodeArray.length;
  919. for (let i = 0; i < _length; i++) {
  920. if (this.EditorBuildingEffectNodeArray[i].buildInfo.buildType === reGameStates.BuildType.Shop) {
  921. //提升销售价格
  922. this.EditorBuildingEffectNodeArray[i].buildInfo.goodsPriceAddValue += _targetBuildingsInfo.buildInfo.specialSetValue;
  923. this.updateBuilding(this.EditorBuildingEffectNodeArray[i]);
  924. this._showUpEffectPrefab(this.EditorBuildingEffectNodeArray[i]);
  925. } else if (this.EditorBuildingEffectNodeArray[i].buildInfo.buildType === reGameStates.BuildType.Special) {
  926. //特殊建筑不影响周围的特殊建筑
  927. continue;
  928. } else {
  929. //剩余的是:
  930. ////农田
  931. // Farmland: -1,
  932. // //采木场
  933. // TimberYard: -1,
  934. // //矿坑
  935. // MiningPit: -1,
  936. //Housing
  937. //Factory
  938. //增加消耗的加成值
  939. this.EditorBuildingEffectNodeArray[i].buildInfo.consumeStrengthAddValue += _targetBuildingsInfo.buildInfo.specialSetValue;
  940. this.updateBuilding(this.EditorBuildingEffectNodeArray[i]);
  941. this._showUpEffectPrefab(this.EditorBuildingEffectNodeArray[i]);
  942. }
  943. }
  944. },
  945. _showUpEffectPrefab(_target) {
  946. if (!this.EffectUpAndDown) return;
  947. let _effectObj = cc.instantiate(this.EffectUpAndDown);
  948. _effectObj.parent = _target.node;
  949. _effectObj.y = _target.node.height + 5;
  950. _effectObj.getChildByName('GoUp').active = true;
  951. setTimeout(() => {
  952. // cc.log('删除上升提示', _effectObj.name);
  953. _effectObj.destroy();
  954. }, 1000);
  955. },
  956. _showDownEffectPrefab(_target) {
  957. if (!this.EffectUpAndDown) return;
  958. let _effectObj = cc.instantiate(this.EffectUpAndDown);
  959. _effectObj.parent = _target.node;
  960. _effectObj.y = _target.node.height + 5;
  961. _effectObj.getChildByName('Down').active = true;
  962. setTimeout(() => {
  963. // cc.log('删除提示', _effectObj.name);
  964. _effectObj.destroy();
  965. }, 1000);
  966. },
  967. _AddTargetEffectArray(_targetBuildingsInfo, _targetArray) {
  968. // cc.log('_targetArray',_targetArray);
  969. //如果此位置有影响的节点
  970. let length = _targetArray.length;
  971. if (length == 0) {
  972. _targetArray.push(_targetBuildingsInfo);
  973. // cc.log('影响力接触到的房子1:', _targetBuildingsInfo.buildInfo.id, _targetBuildingsInfo.buildInfo.buildingName);
  974. return _targetArray;
  975. }
  976. for (let i = 0; i < length; i++) {
  977. if (_targetBuildingsInfo.buildInfo.id === _targetArray[i].buildInfo.id) {
  978. break;
  979. }
  980. //如果遍历完没有存在,则添加进数组
  981. if (i == length - 1) {
  982. _targetArray.push(_targetBuildingsInfo);
  983. // cc.log('影响力接触到的房子2:', _targetBuildingsInfo.buildInfo.id, _targetBuildingsInfo.buildInfo.buildingName);
  984. }
  985. }
  986. return _targetArray;
  987. },
  988. _AddEditorBuildinsEffectArray(_targetBuildingsInfo) {
  989. //如果此位置有影响的节点
  990. let length = this.EditorBuildingEffectNodeArray.length;
  991. if (length == 0) {
  992. this.EditorBuildingEffectNodeArray.push(_targetBuildingsInfo);
  993. // cc.log('影响力接触到的房子1:', _targetBuildingsInfo.buildInfo.id, _targetBuildingsInfo.buildInfo.buildingName);
  994. return true;
  995. }
  996. for (let i = 0; i < length; i++) {
  997. if (_targetBuildingsInfo.buildInfo.id === this.EditorBuildingEffectNodeArray[i].buildInfo.id) {
  998. break;
  999. }
  1000. //如果遍历完没有存在,则添加进数组
  1001. if (i == length - 1) {
  1002. this.EditorBuildingEffectNodeArray.push(_targetBuildingsInfo);
  1003. // cc.log('影响力接触到的房子2:', _targetBuildingsInfo.buildInfo.id, _targetBuildingsInfo.buildInfo.buildingName);
  1004. return true;
  1005. }
  1006. }
  1007. return false;
  1008. },
  1009. //判断两个建筑是否重叠区域
  1010. //取四个顶点来判定,减少判断o(n),顶点为false时候,以此回退点判断,直到判断完四边形的tile位置。
  1011. //buildTilePos 对应 Occupy 数组的y值
  1012. //direction 0,自上而下来检测,1自下而上。根据移动来优先判断检测
  1013. //return Boolean
  1014. areTheraOverlappingAreas(_startTiledPos, _occupyingArea, direction = 1) {
  1015. // // let isHas = false;
  1016. // let areaX = _occupyingArea.x;
  1017. // let areaY = _occupyingArea.y;
  1018. // let _length = this.OccupyArray.length;
  1019. // for (let i = 0; i < areaX; i++) {
  1020. // for (let j = 0; j < areaY; j++) {
  1021. // let indexTemp = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i, _startTiledPos.y - j));
  1022. // for (let K = 0; K < _length; K++) {
  1023. // if (indexTemp == this.OccupyArray[K].y) {
  1024. // cc.log('存在',_startTiledPos)
  1025. // return true;
  1026. // }
  1027. // }
  1028. // }
  1029. // }
  1030. // return false;
  1031. let isHas = false;
  1032. let length = this.OccupyArray.length;
  1033. //判断完四个点
  1034. //判断左上的全部点(0,0)->(0,2) check 0
  1035. for (let i = _occupyingArea.y; i > 0; i--) {
  1036. // let indexTemp = this.WIDHT * (_startTiledPos.y - i + 1) + (_startTiledPos.x - _occupyingArea.x + 1);
  1037. let indexTemp = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - _occupyingArea.x + 1, _startTiledPos.y - i + 1));
  1038. for (let j = 0; j < length; j++) {
  1039. if (indexTemp == this.OccupyArray[j].y) {
  1040. isHas = true;
  1041. break;
  1042. }
  1043. }
  1044. if (isHas) break;
  1045. }
  1046. //判断右上的全部点(0,0)->(2,0) check 1
  1047. for (let i = _occupyingArea.x; i > 0; i--) {
  1048. // let indexTemp = this.WIDHT * (_startTiledPos.y - _occupyingArea.y + 1) + (_startTiledPos.x - i + 1);
  1049. let indexTemp = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i + 1, _startTiledPos.y - _occupyingArea.y + 1));
  1050. for (let j = 0; j < length; j++) {
  1051. if (indexTemp == this.OccupyArray[j].y) {
  1052. isHas = true;
  1053. break;
  1054. }
  1055. }
  1056. if (isHas) break;
  1057. }
  1058. //判断左下的全部点 (0,2)->(2,2) check 2
  1059. for (let i = _occupyingArea.x; i > 0; i--) {
  1060. // let indexTemp = this.WIDHT * (_startTiledPos.y) + (_startTiledPos.x - i + 1);
  1061. let indexTemp = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x - i + 1, _startTiledPos.y));
  1062. for (let j = 0; j < length; j++) {
  1063. if (indexTemp == this.OccupyArray[j].y) {
  1064. isHas = true;
  1065. break;
  1066. }
  1067. }
  1068. if (isHas) break;
  1069. }
  1070. //判断右下的全部点(2,0)->(2,2) check 3
  1071. for (let i = _occupyingArea.y; i > 0; i--) {
  1072. // let indexTemp = this.WIDHT * (_startTiledPos.y - i + 1) + (_startTiledPos.x);
  1073. let indexTemp = GlobalD.TiledMap.getIndex(cc.v2(_startTiledPos.x, _startTiledPos.y - i + 1));
  1074. for (let j = 0; j < length; j++) {
  1075. if (indexTemp == this.OccupyArray[j].y) {
  1076. isHas = true;
  1077. break;
  1078. }
  1079. }
  1080. if (isHas) break;
  1081. }
  1082. return isHas;
  1083. },
  1084. //更新人物的深度检测
  1085. onUpdateVertexZFromSlibling(currentNode, pos, isMovex) {
  1086. let selfIndex = GlobalD.TiledMap.getIndex(pos);
  1087. let minIndex = -1;
  1088. let maxIndex = -1;
  1089. //如果人物是x轴移动的
  1090. if (isMovex) {
  1091. //目标位置在其右边或左边
  1092. minIndex = GlobalD.TiledMap.getIndex(cc.v2(pos.x, pos.y - 1));
  1093. maxIndex = GlobalD.TiledMap.getIndex(cc.v2(pos.x, pos.y + 1));
  1094. } else {
  1095. //目标位置在其上面或下面
  1096. minIndex = GlobalD.TiledMap.getIndex(cc.v2(pos.x - 1, pos.y));
  1097. maxIndex = GlobalD.TiledMap.getIndex(cc.v2(pos.x + 1, pos.y));
  1098. }
  1099. let length = this.sliblingIndexArray.length;
  1100. for (let i = 0; i < length; i++) {
  1101. //x轴向移动时候目标位置在其右边,y移动时候目标位置在其上面,反之亦然
  1102. if (selfIndex > this.sliblingIndexArray[i].y && minIndex == this.sliblingIndexArray[i].y) {
  1103. currentNode.parent.insertChild(currentNode, this.sliblingIndexArray[i].x + 1);
  1104. } else if (selfIndex <= this.sliblingIndexArray[i].y && maxIndex == this.sliblingIndexArray[i].y) {
  1105. currentNode.parent.insertChild(currentNode, this.sliblingIndexArray[i].x);
  1106. }
  1107. }
  1108. },
  1109. //计算Z排序
  1110. onUpdateVertexZFromZIndex(_itemNode, tilePos) {
  1111. let lowestZ = -(GlobalD.TiledMap._tiledMap.getMapSize().width + GlobalD.TiledMap._tiledMap.getMapSize().height);
  1112. let currentZ = tilePos.x + tilePos.y;
  1113. let vertexZ = lowestZ + currentZ - 1;
  1114. // this._SortEqualFromZIndex(_itemNode, tilePos);
  1115. _itemNode.zIndex = vertexZ;
  1116. // cc.log('onUpdateVertexZFromZIndex ', _itemNode.getSiblingIndex());
  1117. // _itemNode.parent.sortAllChildren();
  1118. return vertexZ;
  1119. },
  1120. //给同一条线的 ZIndex的节点排序
  1121. _SortEqualFromZIndex(currentNode, tilePos) {
  1122. let length = this.sliblingIndexArray.length;
  1123. // let sliblingTemp = getSiblingIndex();
  1124. let targetIndex = GlobalD.TiledMap.getIndex(cc.v2(tilePos.x + 1, tilePos.y - 1));
  1125. for (let i = 0; i < length; i++) {
  1126. if (targetIndex == this.sliblingIndexArray[i].y) {
  1127. cc.log('targetIndex', this.sliblingIndexArray[i].x, currentNode.getSiblingIndex(), cc.v2(tilePos.x + 1, tilePos.y - 1));
  1128. currentNode.parent.insertChild(currentNode, this.sliblingIndexArray[i].x - 1);
  1129. cc.log('targetIndex', this.sliblingIndexArray[i].x, currentNode.getSiblingIndex(), cc.v2(tilePos.x + 1, tilePos.y - 1));
  1130. break;
  1131. }
  1132. }
  1133. },
  1134. //与建筑物重叠但是不消失
  1135. onUpdateOverlapVertexZ(currentNode, selfNode, isAtTheTop) {
  1136. //如果在最顶部
  1137. if (isAtTheTop) {
  1138. currentNode.parent.insertChild(currentNode, selfNode.getSiblingIndex() + 1);
  1139. } else {
  1140. currentNode.parent.insertChild(currentNode, selfNode.getSiblingIndex());
  1141. }
  1142. },
  1143. //设置需要生产的商品
  1144. onNotificationFactory(reqBuildingsInfo) {
  1145. //通知最近的没有被占用的工厂,分配资源
  1146. let _NearestFactory = null, startIndex = 0;
  1147. //一对一工厂,寻找距离最近的
  1148. if (reqBuildingsInfo.buildInfo._targetBuildingsInfo) {
  1149. // cc.log("商店存在目标:", reqBuildingsInfo.buildInfo._targetBuildingsInfo.buildInfo);
  1150. return;
  1151. }
  1152. for (let i = 0; i < this.FactoryArray.length; i++) {
  1153. //要注意的是,最近的工厂可能没有道路,所以要检测道路,没道路的就去掉
  1154. if (!this.onCheckForRoads(reqBuildingsInfo, this.FactoryArray[i])) continue;
  1155. //如果工厂不存在商店
  1156. if (!this.FactoryArray[i].buildInfo.isItStopOperation
  1157. && !this.FactoryArray[i].buildInfo._targetBuildingsInfo) {
  1158. _NearestFactory = this.FactoryArray[i];
  1159. startIndex = i;
  1160. break;
  1161. }
  1162. }
  1163. // cc.log("商店不存在目标:", reqBuildingsInfo.buildInfo);
  1164. if (_NearestFactory && startIndex + 1 < this.FactoryArray.length) {
  1165. for (let i = startIndex + 1; i < this.FactoryArray.length; i++) {
  1166. //如果工厂停运,并且存在商店
  1167. if (this.FactoryArray[i].buildInfo.isItStopOperation || this.FactoryArray[i].buildInfo._targetBuildingsInfo) {
  1168. // cc.log('被占用了!');
  1169. continue;
  1170. }
  1171. // cc.log('检测最近的工厂',_NearestFactory);
  1172. //要注意的是,最近的工厂可能没有道路,所以要检测道路,没道路的就去掉
  1173. if (!this.onCheckForRoads(reqBuildingsInfo, this.FactoryArray[i])) continue;
  1174. if (reqBuildingsInfo.node.position.sub(_NearestFactory.node.position).mag() >
  1175. reqBuildingsInfo.node.position.sub(this.FactoryArray[i].node.position).mag()) {
  1176. _NearestFactory = this.FactoryArray[i];
  1177. }
  1178. }
  1179. }
  1180. if (_NearestFactory) {
  1181. // cc.log('_NearestFactory == ',reqBuildingsInfo.buildInfo.id, _NearestFactory.buildInfo.startTilePos);
  1182. // cc.log('商店设置并通知工厂!');
  1183. //设置商店的的目标为工厂
  1184. reqBuildingsInfo.buildInfo._targetBuildingsInfo = _NearestFactory;
  1185. //如果是最近的工厂,并且没有被占用的,
  1186. //设置工厂对应的商店建筑
  1187. _NearestFactory.buildInfo._goods = reqBuildingsInfo.buildInfo._goods;
  1188. _NearestFactory.buildInfo._targetBuildingsInfo = reqBuildingsInfo;
  1189. //添加需要工作的工厂
  1190. } else {
  1191. // cc.log('请建造工厂!');
  1192. }
  1193. },
  1194. //检测两个建筑是否通路
  1195. onCheckForRoads(selfBuildingsInfo, targetBuildingsInfo) {
  1196. //自己的节点
  1197. let selfPositionArray = selfBuildingsInfo.buildInfo._gotoPosistion;
  1198. let selfLength = selfPositionArray.length;
  1199. //目标的节点
  1200. let targetPositionArray = targetBuildingsInfo.buildInfo._gotoPosistion;
  1201. let targetLength = targetPositionArray.length;
  1202. // cc.log('检测道路', targetBuildingsInfo.buildInfo.startTilePos,
  1203. // selfBuildingsInfo.buildInfo.startTilePos);
  1204. for (let i = 0; i < selfLength; i++) {
  1205. for (let j = 0; j < targetLength; j++) {
  1206. //如果存在道路,返回true;
  1207. if (this._getPath(selfPositionArray[i], targetPositionArray[j])) {
  1208. // cc.log('存在道路', targetBuildingsInfo.buildInfo.startTilePos);
  1209. return true;
  1210. }
  1211. }
  1212. }
  1213. // cc.log('不存在道路', targetBuildingsInfo.buildInfo.startTilePos);
  1214. return false;
  1215. },
  1216. //获得寻路路径
  1217. _getPath(start, end) {
  1218. AStar.setStart(start);
  1219. let getPath = AStar.pathFind(start.x, start.y, end.x, end.y);
  1220. // //路径取反
  1221. // getPath.reverse();
  1222. if (getPath.length == 0) {
  1223. return false;
  1224. } else {
  1225. // if (this._path.length == 0 || this._path.length > getPath.length) {
  1226. // this._path = getPath;
  1227. // }
  1228. return true;
  1229. }
  1230. },
  1231. // 把工人添加进数组
  1232. onSetWorkerArray(item) {
  1233. // cc.log("onSetWorkerArray = ", item);
  1234. this.WorkerArray.push(item);
  1235. },
  1236. // 更新工人数组
  1237. // onUpdateWorkerArray(item) {
  1238. // // cc.log("onUpdateSalesMaterialsArray1",this.MaterialsArray);
  1239. // for (let i = 0; i < this.WorkerArray.length; i++) {
  1240. // if (item.characterInfo.id == this.WorkerArray[i].characterInfo.id) {
  1241. // this.WorkerArray.splice(i, 1, item);
  1242. // }
  1243. // }
  1244. // },
  1245. //移除工人对象
  1246. onRemoveAIFromWorkerArray(index) {
  1247. this.WorkerArray[index].onWorkerRemoveMyself();
  1248. this.WorkerArray.splice(index, 1);
  1249. //移除工人信息记录的信息
  1250. this.WorkerInitInfoArray.splice(index, 1);
  1251. GlobalD.GameData.SetWorkerCharacterInfoArray(this.WorkerInitInfoArray);
  1252. },
  1253. //把工人信息记录起来
  1254. onAddWorkerCharacterInfoToArray(_characterInfo, _isInit = false) {
  1255. this.WorkerInitInfoArray.push(_characterInfo);
  1256. //如果不是初始化的数据,就记录到内存中
  1257. if (!_isInit)
  1258. GlobalD.GameData.SetWorkerCharacterInfoArray(this.WorkerInitInfoArray);
  1259. },
  1260. // 更新工人数组
  1261. onUpdateWorkerCharacterInfoToArray(_characterInfo) {
  1262. // cc.log('更新人工数组', _characterInfo);
  1263. for (let i = 0; i < this.WorkerInitInfoArray.length; i++) {
  1264. if (_characterInfo.id == this.WorkerInitInfoArray.id) {
  1265. this.WorkerInitInfoArray.splice(i, 1, _characterInfo);
  1266. GlobalD.GameData.SetWorkerCharacterInfoArray(this.WorkerInitInfoArray);
  1267. break;
  1268. }
  1269. }
  1270. },
  1271. AllowFoodTrade(event, customEventData) {
  1272. if (customEventData == '0') {
  1273. this.FoodTradeState.getComponent(cc.Sprite).spriteFrame = this.NoAllowPrefabs;
  1274. GlobalD.GameData.SetFoodTradeState(0);
  1275. this.ClosingAllSales(this.FarmlandBuildingArray);
  1276. }
  1277. else {
  1278. this.FoodTradeState.getComponent(cc.Sprite).spriteFrame = this.AllowPrefabs;
  1279. GlobalD.GameData.SetFoodTradeState(1);
  1280. this.OpenAllSale(this.FarmlandBuildingArray);
  1281. }
  1282. },
  1283. AllowWoodTrade(event, customEventData) {
  1284. if (customEventData == '0') {
  1285. this.WoodTradeState.getComponent(cc.Sprite).spriteFrame = this.NoAllowPrefabs;
  1286. GlobalD.GameData.SetWoodTradeState(0);
  1287. this.ClosingAllSales(this.TimberYardBuildingArray);
  1288. }
  1289. else {
  1290. this.WoodTradeState.getComponent(cc.Sprite).spriteFrame = this.AllowPrefabs;
  1291. GlobalD.GameData.SetWoodTradeState(1);
  1292. this.OpenAllSale(this.TimberYardBuildingArray);
  1293. }
  1294. },
  1295. AllowMineralTrade(event, customEventData) {
  1296. if (customEventData == '0') {
  1297. this.MineralTradeState.getComponent(cc.Sprite).spriteFrame = this.NoAllowPrefabs;
  1298. GlobalD.GameData.SetMineralTradeState(0);
  1299. this.ClosingAllSales(this.MiningPitBuildingArray);
  1300. }
  1301. else {
  1302. this.MineralTradeState.getComponent(cc.Sprite).spriteFrame = this.AllowPrefabs;
  1303. GlobalD.GameData.SetMineralTradeState(1);
  1304. this.OpenAllSale(this.MiningPitBuildingArray);
  1305. }
  1306. },
  1307. onUpdatePersonInventory(_type, _num) {
  1308. switch (_type) {
  1309. case reGameStates.BuildType.Farmland:
  1310. this.cropContentNum += _num;
  1311. this._ManageUIScript.cropContent.string = this.cropContentNum;
  1312. break;
  1313. case reGameStates.BuildType.MiningPit:
  1314. this.mineContentNum += _num;
  1315. this._ManageUIScript.mineContent.string = this.mineContentNum;
  1316. break;
  1317. case reGameStates.BuildType.TimberYard:
  1318. this.woodContentNum += _num;
  1319. this._ManageUIScript.woodContent.string = this.woodContentNum;
  1320. break;
  1321. }
  1322. },
  1323. // //开放所有销售
  1324. OpenAllSale(AArray) {
  1325. for (let i = 0; i < AArray.length; i++) {
  1326. AArray[i].buildInfo.isItSaleable = true;
  1327. AArray[i].node.getComponent("buildingTips").stopBoard();
  1328. }
  1329. },
  1330. // //关闭所有销售
  1331. ClosingAllSales(AArray) {
  1332. for (let i = 0; i < AArray.length; i++) {
  1333. AArray[i].buildInfo.isItSaleable = false;
  1334. }
  1335. },
  1336. onResetScene() {
  1337. //从新开始游戏
  1338. cc.director.loadScene("MyCityScene");
  1339. },
  1340. //获取当前编辑建筑
  1341. onGetCurrentBuildingTarget() {
  1342. return this.currentEditorBuilding;
  1343. },
  1344. onSetCurrentBuildingTarget(_target) {
  1345. this.currentEditorBuilding = _target;
  1346. },
  1347. onClearCurrentBuildingTarget() {
  1348. this.currentEditorBuilding = null
  1349. },
  1350. //获取当前选中的建筑
  1351. onGetCurrentSelectLeaseLandInfo() {
  1352. return this.currentSelectLeaseLandInfo;
  1353. },
  1354. onSetCurrentSelectLeaseLandInfo(_target) {
  1355. this.currentSelectLeaseLandInfo = _target;
  1356. },
  1357. //创建不同的公路样式
  1358. onCreateDifferentRoadStyles(data) {
  1359. let _StaylesAndIndex = new highwayInfo();
  1360. _StaylesAndIndex.highwayInfoId = data._buildId;
  1361. _StaylesAndIndex.highwayInfoType = data._highwayType;// reGameStates.HighwayType.moveX;
  1362. _StaylesAndIndex.highwayInfoIndex = data._roadIndex;
  1363. this.AllHighwayStylesAndIndex.push(_StaylesAndIndex);
  1364. data._hightwayNode.getComponent('HighwayInfo').onChangeHighwayStyles(data._highwayType);
  1365. },
  1366. //更新公路样式
  1367. onUpdateDifferentRoadStyles(data) {
  1368. let _length = this.AllHighwayStylesAndIndex.length;
  1369. for (let i = 0; i < _length; i++) {
  1370. if (data._roadIndex == this.AllHighwayStylesAndIndex[i].highwayInfoIndex) {
  1371. this.AllHighwayStylesAndIndex[i].highwayInfoType = data._highwayType;
  1372. break;
  1373. }
  1374. }
  1375. },
  1376. });