Skip to content

Commit 759252a

Browse files
committed
Better handling of resizing a tilemap
Force the tilesets and tiles to inherit their tile size from the tilemap
1 parent e296048 commit 759252a

4 files changed

Lines changed: 54 additions & 18 deletions

File tree

v3/src/gameobjects/tilemap/Tile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ var Tile = new Class({
120120
this.collisionCallbackContext = context;
121121
},
122122

123+
setSize: function (tileWidth, tileHeight)
124+
{
125+
this.worldX = this.x * tileWidth;
126+
this.worldY = this.y * tileHeight;
127+
this.width = tileWidth;
128+
this.height = tileHeight;
129+
},
130+
123131
// True if this tile can collide on any of its faces or has a collision callback set.
124132
canCollide: {
125133
get: function ()

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ var Tilemap = new Class({
4444
if (key === undefined || key === null) { key = tilesetName; }
4545
if (tileWidth === undefined) { tileWidth = this.tileWidth; }
4646
if (tileHeight === undefined) { tileHeight = this.tileHeight; }
47-
if (tileMargin === undefined) { tileMargin = 0; }
48-
if (tileSpacing === undefined) { tileSpacing = 0; }
49-
if (gid === undefined) { gid = 0; }
50-
51-
// In-case we're working from a blank map
52-
if (tileWidth === 0) { tileWidth = 32; }
53-
if (tileHeight === 0) { tileHeight = 32; }
5447

5548
if (!this.scene.sys.textures.exists(key))
5649
{
@@ -72,20 +65,22 @@ var Tilemap = new Class({
7265

7366
if (this.tilesets[index])
7467
{
68+
this.tilesets[index].setTileSize(tileWidth, tileHeight);
69+
this.tilesets[index].setSpacing(tileMargin, tileSpacing);
7570
this.tilesets[index].setImage(texture);
7671
return this.tilesets[index];
7772
}
78-
else
79-
{
80-
var tileset = new Tileset(tilesetName, gid, tileWidth, tileHeight, tileMargin, tileSpacing, {});
81-
tileset.setImage(texture);
8273

83-
this.tilesets.push(tileset);
84-
85-
// TODO: add in GID & master list of tiles
86-
}
74+
if (tileMargin === undefined) { tileMargin = 0; }
75+
if (tileSpacing === undefined) { tileSpacing = 0; }
76+
if (gid === undefined) { gid = 0; }
8777

78+
var tileset = new Tileset(tilesetName, gid, tileWidth, tileHeight, tileMargin, tileSpacing, {});
79+
tileset.setImage(texture);
80+
this.tilesets.push(tileset);
8881
return tileset;
82+
83+
// TODO: add in GID & master list of tiles
8984
},
9085

9186
// Creates & selects
@@ -416,6 +411,28 @@ var Tilemap = new Class({
416411
this.tileHeight = tileHeight;
417412
this.widthInPixels = this.width * tileWidth;
418413
this.heightInPixels = this.height * tileHeight;
414+
415+
// Update all the layers & tiles
416+
for (var i = 0; i < this.layers.length; i++)
417+
{
418+
this.layers[i].tileWidth = tileWidth;
419+
this.layers[i].tileHeight = tileHeight;
420+
421+
var mapData = this.layers[i].data;
422+
var mapWidth = this.layers[i].width;
423+
var mapHeight = this.layers[i].height;
424+
425+
for (var row = 0; row < mapHeight; ++row)
426+
{
427+
for (var col = 0; col < mapWidth; ++col)
428+
{
429+
var tile = mapData[row][col];
430+
if (tile !== null) { tile.setSize(tileWidth, tileHeight); }
431+
}
432+
}
433+
}
434+
435+
return this;
419436
},
420437

421438
shuffle: function (tileX, tileY, width, height, layer)

v3/src/gameobjects/tilemap/Tileset.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,21 @@ var Tileset = new Class({
4545
return this.texCoordinates[tileIndex - this.firstgid];
4646
},
4747

48+
setTileSize: function (tileWidth, tileHeight)
49+
{
50+
if (tileWidth !== undefined) { this.tileWidth = tileWidth; }
51+
if (tileHeight !== undefined) { this.tileHeight = tileHeight; }
52+
53+
if (this.image)
54+
{
55+
this.updateTileData(this.image.source[0].width, this.image.source[0].height);
56+
}
57+
},
58+
4859
setSpacing: function (margin, spacing)
4960
{
50-
this.tileMargin = margin;
51-
this.tileSpacing = spacing;
61+
if (margin !== undefined) { this.tileMargin = margin; }
62+
if (spacing !== undefined) { this.tileSpacing = spacing; }
5263

5364
if (this.image)
5465
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var StaticTilemapLayer = new Class({
5353
this.setPosition(x, y);
5454
this.setSizeToFrame();
5555
this.setOrigin();
56-
this.setSize(this.map.tileWidth * this.layer.width, this.map.tileheight * this.layer.height);
56+
this.setSize(this.map.tileWidth * this.layer.width, this.map.tileHeight * this.layer.height);
5757

5858
this.skipIndexZero = false;
5959

0 commit comments

Comments
 (0)