ManageGame.js 63 KB

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