CCTMXLayer.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
  44. //size of the layer in tiles
  45. _layerSize: null,
  46. _mapTileSize: null,
  47. _tiles: null,
  48. _tileSet: null,
  49. _layerOrientation: null,
  50. _properties: null,
  51. //name of the layer
  52. _layerName: "",
  53. //TMX Layer supports opacity
  54. _opacity: 255,
  55. _minGID: null,
  56. _maxGID: null,
  57. //Only used when vertexZ is used
  58. _vertexZvalue: null,
  59. _useAutomaticVertexZ: null,
  60. _alphaFuncValue: null,
  61. //used for optimization
  62. _reusedTile: null,
  63. _atlasIndexArray: null,
  64. //used for retina display
  65. _contentScaleFactor: null,
  66. _cacheCanvas:null,
  67. _cacheContext:null,
  68. _cacheTexture:null,
  69. // Sub caches for avoid Chrome big image draw issue
  70. _subCacheCanvas:null,
  71. _subCacheContext:null,
  72. _subCacheCount:0,
  73. _subCacheWidth:0,
  74. // Maximum pixel number by cache, a little more than 3072*3072, real limit is 4096*4096
  75. _maxCachePixel:10000000,
  76. /**
  77. * Constructor
  78. */
  79. ctor:function () {
  80. cc.SpriteBatchNode.prototype.ctor.call(this);
  81. this._descendants = [];
  82. this._layerSize = cc.SizeZero();
  83. this._mapTileSize = cc.SizeZero();
  84. if(cc.renderContextType === cc.CANVAS){
  85. var locCanvas = cc.canvas;
  86. var tmpCanvas = document.createElement('canvas');
  87. tmpCanvas.width = locCanvas.width;
  88. tmpCanvas.height = locCanvas.height;
  89. this._cacheCanvas = tmpCanvas;
  90. this._cacheContext = this._cacheCanvas.getContext('2d');
  91. var tempTexture = new cc.Texture2D();
  92. tempTexture.initWithElement(tmpCanvas);
  93. tempTexture.handleLoadedTexture();
  94. this._cacheTexture = tempTexture;
  95. this.setContentSize(locCanvas.width, locCanvas.height);
  96. }
  97. },
  98. /**
  99. * Sets the untransformed size of the TMXLayer.
  100. * @override
  101. * @param {cc.Size|Number} size The untransformed size of the TMXLayer or The untransformed size's width of the TMXLayer.
  102. * @param {Number} [height] The untransformed size's height of the TMXLayer.
  103. */
  104. setContentSize:function (size, height) {
  105. var locContentSize = this._contentSize;
  106. cc.Node.prototype.setContentSize.call(this, size, height);
  107. if(cc.renderContextType === cc.CANVAS){
  108. var locCanvas = this._cacheCanvas;
  109. var scaleFactor = cc.CONTENT_SCALE_FACTOR();
  110. locCanvas.width = 0 | (locContentSize.width * 1.5 * scaleFactor);
  111. locCanvas.height = 0 | (locContentSize.height * 1.5 * scaleFactor);
  112. this._cacheContext.translate(0, locCanvas.height);
  113. var locTexContentSize = this._cacheTexture._contentSize;
  114. locTexContentSize.width = locCanvas.width;
  115. locTexContentSize.height = locCanvas.height;
  116. // Init sub caches if needed
  117. var totalPixel = locCanvas.width * locCanvas.height;
  118. if(totalPixel > this._maxCachePixel) {
  119. if(!this._subCacheCanvas) this._subCacheCanvas = [];
  120. if(!this._subCacheContext) this._subCacheContext = [];
  121. this._subCacheCount = Math.ceil( totalPixel / this._maxCachePixel );
  122. var locSubCacheCanvas = this._subCacheCanvas, i;
  123. for(i = 0; i < this._subCacheCount; i++) {
  124. if(!locSubCacheCanvas[i]) {
  125. locSubCacheCanvas[i] = document.createElement('canvas');
  126. this._subCacheContext[i] = locSubCacheCanvas[i].getContext('2d');
  127. }
  128. var tmpCanvas = locSubCacheCanvas[i];
  129. tmpCanvas.width = this._subCacheWidth = Math.round( locCanvas.width / this._subCacheCount );
  130. tmpCanvas.height = locCanvas.height;
  131. }
  132. // Clear wasted cache to release memory
  133. for(i = this._subCacheCount; i < locSubCacheCanvas.length; i++) {
  134. tmpCanvas.width = 0;
  135. tmpCanvas.height = 0;
  136. }
  137. }
  138. // Otherwise use count as a flag to disable sub caches
  139. else this._subCacheCount = 0;
  140. }
  141. },
  142. /**
  143. * Return texture of cc.SpriteBatchNode
  144. * @return {cc.Texture2D}
  145. */
  146. getTexture:function () {
  147. if(cc.renderContextType === cc.CANVAS)
  148. return this._cacheTexture;
  149. else
  150. return cc.SpriteBatchNode.prototype.getTexture.call(this);
  151. },
  152. /**
  153. * don't call visit on it's children ( override visit of cc.Node )
  154. * @override
  155. * @param {CanvasRenderingContext2D} ctx
  156. */
  157. visit: null,
  158. _visitForCanvas: function (ctx) {
  159. var context = ctx || cc.renderContext;
  160. // quick return if not visible
  161. if (!this._visible)
  162. return;
  163. context.save();
  164. this.transform(ctx);
  165. var i, locChildren = this._children;
  166. if (this._cacheDirty) {
  167. //
  168. var eglViewer = cc.EGLView.getInstance();
  169. eglViewer._setScaleXYForRenderTexture();
  170. //add dirty region
  171. var locCacheContext = this._cacheContext, locCacheCanvas = this._cacheCanvas;
  172. locCacheContext.clearRect(0, 0, locCacheCanvas.width, -locCacheCanvas.height);
  173. locCacheContext.save();
  174. locCacheContext.translate(this._anchorPointInPoints.x, -(this._anchorPointInPoints.y));
  175. if (locChildren) {
  176. this.sortAllChildren();
  177. for (i = 0; i < locChildren.length; i++) {
  178. if (locChildren[i])
  179. locChildren[i].visit(locCacheContext);
  180. }
  181. }
  182. locCacheContext.restore();
  183. // Update sub caches if needed
  184. if(this._subCacheCount > 0) {
  185. var subCacheW = this._subCacheWidth, subCacheH = locCacheCanvas.height;
  186. for(i = 0; i < this._subCacheCount; i++) {
  187. this._subCacheContext[i].drawImage(locCacheCanvas, i * subCacheW, 0, subCacheW, subCacheH, 0, 0, subCacheW, subCacheH);
  188. }
  189. }
  190. //reset Scale
  191. eglViewer._resetScale();
  192. this._cacheDirty = false;
  193. }
  194. // draw RenderTexture
  195. this.draw(ctx);
  196. context.restore();
  197. },
  198. /**
  199. * draw cc.SpriteBatchNode (override draw of cc.Node)
  200. * @param {CanvasRenderingContext2D} ctx
  201. */
  202. draw:null,
  203. _drawForCanvas:function (ctx) {
  204. var context = ctx || cc.renderContext;
  205. //context.globalAlpha = this._opacity / 255;
  206. var posX = 0 | ( -this._anchorPointInPoints.x), posY = 0 | ( -this._anchorPointInPoints.y);
  207. var eglViewer = cc.EGLView.getInstance();
  208. var locCacheCanvas = this._cacheCanvas;
  209. //direct draw image by canvas drawImage
  210. if (locCacheCanvas) {
  211. var locSubCacheCount = this._subCacheCount, locCanvasHeight = locCacheCanvas.height * eglViewer._scaleY;
  212. if(locSubCacheCount > 0) {
  213. var locSubCacheCanvasArr = this._subCacheCanvas;
  214. for(var i = 0; i < locSubCacheCount; i++){
  215. var selSubCanvas = locSubCacheCanvasArr[i];
  216. context.drawImage(locSubCacheCanvasArr[i], 0, 0, selSubCanvas.width, selSubCanvas.height,
  217. posX + i * this._subCacheWidth, -(posY + locCanvasHeight), selSubCanvas.width * eglViewer._scaleX, locCanvasHeight);
  218. }
  219. } else{
  220. //context.drawImage(locCacheCanvas, 0, 0, locCacheCanvas.width, locCacheCanvas.height,
  221. // posX, -(posY + locCacheCanvas.height ), locCacheCanvas.width, locCacheCanvas.height );
  222. context.drawImage(locCacheCanvas, 0, 0, locCacheCanvas.width, locCacheCanvas.height,
  223. posX, -(posY + locCanvasHeight), locCacheCanvas.width * eglViewer._scaleX, locCanvasHeight);
  224. }
  225. }
  226. },
  227. /**
  228. * @return {cc.Size}
  229. */
  230. getLayerSize:function () {
  231. return cc.size(this._layerSize.width, this._layerSize.height);
  232. },
  233. /**
  234. * @param {cc.Size} Var
  235. */
  236. setLayerSize:function (Var) {
  237. this._layerSize.width = Var.width;
  238. this._layerSize.height = Var.height;
  239. },
  240. /**
  241. * Size of the map's tile (could be different from the tile's size)
  242. * @return {cc.Size}
  243. */
  244. getMapTileSize:function () {
  245. return cc.size(this._mapTileSize.width,this._mapTileSize.height);
  246. },
  247. /**
  248. * @param {cc.Size} Var
  249. */
  250. setMapTileSize:function (Var) {
  251. this._mapTileSize.width = Var.width;
  252. this._mapTileSize.height = Var.height;
  253. },
  254. /**
  255. * Pointer to the map of tiles
  256. * @return {Array}
  257. */
  258. getTiles:function () {
  259. return this._tiles;
  260. },
  261. /**
  262. * @param {Array} Var
  263. */
  264. setTiles:function (Var) {
  265. this._tiles = Var;
  266. },
  267. /**
  268. * Tile set information for the layer
  269. * @return {cc.TMXTilesetInfo}
  270. */
  271. getTileSet:function () {
  272. return this._tileSet;
  273. },
  274. /**
  275. * @param {cc.TMXTilesetInfo} Var
  276. */
  277. setTileSet:function (Var) {
  278. this._tileSet = Var;
  279. },
  280. /**
  281. * Layer orientation, which is the same as the map orientation
  282. * @return {Number}
  283. */
  284. getLayerOrientation:function () {
  285. return this._layerOrientation;
  286. },
  287. /**
  288. * @param {Number} Var
  289. */
  290. setLayerOrientation:function (Var) {
  291. this._layerOrientation = Var;
  292. },
  293. /**
  294. * properties from the layer. They can be added using Tiled
  295. * @return {Array}
  296. */
  297. getProperties:function () {
  298. return this._properties;
  299. },
  300. /**
  301. * @param {Array} Var
  302. */
  303. setProperties:function (Var) {
  304. this._properties = Var;
  305. },
  306. /**
  307. * Initializes a cc.TMXLayer with a tileset info, a layer info and a map info
  308. * @param {cc.TMXTilesetInfo} tilesetInfo
  309. * @param {cc.TMXLayerInfo} layerInfo
  310. * @param {cc.TMXMapInfo} mapInfo
  311. * @return {Boolean}
  312. */
  313. initWithTilesetInfo:function (tilesetInfo, layerInfo, mapInfo) {
  314. // XXX: is 35% a good estimate ?
  315. var size = layerInfo._layerSize;
  316. var totalNumberOfTiles = parseInt(size.width * size.height);
  317. var capacity = totalNumberOfTiles * 0.35 + 1; // 35 percent is occupied ?
  318. var texture;
  319. if (tilesetInfo)
  320. texture = cc.TextureCache.getInstance().addImage(tilesetInfo.sourceImage);
  321. if (this.initWithTexture(texture, capacity)) {
  322. // layerInfo
  323. this._layerName = layerInfo.name;
  324. this._layerSize = size;
  325. this._tiles = layerInfo._tiles;
  326. this._minGID = layerInfo._minGID;
  327. this._maxGID = layerInfo._maxGID;
  328. this._opacity = layerInfo._opacity;
  329. this.setProperties(layerInfo.getProperties());
  330. this._contentScaleFactor = cc.Director.getInstance().getContentScaleFactor();
  331. // tilesetInfo
  332. this._tileSet = tilesetInfo;
  333. // mapInfo
  334. this._mapTileSize = mapInfo.getTileSize();
  335. this._layerOrientation = mapInfo.getOrientation();
  336. // offset (after layer orientation is set);
  337. var offset = this._calculateLayerOffset(layerInfo.offset);
  338. this.setPosition(cc.POINT_PIXELS_TO_POINTS(offset));
  339. this._atlasIndexArray = [];
  340. this.setContentSize(cc.SIZE_PIXELS_TO_POINTS(cc.size(this._layerSize.width * this._mapTileSize.width,
  341. this._layerSize.height * this._mapTileSize.height)));
  342. this._useAutomaticVertexZ = false;
  343. this._vertexZvalue = 0;
  344. return true;
  345. }
  346. return false;
  347. },
  348. /**
  349. * <p>Dealloc the map that contains the tile position from memory. <br />
  350. * Unless you want to know at runtime the tiles positions, you can safely call this method. <br />
  351. * If you are going to call layer.getTileGIDAt() then, don't release the map</p>
  352. */
  353. releaseMap:function () {
  354. if (this._tiles)
  355. this._tiles = null;
  356. if (this._atlasIndexArray)
  357. this._atlasIndexArray = null;
  358. },
  359. /**
  360. * <p>Returns the tile (cc.Sprite) at a given a tile coordinate. <br/>
  361. * The returned cc.Sprite will be already added to the cc.TMXLayer. Don't add it again.<br/>
  362. * The cc.Sprite can be treated like any other cc.Sprite: rotated, scaled, translated, opacity, color, etc. <br/>
  363. * You can remove either by calling: <br/>
  364. * - layer.removeChild(sprite, cleanup); <br/>
  365. * - or layer.removeTileAt(ccp(x,y)); </p>
  366. * @param {cc.Point} pos
  367. * @return {cc.Sprite}
  368. */
  369. getTileAt: function (pos) {
  370. if(!pos)
  371. throw "cc.TMXLayer.getTileAt(): pos should be non-null";
  372. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  373. throw "cc.TMXLayer.getTileAt(): invalid position";
  374. if(!this._tiles || !this._atlasIndexArray){
  375. cc.log("cc.TMXLayer.getTileAt(): TMXLayer: the tiles map has been released");
  376. return null;
  377. }
  378. var tile = null;
  379. var gid = this.getTileGIDAt(pos);
  380. // if GID == 0, then no tile is present
  381. if (gid === 0)
  382. return tile;
  383. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  384. tile = this.getChildByTag(z);
  385. // tile not created yet. create it
  386. if (!tile) {
  387. var rect = this._tileSet.rectForGID(gid);
  388. rect = cc.RECT_PIXELS_TO_POINTS(rect);
  389. tile = new cc.Sprite();
  390. tile.initWithTexture(this.getTexture(), rect);
  391. tile.setBatchNode(this);
  392. tile.setPosition(this.getPositionAt(pos));
  393. tile.setVertexZ(this._vertexZForPos(pos));
  394. tile.setAnchorPoint(0,0);
  395. tile.setOpacity(this._opacity);
  396. var indexForZ = this._atlasIndexForExistantZ(z);
  397. this.addSpriteWithoutQuad(tile, indexForZ, z);
  398. }
  399. return tile;
  400. },
  401. /**
  402. * Returns the tile gid at a given tile coordinate. <br />
  403. * if it returns 0, it means that the tile is empty. <br />
  404. * This method requires the the tile map has not been previously released (eg. don't call layer.releaseMap())<br />
  405. * @param {cc.Point} pos
  406. * @return {Number}
  407. */
  408. getTileGIDAt:function (pos) {
  409. if(!pos)
  410. throw "cc.TMXLayer.getTileGIDAt(): pos should be non-null";
  411. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  412. throw "cc.TMXLayer.getTileGIDAt(): invalid position";
  413. if(!this._tiles || !this._atlasIndexArray){
  414. cc.log("cc.TMXLayer.getTileGIDAt(): TMXLayer: the tiles map has been released");
  415. return null;
  416. }
  417. var idx = 0 | (pos.x + pos.y * this._layerSize.width);
  418. // Bits on the far end of the 32-bit global tile ID are used for tile flags
  419. var tile = this._tiles[idx];
  420. return (tile & cc.TMX_TILE_FLIPPED_MASK) >>> 0;
  421. },
  422. // XXX: deprecated
  423. // tileGIDAt:getTileGIDAt,
  424. /**
  425. * lipped tiles can be changed dynamically
  426. * @param {cc.Point} pos
  427. * @return {Number}
  428. */
  429. getTileFlagsAt:function (pos) {
  430. if(!pos)
  431. throw "cc.TMXLayer.getTileFlagsAt(): pos should be non-null";
  432. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  433. throw "cc.TMXLayer.getTileFlagsAt(): invalid position";
  434. if(!this._tiles || !this._atlasIndexArray){
  435. cc.log("cc.TMXLayer.getTileFlagsAt(): TMXLayer: the tiles map has been released");
  436. return null;
  437. }
  438. var idx = 0 | (pos.x + pos.y * this._layerSize.width);
  439. // Bits on the far end of the 32-bit global tile ID are used for tile flags
  440. var tile = this._tiles[idx];
  441. return (tile & cc.TMX_TILE_FLIPPED_ALL) >>> 0;
  442. },
  443. // XXX: deprecated
  444. // tileFlagAt:getTileFlagsAt,
  445. /**
  446. * <p>Sets the tile gid (gid = tile global id) at a given tile coordinate.<br />
  447. * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor . Tileset Mgr +1.<br />
  448. * If a tile is already placed at that position, then it will be removed.</p>
  449. * @param {Number} gid
  450. * @param {cc.Point} pos
  451. * @param {Number} flags
  452. */
  453. setTileGID:function (gid, pos, flags) {
  454. if(!pos)
  455. throw "cc.TMXLayer.setTileGID(): pos should be non-null";
  456. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  457. throw "cc.TMXLayer.setTileGID(): invalid position";
  458. if(!this._tiles || !this._atlasIndexArray){
  459. cc.log("cc.TMXLayer.setTileGID(): TMXLayer: the tiles map has been released");
  460. return null;
  461. }
  462. if(gid !== 0 && gid < this._tileSet.firstGid){
  463. cc.log( "cc.TMXLayer.setTileGID(): invalid gid:" + gid);
  464. return null;
  465. }
  466. flags = flags || 0;
  467. this._setNodeDirtyForCache();
  468. var currentFlags = this.getTileFlagsAt(pos);
  469. var currentGID = this.getTileGIDAt(pos);
  470. if (currentGID != gid || currentFlags != flags) {
  471. var gidAndFlags = (gid | flags) >>> 0;
  472. // setting gid=0 is equal to remove the tile
  473. if (gid === 0)
  474. this.removeTileAt(pos);
  475. else if (currentGID === 0) // empty tile. create a new one
  476. this._insertTileForGID(gidAndFlags, pos);
  477. else { // modifying an existing tile with a non-empty tile
  478. var z = pos.x + pos.y * this._layerSize.width;
  479. var sprite = this.getChildByTag(z);
  480. if (sprite) {
  481. var rect = this._tileSet.rectForGID(gid);
  482. rect = cc.RECT_PIXELS_TO_POINTS(rect);
  483. sprite.setTextureRect(rect, false, rect._size);
  484. if (flags != null)
  485. this._setupTileSprite(sprite, pos, gidAndFlags);
  486. this._tiles[z] = gidAndFlags;
  487. } else
  488. this._updateTileForGID(gidAndFlags, pos);
  489. }
  490. }
  491. },
  492. /**
  493. * Removes a tile at given tile coordinate
  494. * @param {cc.Point} pos
  495. */
  496. removeTileAt:function (pos) {
  497. if(!pos)
  498. throw "cc.TMXLayer.removeTileAt(): pos should be non-null";
  499. if(pos.x >= this._layerSize.width || pos.y >= this._layerSize.height || pos.x < 0 || pos.y < 0)
  500. throw "cc.TMXLayer.removeTileAt(): invalid position";
  501. if(!this._tiles || !this._atlasIndexArray){
  502. cc.log("cc.TMXLayer.removeTileAt(): TMXLayer: the tiles map has been released");
  503. return null;
  504. }
  505. var gid = this.getTileGIDAt(pos);
  506. if (gid !== 0) {
  507. if (cc.renderContextType === cc.CANVAS)
  508. this._setNodeDirtyForCache();
  509. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  510. var atlasIndex = this._atlasIndexForExistantZ(z);
  511. // remove tile from GID map
  512. this._tiles[z] = 0;
  513. // remove tile from atlas position array
  514. cc.ArrayRemoveObjectAtIndex(this._atlasIndexArray, atlasIndex);
  515. // remove it from sprites and/or texture atlas
  516. var sprite = this.getChildByTag(z);
  517. if (sprite)
  518. cc.SpriteBatchNode.prototype.removeChild.call(this, sprite, true); //this.removeChild(sprite, true);
  519. else {
  520. if(cc.renderContextType === cc.WEBGL)
  521. this._textureAtlas.removeQuadAtIndex(atlasIndex);
  522. // update possible children
  523. if (this._children) {
  524. var locChildren = this._children;
  525. for (var i = 0, len = locChildren.length; i < len; i++) {
  526. var child = locChildren[i];
  527. if (child) {
  528. var ai = child.getAtlasIndex();
  529. if (ai >= atlasIndex)
  530. child.setAtlasIndex(ai - 1);
  531. }
  532. }
  533. }
  534. }
  535. }
  536. },
  537. /**
  538. * Returns the position in pixels of a given tile coordinate
  539. * @param {cc.Point} pos
  540. * @return {cc.Point}
  541. */
  542. getPositionAt:function (pos) {
  543. var ret = cc.PointZero();
  544. switch (this._layerOrientation) {
  545. case cc.TMX_ORIENTATION_ORTHO:
  546. ret = this._positionForOrthoAt(pos);
  547. break;
  548. case cc.TMX_ORIENTATION_ISO:
  549. ret = this._positionForIsoAt(pos);
  550. break;
  551. case cc.TMX_ORIENTATION_HEX:
  552. ret = this._positionForHexAt(pos);
  553. break;
  554. }
  555. return cc.POINT_PIXELS_TO_POINTS(ret);
  556. },
  557. // XXX: Deprecated. For backward compatibility only
  558. // positionAt:getPositionAt,
  559. /**
  560. * Return the value for the specific property name
  561. * @param {String} propertyName
  562. * @return {*}
  563. */
  564. getProperty:function (propertyName) {
  565. return this._properties[propertyName];
  566. },
  567. /**
  568. * Creates the tiles
  569. */
  570. setupTiles:function () {
  571. // Optimization: quick hack that sets the image size on the tileset
  572. if (cc.renderContextType === cc.CANVAS) {
  573. this._tileSet.imageSize = this._originalTexture.getContentSizeInPixels();
  574. } else {
  575. this._tileSet.imageSize = this._textureAtlas.getTexture().getContentSizeInPixels();
  576. // By default all the tiles are aliased
  577. // pros:
  578. // - easier to render
  579. // cons:
  580. // - difficult to scale / rotate / etc.
  581. this._textureAtlas.getTexture().setAliasTexParameters();
  582. }
  583. // Parse cocos2d properties
  584. this._parseInternalProperties();
  585. if (cc.renderContextType === cc.CANVAS)
  586. this._setNodeDirtyForCache();
  587. var locLayerHeight = this._layerSize.height, locLayerWidth = this._layerSize.width;
  588. for (var y = 0; y < locLayerHeight; y++) {
  589. for (var x = 0; x < locLayerWidth; x++) {
  590. var pos = x + locLayerWidth * y;
  591. var gid = this._tiles[pos];
  592. // XXX: gid == 0 -. empty tile
  593. if (gid !== 0) {
  594. this._appendTileForGID(gid, cc.p(x, y));
  595. // Optimization: update min and max GID rendered by the layer
  596. this._minGID = Math.min(gid, this._minGID);
  597. this._maxGID = Math.max(gid, this._maxGID);
  598. }
  599. }
  600. }
  601. if (!((this._maxGID >= this._tileSet.firstGid) && (this._minGID >= this._tileSet.firstGid))) {
  602. cc.log("cocos2d:TMX: Only 1 tileset per layer is supported");
  603. }
  604. },
  605. /**
  606. * cc.TMXLayer doesn't support adding a cc.Sprite manually.
  607. * @warning addChild(child); is not supported on cc.TMXLayer. Instead of setTileGID.
  608. * @param {cc.Node} child
  609. * @param {number} zOrder
  610. * @param {number} tag
  611. */
  612. addChild:function (child, zOrder, tag) {
  613. cc.log("addChild: is not supported on cc.TMXLayer. Instead use setTileGID or tileAt.");
  614. },
  615. /**
  616. * Remove child
  617. * @param {cc.Sprite} sprite
  618. * @param {Boolean} cleanup
  619. */
  620. removeChild:function (sprite, cleanup) {
  621. // allows removing nil objects
  622. if (!sprite)
  623. return;
  624. if(this._children.indexOf(sprite) === -1){
  625. cc.log("cc.TMXLayer.removeChild(): Tile does not belong to TMXLayer");
  626. return;
  627. }
  628. if (cc.renderContextType === cc.CANVAS)
  629. this._setNodeDirtyForCache();
  630. var atlasIndex = sprite.getAtlasIndex(); //cc.ArrayGetIndexOfObject(this._children, sprite);
  631. var zz = this._atlasIndexArray[atlasIndex];
  632. this._tiles[zz] = 0;
  633. cc.ArrayRemoveObjectAtIndex(this._atlasIndexArray, atlasIndex);
  634. cc.SpriteBatchNode.prototype.removeChild.call(this, sprite, cleanup);
  635. },
  636. /**
  637. * @return {String}
  638. */
  639. getLayerName:function () {
  640. return this._layerName.toString();
  641. },
  642. /**
  643. * @param {String} layerName
  644. */
  645. setLayerName:function (layerName) {
  646. this._layerName = layerName;
  647. },
  648. _positionForIsoAt:function (pos) {
  649. return cc.p(this._mapTileSize.width / 2 * ( this._layerSize.width + pos.x - pos.y - 1),
  650. this._mapTileSize.height / 2 * (( this._layerSize.height * 2 - pos.x - pos.y) - 2));
  651. },
  652. _positionForOrthoAt:function (pos) {
  653. return cc.p(pos.x * this._mapTileSize.width,
  654. (this._layerSize.height - pos.y - 1) * this._mapTileSize.height);
  655. },
  656. _positionForHexAt:function (pos) {
  657. var diffY = (pos.x % 2 == 1) ? (-this._mapTileSize.height / 2) : 0;
  658. return cc.p(pos.x * this._mapTileSize.width * 3 / 4,
  659. (this._layerSize.height - pos.y - 1) * this._mapTileSize.height + diffY);
  660. },
  661. _calculateLayerOffset:function (pos) {
  662. var ret = cc.PointZero();
  663. switch (this._layerOrientation) {
  664. case cc.TMX_ORIENTATION_ORTHO:
  665. ret = cc.p(pos.x * this._mapTileSize.width, -pos.y * this._mapTileSize.height);
  666. break;
  667. case cc.TMX_ORIENTATION_ISO:
  668. ret = cc.p((this._mapTileSize.width / 2) * (pos.x - pos.y),
  669. (this._mapTileSize.height / 2 ) * (-pos.x - pos.y));
  670. break;
  671. case cc.TMX_ORIENTATION_HEX:
  672. if(pos.x !== 0 || pos.y !== 0)
  673. cc.log("offset for hexagonal map not implemented yet");
  674. break;
  675. }
  676. return ret;
  677. },
  678. _appendTileForGID:function (gid, pos) {
  679. var rect = this._tileSet.rectForGID(gid);
  680. rect = cc.RECT_PIXELS_TO_POINTS(rect);
  681. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  682. var tile = this._reusedTileWithRect(rect);
  683. this._setupTileSprite(tile, pos, gid);
  684. // optimization:
  685. // The difference between appendTileForGID and insertTileforGID is that append is faster, since
  686. // it appends the tile at the end of the texture atlas
  687. var indexForZ = this._atlasIndexArray.length;
  688. // don't add it using the "standard" way.
  689. this.insertQuadFromSprite(tile, indexForZ);
  690. // append should be after addQuadFromSprite since it modifies the quantity values
  691. this._atlasIndexArray = cc.ArrayAppendObjectToIndex(this._atlasIndexArray, z, indexForZ);
  692. return tile;
  693. },
  694. _insertTileForGID:function (gid, pos) {
  695. var rect = this._tileSet.rectForGID(gid);
  696. rect = cc.RECT_PIXELS_TO_POINTS(rect);
  697. var z = 0 | (pos.x + pos.y * this._layerSize.width);
  698. var tile = this._reusedTileWithRect(rect);
  699. this._setupTileSprite(tile, pos, gid);
  700. // get atlas index
  701. var indexForZ = this._atlasIndexForNewZ(z);
  702. // Optimization: add the quad without adding a child
  703. this.insertQuadFromSprite(tile, indexForZ);
  704. // insert it into the local atlasindex array
  705. this._atlasIndexArray = cc.ArrayAppendObjectToIndex(this._atlasIndexArray, z, indexForZ);
  706. // update possible children
  707. if (this._children) {
  708. var locChildren = this._children;
  709. for (var i = 0, len = locChildren.length; i < len; i++) {
  710. var child = locChildren[i];
  711. if (child) {
  712. var ai = child.getAtlasIndex();
  713. if (ai >= indexForZ)
  714. child.setAtlasIndex(ai + 1);
  715. }
  716. }
  717. }
  718. this._tiles[z] = gid;
  719. return tile;
  720. },
  721. _updateTileForGID:function (gid, pos) {
  722. var rect = this._tileSet.rectForGID(gid);
  723. var locScaleFactor = this._contentScaleFactor;
  724. rect = cc.rect(rect.x / locScaleFactor, rect.y / locScaleFactor,
  725. rect.width / locScaleFactor, rect.height / locScaleFactor);
  726. var z = pos.x + pos.y * this._layerSize.width;
  727. var tile = this._reusedTileWithRect(rect);
  728. this._setupTileSprite(tile, pos, gid);
  729. // get atlas index
  730. var indexForZ = this._atlasIndexForExistantZ(z);
  731. tile.setAtlasIndex(indexForZ);
  732. tile.setDirty(true);
  733. tile.updateTransform();
  734. this._tiles[z] = gid;
  735. return tile;
  736. },
  737. //The layer recognizes some special properties, like cc_vertez
  738. _parseInternalProperties:function () {
  739. // if cc_vertex=automatic, then tiles will be rendered using vertexz
  740. var vertexz = this.getProperty("cc_vertexz");
  741. if (vertexz) {
  742. if (vertexz == "automatic") {
  743. this._useAutomaticVertexZ = true;
  744. var alphaFuncVal = this.getProperty("cc_alpha_func");
  745. var alphaFuncValue = 0;
  746. if (alphaFuncVal)
  747. alphaFuncValue = parseFloat(alphaFuncVal);
  748. if (cc.renderContextType === cc.WEBGL) {
  749. this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURECOLORALPHATEST));
  750. var alphaValueLocation = cc.renderContext.getUniformLocation(this.getShaderProgram().getProgram(), cc.UNIFORM_ALPHA_TEST_VALUE_S);
  751. // NOTE: alpha test shader is hard-coded to use the equivalent of a glAlphaFunc(GL_GREATER) comparison
  752. this.getShaderProgram().use();
  753. this.getShaderProgram().setUniformLocationWith1f(alphaValueLocation, alphaFuncValue);
  754. }
  755. } else
  756. this._vertexZvalue = parseInt(vertexz, 10);
  757. }
  758. },
  759. _setupTileSprite:function (sprite, pos, gid) {
  760. var z = pos.x + pos.y * this._layerSize.width;
  761. sprite.setPosition(this.getPositionAt(pos));
  762. if (cc.renderContextType === cc.WEBGL)
  763. sprite.setVertexZ(this._vertexZForPos(pos));
  764. else
  765. sprite.setTag(z);
  766. sprite.setAnchorPoint(0,0);
  767. sprite.setOpacity(this._opacity);
  768. if (cc.renderContextType === cc.WEBGL) {
  769. sprite.setRotation(0.0);
  770. }
  771. sprite.setFlippedX(false);
  772. sprite.setFlippedY(false);
  773. // Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles.
  774. if ((gid & cc.TMX_TILE_DIAGONAL_FLAG) >>> 0) {
  775. // put the anchor in the middle for ease of rotation.
  776. sprite.setAnchorPoint(0.5, 0.5);
  777. sprite.setPosition(this.getPositionAt(pos).x + sprite.getContentSize().height / 2,
  778. this.getPositionAt(pos).y + sprite.getContentSize().width / 2);
  779. var flag = (gid & (cc.TMX_TILE_HORIZONTAL_FLAG | cc.TMX_TILE_VERTICAL_FLAG) >>> 0) >>> 0;
  780. // handle the 4 diagonally flipped states.
  781. if (flag == cc.TMX_TILE_HORIZONTAL_FLAG)
  782. sprite.setRotation(90);
  783. else if (flag == cc.TMX_TILE_VERTICAL_FLAG)
  784. sprite.setRotation(270);
  785. else if (flag == (cc.TMX_TILE_VERTICAL_FLAG | cc.TMX_TILE_HORIZONTAL_FLAG) >>> 0) {
  786. sprite.setRotation(90);
  787. sprite.setFlippedX(true);
  788. } else {
  789. sprite.setRotation(270);
  790. sprite.setFlippedX(true);
  791. }
  792. } else {
  793. if ((gid & cc.TMX_TILE_HORIZONTAL_FLAG) >>> 0)
  794. sprite.setFlippedX(true);
  795. if ((gid & cc.TMX_TILE_VERTICAL_FLAG) >>> 0)
  796. sprite.setFlippedY(true);
  797. }
  798. },
  799. _reusedTileWithRect:function (rect) {
  800. if(cc.renderContextType === cc.WEBGL){
  801. if (!this._reusedTile) {
  802. this._reusedTile = new cc.Sprite();
  803. this._reusedTile.initWithTexture(this.getTexture(), rect, false);
  804. this._reusedTile.setBatchNode(this);
  805. } else {
  806. // XXX HACK: Needed because if "batch node" is nil,
  807. // then the Sprite'squad will be reset
  808. this._reusedTile.setBatchNode(null);
  809. // Re-init the sprite
  810. this._reusedTile.setTextureRect(rect, false, rect._size);
  811. // restore the batch node
  812. this._reusedTile.setBatchNode(this);
  813. }
  814. } else {
  815. this._reusedTile = new cc.Sprite();
  816. this._reusedTile.initWithTexture(this._textureForCanvas, rect, false);
  817. this._reusedTile.setBatchNode(this);
  818. this._reusedTile.setParent(this);
  819. }
  820. return this._reusedTile;
  821. },
  822. _vertexZForPos:function (pos) {
  823. var ret = 0;
  824. var maxVal = 0;
  825. if (this._useAutomaticVertexZ) {
  826. switch (this._layerOrientation) {
  827. case cc.TMX_ORIENTATION_ISO:
  828. maxVal = this._layerSize.width + this._layerSize.height;
  829. ret = -(maxVal - (pos.x + pos.y));
  830. break;
  831. case cc.TMX_ORIENTATION_ORTHO:
  832. ret = -(this._layerSize.height - pos.y);
  833. break;
  834. case cc.TMX_ORIENTATION_HEX:
  835. cc.log("TMX Hexa zOrder not supported");
  836. break;
  837. default:
  838. cc.log("TMX invalid value");
  839. break;
  840. }
  841. } else
  842. ret = this._vertexZvalue;
  843. return ret;
  844. },
  845. _atlasIndexForExistantZ:function (z) {
  846. var item;
  847. if (this._atlasIndexArray) {
  848. var locAtlasIndexArray = this._atlasIndexArray;
  849. for (var i = 0, len = locAtlasIndexArray.length; i < len; i++) {
  850. item = locAtlasIndexArray[i];
  851. if (item == z)
  852. break;
  853. }
  854. }
  855. if(!item)
  856. cc.log("cc.TMXLayer._atlasIndexForExistantZ(): TMX atlas index not found. Shall not happen");
  857. return i;
  858. },
  859. _atlasIndexForNewZ:function (z) {
  860. var locAtlasIndexArray = this._atlasIndexArray;
  861. for (var i = 0, len = locAtlasIndexArray.length; i < len; i++) {
  862. var val = locAtlasIndexArray[i];
  863. if (z < val)
  864. break;
  865. }
  866. return i;
  867. }
  868. });
  869. if(cc.Browser.supportWebGL){
  870. cc.TMXLayer.prototype.draw = cc.SpriteBatchNode.prototype.draw;
  871. cc.TMXLayer.prototype.visit = cc.SpriteBatchNode.prototype.visit;
  872. }else{
  873. cc.TMXLayer.prototype.draw = cc.TMXLayer.prototype._drawForCanvas;
  874. cc.TMXLayer.prototype.visit = cc.TMXLayer.prototype._visitForCanvas;
  875. }
  876. /**
  877. * Creates a cc.TMXLayer with an tile set info, a layer info and a map info
  878. * @param {cc.TMXTilesetInfo} tilesetInfo
  879. * @param {cc.TMXLayerInfo} layerInfo
  880. * @param {cc.TMXMapInfo} mapInfo
  881. * @return {cc.TMXLayer|Null}
  882. */
  883. cc.TMXLayer.create = function (tilesetInfo, layerInfo, mapInfo) {
  884. var ret = new cc.TMXLayer();
  885. if (ret.initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo))
  886. return ret;
  887. return null;
  888. };