laya.tiledmap.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. (function (exports, Laya) {
  2. 'use strict';
  3. class GridSprite extends Laya.Sprite {
  4. constructor() {
  5. super(...arguments);
  6. this.relativeX = 0;
  7. this.relativeY = 0;
  8. this.isAloneObject = false;
  9. this.isHaveAnimation = false;
  10. this.drawImageNum = 0;
  11. this._map = null;
  12. }
  13. initData(map, objectKey = false) {
  14. this._map = map;
  15. this.isAloneObject = objectKey;
  16. }
  17. addAniSprite(sprite) {
  18. if (this.aniSpriteArray == null) {
  19. this.aniSpriteArray = [];
  20. }
  21. this.aniSpriteArray.push(sprite);
  22. }
  23. show() {
  24. if (!this.visible) {
  25. this.visible = true;
  26. if (this.aniSpriteArray == null) {
  27. return;
  28. }
  29. var tAniSprite;
  30. for (var i = 0; i < this.aniSpriteArray.length; i++) {
  31. tAniSprite = this.aniSpriteArray[i];
  32. tAniSprite.show();
  33. }
  34. }
  35. }
  36. hide() {
  37. if (this.visible) {
  38. this.visible = false;
  39. if (this.aniSpriteArray == null) {
  40. return;
  41. }
  42. var tAniSprite;
  43. for (var i = 0; i < this.aniSpriteArray.length; i++) {
  44. tAniSprite = this.aniSpriteArray[i];
  45. tAniSprite.hide();
  46. }
  47. }
  48. }
  49. updatePos() {
  50. if (this.isAloneObject) {
  51. if (this._map) {
  52. this.x = this.relativeX - this._map._viewPortX;
  53. this.y = this.relativeY - this._map._viewPortY;
  54. }
  55. if (this.x < 0 || this.x > this._map.viewPortWidth || this.y < 0 || this.y > this._map.viewPortHeight) {
  56. this.hide();
  57. }
  58. else {
  59. this.show();
  60. }
  61. }
  62. else {
  63. if (this._map) {
  64. this.x = this.relativeX - this._map._viewPortX;
  65. this.y = this.relativeY - this._map._viewPortY;
  66. }
  67. }
  68. }
  69. clearAll() {
  70. if (this._map) {
  71. this._map = null;
  72. }
  73. this.visible = false;
  74. var tAniSprite;
  75. if (this.aniSpriteArray != null) {
  76. for (var i = 0; i < this.aniSpriteArray.length; i++) {
  77. tAniSprite = this.aniSpriteArray[i];
  78. tAniSprite.clearAll();
  79. }
  80. }
  81. this.destroy();
  82. this.relativeX = 0;
  83. this.relativeY = 0;
  84. this.isHaveAnimation = false;
  85. this.aniSpriteArray = null;
  86. this.drawImageNum = 0;
  87. }
  88. }
  89. class IMap {
  90. }
  91. IMap.TiledMap = null;
  92. class TileAniSprite extends Laya.Sprite {
  93. constructor() {
  94. super(...arguments);
  95. this._tileTextureSet = null;
  96. this._aniName = null;
  97. }
  98. setTileTextureSet(aniName, tileTextureSet) {
  99. this._aniName = aniName;
  100. this._tileTextureSet = tileTextureSet;
  101. tileTextureSet.addAniSprite(this._aniName, this);
  102. }
  103. show() {
  104. this._tileTextureSet.addAniSprite(this._aniName, this);
  105. }
  106. hide() {
  107. this._tileTextureSet.removeAniSprite(this._aniName);
  108. }
  109. clearAll() {
  110. this._tileTextureSet.removeAniSprite(this._aniName);
  111. this.destroy();
  112. this._tileTextureSet = null;
  113. this._aniName = null;
  114. }
  115. }
  116. class MapLayer extends Laya.Sprite {
  117. constructor() {
  118. super(...arguments);
  119. this._mapData = null;
  120. this._tileWidthHalf = 0;
  121. this._tileHeightHalf = 0;
  122. this._mapWidthHalf = 0;
  123. this._mapHeightHalf = 0;
  124. this._gridSpriteArray = [];
  125. this._objDic = null;
  126. this._dataDic = null;
  127. this._tempMapPos = new Laya.Point();
  128. this.layerName = null;
  129. }
  130. init(layerData, map) {
  131. this._map = map;
  132. this._mapData = layerData.data;
  133. var tHeight = layerData.height;
  134. var tWidth = layerData.width;
  135. var tTileW = map.tileWidth;
  136. var tTileH = map.tileHeight;
  137. this.layerName = layerData.name;
  138. this._properties = layerData.properties;
  139. this.alpha = layerData.opacity;
  140. this._tileWidthHalf = tTileW / 2;
  141. this._tileHeightHalf = tTileH / 2;
  142. this._mapWidthHalf = this._map.width / 2 - this._tileWidthHalf;
  143. this._mapHeightHalf = this._map.height / 2;
  144. switch (layerData.type) {
  145. case "tilelayer":
  146. break;
  147. case "objectgroup":
  148. var tArray = layerData.objects;
  149. if (tArray.length > 0) {
  150. this._objDic = {};
  151. this._dataDic = {};
  152. }
  153. var tObjectData;
  154. var tObjWidth;
  155. var tObjHeight;
  156. for (var i = 0; i < tArray.length; i++) {
  157. tObjectData = tArray[i];
  158. this._dataDic[tObjectData.name] = tObjectData;
  159. if (tObjectData.visible == true) {
  160. tObjWidth = tObjectData.width;
  161. tObjHeight = tObjectData.height;
  162. var tSprite = map.getSprite(tObjectData.gid, tObjWidth, tObjHeight);
  163. if (tSprite != null) {
  164. switch (this._map.orientation) {
  165. case IMap.TiledMap.ORIENTATION_ISOMETRIC:
  166. this.getScreenPositionByTilePos(tObjectData.x / tTileH, tObjectData.y / tTileH, Laya.Point.TEMP);
  167. tSprite.pivot(tObjWidth / 2, tObjHeight / 2);
  168. tSprite.rotation = tObjectData.rotation;
  169. tSprite.x = tSprite.relativeX = Laya.Point.TEMP.x + this._map.viewPortX;
  170. tSprite.y = tSprite.relativeY = Laya.Point.TEMP.y + this._map.viewPortY - tObjHeight / 2;
  171. break;
  172. case IMap.TiledMap.ORIENTATION_STAGGERED:
  173. tSprite.pivot(tObjWidth / 2, tObjHeight / 2);
  174. tSprite.rotation = tObjectData.rotation;
  175. tSprite.x = tSprite.relativeX = tObjectData.x + tObjWidth / 2;
  176. tSprite.y = tSprite.relativeY = tObjectData.y - tObjHeight / 2;
  177. break;
  178. case IMap.TiledMap.ORIENTATION_ORTHOGONAL:
  179. tSprite.pivot(tObjWidth / 2, tObjHeight / 2);
  180. tSprite.rotation = tObjectData.rotation;
  181. tSprite.x = tSprite.relativeX = tObjectData.x + tObjWidth / 2;
  182. tSprite.y = tSprite.relativeY = tObjectData.y - tObjHeight / 2;
  183. break;
  184. case IMap.TiledMap.ORIENTATION_HEXAGONAL:
  185. tSprite.x = tSprite.relativeX = tObjectData.x;
  186. tSprite.y = tSprite.relativeY = tObjectData.y;
  187. break;
  188. }
  189. this.addChild(tSprite);
  190. this._gridSpriteArray.push(tSprite);
  191. this._objDic[tObjectData.name] = tSprite;
  192. }
  193. }
  194. }
  195. break;
  196. }
  197. }
  198. getObjectByName(objName) {
  199. if (this._objDic) {
  200. return this._objDic[objName];
  201. }
  202. return null;
  203. }
  204. getObjectDataByName(objName) {
  205. if (this._dataDic) {
  206. return this._dataDic[objName];
  207. }
  208. return null;
  209. }
  210. getLayerProperties(name) {
  211. if (this._properties) {
  212. return this._properties[name];
  213. }
  214. return null;
  215. }
  216. getTileData(tileX, tileY) {
  217. if (tileY >= 0 && tileY < this._map.numRowsTile && tileX >= 0 && tileX < this._map.numColumnsTile) {
  218. var tIndex = tileY * this._map.numColumnsTile + tileX;
  219. var tMapData = this._mapData;
  220. if (tMapData != null && tIndex < tMapData.length) {
  221. return tMapData[tIndex];
  222. }
  223. }
  224. return 0;
  225. }
  226. getScreenPositionByTilePos(tileX, tileY, screenPos = null) {
  227. if (screenPos) {
  228. switch (this._map.orientation) {
  229. case IMap.TiledMap.ORIENTATION_ISOMETRIC:
  230. screenPos.x = this._map.width / 2 - (tileY - tileX) * this._tileWidthHalf;
  231. screenPos.y = (tileY + tileX) * this._tileHeightHalf;
  232. break;
  233. case IMap.TiledMap.ORIENTATION_STAGGERED:
  234. tileX = Math.floor(tileX);
  235. tileY = Math.floor(tileY);
  236. screenPos.x = tileX * this._map.tileWidth + (tileY & 1) * this._tileWidthHalf;
  237. screenPos.y = tileY * this._tileHeightHalf;
  238. break;
  239. case IMap.TiledMap.ORIENTATION_ORTHOGONAL:
  240. screenPos.x = tileX * this._map.tileWidth;
  241. screenPos.y = tileY * this._map.tileHeight;
  242. break;
  243. case IMap.TiledMap.ORIENTATION_HEXAGONAL:
  244. tileX = Math.floor(tileX);
  245. tileY = Math.floor(tileY);
  246. var tTileHeight = this._map.tileHeight * 2 / 3;
  247. screenPos.x = (tileX * this._map.tileWidth + tileY % 2 * this._tileWidthHalf) % this._map.gridWidth;
  248. screenPos.y = (tileY * tTileHeight) % this._map.gridHeight;
  249. break;
  250. }
  251. screenPos.x = (screenPos.x + this._map.viewPortX) * this._map.scale;
  252. screenPos.y = (screenPos.y + this._map.viewPortY) * this._map.scale;
  253. }
  254. }
  255. getTileDataByScreenPos(screenX, screenY) {
  256. var tData = 0;
  257. if (this.getTilePositionByScreenPos(screenX, screenY, this._tempMapPos)) {
  258. tData = this.getTileData(Math.floor(this._tempMapPos.x), Math.floor(this._tempMapPos.y));
  259. }
  260. return tData;
  261. }
  262. getTilePositionByScreenPos(screenX, screenY, result = null) {
  263. screenX = screenX / this._map.scale - this._map.viewPortX;
  264. screenY = screenY / this._map.scale - this._map.viewPortY;
  265. var tTileW = this._map.tileWidth;
  266. var tTileH = this._map.tileHeight;
  267. var tV = 0;
  268. var tU = 0;
  269. switch (this._map.orientation) {
  270. case IMap.TiledMap.ORIENTATION_ISOMETRIC:
  271. var tDirX = screenX - this._map.width / 2;
  272. var tDirY = screenY;
  273. tV = -(tDirX / tTileW - tDirY / tTileH);
  274. tU = tDirX / tTileW + tDirY / tTileH;
  275. if (result) {
  276. result.x = tU;
  277. result.y = tV;
  278. }
  279. return true;
  280. break;
  281. case IMap.TiledMap.ORIENTATION_STAGGERED:
  282. if (result) {
  283. var cx, cy, rx, ry;
  284. cx = Math.floor(screenX / tTileW) * tTileW + tTileW / 2;
  285. cy = Math.floor(screenY / tTileH) * tTileH + tTileH / 2;
  286. rx = (screenX - cx) * tTileH / 2;
  287. ry = (screenY - cy) * tTileW / 2;
  288. if (Math.abs(rx) + Math.abs(ry) <= tTileW * tTileH / 4) {
  289. tU = Math.floor(screenX / tTileW);
  290. tV = Math.floor(screenY / tTileH) * 2;
  291. }
  292. else {
  293. screenX = screenX - tTileW / 2;
  294. tU = Math.floor(screenX / tTileW) + 1;
  295. screenY = screenY - tTileH / 2;
  296. tV = Math.floor(screenY / tTileH) * 2 + 1;
  297. }
  298. result.x = tU - (tV & 1);
  299. result.y = tV;
  300. }
  301. return true;
  302. break;
  303. case IMap.TiledMap.ORIENTATION_ORTHOGONAL:
  304. tU = screenX / tTileW;
  305. tV = screenY / tTileH;
  306. if (result) {
  307. result.x = tU;
  308. result.y = tV;
  309. }
  310. return true;
  311. break;
  312. case IMap.TiledMap.ORIENTATION_HEXAGONAL:
  313. var tTileHeight = tTileH * 2 / 3;
  314. tV = screenY / tTileHeight;
  315. tU = (screenX - tV % 2 * this._tileWidthHalf) / tTileW;
  316. if (result) {
  317. result.x = tU;
  318. result.y = tV;
  319. }
  320. break;
  321. }
  322. return false;
  323. }
  324. getDrawSprite(gridX, gridY) {
  325. var tSprite = new GridSprite();
  326. tSprite.relativeX = gridX * this._map.gridWidth;
  327. tSprite.relativeY = gridY * this._map.gridHeight;
  328. tSprite.initData(this._map);
  329. this._gridSpriteArray.push(tSprite);
  330. return tSprite;
  331. }
  332. updateGridPos() {
  333. var tSprite;
  334. for (var i = 0; i < this._gridSpriteArray.length; i++) {
  335. tSprite = this._gridSpriteArray[i];
  336. if ((tSprite.visible || tSprite.isAloneObject) && tSprite.drawImageNum > 0) {
  337. tSprite.updatePos();
  338. }
  339. }
  340. }
  341. drawTileTexture(gridSprite, tileX, tileY) {
  342. if (tileY >= 0 && tileY < this._map.numRowsTile && tileX >= 0 && tileX < this._map.numColumnsTile) {
  343. var tIndex = tileY * this._map.numColumnsTile + tileX;
  344. var tMapData = this._mapData;
  345. if (tMapData != null && tIndex < tMapData.length) {
  346. if (tMapData[tIndex] != 0) {
  347. var tTileTexSet = this._map.getTexture(tMapData[tIndex]);
  348. if (tTileTexSet) {
  349. var tX = 0;
  350. var tY = 0;
  351. var tTexture = tTileTexSet.texture;
  352. switch (this._map.orientation) {
  353. case IMap.TiledMap.ORIENTATION_STAGGERED:
  354. tX = tileX * this._map.tileWidth % this._map.gridWidth + (tileY & 1) * this._tileWidthHalf;
  355. tY = tileY * this._tileHeightHalf % this._map.gridHeight;
  356. break;
  357. case IMap.TiledMap.ORIENTATION_ORTHOGONAL:
  358. tX = tileX * this._map.tileWidth % this._map.gridWidth;
  359. tY = tileY * this._map.tileHeight % this._map.gridHeight;
  360. break;
  361. case IMap.TiledMap.ORIENTATION_ISOMETRIC:
  362. tX = (this._mapWidthHalf + (tileX - tileY) * this._tileWidthHalf) % this._map.gridWidth;
  363. tY = ((tileX + tileY) * this._tileHeightHalf) % this._map.gridHeight;
  364. break;
  365. case IMap.TiledMap.ORIENTATION_HEXAGONAL:
  366. var tTileHeight = this._map.tileHeight * 2 / 3;
  367. tX = (tileX * this._map.tileWidth + tileY % 2 * this._tileWidthHalf) % this._map.gridWidth;
  368. tY = (tileY * tTileHeight) % this._map.gridHeight;
  369. break;
  370. }
  371. if (tTileTexSet.isAnimation) {
  372. var tAnimationSprite = new TileAniSprite();
  373. tAnimationSprite.x = tX;
  374. tAnimationSprite.y = tY;
  375. tAnimationSprite.setTileTextureSet(tIndex.toString(), tTileTexSet);
  376. gridSprite.addAniSprite(tAnimationSprite);
  377. gridSprite.addChild(tAnimationSprite);
  378. gridSprite.isHaveAnimation = true;
  379. }
  380. else {
  381. gridSprite.graphics.drawImage(tTileTexSet.texture, tX + tTileTexSet.offX, tY + tTileTexSet.offY);
  382. }
  383. return true;
  384. }
  385. }
  386. }
  387. }
  388. return false;
  389. }
  390. clearAll() {
  391. this._map = null;
  392. this._mapData = null;
  393. this._tileWidthHalf = 0;
  394. this._tileHeightHalf = 0;
  395. this._mapWidthHalf = 0;
  396. this._mapHeightHalf = 0;
  397. this.layerName = null;
  398. var i = 0;
  399. if (this._objDic) {
  400. for (var p in this._objDic) {
  401. delete this._objDic[p];
  402. }
  403. this._objDic = null;
  404. }
  405. if (this._dataDic) {
  406. for (p in this._dataDic) {
  407. delete this._dataDic[p];
  408. }
  409. this._dataDic = null;
  410. }
  411. var tGridSprite;
  412. for (i = 0; i < this._gridSpriteArray.length; i++) {
  413. tGridSprite = this._gridSpriteArray[i];
  414. tGridSprite.clearAll();
  415. }
  416. this._properties = null;
  417. this._tempMapPos = null;
  418. this.tarLayer = null;
  419. }
  420. }
  421. class TileTexSet {
  422. constructor() {
  423. this.gid = -1;
  424. this.offX = 0;
  425. this.offY = 0;
  426. this.textureArray = null;
  427. this.durationTimeArray = null;
  428. this.animationTotalTime = 0;
  429. this.isAnimation = false;
  430. this._spriteNum = 0;
  431. this._aniDic = null;
  432. this._frameIndex = 0;
  433. this._time = 0;
  434. this._interval = 0;
  435. this._preFrameTime = 0;
  436. }
  437. addAniSprite(aniName, sprite) {
  438. if (this.animationTotalTime == 0) {
  439. return;
  440. }
  441. if (this._aniDic == null) {
  442. this._aniDic = {};
  443. }
  444. if (this._spriteNum == 0) {
  445. Laya.ILaya.timer.frameLoop(3, this, this.animate);
  446. this._preFrameTime = Laya.ILaya.Browser.now();
  447. this._frameIndex = 0;
  448. this._time = 0;
  449. this._interval = 0;
  450. }
  451. this._spriteNum++;
  452. this._aniDic[aniName] = sprite;
  453. if (this.textureArray && this._frameIndex < this.textureArray.length) {
  454. var tTileTextureSet = this.textureArray[this._frameIndex];
  455. this.drawTexture(sprite, tTileTextureSet);
  456. }
  457. }
  458. animate() {
  459. if (this.textureArray && this.textureArray.length > 0 && this.durationTimeArray && this.durationTimeArray.length > 0) {
  460. var tNow = Laya.ILaya.Browser.now();
  461. this._interval = tNow - this._preFrameTime;
  462. this._preFrameTime = tNow;
  463. if (this._interval > this.animationTotalTime) {
  464. this._interval = this._interval % this.animationTotalTime;
  465. }
  466. this._time += this._interval;
  467. var tTime = this.durationTimeArray[this._frameIndex];
  468. while (this._time > tTime) {
  469. this._time -= tTime;
  470. this._frameIndex++;
  471. if (this._frameIndex >= this.durationTimeArray.length || this._frameIndex >= this.textureArray.length) {
  472. this._frameIndex = 0;
  473. }
  474. var tTileTextureSet = this.textureArray[this._frameIndex];
  475. var tSprite;
  476. for (var p in this._aniDic) {
  477. tSprite = this._aniDic[p];
  478. this.drawTexture(tSprite, tTileTextureSet);
  479. }
  480. tTime = this.durationTimeArray[this._frameIndex];
  481. }
  482. }
  483. }
  484. drawTexture(sprite, tileTextSet) {
  485. sprite.graphics.clear(true);
  486. sprite.graphics.drawImage(tileTextSet.texture, tileTextSet.offX, tileTextSet.offY);
  487. }
  488. removeAniSprite(_name) {
  489. if (this._aniDic && this._aniDic[_name]) {
  490. delete this._aniDic[_name];
  491. this._spriteNum--;
  492. if (this._spriteNum == 0) {
  493. Laya.ILaya.timer.clear(this, this.animate);
  494. }
  495. }
  496. }
  497. showDebugInfo() {
  498. var tInfo = null;
  499. if (this._spriteNum > 0) {
  500. tInfo = "TileTextureSet::gid:" + this.gid.toString() + " 动画数:" + this._spriteNum.toString();
  501. }
  502. return tInfo;
  503. }
  504. clearAll() {
  505. this.gid = -1;
  506. if (this.texture) {
  507. this.texture.destroy();
  508. this.texture = null;
  509. }
  510. this.offX = 0;
  511. this.offY = 0;
  512. this.textureArray = null;
  513. this.durationTimeArray = null;
  514. this.isAnimation = false;
  515. this._spriteNum = 0;
  516. this._aniDic = null;
  517. this._frameIndex = 0;
  518. this._preFrameTime = 0;
  519. this._time = 0;
  520. this._interval = 0;
  521. }
  522. }
  523. class TiledMap {
  524. constructor() {
  525. this._tileTexSetArr = [];
  526. this._texArray = [];
  527. this._x = 0;
  528. this._y = 0;
  529. this._width = 0;
  530. this._height = 0;
  531. this._mapW = 0;
  532. this._mapH = 0;
  533. this._mapTileW = 0;
  534. this._mapTileH = 0;
  535. this._rect = new Laya.Rectangle();
  536. this._paddingRect = new Laya.Rectangle();
  537. this._mapSprite = null;
  538. this._layerArray = [];
  539. this._renderLayerArray = [];
  540. this._gridArray = [];
  541. this._showGridKey = false;
  542. this._totalGridNum = 0;
  543. this._gridW = 0;
  544. this._gridH = 0;
  545. this._gridWidth = 450;
  546. this._gridHeight = 450;
  547. this._jsonLoader = null;
  548. this._loader = null;
  549. this._tileSetArray = [];
  550. this._currTileSet = null;
  551. this._completeHandler = null;
  552. this._mapRect = new GRect();
  553. this._mapLastRect = new GRect();
  554. this._index = 0;
  555. this._animationDic = {};
  556. this._tileProperties = {};
  557. this._tileProperties2 = {};
  558. this._orientation = "orthogonal";
  559. this._renderOrder = "right-down";
  560. this._colorArray = ["FF", "00", "33", "66"];
  561. this._scale = 1;
  562. this._pivotScaleX = 0.5;
  563. this._pivotScaleY = 0.5;
  564. this._centerX = 0;
  565. this._centerY = 0;
  566. this._viewPortX = 0;
  567. this._viewPortY = 0;
  568. this._viewPortWidth = 0;
  569. this._viewPortHeight = 0;
  570. this._enableLinear = true;
  571. this._limitRange = false;
  572. this.autoCache = true;
  573. this.autoCacheType = "normal";
  574. this.enableMergeLayer = false;
  575. this.removeCoveredTile = false;
  576. this.showGridTextureCount = false;
  577. this.antiCrack = true;
  578. this.cacheAllAfterInit = false;
  579. this._texutreStartDic = {};
  580. }
  581. createMap(mapName, viewRect, completeHandler, viewRectPadding = null, gridSize = null, enableLinear = true, limitRange = false) {
  582. this._enableLinear = enableLinear;
  583. this._limitRange = limitRange;
  584. this._rect.x = viewRect.x;
  585. this._rect.y = viewRect.y;
  586. this._rect.width = viewRect.width;
  587. this._rect.height = viewRect.height;
  588. this._viewPortWidth = viewRect.width / this._scale;
  589. this._viewPortHeight = viewRect.height / this._scale;
  590. this._completeHandler = completeHandler;
  591. if (viewRectPadding) {
  592. this._paddingRect.copyFrom(viewRectPadding);
  593. }
  594. else {
  595. this._paddingRect.setTo(0, 0, 0, 0);
  596. }
  597. if (gridSize) {
  598. this._gridWidth = gridSize.x;
  599. this._gridHeight = gridSize.y;
  600. }
  601. var tIndex = mapName.lastIndexOf("/");
  602. if (tIndex > -1) {
  603. this._resPath = mapName.substr(0, tIndex);
  604. this._pathArray = this._resPath.split("/");
  605. }
  606. else {
  607. this._resPath = "";
  608. this._pathArray = [];
  609. }
  610. this._jsonLoader = new Laya.Loader();
  611. this._jsonLoader.once("complete", this, this.onJsonComplete);
  612. this._jsonLoader.load(mapName, Laya.Loader.JSON, false);
  613. }
  614. onJsonComplete(e) {
  615. this._mapSprite = new Laya.Sprite();
  616. Laya.ILaya.stage.addChild(this._mapSprite);
  617. var tJsonData = this._jsonData = e;
  618. this._properties = tJsonData.properties;
  619. this._orientation = tJsonData.orientation;
  620. this._renderOrder = tJsonData.renderorder;
  621. this._mapW = tJsonData.width;
  622. this._mapH = tJsonData.height;
  623. this._mapTileW = tJsonData.tilewidth;
  624. this._mapTileH = tJsonData.tileheight;
  625. this._width = this._mapTileW * this._mapW;
  626. this._height = this._mapTileH * this._mapH;
  627. if (this._orientation == TiledMap.ORIENTATION_STAGGERED) {
  628. this._height = (0.5 + this._mapH * 0.5) * this._mapTileH;
  629. }
  630. this._mapLastRect.top = this._mapLastRect.bottom = this._mapLastRect.left = this._mapLastRect.right = -1;
  631. var tArray = tJsonData.tilesets;
  632. var tileset;
  633. var tTileSet;
  634. var i = 0;
  635. for (i = 0; i < tArray.length; i++) {
  636. tileset = tArray[i];
  637. tTileSet = new TileSet();
  638. tTileSet.init(tileset);
  639. if (tTileSet.properties && tTileSet.properties.ignore)
  640. continue;
  641. this._tileProperties[i] = tTileSet.tileproperties;
  642. this.addTileProperties(tTileSet.tileproperties);
  643. this._tileSetArray.push(tTileSet);
  644. var tTiles = tileset.tiles;
  645. if (tTiles) {
  646. for (var p in tTiles) {
  647. var tAnimation = tTiles[p].animation;
  648. if (tAnimation) {
  649. var tAniData = new TileMapAniData();
  650. this._animationDic[p] = tAniData;
  651. tAniData.image = tileset.image;
  652. for (var j = 0; j < tAnimation.length; j++) {
  653. var tAnimationItem = tAnimation[j];
  654. tAniData.mAniIdArray.push(tAnimationItem.tileid);
  655. tAniData.mDurationTimeArray.push(tAnimationItem.duration);
  656. }
  657. }
  658. }
  659. }
  660. }
  661. this._tileTexSetArr.push(null);
  662. if (this._tileSetArray.length > 0) {
  663. tTileSet = this._currTileSet = this._tileSetArray.shift();
  664. this._loader = new Laya.Loader();
  665. this._loader.once("complete", this, this.onTextureComplete);
  666. var tPath = this.mergePath(this._resPath, tTileSet.image);
  667. this._loader.load(tPath, Laya.Loader.IMAGE, false);
  668. }
  669. }
  670. mergePath(resPath, relativePath) {
  671. var tResultPath = "";
  672. var tImageArray = relativePath.split("/");
  673. var tParentPathNum = 0;
  674. var i = 0;
  675. for (i = tImageArray.length - 1; i >= 0; i--) {
  676. if (tImageArray[i] == "..") {
  677. tParentPathNum++;
  678. }
  679. }
  680. if (tParentPathNum == 0) {
  681. if (this._pathArray.length > 0) {
  682. tResultPath = resPath + "/" + relativePath;
  683. }
  684. else {
  685. tResultPath = relativePath;
  686. }
  687. return tResultPath;
  688. }
  689. var tSrcNum = this._pathArray.length - tParentPathNum;
  690. if (tSrcNum < 0) {
  691. console.log("[error]path does not exist", this._pathArray, tImageArray, resPath, relativePath);
  692. }
  693. for (i = 0; i < tSrcNum; i++) {
  694. if (i == 0) {
  695. tResultPath += this._pathArray[i];
  696. }
  697. else {
  698. tResultPath = tResultPath + "/" + this._pathArray[i];
  699. }
  700. }
  701. for (i = tParentPathNum; i < tImageArray.length; i++) {
  702. tResultPath = tResultPath + "/" + tImageArray[i];
  703. }
  704. return tResultPath;
  705. }
  706. onTextureComplete(e) {
  707. var json = this._jsonData;
  708. var tTexture = e;
  709. if (!this._enableLinear) {
  710. tTexture.bitmap.minFifter = 0x2600;
  711. tTexture.bitmap.magFifter = 0x2600;
  712. }
  713. this._texArray.push(tTexture);
  714. var tTileSet = this._currTileSet;
  715. var tTileTextureW = tTileSet.tilewidth;
  716. var tTileTextureH = tTileSet.tileheight;
  717. var tImageWidth = tTileSet.imagewidth;
  718. var tImageHeight = tTileSet.imageheight;
  719. var tFirstgid = tTileSet.firstgid;
  720. var tTileWNum = Math.floor((tImageWidth - tTileSet.margin - tTileTextureW) / (tTileTextureW + tTileSet.spacing)) + 1;
  721. var tTileHNum = Math.floor((tImageHeight - tTileSet.margin - tTileTextureH) / (tTileTextureH + tTileSet.spacing)) + 1;
  722. var tTileTexSet = null;
  723. this._texutreStartDic[tTileSet.image] = this._tileTexSetArr.length;
  724. for (var i = 0; i < tTileHNum; i++) {
  725. for (var j = 0; j < tTileWNum; j++) {
  726. tTileTexSet = new TileTexSet();
  727. tTileTexSet.offX = tTileSet.titleoffsetX;
  728. tTileTexSet.offY = tTileSet.titleoffsetY - (tTileTextureH - this._mapTileH);
  729. tTileTexSet.texture = Laya.Texture.createFromTexture(tTexture, tTileSet.margin + (tTileTextureW + tTileSet.spacing) * j, tTileSet.margin + (tTileTextureH + tTileSet.spacing) * i, tTileTextureW, tTileTextureH);
  730. if (this.antiCrack)
  731. this.adptTexture(tTileTexSet.texture);
  732. this._tileTexSetArr.push(tTileTexSet);
  733. tTileTexSet.gid = this._tileTexSetArr.length;
  734. }
  735. }
  736. if (this._tileSetArray.length > 0) {
  737. tTileSet = this._currTileSet = this._tileSetArray.shift();
  738. this._loader.once("complete", this, this.onTextureComplete);
  739. var tPath = this.mergePath(this._resPath, tTileSet.image);
  740. this._loader.load(tPath, Laya.Loader.IMAGE, false);
  741. }
  742. else {
  743. this._currTileSet = null;
  744. this.initMap();
  745. }
  746. }
  747. adptTexture(tex) {
  748. if (!tex)
  749. return;
  750. var pX = tex.uv[0];
  751. var pX1 = tex.uv[2];
  752. var pY = tex.uv[1];
  753. var pY1 = tex.uv[7];
  754. var dW = 1 / tex.bitmap.width;
  755. var dH = 1 / tex.bitmap.height;
  756. var Tex = tex;
  757. Tex.uv[0] = Tex.uv[6] = pX + dW;
  758. Tex.uv[2] = Tex.uv[4] = pX1 - dW;
  759. Tex.uv[1] = Tex.uv[3] = pY + dH;
  760. Tex.uv[5] = Tex.uv[7] = pY1 - dH;
  761. }
  762. initMap() {
  763. var i, n;
  764. for (var p in this._animationDic) {
  765. var tAniData = this._animationDic[p];
  766. var gStart;
  767. gStart = this._texutreStartDic[tAniData.image];
  768. var tTileTexSet = this.getTexture(parseInt(p) + gStart);
  769. if (tAniData.mAniIdArray.length > 0) {
  770. tTileTexSet.textureArray = [];
  771. tTileTexSet.durationTimeArray = tAniData.mDurationTimeArray;
  772. tTileTexSet.isAnimation = true;
  773. tTileTexSet.animationTotalTime = 0;
  774. for (i = 0, n = tTileTexSet.durationTimeArray.length; i < n; i++) {
  775. tTileTexSet.animationTotalTime += tTileTexSet.durationTimeArray[i];
  776. }
  777. for (i = 0, n = tAniData.mAniIdArray.length; i < n; i++) {
  778. var tTexture = this.getTexture(tAniData.mAniIdArray[i] + gStart);
  779. tTileTexSet.textureArray.push(tTexture);
  780. }
  781. }
  782. }
  783. this._gridWidth = Math.floor(this._gridWidth / this._mapTileW) * this._mapTileW;
  784. this._gridHeight = Math.floor(this._gridHeight / this._mapTileH) * this._mapTileH;
  785. if (this._gridWidth < this._mapTileW) {
  786. this._gridWidth = this._mapTileW;
  787. }
  788. if (this._gridHeight < this._mapTileH) {
  789. this._gridHeight = this._mapTileH;
  790. }
  791. this._gridW = Math.ceil(this._width / this._gridWidth);
  792. this._gridH = Math.ceil(this._height / this._gridHeight);
  793. this._totalGridNum = this._gridW * this._gridH;
  794. for (i = 0; i < this._gridH; i++) {
  795. var tGridArray = [];
  796. this._gridArray.push(tGridArray);
  797. for (var j = 0; j < this._gridW; j++) {
  798. tGridArray.push(null);
  799. }
  800. }
  801. var tLayerArray = this._jsonData.layers;
  802. var isFirst = true;
  803. var tLayerTarLayerName;
  804. var preLayerTarName;
  805. var preLayer;
  806. for (var tLayerLoop = 0; tLayerLoop < tLayerArray.length; tLayerLoop++) {
  807. var tLayerData = tLayerArray[tLayerLoop];
  808. if (tLayerData.visible == true) {
  809. var tMapLayer = new MapLayer();
  810. tMapLayer.init(tLayerData, this);
  811. if (!this.enableMergeLayer) {
  812. this._mapSprite.addChild(tMapLayer);
  813. this._renderLayerArray.push(tMapLayer);
  814. }
  815. else {
  816. tLayerTarLayerName = tMapLayer.getLayerProperties("layer");
  817. isFirst = isFirst || (!preLayer) || (tLayerTarLayerName != preLayerTarName);
  818. if (isFirst) {
  819. isFirst = false;
  820. tMapLayer.tarLayer = tMapLayer;
  821. preLayer = tMapLayer;
  822. this._mapSprite.addChild(tMapLayer);
  823. this._renderLayerArray.push(tMapLayer);
  824. }
  825. else {
  826. tMapLayer.tarLayer = preLayer;
  827. }
  828. preLayerTarName = tLayerTarLayerName;
  829. }
  830. this._layerArray.push(tMapLayer);
  831. }
  832. }
  833. if (this.removeCoveredTile) {
  834. this.adptTiledMapData();
  835. }
  836. if (this.cacheAllAfterInit) {
  837. this.cacheAllGrid();
  838. }
  839. this.moveViewPort(this._rect.x, this._rect.y);
  840. if (this._completeHandler != null) {
  841. this._completeHandler.run();
  842. }
  843. }
  844. addTileProperties(tileDataDic) {
  845. var key;
  846. for (key in tileDataDic) {
  847. this._tileProperties2[key] = tileDataDic[key];
  848. }
  849. }
  850. getTileUserData(id, sign, defaultV = null) {
  851. if (!this._tileProperties2 || !this._tileProperties2[id] || !(sign in this._tileProperties2[id]))
  852. return defaultV;
  853. return this._tileProperties2[id][sign];
  854. }
  855. adptTiledMapData() {
  856. var i, len;
  857. len = this._layerArray.length;
  858. var tLayer;
  859. var noNeeds = {};
  860. var tDatas;
  861. for (i = len - 1; i >= 0; i--) {
  862. tLayer = this._layerArray[i];
  863. tDatas = tLayer._mapData;
  864. if (!tDatas)
  865. continue;
  866. this.removeCoverd(tDatas, noNeeds);
  867. this.collectCovers(tDatas, noNeeds, i);
  868. }
  869. }
  870. removeCoverd(datas, noNeeds) {
  871. var i, len;
  872. len = datas.length;
  873. for (i = 0; i < len; i++) {
  874. if (noNeeds[i]) {
  875. datas[i] = 0;
  876. }
  877. }
  878. }
  879. collectCovers(datas, noNeeds, layer) {
  880. var i, len;
  881. len = datas.length;
  882. var tTileData;
  883. var isCover;
  884. for (i = 0; i < len; i++) {
  885. tTileData = datas[i];
  886. if (tTileData > 0) {
  887. isCover = this.getTileUserData(tTileData - 1, "type", 0);
  888. if (isCover > 0) {
  889. noNeeds[i] = tTileData;
  890. }
  891. }
  892. }
  893. }
  894. getTexture(index) {
  895. if (index < this._tileTexSetArr.length) {
  896. return this._tileTexSetArr[index];
  897. }
  898. return null;
  899. }
  900. getMapProperties(name) {
  901. if (this._properties) {
  902. return this._properties[name];
  903. }
  904. return null;
  905. }
  906. getTileProperties(index, id, name) {
  907. if (this._tileProperties[index] && this._tileProperties[index][id]) {
  908. return this._tileProperties[index][id][name];
  909. }
  910. return null;
  911. }
  912. getSprite(index, width, height) {
  913. if (0 < this._tileTexSetArr.length) {
  914. var tGridSprite = new GridSprite();
  915. tGridSprite.initData(this, true);
  916. tGridSprite.size(width, height);
  917. var tTileTexSet = this._tileTexSetArr[index];
  918. if (tTileTexSet != null && tTileTexSet.texture != null) {
  919. if (tTileTexSet.isAnimation) {
  920. var tAnimationSprite = new TileAniSprite();
  921. this._index++;
  922. tAnimationSprite.setTileTextureSet(this._index.toString(), tTileTexSet);
  923. tGridSprite.addAniSprite(tAnimationSprite);
  924. tGridSprite.addChild(tAnimationSprite);
  925. }
  926. else {
  927. tGridSprite.graphics.drawImage(tTileTexSet.texture, 0, 0, width, height);
  928. }
  929. tGridSprite.drawImageNum++;
  930. }
  931. return tGridSprite;
  932. }
  933. return null;
  934. }
  935. setViewPortPivotByScale(scaleX, scaleY) {
  936. this._pivotScaleX = scaleX;
  937. this._pivotScaleY = scaleY;
  938. }
  939. set scale(scale) {
  940. if (scale <= 0)
  941. return;
  942. this._scale = scale;
  943. this._viewPortWidth = this._rect.width / scale;
  944. this._viewPortHeight = this._rect.height / scale;
  945. this._mapSprite.scale(this._scale, this._scale);
  946. this.updateViewPort();
  947. }
  948. get scale() {
  949. return this._scale;
  950. }
  951. moveViewPort(moveX, moveY) {
  952. this._x = -moveX;
  953. this._y = -moveY;
  954. this._rect.x = moveX;
  955. this._rect.y = moveY;
  956. this.updateViewPort();
  957. }
  958. changeViewPort(moveX, moveY, width, height) {
  959. if (moveX == this._rect.x && moveY == this._rect.y && width == this._rect.width && height == this._rect.height)
  960. return;
  961. this._x = -moveX;
  962. this._y = -moveY;
  963. this._rect.x = moveX;
  964. this._rect.y = moveY;
  965. this._rect.width = width;
  966. this._rect.height = height;
  967. this._viewPortWidth = width / this._scale;
  968. this._viewPortHeight = height / this._scale;
  969. this.updateViewPort();
  970. }
  971. changeViewPortBySize(width, height, rect = null) {
  972. if (rect == null) {
  973. rect = new Laya.Rectangle();
  974. }
  975. this._centerX = this._rect.x + this._rect.width * this._pivotScaleX;
  976. this._centerY = this._rect.y + this._rect.height * this._pivotScaleY;
  977. rect.x = this._centerX - width * this._pivotScaleX;
  978. rect.y = this._centerY - height * this._pivotScaleY;
  979. rect.width = width;
  980. rect.height = height;
  981. this.changeViewPort(rect.x, rect.y, rect.width, rect.height);
  982. return rect;
  983. }
  984. updateViewPort() {
  985. this._centerX = this._rect.x + this._rect.width * this._pivotScaleX;
  986. this._centerY = this._rect.y + this._rect.height * this._pivotScaleY;
  987. var posChanged = false;
  988. var preValue = this._viewPortX;
  989. this._viewPortX = this._centerX - this._rect.width * this._pivotScaleX / this._scale;
  990. if (preValue != this._viewPortX) {
  991. posChanged = true;
  992. }
  993. else {
  994. preValue = this._viewPortY;
  995. }
  996. this._viewPortY = this._centerY - this._rect.height * this._pivotScaleY / this._scale;
  997. if (!posChanged && preValue != this._viewPortY) {
  998. posChanged = true;
  999. }
  1000. if (this._limitRange) {
  1001. var tRight = this._viewPortX + this._viewPortWidth;
  1002. if (tRight > this._width) {
  1003. this._viewPortX = this._width - this._viewPortWidth;
  1004. }
  1005. var tBottom = this._viewPortY + this._viewPortHeight;
  1006. if (tBottom > this._height) {
  1007. this._viewPortY = this._height - this._viewPortHeight;
  1008. }
  1009. if (this._viewPortX < 0) {
  1010. this._viewPortX = 0;
  1011. }
  1012. if (this._viewPortY < 0) {
  1013. this._viewPortY = 0;
  1014. }
  1015. }
  1016. var tPaddingRect = this._paddingRect;
  1017. this._mapRect.top = Math.floor((this._viewPortY - tPaddingRect.y) / this._gridHeight);
  1018. this._mapRect.bottom = Math.floor((this._viewPortY + this._viewPortHeight + tPaddingRect.height + tPaddingRect.y) / this._gridHeight);
  1019. this._mapRect.left = Math.floor((this._viewPortX - tPaddingRect.x) / this._gridWidth);
  1020. this._mapRect.right = Math.floor((this._viewPortX + this._viewPortWidth + tPaddingRect.width + tPaddingRect.x) / this._gridWidth);
  1021. if (this._mapRect.top != this._mapLastRect.top || this._mapRect.bottom != this._mapLastRect.bottom || this._mapRect.left != this._mapLastRect.left || this._mapRect.right != this._mapLastRect.right) {
  1022. this.clipViewPort();
  1023. this._mapLastRect.top = this._mapRect.top;
  1024. this._mapLastRect.bottom = this._mapRect.bottom;
  1025. this._mapLastRect.left = this._mapRect.left;
  1026. this._mapLastRect.right = this._mapRect.right;
  1027. posChanged = true;
  1028. }
  1029. if (!posChanged)
  1030. return;
  1031. var tMapLayer;
  1032. var len = this._renderLayerArray.length;
  1033. for (var i = 0; i < len; i++) {
  1034. tMapLayer = this._renderLayerArray[i];
  1035. if (tMapLayer._gridSpriteArray.length > 0)
  1036. tMapLayer.updateGridPos();
  1037. }
  1038. }
  1039. clipViewPort() {
  1040. var tSub = 0;
  1041. var tAdd = 0;
  1042. var i, j;
  1043. if (this._mapRect.left > this._mapLastRect.left) {
  1044. tSub = this._mapRect.left - this._mapLastRect.left;
  1045. if (tSub > 0) {
  1046. for (j = this._mapLastRect.left; j < this._mapLastRect.left + tSub; j++) {
  1047. for (i = this._mapLastRect.top; i <= this._mapLastRect.bottom; i++) {
  1048. this.hideGrid(j, i);
  1049. }
  1050. }
  1051. }
  1052. }
  1053. else {
  1054. tAdd = Math.min(this._mapLastRect.left, this._mapRect.right + 1) - this._mapRect.left;
  1055. if (tAdd > 0) {
  1056. for (j = this._mapRect.left; j < this._mapRect.left + tAdd; j++) {
  1057. for (i = this._mapRect.top; i <= this._mapRect.bottom; i++) {
  1058. this.showGrid(j, i);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. if (this._mapRect.right > this._mapLastRect.right) {
  1064. tAdd = this._mapRect.right - this._mapLastRect.right;
  1065. if (tAdd > 0) {
  1066. for (j = Math.max(this._mapLastRect.right + 1, this._mapRect.left); j <= this._mapLastRect.right + tAdd; j++) {
  1067. for (i = this._mapRect.top; i <= this._mapRect.bottom; i++) {
  1068. this.showGrid(j, i);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. else {
  1074. tSub = this._mapLastRect.right - this._mapRect.right;
  1075. if (tSub > 0) {
  1076. for (j = this._mapRect.right + 1; j <= this._mapRect.right + tSub; j++) {
  1077. for (i = this._mapLastRect.top; i <= this._mapLastRect.bottom; i++) {
  1078. this.hideGrid(j, i);
  1079. }
  1080. }
  1081. }
  1082. }
  1083. if (this._mapRect.top > this._mapLastRect.top) {
  1084. tSub = this._mapRect.top - this._mapLastRect.top;
  1085. if (tSub > 0) {
  1086. for (i = this._mapLastRect.top; i < this._mapLastRect.top + tSub; i++) {
  1087. for (j = this._mapLastRect.left; j <= this._mapLastRect.right; j++) {
  1088. this.hideGrid(j, i);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. else {
  1094. tAdd = Math.min(this._mapLastRect.top, this._mapRect.bottom + 1) - this._mapRect.top;
  1095. if (tAdd > 0) {
  1096. for (i = this._mapRect.top; i < this._mapRect.top + tAdd; i++) {
  1097. for (j = this._mapRect.left; j <= this._mapRect.right; j++) {
  1098. this.showGrid(j, i);
  1099. }
  1100. }
  1101. }
  1102. }
  1103. if (this._mapRect.bottom > this._mapLastRect.bottom) {
  1104. tAdd = this._mapRect.bottom - this._mapLastRect.bottom;
  1105. if (tAdd > 0) {
  1106. for (i = Math.max(this._mapLastRect.bottom + 1, this._mapRect.top); i <= this._mapLastRect.bottom + tAdd; i++) {
  1107. for (j = this._mapRect.left; j <= this._mapRect.right; j++) {
  1108. this.showGrid(j, i);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. else {
  1114. tSub = this._mapLastRect.bottom - this._mapRect.bottom;
  1115. if (tSub > 0) {
  1116. for (i = this._mapRect.bottom + 1; i <= this._mapRect.bottom + tSub; i++) {
  1117. for (j = this._mapLastRect.left; j <= this._mapLastRect.right; j++) {
  1118. this.hideGrid(j, i);
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. showGrid(gridX, gridY) {
  1125. if (gridX < 0 || gridX >= this._gridW || gridY < 0 || gridY >= this._gridH) {
  1126. return;
  1127. }
  1128. var i;
  1129. var tGridSprite;
  1130. var tTempArray = this._gridArray[gridY][gridX];
  1131. if (tTempArray == null) {
  1132. tTempArray = this.getGridArray(gridX, gridY);
  1133. }
  1134. else {
  1135. for (i = 0; i < tTempArray.length && i < this._layerArray.length; i++) {
  1136. var tLayerSprite = this._layerArray[i];
  1137. if (tLayerSprite && tTempArray[i]) {
  1138. tGridSprite = tTempArray[i];
  1139. if (tGridSprite.visible == false && tGridSprite.drawImageNum > 0) {
  1140. tGridSprite.show();
  1141. }
  1142. }
  1143. }
  1144. }
  1145. }
  1146. cacheAllGrid() {
  1147. var i, j;
  1148. var tempArr;
  1149. for (i = 0; i < this._gridW; i++) {
  1150. for (j = 0; j < this._gridH; j++) {
  1151. tempArr = this.getGridArray(i, j);
  1152. this.cacheGridsArray(tempArr);
  1153. }
  1154. }
  1155. }
  1156. cacheGridsArray(arr) {
  1157. var canvas;
  1158. if (!TiledMap._tempCanvas) {
  1159. TiledMap._tempCanvas = new Laya.HTMLCanvas();
  1160. var tx = TiledMap._tempCanvas.context;
  1161. if (!tx) {
  1162. tx = TiledMap._tempCanvas.getContext('2d');
  1163. }
  1164. }
  1165. canvas = TiledMap._tempCanvas;
  1166. canvas.context.asBitmap = false;
  1167. var i, len;
  1168. len = arr.length;
  1169. var tGrid;
  1170. for (i = 0; i < len; i++) {
  1171. tGrid = arr[i];
  1172. canvas.clear();
  1173. canvas.size(1, 1);
  1174. tGrid.render(canvas.context, 0, 0);
  1175. tGrid.hide();
  1176. }
  1177. canvas.clear();
  1178. canvas.size(1, 1);
  1179. }
  1180. getGridArray(gridX, gridY) {
  1181. var i, j;
  1182. var tGridSprite;
  1183. var tTempArray = this._gridArray[gridY][gridX];
  1184. if (tTempArray == null) {
  1185. tTempArray = this._gridArray[gridY][gridX] = [];
  1186. var tLeft = 0;
  1187. var tRight = 0;
  1188. var tTop = 0;
  1189. var tBottom = 0;
  1190. var tGridWidth = this._gridWidth;
  1191. var tGridHeight = this._gridHeight;
  1192. switch (this.orientation) {
  1193. case TiledMap.ORIENTATION_ISOMETRIC:
  1194. tLeft = Math.floor(gridX * tGridWidth);
  1195. tRight = Math.floor(gridX * tGridWidth + tGridWidth);
  1196. tTop = Math.floor(gridY * tGridHeight);
  1197. tBottom = Math.floor(gridY * tGridHeight + tGridHeight);
  1198. var tLeft1, tRight1, tTop1, tBottom1;
  1199. break;
  1200. case TiledMap.ORIENTATION_STAGGERED:
  1201. tLeft = Math.floor(gridX * tGridWidth / this._mapTileW);
  1202. tRight = Math.floor((gridX * tGridWidth + tGridWidth) / this._mapTileW);
  1203. tTop = Math.floor(gridY * tGridHeight / (this._mapTileH / 2));
  1204. tBottom = Math.floor((gridY * tGridHeight + tGridHeight) / (this._mapTileH / 2));
  1205. break;
  1206. case TiledMap.ORIENTATION_ORTHOGONAL:
  1207. tLeft = Math.floor(gridX * tGridWidth / this._mapTileW);
  1208. tRight = Math.floor((gridX * tGridWidth + tGridWidth) / this._mapTileW);
  1209. tTop = Math.floor(gridY * tGridHeight / this._mapTileH);
  1210. tBottom = Math.floor((gridY * tGridHeight + tGridHeight) / this._mapTileH);
  1211. break;
  1212. case TiledMap.ORIENTATION_HEXAGONAL:
  1213. var tHeight = this._mapTileH * 2 / 3;
  1214. tLeft = Math.floor(gridX * tGridWidth / this._mapTileW);
  1215. tRight = Math.ceil((gridX * tGridWidth + tGridWidth) / this._mapTileW);
  1216. tTop = Math.floor(gridY * tGridHeight / tHeight);
  1217. tBottom = Math.ceil((gridY * tGridHeight + tGridHeight) / tHeight);
  1218. break;
  1219. }
  1220. var tLayer = null;
  1221. var tTGridSprite;
  1222. var tDrawMapLayer;
  1223. for (var z = 0; z < this._layerArray.length; z++) {
  1224. tLayer = this._layerArray[z];
  1225. if (this.enableMergeLayer) {
  1226. if (tLayer.tarLayer != tDrawMapLayer) {
  1227. tTGridSprite = null;
  1228. tDrawMapLayer = tLayer.tarLayer;
  1229. }
  1230. if (!tTGridSprite) {
  1231. tTGridSprite = tDrawMapLayer.getDrawSprite(gridX, gridY);
  1232. tTempArray.push(tTGridSprite);
  1233. }
  1234. tGridSprite = tTGridSprite;
  1235. }
  1236. else {
  1237. tGridSprite = tLayer.getDrawSprite(gridX, gridY);
  1238. tTempArray.push(tGridSprite);
  1239. }
  1240. var tColorStr;
  1241. if (this._showGridKey) {
  1242. tColorStr = "#";
  1243. tColorStr += this._colorArray[Math.floor(Math.random() * this._colorArray.length)];
  1244. tColorStr += this._colorArray[Math.floor(Math.random() * this._colorArray.length)];
  1245. tColorStr += this._colorArray[Math.floor(Math.random() * this._colorArray.length)];
  1246. }
  1247. switch (this.orientation) {
  1248. case TiledMap.ORIENTATION_ISOMETRIC:
  1249. var tHalfTileHeight = this.tileHeight / 2;
  1250. var tHalfTileWidth = this.tileWidth / 2;
  1251. var tHalfMapWidth = this._width / 2;
  1252. tTop1 = Math.floor(tTop / tHalfTileHeight);
  1253. tBottom1 = Math.floor(tBottom / tHalfTileHeight);
  1254. tLeft1 = this._mapW + Math.floor((tLeft - tHalfMapWidth) / tHalfTileWidth);
  1255. tRight1 = this._mapW + Math.floor((tRight - tHalfMapWidth) / tHalfTileWidth);
  1256. var tMapW = this._mapW * 2;
  1257. var tMapH = this._mapH * 2;
  1258. if (tTop1 < 0) {
  1259. tTop1 = 0;
  1260. }
  1261. if (tTop1 >= tMapH) {
  1262. tTop1 = tMapH - 1;
  1263. }
  1264. if (tBottom1 < 0) {
  1265. tBottom = 0;
  1266. }
  1267. if (tBottom1 >= tMapH) {
  1268. tBottom1 = tMapH - 1;
  1269. }
  1270. tGridSprite.zOrder = this._totalGridNum * z + gridY * this._gridW + gridX;
  1271. for (i = tTop1; i < tBottom1; i++) {
  1272. for (j = 0; j <= i; j++) {
  1273. var tIndexX = i - j;
  1274. var tIndexY = j;
  1275. var tIndexValue = (tIndexX - tIndexY) + this._mapW;
  1276. if (tIndexValue > tLeft1 && tIndexValue <= tRight1) {
  1277. if (tLayer.drawTileTexture(tGridSprite, tIndexX, tIndexY)) {
  1278. tGridSprite.drawImageNum++;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. break;
  1284. case TiledMap.ORIENTATION_STAGGERED:
  1285. tGridSprite.zOrder = z * this._totalGridNum + gridY * this._gridW + gridX;
  1286. for (i = tTop; i < tBottom; i++) {
  1287. for (j = tLeft; j < tRight; j++) {
  1288. if (tLayer.drawTileTexture(tGridSprite, j, i)) {
  1289. tGridSprite.drawImageNum++;
  1290. }
  1291. }
  1292. }
  1293. break;
  1294. case TiledMap.ORIENTATION_ORTHOGONAL:
  1295. case TiledMap.ORIENTATION_HEXAGONAL:
  1296. switch (this._renderOrder) {
  1297. case TiledMap.RENDERORDER_RIGHTDOWN:
  1298. tGridSprite.zOrder = z * this._totalGridNum + gridY * this._gridW + gridX;
  1299. for (i = tTop; i < tBottom; i++) {
  1300. for (j = tLeft; j < tRight; j++) {
  1301. if (tLayer.drawTileTexture(tGridSprite, j, i)) {
  1302. tGridSprite.drawImageNum++;
  1303. }
  1304. }
  1305. }
  1306. break;
  1307. case TiledMap.RENDERORDER_RIGHTUP:
  1308. tGridSprite.zOrder = z * this._totalGridNum + (this._gridH - 1 - gridY) * this._gridW + gridX;
  1309. for (i = tBottom - 1; i >= tTop; i--) {
  1310. for (j = tLeft; j < tRight; j++) {
  1311. if (tLayer.drawTileTexture(tGridSprite, j, i)) {
  1312. tGridSprite.drawImageNum++;
  1313. }
  1314. }
  1315. }
  1316. break;
  1317. case TiledMap.RENDERORDER_LEFTDOWN:
  1318. tGridSprite.zOrder = z * this._totalGridNum + gridY * this._gridW + (this._gridW - 1 - gridX);
  1319. for (i = tTop; i < tBottom; i++) {
  1320. for (j = tRight - 1; j >= tLeft; j--) {
  1321. if (tLayer.drawTileTexture(tGridSprite, j, i)) {
  1322. tGridSprite.drawImageNum++;
  1323. }
  1324. }
  1325. }
  1326. break;
  1327. case TiledMap.RENDERORDER_LEFTUP:
  1328. tGridSprite.zOrder = z * this._totalGridNum + (this._gridH - 1 - gridY) * this._gridW + (this._gridW - 1 - gridX);
  1329. for (i = tBottom - 1; i >= tTop; i--) {
  1330. for (j = tRight - 1; j >= tLeft; j--) {
  1331. if (tLayer.drawTileTexture(tGridSprite, j, i)) {
  1332. tGridSprite.drawImageNum++;
  1333. }
  1334. }
  1335. }
  1336. break;
  1337. }
  1338. break;
  1339. }
  1340. if (!tGridSprite.isHaveAnimation) {
  1341. tGridSprite.autoSize = true;
  1342. if (this.autoCache)
  1343. tGridSprite.cacheAs = this.autoCacheType;
  1344. tGridSprite.autoSize = false;
  1345. }
  1346. if (!this.enableMergeLayer) {
  1347. if (tGridSprite.drawImageNum > 0) {
  1348. tLayer.addChild(tGridSprite);
  1349. }
  1350. if (this._showGridKey) {
  1351. tGridSprite.graphics.drawRect(0, 0, tGridWidth, tGridHeight, null, tColorStr);
  1352. }
  1353. }
  1354. else {
  1355. if (tTGridSprite && tTGridSprite.drawImageNum > 0 && tDrawMapLayer) {
  1356. tDrawMapLayer.addChild(tTGridSprite);
  1357. }
  1358. }
  1359. }
  1360. if (this.enableMergeLayer && this.showGridTextureCount) {
  1361. if (tTGridSprite) {
  1362. tTGridSprite.graphics.fillText(tTGridSprite.drawImageNum + "", 20, 20, null, "#ff0000", "left");
  1363. }
  1364. }
  1365. }
  1366. return tTempArray;
  1367. }
  1368. hideGrid(gridX, gridY) {
  1369. if (gridX < 0 || gridX >= this._gridW || gridY < 0 || gridY >= this._gridH) {
  1370. return;
  1371. }
  1372. var tTempArray = this._gridArray[gridY][gridX];
  1373. if (tTempArray) {
  1374. var tGridSprite;
  1375. for (var i = 0; i < tTempArray.length; i++) {
  1376. tGridSprite = tTempArray[i];
  1377. if (tGridSprite.drawImageNum > 0) {
  1378. if (tGridSprite != null) {
  1379. tGridSprite.hide();
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. getLayerObject(layerName, objectName) {
  1386. var tLayer = null;
  1387. for (var i = 0; i < this._layerArray.length; i++) {
  1388. tLayer = this._layerArray[i];
  1389. if (tLayer.layerName == layerName) {
  1390. break;
  1391. }
  1392. }
  1393. if (tLayer) {
  1394. return tLayer.getObjectByName(objectName);
  1395. }
  1396. return null;
  1397. }
  1398. destroy() {
  1399. this._orientation = TiledMap.ORIENTATION_ORTHOGONAL;
  1400. this._jsonData = null;
  1401. var i = 0;
  1402. this._gridArray = [];
  1403. var tTileTexSet;
  1404. for (i = 0; i < this._tileTexSetArr.length; i++) {
  1405. tTileTexSet = this._tileTexSetArr[i];
  1406. if (tTileTexSet) {
  1407. tTileTexSet.clearAll();
  1408. }
  1409. }
  1410. this._tileTexSetArr = [];
  1411. var tTexture;
  1412. for (i = 0; i < this._texArray.length; i++) {
  1413. tTexture = this._texArray[i];
  1414. tTexture.destroy();
  1415. }
  1416. this._texArray = [];
  1417. this._width = 0;
  1418. this._height = 0;
  1419. this._mapW = 0;
  1420. this._mapH = 0;
  1421. this._mapTileW = 0;
  1422. this._mapTileH = 0;
  1423. this._rect.setTo(0, 0, 0, 0);
  1424. var tLayer;
  1425. for (i = 0; i < this._layerArray.length; i++) {
  1426. tLayer = this._layerArray[i];
  1427. tLayer.clearAll();
  1428. }
  1429. this._layerArray = [];
  1430. this._renderLayerArray = [];
  1431. if (this._mapSprite) {
  1432. this._mapSprite.destroy();
  1433. this._mapSprite = null;
  1434. }
  1435. this._jsonLoader = null;
  1436. this._loader = null;
  1437. var tDic = this._animationDic;
  1438. for (var p in tDic) {
  1439. delete tDic[p];
  1440. }
  1441. this._properties = null;
  1442. tDic = this._tileProperties;
  1443. for (p in tDic) {
  1444. delete tDic[p];
  1445. }
  1446. this._currTileSet = null;
  1447. this._completeHandler = null;
  1448. this._mapRect.clearAll();
  1449. this._mapLastRect.clearAll();
  1450. this._tileSetArray = [];
  1451. this._gridWidth = 450;
  1452. this._gridHeight = 450;
  1453. this._gridW = 0;
  1454. this._gridH = 0;
  1455. this._x = 0;
  1456. this._y = 0;
  1457. this._index = 0;
  1458. this._enableLinear = true;
  1459. this._resPath = null;
  1460. this._pathArray = null;
  1461. }
  1462. get tileWidth() {
  1463. return this._mapTileW;
  1464. }
  1465. get tileHeight() {
  1466. return this._mapTileH;
  1467. }
  1468. get width() {
  1469. return this._width;
  1470. }
  1471. get height() {
  1472. return this._height;
  1473. }
  1474. get numColumnsTile() {
  1475. return this._mapW;
  1476. }
  1477. get numRowsTile() {
  1478. return this._mapH;
  1479. }
  1480. get viewPortX() {
  1481. return -this._viewPortX;
  1482. }
  1483. get viewPortY() {
  1484. return -this._viewPortY;
  1485. }
  1486. get viewPortWidth() {
  1487. return this._viewPortWidth;
  1488. }
  1489. get viewPortHeight() {
  1490. return this._viewPortHeight;
  1491. }
  1492. get x() {
  1493. return this._x;
  1494. }
  1495. get y() {
  1496. return this._y;
  1497. }
  1498. get gridWidth() {
  1499. return this._gridWidth;
  1500. }
  1501. get gridHeight() {
  1502. return this._gridHeight;
  1503. }
  1504. get numColumnsGrid() {
  1505. return this._gridW;
  1506. }
  1507. get numRowsGrid() {
  1508. return this._gridH;
  1509. }
  1510. get orientation() {
  1511. return this._orientation;
  1512. }
  1513. get renderOrder() {
  1514. return this._renderOrder;
  1515. }
  1516. mapSprite() {
  1517. return this._mapSprite;
  1518. }
  1519. getLayerByName(layerName) {
  1520. var tMapLayer;
  1521. for (var i = 0; i < this._layerArray.length; i++) {
  1522. tMapLayer = this._layerArray[i];
  1523. if (layerName == tMapLayer.layerName) {
  1524. return tMapLayer;
  1525. }
  1526. }
  1527. return null;
  1528. }
  1529. getLayerByIndex(index) {
  1530. if (index < this._layerArray.length) {
  1531. return this._layerArray[index];
  1532. }
  1533. return null;
  1534. }
  1535. }
  1536. TiledMap.ORIENTATION_ORTHOGONAL = "orthogonal";
  1537. TiledMap.ORIENTATION_ISOMETRIC = "isometric";
  1538. TiledMap.ORIENTATION_STAGGERED = "staggered";
  1539. TiledMap.ORIENTATION_HEXAGONAL = "hexagonal";
  1540. TiledMap.RENDERORDER_RIGHTDOWN = "right-down";
  1541. TiledMap.RENDERORDER_RIGHTUP = "right-up";
  1542. TiledMap.RENDERORDER_LEFTDOWN = "left-down";
  1543. TiledMap.RENDERORDER_LEFTUP = "left-up";
  1544. class GRect {
  1545. clearAll() {
  1546. this.left = this.top = this.right = this.bottom = 0;
  1547. }
  1548. }
  1549. class TileMapAniData {
  1550. constructor() {
  1551. this.mAniIdArray = [];
  1552. this.mDurationTimeArray = [];
  1553. this.mTileTexSetArr = [];
  1554. }
  1555. }
  1556. class TileSet {
  1557. constructor() {
  1558. this.firstgid = 0;
  1559. this.image = "";
  1560. this.imageheight = 0;
  1561. this.imagewidth = 0;
  1562. this.margin = 0;
  1563. this.name = 0;
  1564. this.spacing = 0;
  1565. this.tileheight = 0;
  1566. this.tilewidth = 0;
  1567. this.titleoffsetX = 0;
  1568. this.titleoffsetY = 0;
  1569. }
  1570. init(data) {
  1571. this.firstgid = data.firstgid;
  1572. this.image = data.image;
  1573. this.imageheight = data.imageheight;
  1574. this.imagewidth = data.imagewidth;
  1575. this.margin = data.margin;
  1576. this.name = data.name;
  1577. this.properties = data.properties;
  1578. this.spacing = data.spacing;
  1579. this.tileheight = data.tileheight;
  1580. this.tilewidth = data.tilewidth;
  1581. this.tileproperties = data.tileproperties;
  1582. var tTileoffset = data.tileoffset;
  1583. if (tTileoffset) {
  1584. this.titleoffsetX = tTileoffset.x;
  1585. this.titleoffsetY = tTileoffset.y;
  1586. }
  1587. }
  1588. }
  1589. IMap.TiledMap = TiledMap;
  1590. exports.GridSprite = GridSprite;
  1591. exports.IMap = IMap;
  1592. exports.MapLayer = MapLayer;
  1593. exports.TileAniSprite = TileAniSprite;
  1594. exports.TileTexSet = TileTexSet;
  1595. exports.TiledMap = TiledMap;
  1596. }(window.Laya = window.Laya || {}, Laya));