Skip to content

Commit 6ef7033

Browse files
committed
Added in parent matrix support and refactored the drawing
1 parent 31421ee commit 6ef7033

1 file changed

Lines changed: 41 additions & 19 deletions

File tree

src/tilemaps/staticlayer/StaticTilemapLayerCanvasRenderer.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,54 @@
1717
* @param {Phaser.Tilemaps.StaticTilemapLayer} src - The Game Object being rendered in this call.
1818
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
1919
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
20+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2021
*/
21-
var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
22+
var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2223
{
2324
src.cull(camera);
2425

2526
var renderTiles = src.culledTiles;
26-
var tileset = this.tileset;
27-
var ctx = renderer.currentContext;
2827
var tileCount = renderTiles.length;
2928

30-
var image = tileset.image.getSourceImage();
29+
if (tileCount === 0)
30+
{
31+
return;
32+
}
3133

3234
var camMatrix = renderer._tempMatrix1;
3335
var layerMatrix = renderer._tempMatrix2;
3436
var calcMatrix = renderer._tempMatrix3;
3537

3638
layerMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY);
3739

38-
ctx.save();
39-
4040
camMatrix.copyFrom(camera.matrix);
4141

42-
layerMatrix.e -= camera.scrollX * src.scrollFactorX;
43-
layerMatrix.f -= camera.scrollY * src.scrollFactorY;
42+
if (parentMatrix)
43+
{
44+
// Multiply the camera by the parent matrix
45+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
46+
47+
// Undo the camera scroll
48+
layerMatrix.e = src.x;
49+
layerMatrix.f = src.y;
50+
51+
// Multiply by the Sprite matrix, store result in calcMatrix
52+
camMatrix.multiply(layerMatrix, calcMatrix);
53+
}
54+
else
55+
{
56+
layerMatrix.e -= camera.scrollX * src.scrollFactorX;
57+
layerMatrix.f -= camera.scrollY * src.scrollFactorY;
4458

45-
// Multiply by the Sprite matrix, store result in calcMatrix
46-
camMatrix.multiply(layerMatrix, calcMatrix);
59+
// Multiply by the Sprite matrix, store result in calcMatrix
60+
camMatrix.multiply(layerMatrix, calcMatrix);
61+
}
62+
63+
var tileset = src.tileset;
64+
var ctx = renderer.currentContext;
65+
var image = tileset.image.getSourceImage();
66+
67+
ctx.save();
4768

4869
calcMatrix.copyToContext(ctx);
4970

@@ -55,15 +76,16 @@ var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPer
5576

5677
var tileTexCoords = tileset.getTileTextureCoordinates(tile.index);
5778

58-
if (tileTexCoords === null) { continue; }
59-
60-
ctx.drawImage(
61-
image,
62-
tileTexCoords.x, tileTexCoords.y,
63-
tile.width, tile.height,
64-
tile.pixelX, tile.pixelY,
65-
tile.width, tile.height
66-
);
79+
if (tileTexCoords)
80+
{
81+
ctx.drawImage(
82+
image,
83+
tileTexCoords.x, tileTexCoords.y,
84+
tile.width, tile.height,
85+
tile.pixelX, tile.pixelY,
86+
tile.width, tile.height
87+
);
88+
}
6789
}
6890

6991
ctx.restore();

0 commit comments

Comments
 (0)