Skip to content

Commit c0e5197

Browse files
committed
Static Tilemap Layers now support tile rotation and flipping. Previously this was a feature only for Dynamic Tilemap Layers, but now both have it. Close phaserjs#4037
1 parent 94d21da commit c0e5197

3 files changed

Lines changed: 75 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* `debugShowConvexHulls` is a new Matter Physics debug option that allows you to control if the convex hull of a body is drawn to the debug Graphic. The default is `false`.
1111
* `debugConvexHullColor` is a new Matter Physics debug option that lets you set the color of the convex hull, if being drawn to the debug Graphic.
1212
* `debugShowSleeping` is a new Matter Physics debug option that lets you draw sleeping bodies at 50% opacity.
13+
* Static Tilemap Layers now support tile rotation and flipping. Previously this was a feature only for Dynamic Tilemap Layers, but now both have it. Close #4037 (thanks @thisredone)
1314

1415
### Updates
1516

src/tilemaps/staticlayer/StaticTilemapLayer.js

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var CONST = require('../../const');
1010
var GameObject = require('../../gameobjects/GameObject');
1111
var StaticTilemapLayerRender = require('./StaticTilemapLayerRender');
1212
var TilemapComponents = require('../components');
13+
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
1314
var Utils = require('../../renderer/webgl/Utils');
1415

1516
/**
@@ -301,6 +302,16 @@ var StaticTilemapLayer = new Class({
301302
*/
302303
this._renderOrder = 0;
303304

305+
/**
306+
* A temporary Transform Matrix, re-used internally during batching.
307+
*
308+
* @name Phaser.Tilemaps.StaticTilemapLayer#_tempMatrix
309+
* @private
310+
* @type {Phaser.GameObjects.Components.TransformMatrix}
311+
* @since 3.14.0
312+
*/
313+
this._tempMatrix = new TransformMatrix();
314+
304315
this.setAlpha(this.layer.alpha);
305316
this.setPosition(x, y);
306317
this.setOrigin();
@@ -527,11 +538,6 @@ var StaticTilemapLayer = new Class({
527538
*/
528539
batchTile: function (vOffset, tile, tileset, width, height, camera)
529540
{
530-
var tx = tile.pixelX;
531-
var ty = tile.pixelY;
532-
var txw = tx + tile.width;
533-
var tyh = ty + tile.height;
534-
535541
var texCoords = tileset.getTileTextureCoordinates(tile.index);
536542

537543
if (!texCoords)
@@ -544,16 +550,47 @@ var StaticTilemapLayer = new Class({
544550
var u1 = (texCoords.x + tile.width) / width;
545551
var v1 = (texCoords.y + tile.height) / height;
546552

553+
var matrix = this._tempMatrix;
554+
555+
var tileWidth = tile.width;
556+
var tileHeight = tile.height;
557+
558+
var halfTileWidth = tileWidth / 2;
559+
var halfTileHeight = tileHeight / 2;
560+
561+
var x = -halfTileWidth;
562+
var y = -halfTileHeight;
563+
564+
if (tile.flipX)
565+
{
566+
tileWidth *= -1;
567+
x += tile.width;
568+
}
569+
570+
if (tile.flipY)
571+
{
572+
tileHeight *= -1;
573+
y += tile.height;
574+
}
575+
576+
var xw = x + tileWidth;
577+
var yh = y + tileHeight;
578+
579+
matrix.applyITRS(halfTileWidth + tile.pixelX, halfTileHeight + tile.pixelY, tile.rotation, 1, 1);
580+
547581
var tint = Utils.getTintAppendFloatAlpha(0xffffff, camera.alpha * this.alpha * tile.alpha);
548582

549-
var tx0 = tx;
550-
var ty0 = ty;
551-
var tx1 = tx;
552-
var ty1 = tyh;
553-
var tx2 = txw;
554-
var ty2 = tyh;
555-
var tx3 = txw;
556-
var ty3 = ty;
583+
var tx0 = matrix.getX(x, y);
584+
var ty0 = matrix.getY(x, y);
585+
586+
var tx1 = matrix.getX(x, yh);
587+
var ty1 = matrix.getY(x, yh);
588+
589+
var tx2 = matrix.getX(xw, yh);
590+
var ty2 = matrix.getY(xw, yh);
591+
592+
var tx3 = matrix.getX(xw, y);
593+
var ty3 = matrix.getY(xw, y);
557594

558595
if (camera.roundPixels)
559596
{

src/tilemaps/staticlayer/StaticTilemapLayerCanvasRenderer.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPer
6868

6969
calcMatrix.copyToContext(ctx);
7070

71+
var alpha = camera.alpha * src.alpha;
72+
7173
ctx.globalAlpha = camera.alpha * src.alpha;
7274

7375
for (var i = 0; i < tileCount; i++)
@@ -78,13 +80,34 @@ var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPer
7880

7981
if (tileTexCoords)
8082
{
83+
var halfWidth = tile.width / 2;
84+
var halfHeight = tile.height / 2;
85+
86+
ctx.save();
87+
88+
ctx.translate(tile.pixelX + halfWidth, tile.pixelY + halfHeight);
89+
90+
if (tile.rotation !== 0)
91+
{
92+
ctx.rotate(tile.rotation);
93+
}
94+
95+
if (tile.flipX || tile.flipY)
96+
{
97+
ctx.scale((tile.flipX) ? -1 : 1, (tile.flipY) ? -1 : 1);
98+
}
99+
100+
ctx.globalAlpha = alpha * tile.alpha;
101+
81102
ctx.drawImage(
82103
image,
83104
tileTexCoords.x, tileTexCoords.y,
84105
tile.width, tile.height,
85-
tile.pixelX, tile.pixelY,
106+
-halfWidth, -halfHeight,
86107
tile.width, tile.height
87108
);
109+
110+
ctx.restore();
88111
}
89112
}
90113

0 commit comments

Comments
 (0)