CCTMXLayer.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * <p>cc.TMXLayer represents the TMX layer. </p>
  24. *
  25. * <p>It is a subclass of cc.SpriteBatchNode. By default the tiles are rendered using a cc.TextureAtlas. <br />
  26. * If you modify a tile on runtime, then, that tile will become a cc.Sprite, otherwise no cc.Sprite objects are created. <br />
  27. * The benefits of using cc.Sprite objects as tiles are: <br />
  28. * - tiles (cc.Sprite) can be rotated/scaled/moved with a nice API </p>
  29. *
  30. * <p>If the layer contains a property named "cc.vertexz" with an integer (in can be positive or negative), <br />
  31. * then all the tiles belonging to the layer will use that value as their OpenGL vertex Z for depth. </p>
  32. *
  33. * <p>On the other hand, if the "cc.vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value. <br />
  34. * Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be: </p>
  35. *
  36. * glAlphaFunc( GL_GREATER, value ) <br />
  37. *
  38. * <p>"value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer. <br />
  39. * The value 0 should work for most cases, but if you have tiles that are semi-transparent, then you might want to use a different value, like 0.5.</p>
  40. * @class
  41. * @extends cc.SpriteBatchNode
  42. *
  43. * @property {Array} tiles - Tiles for layer
  44. * @property {cc.TMXTilesetInfo} tileset - Tileset for layer
  45. * @property {Number} layerOrientation - Layer orientation
  46. * @property {Array} properties - Properties from the layer. They can be added using tilemap editors
  47. * @property {String} layerName - Name of the layer
  48. * @property {Number} layerWidth - Width of the layer
  49. * @property {Number} layerHeight - Height of the layer
  50. * @property {Number} tileWidth - Width of a tile
  51. * @property {Number} tileHeight - Height of a tile
  52. */
  53. cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
  54. tiles: null,
  55. tileset: null,
  56. layerOrientation: null,
  57. properties: null,
  58. layerName: "",
  59. //size of the layer in tiles
  60. _layerSize: null,
  61. _mapTileSize: null,
  62. //TMX Layer supports opacity
  63. _opacity: 255,
  64. _minGID: null,
  65. _maxGID: null,
  66. //Only used when vertexZ is used
  67. _vertexZvalue: null,
  68. _useAutomaticVertexZ: null,
  69. _alphaFuncValue: null,
  70. //used for optimization
  71. _reusedTile: null,
  72. _atlasIndexArray: null,
  73. //used for retina display
  74. _contentScaleFactor: null,
  75. _cacheCanvas:null,
  76. _cacheContext:null,
  77. _cacheTexture:null,
  78. // Sub caches for avoid Chrome big image draw issue
  79. _subCacheCanvas:null,
  80. _subCacheContext:null,
  81. _subCacheCount:0,
  82. _subCacheWidth:0,
  83. // Maximum pixel number by cache, a little more than 3072*3072, real limit is 4096*4096
  84. _maxCachePixel:10000000,
  85. _className:"TMXLayer",
  86. /**
  87. * Creates a cc.TMXLayer with an tile set info, a layer info and a map info <br/>
  88. * Constructor of cc.TMXLayer
  89. * @param {cc.TMXTilesetInfo} tilesetInfo
  90. * @param {cc.TMXLayerInfo} layerInfo
  91. * @param {cc.TMXMapInfo} mapInfo
  92. */
  93. ctor:function (tilesetInfo, layerInfo, mapInfo) {
  94. cc.SpriteBatchNode.prototype.ctor.call(this);
  95. this._descendants = [];
  96. this._layerSize = cc.size(0, 0);
  97. this._mapTileSize = cc.size(0, 0);
  98. if(cc._renderType === cc._RENDER_TYPE_CANVAS){
  99. var locCanvas = cc._canvas;
  100. var tmpCanvas = cc.newElement('canvas');
  101. tmpCanvas.width = locCanvas.width;
  102. tmpCanvas.height = locCanvas.height;
  103. this._cacheCanvas = tmpCanvas;
  104. this._cacheContext = this._cacheCanvas.getContext('2d');
  105. var tempTexture = new cc.Texture2D();
  106. tempTexture.initWithElement(tmpCanvas);
  107. tempTexture.handleLoadedTexture();
  108. this._cacheTexture = tempTexture;
  109. this.width = locCanvas.width;
  110. this.height = locCanvas.height;
  111. // This class uses cache, so its default cachedParent should be himself
  112. this._cachedParent = this;
  113. }
  114. if(mapInfo !== undefined)
  115. this.initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo);
  116. },
  117. /**
  118. * Sets the untransformed size of the TMXLayer.
  119. * @override
  120. * @param {cc.Size|Number} size The untransformed size of the TMXLayer or The untransformed size's width of the TMXLayer.
  121. * @param {Number} [height] The untransformed size's height of the TMXLayer.
  122. */
  123. setContentSize:function (size, height) {
  124. var locContentSize = this._contentSize;
  125. cc.Node.prototype.setContentSize.call(this, size, height);
  126. if(cc._renderType === cc._RENDER_TYPE_CANVAS){
  127. var locCanvas = this._cacheCanvas;
  128. var scaleFactor = cc.contentScaleFactor();
  129. locCanvas.width = 0 | (locContentSize.width * 1.5 * scaleFactor);
  130. locCanvas.height = 0 | (locContentSize.height * 1.5 * scaleFactor);
  131. if(this.layerOrientation === cc.TMX_ORIENTATION_HEX)
  132. this._cacheContext.translate(0, locCanvas.height - (this._mapTileSize.height * 0.5)); //translate for hexagonal
  133. else
  134. this._cacheContext.translate(0, locCanvas.height);
  135. var locTexContentSize = this._cacheTexture._contentSize;
  136. locTexContentSize.width = locCanvas.width;
  137. locTexContentSize.height = locCanvas.height;
  138. // Init sub caches if needed
  139. var totalPixel = locCanvas.width * locCanvas.height;
  140. if(totalPixel > this._maxCachePixel) {
  141. if(!this._subCacheCanvas) this._subCacheCanvas = [];
  142. if(!this._subCacheContext) this._subCacheContext = [];
  143. this._subCacheCount = Math.ceil( totalPixel / this._maxCachePixel );
  144. var locSubCacheCanvas = this._subCacheCanvas, i;
  145. for(i = 0; i < this._subCacheCount; i++) {
  146. if(!locSubCacheCanvas[i]) {
  147. locSubCacheCanvas[i] = document.createElement('canvas');
  148. this._subCacheContext[i] = locSubCacheCanvas[i].getContext('2d');
  149. }
  150. var tmpCanvas = locSubCacheCanvas[i];
  151. tmpCanvas.width = this._subCacheWidth = Math.round( locCanvas.width / this._subCacheCount );
  152. tmpCanvas.height = locCanvas.height;
  153. }
  154. // Clear wasted cache to release memory
  155. for(i = this._subCacheCount; i < locSubCacheCanvas.length; i++) {
  156. tmpCanvas.width = 0;
  157. tmpCanvas.height = 0;
  158. }
  159. }
  160. // Otherwise use count as a flag to disable sub caches
  161. else this._subCacheCount = 0;
  162. }
  163. },
  164. /**
  165. * Return texture of cc.SpriteBatchNode
  166. * @function
  167. * @return {cc.Texture2D}
  168. */
  169. getTexture: null,
  170. _getTextureForCanvas:function () {
  171. return this._cacheTexture;
  172. },
  173. /**
  174. * don't call visit on it's children ( override visit of cc.Node )
  175. * @function
  176. * @override
  177. * @param {CanvasRenderingContext2D} ctx
  178. */
  179. visit: null,
  180. _visitForCanvas: function (ctx) {
  181. var context = ctx || cc._renderContext;
  182. // quick return if not visible
  183. if (!this._visible)
  184. return;
  185. context.save();
  186. this.transform(ctx);
  187. var i, locChildren = this._children;
  188. if (this._cacheDirty) {
  189. //
  190. var eglViewer = cc.view;
  191. eglViewer._setScaleXYForRenderTexture();
  192. //add dirty region
  193. var locCacheContext = this._cacheContext, locCacheCanvas = this._cacheCanvas;
  194. locCacheContext.clearRect(0, 0, locCacheCanvas.width, -locCacheCanvas.height);
  195. locCacheContext.save();
  196. locCacheContext.translate(this._anchorPointInPoints.x, -(this._anchorPointInPoints.y));
  197. if (locChildren) {
  198. this.sortAllChildren();
  199. for (i = 0; i < locChildren.length; i++) {
  200. if (locChildren[i])
  201. locChildren[i].visit(locCacheContext);
  202. }
  203. }
  204. locCacheContext.restore();
  205. // Update sub caches if needed
  206. if(this._subCacheCount > 0) {
  207. var subCacheW = this._subCacheWidth, subCacheH = locCacheCanvas.height;
  208. for(i = 0; i < this._subCacheCount; i++) {
  209. this._subCacheContext[i].drawImage(locCacheCanvas, i * subCacheW, 0, subCacheW, subCacheH, 0, 0, subCacheW, subCacheH);
  210. }
  211. }
  212. //reset Scale
  213. eglViewer._resetScale();
  214. this._cacheDirty = false;
  215. }
  216. // draw RenderTexture
  217. this.draw(ctx);
  218. context.restore();
  219. },
  220. /**
  221. * draw cc.SpriteBatchNode (override draw of cc.Node)
  222. * @function
  223. * @param {CanvasRenderingContext2D} ctx
  224. */
  225. draw:null,
  226. _drawForCanvas:function (ctx) {
  227. var context = ctx || cc._renderContext;
  228. //context.globalAlpha = this._opacity / 255;
  229. var posX = 0 | ( -this._anchorPointInPoints.x), posY = 0 | ( -this._anchorPointInPoints.y);
  230. var eglViewer = cc.view;
  231. var locCacheCanvas = this._cacheCanvas;
  232. //direct draw image by canvas drawImage
  233. if (locCacheCanvas) {
  234. var locSubCacheCount = this._subCacheCount, locCanvasHeight = locCacheCanvas.height * eglViewer._scaleY;
  235. var halfTileSize = this._mapTileSize.height * 0.5 * eglViewer._scaleY;
  236. if(locSubCacheCount > 0) {
  237. var locSubCacheCanvasArr = this._subCacheCanvas;
  238. for(var i = 0; i < locSubCacheCount; i++){
  239. var selSubCanvas = locSubCacheCanvasArr[i];
  240. if (this.layerOrientation === cc.TMX_ORIENTATION_HEX)
  241. context.drawImage(locSubCacheCanvasArr[i], 0, 0, selSubCanvas.width, selSubCanvas.height,
  242. posX + i * this._subCacheWidth, -(posY + locCanvasHeight) + halfTileSize, selSubCanvas.width * eglViewer._scaleX, locCanvasHeight);
  243. else
  244. context.drawImage(locSubCacheCanvasArr[i], 0, 0, selSubCanvas.width, selSubCanvas.height,
  245. posX + i * this._subCacheWidth, -(posY + locCanvasHeight), selSubCanvas.width * eglViewer._scaleX, locCanvasHeight);
  246. }
  247. } else{
  248. if (this.layerOrientation === cc.TMX_ORIENTATION_HEX)
  249. context.drawImage(locCacheCanvas, 0, 0, locCacheCanvas.width, locCacheCanvas.height,
  250. posX, -(posY + locCanvasHeight) + halfTileSize, locCacheCanvas.width * eglViewer._scaleX, locCanvasHeight);
  251. else
  252. context.drawImage(locCacheCanvas, 0, 0, locCacheCanvas.width, locCacheCanvas.height,
  253. posX, -(posY + locCanvasHeight), locCacheCanvas.width * eglViewer._scaleX, locCanvasHeight);
  254. }
  255. }
  256. },
  257. /**
  258. * Gets layer size.
  259. * @return {cc.Size}
  260. */
  261. getLayerSize:function () {
  262. return cc.size(this._layerSize.width, this._layerSize.height);
  263. },
  264. /**
  265. * Set layer size
  266. * @param {cc.Size} Var
  267. */
  268. setLayerSize:function (Var) {
  269. this._layerSize.width = Var.width;
  270. this._layerSize.height = Var.height;
  271. },
  272. _getLayerWidth: function () {
  273. return this._layerSize.width;
  274. },
  275. _setLayerWidth: function (width) {
  276. this._layerSize.width = width;
  277. },
  278. _getLayerHeight: function () {
  279. return this._layerSize.height;
  280. },
  281. _setLayerHeight: function (height) {
  282. this._layerSize.height = height;
  283. },
  284. /**
  285. * Size of the map's tile (could be different from the tile's size)
  286. * @return {cc.Size}
  287. */
  288. getMapTileSize:function () {
  289. return cc.size(this._mapTileSize.width,this._mapTileSize.height);
  290. },
  291. /**
  292. * Set the map tile size.
  293. * @param {cc.Size} Var
  294. */
  295. setMapTileSize:function (Var) {
  296. this._mapTileSize.width = Var.width;
  297. this._mapTileSize.height = Var.height;
  298. },
  299. _getTileWidth: function () {
  300. return this._mapTileSize.width;
  301. },
  302. _setTileWidth: function (width) {
  303. this._mapTileSize.width = width;
  304. },
  305. _getTileHeight: function () {
  306. return this._mapTileSize.height;
  307. },
  308. _setTileHeight: function (height) {
  309. this._mapTileSize.height = height;
  310. },
  311. /**
  312. * Pointer to the map of tiles
  313. * @return {Array}
  314. */
  315. getTiles:function () {
  316. return this.tiles;
  317. },
  318. /**
  319. * Pointer to the map of tiles
  320. * @param {Array} Var
  321. */
  322. setTiles:function (Var) {
  323. this.tiles = Var;
  324. },
  325. /**
  326. * Tile set information for the layer
  327. * @return {cc.TMXTilesetInfo}
  328. */
  329. getTileset:function () {
  330. return this.tileset;
  331. },
  332. /**
  333. * Tile set information for the layer
  334. * @param {cc.TMXTilesetInfo} Var
  335. */
  336. setTileset:function (Var) {
  337. this.tileset = Var;
  338. },
  339. /**
  340. * Layer orientation, which is the same as the map orientation
  341. * @return {Number}
  342. */
  343. getLayerOrientation:function () {
  344. return this.layerOrientation;
  345. },
  346. /**
  347. * Layer orientation, which is the same as the map orientation
  348. * @param {Number} Var
  349. */
  350. setLayerOrientation:function (Var) {
  351. this.layerOrientation = Var;
  352. },
  353. /**
  354. * properties from the layer. They can be added using Tiled
  355. * @return {Array}
  356. */
  357. getProperties:function () {
  358. return this.properties;
  359. },
  360. /**
  361. * properties from the layer. They can be added using Tiled
  362. * @param {Array} Var
  363. */
  364. setProperties:function (Var) {
  365. this.properties = Var;
  366. },
  367. /**
  368. * Initializes a cc.TMXLayer with a tileset info, a layer info and a map info
  369. * @param {cc.TMXTilesetInfo} tilesetInfo
  370. * @param {cc.TMXLayerInfo} layerInfo
  371. * @param {cc.TMXMapInfo} mapInfo
  372. * @return {Boolean}
  373. */
  374. initWithTilesetInfo:function (tilesetInfo, layerInfo, mapInfo) {
  375. // XXX: is 35% a good estimate ?
  376. var size = layerInfo._layerSize;
  377. var totalNumberOfTiles = parseInt(size.width * size.height);
  378. var capacity = totalNumberOfTiles * 0.35 + 1; // 35 percent is occupied ?
  379. var texture;
  380. if (tilesetInfo)
  381. texture = cc.textureCache.addImage(tilesetInfo.sourceImage);
  382. if (this.initWithTexture(texture, capacity)) {
  383. // layerInfo
  384. this.layerName = layerInfo.name;
  385. this._layerSize = size;
  386. this.tiles = layerInfo._tiles;
  387. this._minGID = layerInfo._minGID;
  388. this._maxGID = layerInfo._maxGID;
  389. this._opacity = layerInfo._opacity;
  390. this.properties = layerInfo.properties;
  391. this._contentScaleFactor = cc.director.getContentScaleFactor();
  392. // tilesetInfo
  393. this.tileset = tilesetInfo;
  394. // mapInfo
  395. this._mapTileSize = mapInfo.getTileSize();
  396. this.layerOrientation = mapInfo.orientation;
  397. // offset (after layer orientation is set);
  398. var offset = this._calculateLayerOffset(layerInfo.offset);
  399. this.setPosition(cc.pointPixelsToPoints(offset));
  400. this._atlasIndexArray = [];
  401. this.setContentSize(cc.sizePixelsToPoints(cc.size(this._layerSize.width * this._mapTileSize.width,
  402. this._layerSize.height * this._mapTileSize.height)));
  403. this._useAutomaticVertexZ = false;
  404. this._vertexZvalue = 0;
  405. return true;
  406. }
  407. return false;
  408. },
  409. /**
  410. * <p>Dealloc the map that contains the tile position from memory. <br />
  411. * Unless you want to know at runtime the tiles positions, you can safely call this method. <br />
  412. * If you are going to call layer.getTileGIDAt() then, don't release the map</p>
  413. */
  414. releaseMap:function () {
  415. if (this.tiles)
  416. this.tiles = null;
  417. if (this._atlasIndexArray)
  418. this._atlasIndexArray = null;
  419. },
  420. /**
  421. * <p>Returns the tile (cc.Sprite) at a given a tile coordinate. <br/>
  422. * The returned cc.Sprite will be already added to the cc.TMXLayer. Don't add it again.<br/>
  423. * The cc.Sprite can be treated like any other cc.Sprite: rotated, scaled, translated, opacity, color, etc. <br/>
  424. * You can remove either by calling: <br/>
  425. * - layer.removeChild(sprite, cleanup); <br/>
  426. * - or layer.removeTileAt(ccp(x,y)); </p>
  427. * @param {cc.Point|Number} pos or x
  428. * @param {Number} [y]
  429. * @return {cc.Sprite}
  430. */
  431. getTileAt: function (pos, y) {
  432. if(!pos)
  433. throw "cc.TMXLayer.getTileAt(): pos should be non-null";
  434. if(y !== undefined)
  435. pos = cc.p(pos, y);
  436. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  437. throw "cc.TMXLayer.getTileAt(): invalid position";
  438. if(!this.tiles || !this._atlasIndexArray){
  439. cc.log("cc.TMXLayer.getTileAt(): TMXLayer: the tiles map has been released");
  440. return null;
  441. }
  442. var tile = null, gid = this.getTileGIDAt(pos);
  443. // if GID == 0, then no tile is present
  444. if (gid === 0)
  445. return tile;
  446. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  447. tile = this.getChildByTag(z);
  448. // tile not created yet. create it
  449. if (!tile) {
  450. var rect = this.tileset.rectForGID(gid);
  451. rect = cc.rectPixelsToPoints(rect);
  452. tile = new cc.Sprite();
  453. tile.initWithTexture(this.texture, rect);
  454. tile.batchNode = this;
  455. tile.setPosition(this.getPositionAt(pos));
  456. tile.vertexZ = this._vertexZForPos(pos);
  457. tile.anchorX = 0;
  458. tile.anchorY = 0;
  459. tile.opacity = this._opacity;
  460. var indexForZ = this._atlasIndexForExistantZ(z);
  461. this.addSpriteWithoutQuad(tile, indexForZ, z);
  462. }
  463. return tile;
  464. },
  465. /**
  466. * Returns the tile gid at a given tile coordinate. <br />
  467. * if it returns 0, it means that the tile is empty. <br />
  468. * This method requires the the tile map has not been previously released (eg. don't call layer.releaseMap())<br />
  469. * @param {cc.Point|Number} pos or x
  470. * @param {Number} [y]
  471. * @return {Number}
  472. */
  473. getTileGIDAt:function (pos, y) {
  474. if(!pos)
  475. throw "cc.TMXLayer.getTileGIDAt(): pos should be non-null";
  476. if(y !== undefined)
  477. pos = cc.p(pos, y);
  478. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  479. throw "cc.TMXLayer.getTileGIDAt(): invalid position";
  480. if(!this.tiles || !this._atlasIndexArray){
  481. cc.log("cc.TMXLayer.getTileGIDAt(): TMXLayer: the tiles map has been released");
  482. return null;
  483. }
  484. var idx = 0 | (pos.x + pos.y * this._layerSize.width);
  485. // Bits on the far end of the 32-bit global tile ID are used for tile flags
  486. var tile = this.tiles[idx];
  487. return (tile & cc.TMX_TILE_FLIPPED_MASK) >>> 0;
  488. },
  489. // XXX: deprecated
  490. // tileGIDAt:getTileGIDAt,
  491. /**
  492. * lipped tiles can be changed dynamically
  493. * @param {cc.Point|Number} pos or x
  494. * @param {Number} [y]
  495. * @return {Number}
  496. */
  497. getTileFlagsAt:function (pos, y) {
  498. if(!pos)
  499. throw "cc.TMXLayer.getTileFlagsAt(): pos should be non-null";
  500. if(y !== undefined)
  501. pos = cc.p(pos, y);
  502. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  503. throw "cc.TMXLayer.getTileFlagsAt(): invalid position";
  504. if(!this.tiles || !this._atlasIndexArray){
  505. cc.log("cc.TMXLayer.getTileFlagsAt(): TMXLayer: the tiles map has been released");
  506. return null;
  507. }
  508. var idx = 0 | (pos.x + pos.y * this._layerSize.width);
  509. // Bits on the far end of the 32-bit global tile ID are used for tile flags
  510. var tile = this.tiles[idx];
  511. return (tile & cc.TMX_TILE_FLIPPED_ALL) >>> 0;
  512. },
  513. // XXX: deprecated
  514. // tileFlagAt:getTileFlagsAt,
  515. /**
  516. * <p>Sets the tile gid (gid = tile global id) at a given tile coordinate.<br />
  517. * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor . Tileset Mgr +1.<br />
  518. * If a tile is already placed at that position, then it will be removed.</p>
  519. * @param {Number} gid
  520. * @param {cc.Point|Number} posOrX position or x
  521. * @param {Number} flagsOrY flags or y
  522. * @param {Number} [flags]
  523. */
  524. setTileGID: function(gid, posOrX, flagsOrY, flags) {
  525. if(!posOrX)
  526. throw "cc.TMXLayer.setTileGID(): pos should be non-null";
  527. var pos;
  528. if (flags !== undefined) {
  529. pos = cc.p(posOrX, flagsOrY);
  530. } else {
  531. pos = posOrX;
  532. flags = flagsOrY;
  533. }
  534. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  535. throw "cc.TMXLayer.setTileGID(): invalid position";
  536. if(!this.tiles || !this._atlasIndexArray){
  537. cc.log("cc.TMXLayer.setTileGID(): TMXLayer: the tiles map has been released");
  538. return;
  539. }
  540. if(gid !== 0 && gid < this.tileset.firstGid){
  541. cc.log( "cc.TMXLayer.setTileGID(): invalid gid:" + gid);
  542. return;
  543. }
  544. flags = flags || 0;
  545. this._setNodeDirtyForCache();
  546. var currentFlags = this.getTileFlagsAt(pos);
  547. var currentGID = this.getTileGIDAt(pos);
  548. if (currentGID != gid || currentFlags != flags) {
  549. var gidAndFlags = (gid | flags) >>> 0;
  550. // setting gid=0 is equal to remove the tile
  551. if (gid === 0)
  552. this.removeTileAt(pos);
  553. else if (currentGID === 0) // empty tile. create a new one
  554. this._insertTileForGID(gidAndFlags, pos);
  555. else { // modifying an existing tile with a non-empty tile
  556. var z = pos.x + pos.y * this._layerSize.width;
  557. var sprite = this.getChildByTag(z);
  558. if (sprite) {
  559. var rect = this.tileset.rectForGID(gid);
  560. rect = cc.rectPixelsToPoints(rect);
  561. sprite.setTextureRect(rect, false);
  562. if (flags != null)
  563. this._setupTileSprite(sprite, pos, gidAndFlags);
  564. this.tiles[z] = gidAndFlags;
  565. } else
  566. this._updateTileForGID(gidAndFlags, pos);
  567. }
  568. }
  569. },
  570. /**
  571. * Removes a tile at given tile coordinate
  572. * @param {cc.Point|Number} pos position or x
  573. * @param {Number} [y]
  574. */
  575. removeTileAt:function (pos, y) {
  576. if(!pos)
  577. throw "cc.TMXLayer.removeTileAt(): pos should be non-null";
  578. if(y !== undefined)
  579. pos = cc.p(pos, y);
  580. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  581. throw "cc.TMXLayer.removeTileAt(): invalid position";
  582. if(!this.tiles || !this._atlasIndexArray){
  583. cc.log("cc.TMXLayer.removeTileAt(): TMXLayer: the tiles map has been released");
  584. return;
  585. }
  586. var gid = this.getTileGIDAt(pos);
  587. if (gid !== 0) {
  588. if (cc._renderType === cc._RENDER_TYPE_CANVAS)
  589. this._setNodeDirtyForCache();
  590. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  591. var atlasIndex = this._atlasIndexForExistantZ(z);
  592. // remove tile from GID map
  593. this.tiles[z] = 0;
  594. // remove tile from atlas position array
  595. this._atlasIndexArray.splice(atlasIndex, 1);
  596. // remove it from sprites and/or texture atlas
  597. var sprite = this.getChildByTag(z);
  598. if (sprite)
  599. cc.SpriteBatchNode.prototype.removeChild.call(this, sprite, true); //this.removeChild(sprite, true);
  600. else {
  601. if(cc._renderType === cc._RENDER_TYPE_WEBGL)
  602. this.textureAtlas.removeQuadAtIndex(atlasIndex);
  603. // update possible children
  604. if (this._children) {
  605. var locChildren = this._children;
  606. for (var i = 0, len = locChildren.length; i < len; i++) {
  607. var child = locChildren[i];
  608. if (child) {
  609. var ai = child.atlasIndex;
  610. if (ai >= atlasIndex)
  611. child.atlasIndex = ai - 1;
  612. }
  613. }
  614. }
  615. }
  616. }
  617. },
  618. /**
  619. * Returns the position in pixels of a given tile coordinate
  620. * @param {cc.Point|Number} pos position or x
  621. * @param {Number} [y]
  622. * @return {cc.Point}
  623. */
  624. getPositionAt:function (pos, y) {
  625. if (y !== undefined)
  626. pos = cc.p(pos, y);
  627. var ret = cc.p(0,0);
  628. switch (this.layerOrientation) {
  629. case cc.TMX_ORIENTATION_ORTHO:
  630. ret = this._positionForOrthoAt(pos);
  631. break;
  632. case cc.TMX_ORIENTATION_ISO:
  633. ret = this._positionForIsoAt(pos);
  634. break;
  635. case cc.TMX_ORIENTATION_HEX:
  636. ret = this._positionForHexAt(pos);
  637. break;
  638. }
  639. return cc.pointPixelsToPoints(ret);
  640. },
  641. // XXX: Deprecated. For backward compatibility only
  642. // positionAt:getPositionAt,
  643. /**
  644. * Return the value for the specific property name
  645. * @param {String} propertyName
  646. * @return {*}
  647. */
  648. getProperty:function (propertyName) {
  649. return this.properties[propertyName];
  650. },
  651. /**
  652. * Creates the tiles
  653. */
  654. setupTiles:function () {
  655. // Optimization: quick hack that sets the image size on the tileset
  656. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  657. this.tileset.imageSize = this._originalTexture.getContentSizeInPixels();
  658. } else {
  659. this.tileset.imageSize = this.textureAtlas.texture.getContentSizeInPixels();
  660. // By default all the tiles are aliased
  661. // pros:
  662. // - easier to render
  663. // cons:
  664. // - difficult to scale / rotate / etc.
  665. this.textureAtlas.texture.setAliasTexParameters();
  666. }
  667. // Parse cocos2d properties
  668. this._parseInternalProperties();
  669. if (cc._renderType === cc._RENDER_TYPE_CANVAS)
  670. this._setNodeDirtyForCache();
  671. var locLayerHeight = this._layerSize.height, locLayerWidth = this._layerSize.width;
  672. for (var y = 0; y < locLayerHeight; y++) {
  673. for (var x = 0; x < locLayerWidth; x++) {
  674. var pos = x + locLayerWidth * y;
  675. var gid = this.tiles[pos];
  676. // XXX: gid == 0 -. empty tile
  677. if (gid !== 0) {
  678. this._appendTileForGID(gid, cc.p(x, y));
  679. // Optimization: update min and max GID rendered by the layer
  680. this._minGID = Math.min(gid, this._minGID);
  681. this._maxGID = Math.max(gid, this._maxGID);
  682. }
  683. }
  684. }
  685. if (!((this._maxGID >= this.tileset.firstGid) && (this._minGID >= this.tileset.firstGid))) {
  686. cc.log("cocos2d:TMX: Only 1 tileset per layer is supported");
  687. }
  688. },
  689. /**
  690. * cc.TMXLayer doesn't support adding a cc.Sprite manually.
  691. * @warning addChild(child); is not supported on cc.TMXLayer. Instead of setTileGID.
  692. * @param {cc.Node} child
  693. * @param {number} zOrder
  694. * @param {number} tag
  695. */
  696. addChild:function (child, zOrder, tag) {
  697. cc.log("addChild: is not supported on cc.TMXLayer. Instead use setTileGID or tileAt.");
  698. },
  699. /**
  700. * Remove child
  701. * @param {cc.Sprite} sprite
  702. * @param {Boolean} cleanup
  703. */
  704. removeChild:function (sprite, cleanup) {
  705. // allows removing nil objects
  706. if (!sprite)
  707. return;
  708. if(this._children.indexOf(sprite) === -1){
  709. cc.log("cc.TMXLayer.removeChild(): Tile does not belong to TMXLayer");
  710. return;
  711. }
  712. if (cc._renderType === cc._RENDER_TYPE_CANVAS)
  713. this._setNodeDirtyForCache();
  714. var atlasIndex = sprite.atlasIndex;
  715. var zz = this._atlasIndexArray[atlasIndex];
  716. this.tiles[zz] = 0;
  717. this._atlasIndexArray.splice(atlasIndex, 1);
  718. cc.SpriteBatchNode.prototype.removeChild.call(this, sprite, cleanup);
  719. },
  720. /**
  721. * Gets the layer name
  722. * @return {String}
  723. */
  724. getLayerName:function () {
  725. return this.layerName;
  726. },
  727. /**
  728. * Set the layer name
  729. * @param {String} layerName
  730. */
  731. setLayerName:function (layerName) {
  732. this.layerName = layerName;
  733. },
  734. _positionForIsoAt:function (pos) {
  735. return cc.p(this._mapTileSize.width / 2 * ( this._layerSize.width + pos.x - pos.y - 1),
  736. this._mapTileSize.height / 2 * (( this._layerSize.height * 2 - pos.x - pos.y) - 2));
  737. },
  738. _positionForOrthoAt:function (pos) {
  739. return cc.p(pos.x * this._mapTileSize.width,
  740. (this._layerSize.height - pos.y - 1) * this._mapTileSize.height);
  741. },
  742. _positionForHexAt:function (pos) {
  743. var diffY = (pos.x % 2 == 1) ? (-this._mapTileSize.height / 2) : 0;
  744. return cc.p(pos.x * this._mapTileSize.width * 3 / 4,
  745. (this._layerSize.height - pos.y - 1) * this._mapTileSize.height + diffY);
  746. },
  747. _calculateLayerOffset:function (pos) {
  748. var ret = cc.p(0,0);
  749. switch (this.layerOrientation) {
  750. case cc.TMX_ORIENTATION_ORTHO:
  751. ret = cc.p(pos.x * this._mapTileSize.width, -pos.y * this._mapTileSize.height);
  752. break;
  753. case cc.TMX_ORIENTATION_ISO:
  754. ret = cc.p((this._mapTileSize.width / 2) * (pos.x - pos.y),
  755. (this._mapTileSize.height / 2 ) * (-pos.x - pos.y));
  756. break;
  757. case cc.TMX_ORIENTATION_HEX:
  758. if(pos.x !== 0 || pos.y !== 0)
  759. cc.log("offset for hexagonal map not implemented yet");
  760. break;
  761. }
  762. return ret;
  763. },
  764. _appendTileForGID:function (gid, pos) {
  765. var rect = this.tileset.rectForGID(gid);
  766. rect = cc.rectPixelsToPoints(rect);
  767. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  768. var tile = this._reusedTileWithRect(rect);
  769. this._setupTileSprite(tile, pos, gid);
  770. // optimization:
  771. // The difference between appendTileForGID and insertTileforGID is that append is faster, since
  772. // it appends the tile at the end of the texture atlas
  773. var indexForZ = this._atlasIndexArray.length;
  774. // don't add it using the "standard" way.
  775. this.insertQuadFromSprite(tile, indexForZ);
  776. // append should be after addQuadFromSprite since it modifies the quantity values
  777. this._atlasIndexArray.splice(indexForZ, 0, z);
  778. return tile;
  779. },
  780. _insertTileForGID:function (gid, pos) {
  781. var rect = this.tileset.rectForGID(gid);
  782. rect = cc.rectPixelsToPoints(rect);
  783. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  784. var tile = this._reusedTileWithRect(rect);
  785. this._setupTileSprite(tile, pos, gid);
  786. // get atlas index
  787. var indexForZ = this._atlasIndexForNewZ(z);
  788. // Optimization: add the quad without adding a child
  789. this.insertQuadFromSprite(tile, indexForZ);
  790. // insert it into the local atlasindex array
  791. this._atlasIndexArray.splice(indexForZ, 0, z);
  792. // update possible children
  793. if (this._children) {
  794. var locChildren = this._children;
  795. for (var i = 0, len = locChildren.length; i < len; i++) {
  796. var child = locChildren[i];
  797. if (child) {
  798. var ai = child.atlasIndex;
  799. if (ai >= indexForZ)
  800. child.atlasIndex = ai + 1;
  801. }
  802. }
  803. }
  804. this.tiles[z] = gid;
  805. return tile;
  806. },
  807. _updateTileForGID:function (gid, pos) {
  808. var rect = this.tileset.rectForGID(gid);
  809. var locScaleFactor = this._contentScaleFactor;
  810. rect = cc.rect(rect.x / locScaleFactor, rect.y / locScaleFactor,
  811. rect.width / locScaleFactor, rect.height / locScaleFactor);
  812. var z = pos.x + pos.y * this._layerSize.width;
  813. var tile = this._reusedTileWithRect(rect);
  814. this._setupTileSprite(tile, pos, gid);
  815. // get atlas index
  816. tile.atlasIndex = this._atlasIndexForExistantZ(z);
  817. tile.dirty = true;
  818. tile.updateTransform();
  819. this.tiles[z] = gid;
  820. return tile;
  821. },
  822. //The layer recognizes some special properties, like cc_vertez
  823. _parseInternalProperties:function () {
  824. // if cc_vertex=automatic, then tiles will be rendered using vertexz
  825. var vertexz = this.getProperty("cc_vertexz");
  826. if (vertexz) {
  827. if (vertexz == "automatic") {
  828. this._useAutomaticVertexZ = true;
  829. var alphaFuncVal = this.getProperty("cc_alpha_func");
  830. var alphaFuncValue = 0;
  831. if (alphaFuncVal)
  832. alphaFuncValue = parseFloat(alphaFuncVal);
  833. if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
  834. this.shaderProgram = cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURECOLORALPHATEST);
  835. var alphaValueLocation = cc._renderContext.getUniformLocation(this.shaderProgram.getProgram(), cc.UNIFORM_ALPHA_TEST_VALUE_S);
  836. // NOTE: alpha test shader is hard-coded to use the equivalent of a glAlphaFunc(GL_GREATER) comparison
  837. this.shaderProgram.use();
  838. this.shaderProgram.setUniformLocationWith1f(alphaValueLocation, alphaFuncValue);
  839. }
  840. } else
  841. this._vertexZvalue = parseInt(vertexz, 10);
  842. }
  843. },
  844. _setupTileSprite:function (sprite, pos, gid) {
  845. var z = pos.x + pos.y * this._layerSize.width;
  846. sprite.setPosition(this.getPositionAt(pos));
  847. if (cc._renderType === cc._RENDER_TYPE_WEBGL)
  848. sprite.vertexZ = this._vertexZForPos(pos);
  849. else
  850. sprite.tag = z;
  851. sprite.anchorX = 0;
  852. sprite.anchorY = 0;
  853. sprite.opacity = this._opacity;
  854. if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
  855. sprite.rotation = 0.0;
  856. }
  857. sprite.setFlippedX(false);
  858. sprite.setFlippedY(false);
  859. // Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles.
  860. if ((gid & cc.TMX_TILE_DIAGONAL_FLAG) >>> 0) {
  861. // put the anchor in the middle for ease of rotation.
  862. sprite.anchorX = 0.5;
  863. sprite.anchorY = 0.5;
  864. sprite.x = this.getPositionAt(pos).x + sprite.width / 2;
  865. sprite.y = this.getPositionAt(pos).y + sprite.height / 2;
  866. var flag = (gid & (cc.TMX_TILE_HORIZONTAL_FLAG | cc.TMX_TILE_VERTICAL_FLAG) >>> 0) >>> 0;
  867. // handle the 4 diagonally flipped states.
  868. if (flag == cc.TMX_TILE_HORIZONTAL_FLAG)
  869. sprite.rotation = 90;
  870. else if (flag == cc.TMX_TILE_VERTICAL_FLAG)
  871. sprite.rotation = 270;
  872. else if (flag == (cc.TMX_TILE_VERTICAL_FLAG | cc.TMX_TILE_HORIZONTAL_FLAG) >>> 0) {
  873. sprite.rotation = 90;
  874. sprite.setFlippedX(true);
  875. } else {
  876. sprite.rotation = 270;
  877. sprite.setFlippedX(true);
  878. }
  879. } else {
  880. if ((gid & cc.TMX_TILE_HORIZONTAL_FLAG) >>> 0) {
  881. sprite.setFlippedX(true);
  882. }
  883. if ((gid & cc.TMX_TILE_VERTICAL_FLAG) >>> 0) {
  884. sprite.setFlippedY(true);
  885. }
  886. }
  887. },
  888. _reusedTileWithRect:function (rect) {
  889. if(cc._renderType === cc._RENDER_TYPE_WEBGL){
  890. if (!this._reusedTile) {
  891. this._reusedTile = new cc.Sprite();
  892. this._reusedTile.initWithTexture(this.texture, rect, false);
  893. this._reusedTile.batchNode = this;
  894. } else {
  895. // XXX HACK: Needed because if "batch node" is nil,
  896. // then the Sprite'squad will be reset
  897. this._reusedTile.batchNode = null;
  898. // Re-init the sprite
  899. this._reusedTile.setTextureRect(rect, false);
  900. // restore the batch node
  901. this._reusedTile.batchNode = this;
  902. }
  903. } else {
  904. this._reusedTile = new cc.Sprite();
  905. this._reusedTile.initWithTexture(this._textureForCanvas, rect, false);
  906. this._reusedTile.batchNode = this;
  907. this._reusedTile.parent = this;
  908. }
  909. return this._reusedTile;
  910. },
  911. _vertexZForPos:function (pos) {
  912. var ret = 0;
  913. var maxVal = 0;
  914. if (this._useAutomaticVertexZ) {
  915. switch (this.layerOrientation) {
  916. case cc.TMX_ORIENTATION_ISO:
  917. maxVal = this._layerSize.width + this._layerSize.height;
  918. ret = -(maxVal - (pos.x + pos.y));
  919. break;
  920. case cc.TMX_ORIENTATION_ORTHO:
  921. ret = -(this._layerSize.height - pos.y);
  922. break;
  923. case cc.TMX_ORIENTATION_HEX:
  924. cc.log("TMX Hexa zOrder not supported");
  925. break;
  926. default:
  927. cc.log("TMX invalid value");
  928. break;
  929. }
  930. } else
  931. ret = this._vertexZvalue;
  932. return ret;
  933. },
  934. _atlasIndexForExistantZ:function (z) {
  935. var item;
  936. if (this._atlasIndexArray) {
  937. var locAtlasIndexArray = this._atlasIndexArray;
  938. for (var i = 0, len = locAtlasIndexArray.length; i < len; i++) {
  939. item = locAtlasIndexArray[i];
  940. if (item == z)
  941. break;
  942. }
  943. }
  944. if(typeof item != "number")
  945. cc.log("cc.TMXLayer._atlasIndexForExistantZ(): TMX atlas index not found. Shall not happen");
  946. return i;
  947. },
  948. _atlasIndexForNewZ:function (z) {
  949. var locAtlasIndexArray = this._atlasIndexArray;
  950. for (var i = 0, len = locAtlasIndexArray.length; i < len; i++) {
  951. var val = locAtlasIndexArray[i];
  952. if (z < val)
  953. break;
  954. }
  955. return i;
  956. }
  957. });
  958. var _p = cc.TMXLayer.prototype;
  959. if(cc._renderType == cc._RENDER_TYPE_WEBGL){
  960. _p.draw = cc.SpriteBatchNode.prototype.draw;
  961. _p.visit = cc.SpriteBatchNode.prototype.visit;
  962. _p.getTexture = cc.SpriteBatchNode.prototype.getTexture;
  963. }else{
  964. _p.draw = _p._drawForCanvas;
  965. _p.visit = _p._visitForCanvas;
  966. _p.getTexture = _p._getTextureForCanvas;
  967. }
  968. /** @expose */
  969. cc.defineGetterSetter(_p, "texture", _p.getTexture, _p.setTexture);
  970. // Extended properties
  971. /** @expose */
  972. _p.layerWidth;
  973. cc.defineGetterSetter(_p, "layerWidth", _p._getLayerWidth, _p._setLayerWidth);
  974. /** @expose */
  975. _p.layerHeight;
  976. cc.defineGetterSetter(_p, "layerHeight", _p._getLayerHeight, _p._setLayerHeight);
  977. /** @expose */
  978. _p.tileWidth;
  979. cc.defineGetterSetter(_p, "tileWidth", _p._getTileWidth, _p._setTileWidth);
  980. /** @expose */
  981. _p.tileHeight;
  982. cc.defineGetterSetter(_p, "tileHeight", _p._getTileHeight, _p._setTileHeight);
  983. /**
  984. * Creates a cc.TMXLayer with an tile set info, a layer info and a map info
  985. * @deprecated since v3.0 please use new cc.TMXLayer(tilesetInfo, layerInfo, mapInfo) instead.
  986. * @param {cc.TMXTilesetInfo} tilesetInfo
  987. * @param {cc.TMXLayerInfo} layerInfo
  988. * @param {cc.TMXMapInfo} mapInfo
  989. * @return {cc.TMXLayer|Null}
  990. */
  991. cc.TMXLayer.create = function (tilesetInfo, layerInfo, mapInfo) {
  992. return new cc.TMXLayer(tilesetInfo, layerInfo, mapInfo);
  993. };