Skip to content

Commit b66a0aa

Browse files
committed
Merge pull request phaserjs#1605 from mickez/tilemaplayer_scale
Added scaling capability to TilemapLayer
2 parents 35a04b2 + bb2e263 commit b66a0aa

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

src/tilemap/TilemapLayer.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ Phaser.TilemapLayer.prototype.postUpdate = function () {
330330

331331
// Stops you being able to auto-scroll the camera if it's not following a sprite
332332
var camera = this.game.camera;
333-
this.scrollX = camera.x * this.scrollFactorX;
334-
this.scrollY = camera.y * this.scrollFactorY;
333+
this.scrollX = camera.x * this.scrollFactorX / this.scale.x;
334+
this.scrollY = camera.y * this.scrollFactorY / this.scale.y;
335335

336336
this.render();
337337

@@ -358,7 +358,7 @@ Phaser.TilemapLayer.prototype.postUpdate = function () {
358358
*/
359359
Phaser.TilemapLayer.prototype.resizeWorld = function () {
360360

361-
this.game.world.setBounds(0, 0, this.layer.widthInPixels, this.layer.heightInPixels);
361+
this.game.world.setBounds(0, 0, this.layer.widthInPixels * this.scale.x, this.layer.heightInPixels * this.scale.y);
362362

363363
};
364364

@@ -570,11 +570,11 @@ Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides
570570
y = this._fixY(y);
571571

572572
// Convert the pixel values into tile coordinates
573-
var tx = Math.floor(x / this._mc.cw);
574-
var ty = Math.floor(y / this._mc.ch);
573+
var tx = Math.floor(x / (this._mc.cw * this.scale.x));
574+
var ty = Math.floor(y / (this._mc.ch * this.scale.y));
575575
// Don't just use ceil(width/cw) to allow account for x/y diff within cell
576-
var tw = Math.ceil((x + width) / this._mc.cw) - tx;
577-
var th = Math.ceil((y + height) / this._mc.ch) - ty;
576+
var tw = Math.ceil((x + width) / (this._mc.cw * this.scale.x)) - tx;
577+
var th = Math.ceil((y + height) / (this._mc.ch * this.scale.y)) - ty;
578578

579579
while (this._results.length)
580580
{
@@ -674,6 +674,36 @@ Phaser.TilemapLayer.prototype.resetTilesetCache = function ()
674674

675675
};
676676

677+
/**
678+
* This method will set the scale of the tilemap as well as update the underlying block data of this layer
679+
*
680+
* @method Phaser.TilemapLayer#setScale
681+
* @param {number} [xScale=1] - The scale factor along the X-plane
682+
* @param {number} [yScale] - The scale factor along the Y-plane
683+
*/
684+
Phaser.TilemapLayer.prototype.setScale = function(xScale, yScale) {
685+
xScale = xScale || 1;
686+
yScale = yScale || xScale;
687+
688+
for (var y = 0; y < this.layer.data.length; y++)
689+
{
690+
var row = this.layer.data[y];
691+
692+
for (var x = 0; x < row.length; x++)
693+
{
694+
var tile = row[x];
695+
696+
tile.width = this.map.tileWidth * xScale;
697+
tile.height = this.map.tileHeight * yScale;
698+
699+
tile.worldX = tile.x * tile.width;
700+
tile.worldY = tile.y * tile.height;
701+
}
702+
}
703+
704+
this.scale.setTo(xScale, yScale);
705+
};
706+
677707
/**
678708
* Shifts the contents of the canvas - does extra math so that different browsers agree on the result.
679709
*

0 commit comments

Comments
 (0)