TiledMap.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. cc.Class({
  2. extends: cc.Component,
  3. editor: {
  4. requireComponent: cc.TiledMap
  5. },
  6. properties: {
  7. Show: {
  8. default: null,
  9. type: cc.Label,
  10. },
  11. DrawPathNode: cc.Node,
  12. //场景树木的预制
  13. BarrierPrefab: cc.Prefab,
  14. //场景栏杆的预制
  15. RailingXPrefab: cc.Prefab,
  16. RailingYPrefab: cc.Prefab,
  17. MyMapNode: cc.Node,
  18. //没有激活的节点,也就是还没有深度排序的
  19. MyMapNoActivationNode: cc.Node,
  20. //画网格
  21. isDraw: false,
  22. //记录道路的index
  23. _highwayIndex: {
  24. default: [],
  25. type: [cc.Integer],
  26. visible: false,
  27. },
  28. //记录道路的Node节点
  29. _highwayNodes: {
  30. default: [],
  31. type: [cc.Node],
  32. visible: false,
  33. },
  34. _tiledMap: {
  35. default: null,
  36. type: cc.TiledMap,
  37. serializable: false,
  38. visible: false,
  39. },
  40. // _IndexFromTiledPos: {
  41. // default: [],
  42. // type: [cc.Integer],
  43. // serializable: false,
  44. // visible: false,
  45. // },
  46. DynamicMinBoundary: {
  47. default: cc.v2(),
  48. visible: false,
  49. tooltip: '建筑可建区域边界值,针对上面两边边界越界',
  50. },
  51. },
  52. // use this for initialization
  53. onLoad: function () {
  54. //初始化TiledMap全局变量
  55. GlobalD.TiledMap = this;
  56. this._tiledMap = this.node.getComponent(cc.TiledMap);
  57. },
  58. start: function () {
  59. if (this.isDraw && this.DrawPathNode) {
  60. //画网格
  61. for (let i = 0; i < this._tiledMap.getMapSize().width + 1; i++) {
  62. var tilesPos1 = cc.v2(i, 0);
  63. var pos1 = this._locationFromtilePos(tilesPos1);
  64. var tilesPos2 = cc.v2(i, this._tiledMap.getMapSize().width);
  65. var pos2 = this._locationFromtilePos(tilesPos2);
  66. this.DrawPathNode.getComponent('Draw').onDrawFromTwoPoints(pos1, pos2);
  67. }
  68. for (let j = 0; j < this._tiledMap.getMapSize().width + 1; j++) {
  69. var tilesPos1 = cc.v2(0, j);
  70. var pos1 = this._locationFromtilePos(tilesPos1);
  71. var tilesPos2 = cc.v2(this._tiledMap.getMapSize().width, j);
  72. var pos2 = this._locationFromtilePos(tilesPos2);
  73. this.DrawPathNode.getComponent('Draw').onDrawFromTwoPoints(pos1, pos2);
  74. }
  75. this.DrawPathNode.getComponent('Draw').onStroke();
  76. }
  77. },
  78. //初始化TiledMap的障碍物
  79. onInitSolid() {
  80. //设置地图障碍物
  81. //隐藏地图树木
  82. // this._tiledMap.getLayer('Trees').enabled = false;
  83. var objectGroup = this._tiledMap.getObjectGroup('Barrier');
  84. let objects = objectGroup.getObjects();
  85. //-1代表障碍物
  86. let _buildIdSolid = -1;
  87. let length = objects.length;
  88. for (let i = 0; i < length; i++) {
  89. let areaX = objects[i].areaX;
  90. let areaY = objects[i].areaY;
  91. for (let j = 0; j < areaX; j++) {
  92. for (let k = 0; k < areaY; k++) {
  93. let _tilePos = cc.v2(objects[i].startTiledX + j, objects[i].startTiledY + k);
  94. let solidIndex = this.getIndex(_tilePos);
  95. // this.sliblingIndexArray.push(cc.v2(item.node.getSiblingIndex(), buildIndex));
  96. //地图设置障碍物
  97. AStar.setMapSolid(_tilePos.x, _tilePos.y, 1);
  98. // if (objects[i].name == 'TreeBarrier') {
  99. // let BarrierTemp = cc.instantiate(this.BarrierPrefab);
  100. // BarrierTemp.parent = this.MyMapNode;
  101. // let tiledTile = BarrierTemp.addComponent('TiledTile');
  102. // tiledTile.x = _tilePos.x;
  103. // tiledTile.y = _tilePos.y;
  104. // GlobalD.game.onUpdateVertexZFromZIndex(BarrierTemp, _tilePos);
  105. // }
  106. let occupyTemp = cc.v2(_buildIdSolid, solidIndex);
  107. GlobalD.game.OccupyArray.push(occupyTemp);
  108. // console.log('objectGroup objects= ',objects[i].id,targetPosition);
  109. }
  110. }
  111. }
  112. // //栏杆对象
  113. // let _buildIdRailing = -2;
  114. // let RailingXLayer = this._tiledMap.getLayer('RailingX');
  115. // let RailingYLayer = this._tiledMap.getLayer('RailingY');
  116. // RailingXLayer.enabled = false;
  117. // RailingYLayer.enabled = false;
  118. // // let RailingLayer = this._tiledMap.getLayer('Railing');
  119. // // RailingLayer.enabled = false;
  120. // for (let i = 0; i < 32; i++) {
  121. // for (let j = 0; j < 32; j++) {
  122. // let _tilesPos = cc.v2(i, j);
  123. // if (RailingXLayer.getTileGIDAt(_tilesPos)) {
  124. // let solidIndex = this.getIndex(_tilesPos);
  125. // let railingXTemp = cc.instantiate(this.RailingXPrefab);
  126. // railingXTemp.parent = this.MyMapNoActivationNode;
  127. // let tiledTile = railingXTemp.addComponent('TiledTile');
  128. // tiledTile.x = _tilesPos.x;
  129. // tiledTile.y = _tilesPos.y;
  130. // let occupyTemp = cc.v2(_buildIdRailing, solidIndex);
  131. // GlobalD.game.OccupyArray.push(occupyTemp);
  132. // }
  133. // if (RailingYLayer.getTileGIDAt(_tilesPos)) {
  134. // let solidIndex = this.getIndex(_tilesPos);
  135. // let railingYTemp = cc.instantiate(this.RailingYPrefab);
  136. // railingYTemp.parent = this.MyMapNoActivationNode;
  137. // let tiledTile = railingYTemp.addComponent('TiledTile');
  138. // tiledTile.x = _tilesPos.x;
  139. // tiledTile.y = _tilesPos.y;
  140. // let occupyTemp = cc.v2(_buildIdRailing, solidIndex);
  141. // GlobalD.game.OccupyArray.push(occupyTemp);
  142. // }
  143. // if (RailingLayer.getTileGIDAt(_tilesPos)) {
  144. // let railingYTemp = cc.instantiate(this.RailingYPrefab);
  145. // railingYTemp.parent = this.MyMapNoActivationNode;
  146. // let tiledTile = railingYTemp.addComponent('TiledTile');
  147. // tiledTile.x = _tilesPos.x;
  148. // tiledTile.y = _tilesPos.y;
  149. // }
  150. // }
  151. // }
  152. },
  153. //根据tiled坐标清除指定栏杆
  154. onClearRailingFromTilesPos(_regionIndex) {
  155. // cc.log('onClearRailingFromTilesPos', _regionIndex);
  156. var objectGroup = this._tiledMap.getObjectGroup('Region');
  157. let objects = objectGroup.getObjects();
  158. //-1代表障碍物
  159. let _buildIdRailing = -2;
  160. let length = objects.length;
  161. for (let i = 0; i < length; i++) {
  162. if (objects[i].name == 'Region' + _regionIndex) {
  163. // cc.log('清除区域是:', objects[i]);
  164. let areaX = objects[i].areaX;
  165. let areaY = objects[i].areaY;
  166. let startX = objects[i].startTiledX;
  167. let startY = objects[i].startTiledY;
  168. for (let j = 0; j < areaX; j++) {
  169. for (let k = 0; k < areaY; k++) {
  170. // cc.log('清除区域是2:', startX,startY);
  171. let _tilePos = cc.v2(startX + j, startY + k);
  172. let solidIndex = this.getIndex(_tilePos);
  173. //清除栏杆节点
  174. let children = this.MyMapNoActivationNode.children;
  175. let length = children.length;
  176. for (let i = 0; i < length; i++) {
  177. if (cc.isValid(children[i]) && children[i].getComponent('TiledTile').x == _tilePos.x
  178. && children[i].getComponent('TiledTile').y == _tilePos.y) {
  179. children[i].destroy();
  180. }
  181. }
  182. //清除占位信息
  183. let occupyArray = GlobalD.game.OccupyArray;
  184. let length1 = occupyArray.length;
  185. for (let i = length1 - 1; i >= 0; i--) {
  186. if (occupyArray[i].y == solidIndex && occupyArray[i].x == _buildIdRailing) {
  187. GlobalD.game.OccupyArray.splice(i, 1);
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. },
  195. //根据屏幕坐标获取瓷砖块坐标
  196. _tilePosFromLocation(location) {
  197. //mapsize 目前是50*50
  198. var mapSize = this._tiledMap.getMapSize();
  199. //tilesize 目前是100*50
  200. var tileSize = this._tiledMap.getTileSize();
  201. // 触摸的位置信息必须减去瓷砖地图的位置信息,因为地图的位置可能在滚动变化
  202. var pos = new cc.v2();
  203. pos.x = location.x - this._tiledMap.node.getPosition().x;
  204. pos.y = location.y - this._tiledMap.node.getPosition().y;
  205. var halfMapWidth = mapSize.width * 0.5;
  206. var mapHeight = mapSize.height;
  207. var tileWidth = tileSize.width;
  208. var tileHeight = tileSize.height;
  209. //偏移量
  210. var tilePosDiv = cc.v2(pos.x / tileWidth, pos.y / tileHeight);
  211. var inverseTileY = mapHeight - tilePosDiv.y;
  212. // 将得到的计算结果转换成 int,以确保得到的是整数
  213. var posX = parseInt(inverseTileY + tilePosDiv.x - halfMapWidth);
  214. var posY = parseInt(inverseTileY - tilePosDiv.x + halfMapWidth);//****** */Anchor
  215. //外围不可活动区域的瓷砖块数
  216. var borderSize = 0;
  217. //最小活动区域
  218. var playableAreaMin = cc.v2(borderSize, borderSize);
  219. var playableAreaMax = cc.v2(this._tiledMap.getMapSize().width - 1 - borderSize, this._tiledMap.getMapSize().height - 1 - borderSize);
  220. let DeMinX = this.DynamicMinBoundary.x > 1 ? this.DynamicMinBoundary.x - 1 : 0;
  221. let DeMinY = this.DynamicMinBoundary.y > 1 ? this.DynamicMinBoundary.y - 1 : 0;
  222. // 确保坐标在MAP范围内
  223. posX = Math.max(playableAreaMin.x + DeMinX, posX);
  224. posX = Math.min(playableAreaMax.x, posX);
  225. posY = Math.max(playableAreaMin.y + DeMinY, posY);
  226. posY = Math.min(playableAreaMax.y, posY);
  227. // this.Show.string = "(" + posX + "," + posY + ")";
  228. return cc.v2(posX, posY);
  229. },
  230. //把tile坐标转换成屏幕坐标
  231. _locationFromtilePos(tilePos) {
  232. var halfMapWidth = this._tiledMap.getMapSize().width * 0.5;
  233. var mapHeight = this._tiledMap.getMapSize().height;
  234. var tileWidth = 0.0;
  235. var tileHeight = 0.0;
  236. tileWidth = this._tiledMap.getTileSize().width;
  237. tileHeight = this._tiledMap.getTileSize().height;
  238. var tilePosDiv = new cc.v2();
  239. var pos = new cc.v2();
  240. tilePosDiv.y = mapHeight - (tilePos.x + tilePos.y) / 2;
  241. tilePosDiv.x = (tilePos.x - tilePos.y) / 2 + halfMapWidth;//***** */Anchor
  242. pos.x = tilePosDiv.x * tileWidth;
  243. pos.y = tilePosDiv.y * tileHeight;
  244. var outpos = new cc.v2();
  245. outpos.x = pos.x + this._tiledMap.node.getPosition().x;
  246. outpos.y = pos.y + this._tiledMap.node.getPosition().y;
  247. return outpos;
  248. },
  249. //获取tilePos坐标对应的图块居中的位置
  250. getblockInTheMiddle(tilePos) {
  251. let getOriPos = this._locationFromtilePos(tilePos);
  252. // 图块的大小是(165,96)
  253. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  254. return cc.v2(getOriPos.x, getOriPos.y - 48);
  255. },
  256. //获取tilePos坐标对应的图块居中的位置
  257. _getTheMiddleLocationFromtilePos(tilePos) {
  258. let getOriPos = this._locationFromtilePos(tilePos);
  259. // 图块的大小是(165,96)
  260. //我们计算的点事顶点为起始点,所以y轴要减去图块的一半。
  261. return cc.v2(getOriPos.x, getOriPos.y - 48);
  262. },
  263. /**
  264. * 通过指定的 tile 坐标获取对应的 TiledTile。 <br/>
  265. * @method getTiledTileAt
  266. * @param {Integer} x
  267. * @param {Integer} y
  268. * @param {[TiledTile]} tiledsArray
  269. * @return {TiledTile}
  270. */
  271. getTiledTileAt(x, y, tiledsArray) {
  272. let index = Math.floor(x) + Math.floor(y) * this._tiledMap.getMapSize().width;
  273. let tile = null;
  274. if (tiledsArray) {
  275. tile = tiledsArray[index];
  276. } else {//如果不存在,默认获取已保存的数组
  277. tile = this._highwayIndex[index];
  278. }
  279. cc.log('tile', tile);
  280. if (tile) {
  281. return tile;
  282. } else {
  283. return false;
  284. }
  285. },
  286. /**
  287. * 替换为指定的 TiledTile。
  288. * @method setTiledTile
  289. * @param {TiledTile} tiledTile
  290. * @param {[TiledTile]} tiledsArray
  291. * @return {TiledTile}
  292. */
  293. setTiledTile(tiledTile, tiledsArray) {
  294. let index = Math.floor(tiledTile.x) + Math.floor(tiledTile.y) * this._tiledMap.getMapSize().width;
  295. let node = cc.instantiate(tiledTile.node);
  296. if (tiledsArray) {
  297. return tiledsArray[index] = node.getComponent('TiledTile');
  298. } else {//如果不存在,默认获取已保存的数组
  299. return this._highwayIndex[index] = node.getComponent('TiledTile');
  300. }
  301. },
  302. /**
  303. * 存储一个index 值到目标数组。analyticalX 存在的话,保存对应的x值
  304. * @method setIndexArray
  305. * @param {vec2} vector2
  306. * @param {[IndexArray]} indexArray
  307. * @return {IndexArray}
  308. */
  309. setIndexArray(vector2, indexArray) {
  310. let index = Math.floor(vector2.x) + Math.floor(vector2.y) * this._tiledMap.getMapSize().width;
  311. indexArray.push(index);
  312. return indexArray;
  313. },
  314. /**
  315. * 计算一个index 并返回
  316. * @method getIndex
  317. * @param {vec2} vector2
  318. * @return {index}
  319. */
  320. getIndex(vector2) {
  321. let index = Math.floor(vector2.x) + Math.floor(vector2.y) * this._tiledMap.getMapSize().width;
  322. return index;
  323. },
  324. /**
  325. * 判断是否存在一个值在目标数组。
  326. * @method getIndexArrayAt
  327. * @param {vec2} vector2
  328. * @param {[IndexArray]} indexArray
  329. * @return {Boolean}
  330. */
  331. getIndexArrayAt(vector2, indexArray) {
  332. let index = Math.floor(vector2.x) + Math.floor(vector2.y) * this._tiledMap.getMapSize().width;
  333. let length = indexArray.length;
  334. // let isHas = false;
  335. for (let i = 0; i < length; i++) {
  336. if (index == indexArray[i]) {
  337. // isHas = true;
  338. // break;
  339. return true;
  340. }
  341. }
  342. return false;
  343. },
  344. /**
  345. * 解析一个indexArray数组。
  346. * @method analyticalIndexArrayData
  347. * @param {[IndexArray]} indexArray
  348. * @return {[cc.Vec2]]}
  349. */
  350. analyticalIndexArrayData(indexArray) {
  351. let width = this._tiledMap.getMapSize().width;
  352. let length = indexArray.length;
  353. let out = [];
  354. for (let i = 0; i < length; i++) {
  355. //向下取整
  356. let Y = Math.floor(indexArray[i] / width);
  357. //取余数
  358. //cc.log('2130%50 ============',2499%50, Math.floor(2499/50));
  359. let X = indexArray[i] % width;//indexArray[i] - Y*width;
  360. out.push(new cc.Vec2(X, Y));
  361. }
  362. return out;
  363. },
  364. /**
  365. * 解析一个index。
  366. * @method analyticalIndexArrayData
  367. * @param {Index} index
  368. * @return {[cc.Vec2]]}
  369. */
  370. analyticalIndexData(index) {
  371. let width = this._tiledMap.getMapSize().width;
  372. //向下取整
  373. let Y = Math.floor(index / width);
  374. //取余数
  375. let X = index % width;//index[i] - Y*width;
  376. return new cc.Vec2(X, Y);
  377. }
  378. });