Skip to content

Commit 0d38e23

Browse files
committed
Both Static and Dynamic Tilemaps support the new property skipIndexZero which allows them to skip over index 0 tiles. Works in both canvas and webgl. Fix phaserjs#3052
1 parent 954e7ef commit 0d38e23

7 files changed

Lines changed: 136 additions & 71 deletions

File tree

v3/src/gameobjects/tilemap/dynamic/Tile.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,47 @@ function Tile (properties)
44
this.id = properties.id;
55
this.x = properties.x;
66
this.y = properties.y;
7+
78
this.width = properties.width;
89
this.height = properties.height;
10+
911
this.frameX = properties.frameX;
1012
this.frameY = properties.frameY;
1113
this.frameWidth = properties.frameWidth;
1214
this.frameHeight = properties.frameHeight;
15+
1316
this.alpha = 1.0;
1417
this.tint = 0xFFFFFF;
1518
this.visible = true;
19+
1620
this.textureWidth = properties.textureWidth;
1721
this.textureHeight = properties.textureHeight;
22+
1823
this.border = properties.border;
19-
this.center = properties.center;
24+
this.center = properties.center;
2025
}
2126

2227
Tile.prototype.setId = function (id)
2328
{
2429
var tileId = this.id = id;
30+
2531
var tileWidth = this.width;
2632
var tileHeight = this.height;
33+
2734
var setWidth = this.textureWidth / tileWidth;
35+
2836
var tileWidthBorder = (tileWidth + this.border * 2);
2937
var tileHeightBorder = (tileHeight + this.border * 2);
38+
3039
var halfTileWidth = tileWidthBorder * 0.5;
3140
var halfTileHeight = tileHeightBorder * 0.5;
41+
3242
if (!this.center)
3343
{
3444
halfTileWidth = 0;
3545
halfTileHeight = 0;
3646
}
47+
3748
var rectx = (((tileId % setWidth)|0) * tileWidthBorder) + halfTileWidth;
3849
var recty = (((tileId / setWidth)|0) * tileHeightBorder) + halfTileHeight;
3950

v3/src/gameobjects/tilemap/dynamic/Tilemap.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,24 @@ var Tilemap = new Class({
3131
GameObject.call(this, scene, 'Tilemap');
3232

3333
this.mapData = (mapData !== null) ? new Uint32Array(mapData) : new Uint32Array(mapWidth * mapHeight);
34-
this.tileWidth = tileWidth;
35-
this.tileHeight = tileHeight;
3634
this.mapWidth = mapWidth;
3735
this.mapHeight = mapHeight;
36+
37+
this.tileWidth = tileWidth;
38+
this.tileHeight = tileHeight;
39+
3840
this.tileArray = [];
3941
this.culledTiles = [];
4042
this.tileBorder = tileBorder;
43+
4144
this.setTexture(texture, frame);
4245
this.setPosition(x, y);
4346
this.setSizeToFrame();
4447
this.setOrigin();
4548
this.setSize(tileWidth * mapWidth, tileHeight * mapHeight);
49+
50+
this.skipIndexZero = false;
51+
4652
this.buildTilemap(!!scene.sys.game.renderer.gl);
4753
},
4854

@@ -61,15 +67,19 @@ var Tilemap = new Class({
6167
var tileArray = this.tileArray;
6268
var mapData = this.mapData;
6369
var border = this.tileBorder;
70+
6471
var tileWidth = this.tileWidth;
6572
var tileHeight = this.tileHeight;
6673
var tileWidthBorder = tileWidth + border * 2;
6774
var tileHeightBorder = tileHeight + border * 2;
75+
6876
var width = this.texture.source[0].width;
6977
var height = this.texture.source[0].height;
78+
7079
var mapWidth = this.mapWidth;
7180
var mapHeight = this.mapHeight;
7281
var setWidth = width / tileWidth;
82+
7383
var tileWidthBorderHalf = tileWidthBorder * 0.5;
7484
var tileHeightBorderHalf = tileHeightBorder * 0.5;
7585

@@ -157,7 +167,7 @@ var Tilemap = new Class({
157167
// frameX
158168
// frameY
159169
// id
160-
// index = the tile in the tilset to render
170+
// index = the tile in the tileset to render
161171
// textureWidth = tileset texture size
162172
// textureHeight
163173
// tint

v3/src/gameobjects/tilemap/dynamic/TilemapCanvasRenderer.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ var TilemapCanvasRenderer = function (renderer, gameObject, interpolationPercent
1212
var tiles = gameObject.culledTiles;
1313
var tileCount = tiles.length;
1414
var image = gameObject.frame.source.image;
15-
var scrollFactorX = gameObject.scrollFactorX;
16-
var scrollFactorY = gameObject.scrollFactorY;
17-
var alpha = gameObject.alpha;
15+
16+
// var scrollFactorX = gameObject.scrollFactorX;
17+
// var scrollFactorY = gameObject.scrollFactorY;
18+
// var alpha = gameObject.alpha;
19+
1820
var tx = gameObject.x - camera.scrollX * gameObject.scrollFactorX;
1921
var ty = gameObject.y - camera.scrollY * gameObject.scrollFactorY;
2022
var ctx = renderer.gameContext;
@@ -29,11 +31,16 @@ var TilemapCanvasRenderer = function (renderer, gameObject, interpolationPercent
2931
{
3032
var tile = tiles[index];
3133

34+
if (tile.id <= 0 && gameObject.skipIndexZero)
35+
{
36+
continue;
37+
}
38+
3239
ctx.drawImage(
33-
image,
34-
tile.frameX, tile.frameY,
35-
tile.frameWidth, tile.frameHeight,
36-
tile.x, tile.y,
40+
image,
41+
tile.frameX, tile.frameY,
42+
tile.frameWidth, tile.frameHeight,
43+
tile.x, tile.y,
3744
tile.frameWidth, tile.frameHeight
3845
);
3946
}

v3/src/gameobjects/tilemap/dynamic/TilemapWebGLRenderer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ var TilemapWebGLRenderer = function (renderer, gameObject, interpolationPercenta
2525
for (var index = 0; index < length; ++index)
2626
{
2727
var tile = renderTiles[index];
28+
29+
if (tile.id <= 0 && gameObject.skipIndexZero)
30+
{
31+
continue;
32+
}
33+
2834
batch.addTileTextureRect(
2935
texture,
3036
x + tile.x, y + tile.y, tile.width, tile.height, alpha * tile.alpha, tile.tint,

0 commit comments

Comments
 (0)