Skip to content

Commit 69abe49

Browse files
committed
Modified Phaser.Tilemap and PIXI.Tilemap to support the new Phaser.TilemapLayerGL objects.
The PIXI file has a reverse dependency on Phaser for it's 'layer' parameter which contains a Phaser.Tilemap (which holds the map data). The other changes adapt it to the new data source (previously this expected a raw JSON object encoded like the Mario.json map is). The Phaser file now keeps a reference to the data parsed from the original map, and adds a branch in createLayer to build a TilemapLayerGL instead of a TilemapLayer.
1 parent 671fe4e commit 69abe49

2 files changed

Lines changed: 35 additions & 34 deletions

File tree

src/pixi/extras/Tilemap.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
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
{

src/tilemap/Tilemap.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Phaser.Tilemap = function (game, key, tileWidth, tileHeight, width, height) {
3535
this.key = key;
3636

3737
var data = Phaser.TilemapParser.parse(this.game, key, tileWidth, tileHeight, width, height);
38+
this.data = data;
3839

3940
if (data === null)
4041
{
@@ -561,9 +562,10 @@ Phaser.Tilemap.prototype = {
561562
* @param {number} [width] - The rendered width of the layer, should never be wider than Game.width. If not given it will be set to Game.width.
562563
* @param {number} [height] - The rendered height of the layer, should never be wider than Game.height. If not given it will be set to Game.height.
563564
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
565+
* @param {boolean} [pixiTest] - Temporary additional flag to enable tests of the PIXI.Tilemap renderer
564566
* @return {Phaser.TilemapLayer} The TilemapLayer object. This is an extension of Phaser.Sprite and can be moved around the display list accordingly.
565567
*/
566-
createLayer: function (layer, width, height, group) {
568+
createLayer: function (layer, width, height, group, pixiTest) {
567569

568570
// Add Buffer support for the left of the canvas
569571

@@ -584,6 +586,11 @@ Phaser.Tilemap.prototype = {
584586
return;
585587
}
586588

589+
if ( pixiTest )
590+
{
591+
return group.add(new Phaser.TilemapLayerGL(this.game, this, index, width, height));
592+
}
593+
587594
return group.add(new Phaser.TilemapLayer(this.game, this, index, width, height));
588595

589596
},

0 commit comments

Comments
 (0)