ManageGame.js 62 KB

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