Skip to content

Commit e548b73

Browse files
committed
Proper support for multiple tile sizes in a tilemap
- Adds base tile size to Tile class. Tiled positions tiles based on the base size. - Supports proper translation of Tiled's bottom left coord system to Phaser's top left coord for tiles that are bigger or smaller than the base tile size. - Update static renderer to read position/size from tile
1 parent 2ae4bfa commit e548b73

2 files changed

Lines changed: 69 additions & 28 deletions

File tree

v3/src/gameobjects/tilemap/Tile.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ var Tile = new Class({
2525
* @param {integer} y - The y coordinate of this tile in tile coordinates.
2626
* @param {integer} width - Width of the tile in pixels.
2727
* @param {integer} height - Height of the tile in pixels.
28+
* @param {integer} baseWidth - The base width a tile in the map (in pixels). Tiled maps support
29+
* multiple tileset sizes within one map, but they are still placed at intervals of the base
30+
* tile width.
31+
* @param {integer} baseHeight - The base height of the tile in pixels (in pixels). Tiled maps
32+
* support multiple tileset sizes within one map, but they are still placed at intervals of the
33+
* base tile height.
2834
*/
29-
function Tile (layer, index, x, y, width, height)
35+
function Tile (layer, index, x, y, width, height, baseWidth, baseHeight)
3036
{
3137
/**
3238
* The LayerData in the Tilemap data that this tile belongs to.
@@ -53,20 +59,6 @@ var Tile = new Class({
5359
*/
5460
this.y = y;
5561

56-
/**
57-
* The world x coordinate of this tile in pixels. This does not factor in camera scroll,
58-
* layer scale or layer position.
59-
* @property {number} x
60-
*/
61-
this.worldX = x * width;
62-
63-
/**
64-
* The world y coordinate of this tile in pixels. This does not factor in camera scroll,
65-
* layer scale or layer position.
66-
* @property {number} y
67-
*/
68-
this.worldY = y * height;
69-
7062
/**
7163
* The width of the tile in pixels.
7264
* @property {integer} width
@@ -79,6 +71,37 @@ var Tile = new Class({
7971
*/
8072
this.height = height;
8173

74+
/**
75+
* The map's base width of a tile in pixels. Tiled maps support multiple tileset sizes
76+
* within one map, but they are still placed at intervals of the base tile size.
77+
* @property {integer} baseWidth
78+
*/
79+
this.baseWidth = (baseWidth !== undefined) ? baseWidth : width;
80+
81+
/**
82+
* The map's base height of a tile in pixels. Tiled maps support multiple tileset sizes
83+
* within one map, but they are still placed at intervals of the base tile size.
84+
* @property {integer} baseHeight
85+
*/
86+
this.baseHeight = (baseHeight !== undefined) ? baseHeight : height;
87+
88+
89+
/**
90+
* The world x coordinate of the top left of this tile in pixels. This does not factor in
91+
* camera scroll, layer scale or layer position.
92+
* @property {number} x
93+
*/
94+
this.worldX = 0;
95+
96+
/**
97+
* The world y coordinate of the top left of this tile in pixels. This does not factor in
98+
* camera scroll, layer scale or layer position.
99+
* @property {number} y
100+
*/
101+
this.worldY = 0;
102+
103+
this.updateWorldXY();
104+
82105
/**
83106
* Tile specific properties. These usually come from Tiled.
84107
* @property {object} properties
@@ -321,14 +344,34 @@ var Tile = new Class({
321344
*
322345
* @param {integer} tileWidth - The width of the tile in pixels.
323346
* @param {integer} tileHeight - The height of the tile in pixels.
347+
* @param {integer} baseWidth - The base width a tile in the map (in pixels).
348+
* @param {integer} baseHeight - The base height of the tile in pixels (in pixels).
349+
* @returns {this}
350+
*/
351+
setSize: function (tileWidth, tileHeight, baseWidth, baseHeight)
352+
{
353+
if (tileWidth !== undefined) { this.width = tileWidth; }
354+
if (tileHeight !== undefined) { this.height = tileHeight; }
355+
if (baseWidth !== undefined) { this.baseWidth = baseWidth; }
356+
if (baseHeight !== undefined) { this.baseHeight = baseHeight; }
357+
358+
this.updateWorldXY();
359+
360+
return this;
361+
},
362+
363+
/**
364+
* Used internally. Updates the tile's world XY position based on the current tile size.
365+
*
324366
* @returns {this}
325367
*/
326-
setSize: function (tileWidth, tileHeight)
368+
updateWorldXY: function ()
327369
{
328-
this.worldX = this.x * tileWidth;
329-
this.worldY = this.y * tileHeight;
330-
this.width = tileWidth;
331-
this.height = tileHeight;
370+
// Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile is the
371+
// bottom left, while the Phaser renderer assumes the origin is the top left. The y
372+
// coordinate needs to be adjusted by the difference.
373+
this.worldX = this.x * this.baseWidth;
374+
this.worldY = this.y * this.baseHeight - (this.height - this.baseHeight);
332375

333376
return this;
334377
},

v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ var StaticTilemapLayer = new Class({
125125
var tileset = this.tileset;
126126
var mapWidth = this.layer.width;
127127
var mapHeight = this.layer.height;
128-
var tileWidth = this.layer.tileWidth;
129-
var tileHeight = this.layer.tileHeight;
130128
var width = this.texture.source[0].width;
131129
var height = this.texture.source[0].height;
132130
var mapData = this.layer.data;
@@ -170,19 +168,19 @@ var StaticTilemapLayer = new Class({
170168
tile = mapData[row][col];
171169
if (tile === null || (tile.index <= 0 && this.skipIndexZero)) { continue; }
172170

173-
var tx = col * tileWidth;
174-
var ty = row * tileHeight;
175-
var txw = tx + tileWidth;
176-
var tyh = ty + tileHeight;
171+
var tx = tile.worldX;
172+
var ty = tile.worldY;
173+
var txw = tx + tile.width;
174+
var tyh = ty + tile.height;
177175

178176
texCoords = tileset.getTileTextureCoordinates(tile.index);
179177
if (texCoords === null) { continue; }
180178

181179
// Inset UV coordinates by 0.5px to prevent tile bleeding
182180
var u0 = (texCoords.x + 0.5) / width;
183181
var v0 = (texCoords.y + 0.5) / height;
184-
var u1 = (texCoords.x + tileWidth - 0.5) / width;
185-
var v1 = (texCoords.y + tileHeight - 0.5) / height;
182+
var u1 = (texCoords.x + tile.width - 0.5) / width;
183+
var v1 = (texCoords.y + tile.height - 0.5) / height;
186184

187185
var tx0 = tx;
188186
var ty0 = ty;

0 commit comments

Comments
 (0)