|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var CheckIsoBounds = require('./CheckIsoBounds'); |
| 8 | + |
| 9 | +/** |
| 10 | + * Returns the tiles in the given layer that are within the camera's viewport. This is used internally. |
| 11 | + * |
| 12 | + * @function Phaser.Tilemaps.Components.IsometricCullTiles |
| 13 | + * @since 3.50.0 |
| 14 | + * |
| 15 | + * @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon. |
| 16 | + * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to run the cull check against. |
| 17 | + * @param {array} [outputArray] - An optional array to store the Tile objects within. |
| 18 | + * @param {integer} [renderOrder=0] - The rendering order constant. |
| 19 | + * |
| 20 | + * @return {Phaser.Tilemaps.Tile[]} An array of Tile objects. |
| 21 | + */ |
| 22 | +var IsometricCullTiles = function (layer, camera, outputArray, renderOrder) |
| 23 | +{ |
| 24 | + if (outputArray === undefined) { outputArray = []; } |
| 25 | + if (renderOrder === undefined) { renderOrder = 0; } |
| 26 | + |
| 27 | + outputArray.length = 0; |
| 28 | + |
| 29 | + var tilemapLayer = layer.tilemapLayer; |
| 30 | + |
| 31 | + var mapData = layer.data; |
| 32 | + var mapWidth = layer.width; |
| 33 | + var mapHeight = layer.height; |
| 34 | + |
| 35 | + var drawLeft = 0; |
| 36 | + var drawRight = mapWidth; |
| 37 | + var drawTop = 0; |
| 38 | + var drawBottom = mapHeight; |
| 39 | + |
| 40 | + if (!tilemapLayer.skipCull) |
| 41 | + { |
| 42 | + var x; |
| 43 | + var y; |
| 44 | + var tile; |
| 45 | + |
| 46 | + if (renderOrder === 0) |
| 47 | + { |
| 48 | + // right-down |
| 49 | + |
| 50 | + for (y = drawTop; y < drawBottom; y++) |
| 51 | + { |
| 52 | + for (x = drawLeft; mapData[y] && x < drawRight; x++) |
| 53 | + { |
| 54 | + if (CheckIsoBounds(x,y,layer,camera)) |
| 55 | + { |
| 56 | + tile = mapData[y][x]; |
| 57 | + |
| 58 | + if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + outputArray.push(tile); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + else if (renderOrder === 1) |
| 69 | + { |
| 70 | + // left-down |
| 71 | + |
| 72 | + for (y = drawTop; y < drawBottom; y++) |
| 73 | + { |
| 74 | + for (x = drawRight; mapData[y] && x >= drawLeft; x--) |
| 75 | + { |
| 76 | + if (CheckIsoBounds(x,y,layer,camera)) |
| 77 | + { |
| 78 | + tile = mapData[y][x]; |
| 79 | + |
| 80 | + if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0) |
| 81 | + { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + outputArray.push(tile); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + else if (renderOrder === 2) |
| 91 | + { |
| 92 | + // right-up |
| 93 | + |
| 94 | + for (y = drawBottom; y >= drawTop; y--) |
| 95 | + { |
| 96 | + for (x = drawLeft; mapData[y] && x < drawRight; x++) |
| 97 | + { |
| 98 | + if (CheckIsoBounds(x,y,layer,camera)) |
| 99 | + { |
| 100 | + tile = mapData[y][x]; |
| 101 | + |
| 102 | + if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0) |
| 103 | + { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + outputArray.push(tile); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + else if (renderOrder === 3) |
| 113 | + { |
| 114 | + // left-up |
| 115 | + |
| 116 | + for (y = drawBottom; y >= drawTop; y--) |
| 117 | + { |
| 118 | + for (x = drawRight; mapData[y] && x >= drawLeft; x--) |
| 119 | + { |
| 120 | + if (CheckIsoBounds(x,y,layer,camera)) |
| 121 | + { |
| 122 | + tile = mapData[y][x]; |
| 123 | + |
| 124 | + if (!tile || tile.index === -1 || !tile.visible || tile.alpha === 0) |
| 125 | + { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + outputArray.push(tile); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + tilemapLayer.tilesDrawn = outputArray.length; |
| 137 | + tilemapLayer.tilesTotal = mapWidth * mapHeight; |
| 138 | + |
| 139 | + return outputArray; |
| 140 | +}; |
| 141 | + |
| 142 | +module.exports = IsometricCullTiles; |
0 commit comments