11
2-
3- PIXI . Tilemap = function ( texture , map )
2+ /**
3+ * Tilemap - constructor
4+ *
5+ * @param {Array } layer - layer data from the map, arranged in mapheight lists of mapwidth Phaser.Tile objects (2d array)
6+ *
7+ */
8+ PIXI . Tilemap = function ( texture , mapwidth , mapheight , tilewidth , tileheight , layer )
49{
510 PIXI . DisplayObjectContainer . call ( this ) ;
611
@@ -12,17 +17,11 @@ PIXI.Tilemap = function(texture, map)
1217 */
1318 this . texture = texture ;
1419
15- /**
16- * The tilemap object
17- *
18- * @property map
19- * @type Object
20- */
21- this . map = map ;
22-
2320 // faster access to the tile dimensions
24- this . tileWide = this . map . tilewidth ;
25- this . tileHigh = this . map . tileheight ;
21+ this . tileWide = tilewidth ;
22+ this . tileHigh = tileheight ;
23+ this . mapWide = mapwidth ;
24+ this . mapHigh = mapheight ;
2625
2726 // precalculate the width of the source texture in entire tile units
2827 this . texTilesWide = Math . ceil ( this . texture . width / this . tileWide ) ;
@@ -33,8 +32,10 @@ PIXI.Tilemap = function(texture, map)
3332 this . sy = this . tileHigh / this . texture . height ;
3433
3534 // TODO: switch here to create DisplayObjectContainer at correct size for the render mode
36- this . width = this . map . width * this . tileWide ;
37- this . height = this . map . height * this . tileHigh ;
35+ this . width = this . mapWide * this . tileWide ;
36+ this . height = this . mapHigh * this . tileHigh ;
37+
38+ this . layer = layer ;
3839
3940 /**
4041 * Remember last tile drawn to avoid unnecessary set-up
@@ -216,33 +217,26 @@ PIXI.Tilemap.prototype._renderWholeTilemap = function(renderSession)
216217 // bind the source buffer
217218 gl . bindBuffer ( gl . ARRAY_BUFFER , this . positionBuffer ) ;
218219
219- // draw all map layers
220- for ( var l = 0 ; l < this . map . layers . length ; l ++ )
221- {
222- // draw an entire map layer
223- this . _renderLayer ( l , renderSession ) ;
224- }
220+ // draw the entire map layer
221+ this . _renderLayer ( this . layer , renderSession ) ;
225222} ;
226223
227224
228- PIXI . Tilemap . prototype . _renderLayer = function ( _which , renderSession )
225+ PIXI . Tilemap . prototype . _renderLayer = function ( _layer , renderSession )
229226{
230227 var gl = renderSession . gl ;
231228 var shader = renderSession . shaderManager . tilemapShader ;
232229
233- var layer = this . map . layers [ _which ] ;
234- if ( layer )
230+ var wide = _layer . width , high = _layer . height ;
231+ for ( var y = 0 ; y < high ; y ++ )
235232 {
236- var wide = layer . width , high = layer . height ;
237- for ( var y = 0 ; y < high ; y ++ )
233+ var layerRow = _layer . data [ y ] ;
234+ for ( var x = 0 ; x < wide ; x ++ )
238235 {
239- for ( var x = 0 ; x < wide ; x ++ )
240- {
241- this . _renderTile ( gl , shader , x * this . tileWide , y * this . tileHigh , layer . data [ x + y * wide ] - 1 ) ;
242- }
236+ this . _renderTile ( gl , shader , x * this . tileWide , y * this . tileHigh , layerRow [ x ] . index - 1 ) ;
243237 }
244-
245238 }
239+
246240} ;
247241
248242
@@ -326,9 +320,9 @@ PIXI.Tilemap.prototype.getBounds = function(matrix)
326320
327321 var vertices = [
328322 0 , 0 ,
329- this . map . width * this . tileWide , 0 ,
330- this . map . width * this . tileWide , this . map . height * this . tileHigh ,
331- 0 , this . map . height * this . tileHigh
323+ this . mapWide * this . tileWide , 0 ,
324+ this . mapWide * this . tileWide , this . mapHigh * this . tileHigh ,
325+ 0 , this . mapHigh * this . tileHigh
332326 ] ;
333327 for ( var i = 0 , n = vertices . length ; i < n ; i += 2 )
334328 {
0 commit comments