|
17 | 17 | * @param {Phaser.Tilemaps.DynamicTilemapLayer} src - The Game Object being rendered in this call. |
18 | 18 | * @param {number} interpolationPercentage - Reserved for future use and custom pipelines. |
19 | 19 | * @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 |
20 | 21 | */ |
21 | | -var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPercentage, camera) |
| 22 | +var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix) |
22 | 23 | { |
23 | 24 | src.cull(camera); |
24 | 25 |
|
25 | 26 | var renderTiles = src.culledTiles; |
26 | | - var length = renderTiles.length; |
27 | | - var image = src.tileset.image.getSourceImage(); |
28 | | - var tileset = this.tileset; |
| 27 | + var tileCount = renderTiles.length; |
29 | 28 |
|
30 | | - var tx = src.x - camera.scrollX * src.scrollFactorX; |
31 | | - var ty = src.y - camera.scrollY * src.scrollFactorY; |
32 | | - var ctx = renderer.currentContext; |
| 29 | + if (tileCount === 0) |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
33 | 33 |
|
34 | | - ctx.save(); |
35 | | - ctx.translate(tx, ty); |
36 | | - ctx.rotate(src.rotation); |
37 | | - ctx.scale(src.scaleX, src.scaleY); |
38 | | - ctx.scale(src.flipX ? -1 : 1, src.flipY ? -1 : 1); |
| 34 | + var camMatrix = renderer._tempMatrix1; |
| 35 | + var layerMatrix = renderer._tempMatrix2; |
| 36 | + var calcMatrix = renderer._tempMatrix3; |
| 37 | + |
| 38 | + layerMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY); |
| 39 | + |
| 40 | + camMatrix.copyFrom(camera.matrix); |
39 | 41 |
|
40 | | - for (var index = 0; index < length; ++index) |
| 42 | + if (parentMatrix) |
41 | 43 | { |
42 | | - var tile = renderTiles[index]; |
| 44 | + // Multiply the camera by the parent matrix |
| 45 | + camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY); |
43 | 46 |
|
44 | | - var tileTexCoords = tileset.getTileTextureCoordinates(tile.index); |
45 | | - if (tileTexCoords === null) { continue; } |
| 47 | + // Undo the camera scroll |
| 48 | + layerMatrix.e = src.x; |
| 49 | + layerMatrix.f = src.y; |
46 | 50 |
|
47 | | - var halfWidth = tile.width / 2; |
48 | | - var halfHeight = tile.height / 2; |
| 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; |
49 | 58 |
|
50 | | - ctx.save(); |
51 | | - ctx.translate(tile.pixelX + halfWidth, tile.pixelY + halfHeight); |
| 59 | + // Multiply by the Sprite matrix, store result in calcMatrix |
| 60 | + camMatrix.multiply(layerMatrix, calcMatrix); |
| 61 | + } |
52 | 62 |
|
53 | | - if (tile.rotation !== 0) |
54 | | - { |
55 | | - ctx.rotate(tile.rotation); |
56 | | - } |
| 63 | + var tileset = src.tileset; |
| 64 | + var ctx = renderer.currentContext; |
| 65 | + var image = tileset.image.getSourceImage(); |
57 | 66 |
|
58 | | - if (tile.flipX || tile.flipY) |
59 | | - { |
60 | | - ctx.scale(tile.flipX ? -1 : 1, tile.flipY ? -1 : 1); |
61 | | - } |
| 67 | + ctx.save(); |
| 68 | + |
| 69 | + calcMatrix.copyToContext(ctx); |
62 | 70 |
|
63 | | - ctx.globalAlpha = camera.alpha * src.alpha * tile.alpha; |
| 71 | + var alpha = camera.alpha * src.alpha; |
64 | 72 |
|
65 | | - ctx.drawImage( |
66 | | - image, |
67 | | - tileTexCoords.x, tileTexCoords.y, |
68 | | - tile.width, tile.height, |
69 | | - -halfWidth, -halfHeight, |
70 | | - tile.width, tile.height |
71 | | - ); |
| 73 | + for (var i = 0; i < tileCount; i++) |
| 74 | + { |
| 75 | + var tile = renderTiles[i]; |
72 | 76 |
|
73 | | - ctx.restore(); |
| 77 | + var tileTexCoords = tileset.getTileTextureCoordinates(tile.index); |
| 78 | + |
| 79 | + if (tileTexCoords) |
| 80 | + { |
| 81 | + var halfWidth = tile.width / 2; |
| 82 | + var halfHeight = tile.height / 2; |
| 83 | + |
| 84 | + ctx.save(); |
| 85 | + |
| 86 | + ctx.translate(tile.pixelX + halfWidth, tile.pixelY + halfHeight); |
| 87 | + |
| 88 | + if (tile.rotation !== 0) |
| 89 | + { |
| 90 | + ctx.rotate(tile.rotation); |
| 91 | + } |
| 92 | + |
| 93 | + if (tile.flipX || tile.flipY) |
| 94 | + { |
| 95 | + ctx.scale((tile.flipX) ? -1 : 1, (tile.flipY) ? -1 : 1); |
| 96 | + } |
| 97 | + |
| 98 | + ctx.globalAlpha = alpha * tile.alpha; |
| 99 | + |
| 100 | + ctx.drawImage( |
| 101 | + image, |
| 102 | + tileTexCoords.x, tileTexCoords.y, |
| 103 | + tile.width, tile.height, |
| 104 | + -halfWidth, -halfHeight, |
| 105 | + tile.width, tile.height |
| 106 | + ); |
| 107 | + |
| 108 | + ctx.restore(); |
| 109 | + } |
74 | 110 | } |
75 | 111 |
|
76 | 112 | ctx.restore(); |
|
0 commit comments