2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-07 17:10:01 +00:00
|
|
|
var Geom = require('../../geom/');
|
2018-02-07 22:46:07 +00:00
|
|
|
var GetTilesWithin = require('./GetTilesWithin');
|
2018-02-07 17:10:01 +00:00
|
|
|
var Intersects = require('../../geom/intersects/');
|
|
|
|
var NOOP = require('../../utils/NOOP');
|
2020-10-02 10:36:44 +00:00
|
|
|
var Vector2 = require('../../math/Vector2');
|
2017-11-26 13:55:44 +00:00
|
|
|
|
|
|
|
var TriangleToRectangle = function (triangle, rect)
|
|
|
|
{
|
|
|
|
return Intersects.RectangleToTriangle(rect, triangle);
|
|
|
|
};
|
|
|
|
|
2020-10-02 10:36:44 +00:00
|
|
|
var point = new Vector2();
|
|
|
|
var pointStart = new Vector2();
|
|
|
|
var pointEnd = new Vector2();
|
2017-11-27 13:33:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the tiles that overlap with the given shape in the given layer. The shape must be a Circle,
|
|
|
|
* Line, Rectangle or Triangle. The shape should be in world coordinates.
|
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @function Phaser.Tilemaps.Components.GetTilesWithinShape
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-20 15:11:33 +00:00
|
|
|
* @param {(Phaser.Geom.Circle|Phaser.Geom.Line|Phaser.Geom.Rectangle|Phaser.Geom.Triangle)} shape - A shape in world (pixel) coordinates
|
2020-11-23 10:54:14 +00:00
|
|
|
* @param {Phaser.Types.Tilemaps.FilteringOptions} filteringOptions - Optional filters to apply when getting the tiles.
|
|
|
|
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to use when calculating the tile index from the world values.
|
2018-02-08 02:02:37 +00:00
|
|
|
* @param {Phaser.Tilemaps.LayerData} layer - The Tilemap Layer to act upon.
|
2018-03-20 15:11:33 +00:00
|
|
|
*
|
2018-02-08 01:08:59 +00:00
|
|
|
* @return {Phaser.Tilemaps.Tile[]} Array of Tile objects.
|
2017-11-27 13:33:30 +00:00
|
|
|
*/
|
2017-11-26 13:55:44 +00:00
|
|
|
var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
|
|
|
|
{
|
|
|
|
if (shape === undefined) { return []; }
|
|
|
|
|
|
|
|
// intersectTest is a function with parameters: shape, rect
|
|
|
|
var intersectTest = NOOP;
|
2020-09-02 10:54:24 +00:00
|
|
|
|
2020-11-20 12:39:18 +00:00
|
|
|
if (shape instanceof Geom.Circle)
|
2020-09-02 10:54:24 +00:00
|
|
|
{
|
2020-11-20 12:39:18 +00:00
|
|
|
intersectTest = Intersects.CircleToRectangle;
|
|
|
|
}
|
|
|
|
else if (shape instanceof Geom.Rectangle)
|
|
|
|
{
|
|
|
|
intersectTest = Intersects.RectangleToRectangle;
|
|
|
|
}
|
|
|
|
else if (shape instanceof Geom.Triangle)
|
|
|
|
{
|
|
|
|
intersectTest = TriangleToRectangle;
|
|
|
|
}
|
|
|
|
else if (shape instanceof Geom.Line)
|
|
|
|
{
|
|
|
|
intersectTest = Intersects.LineToRectangle;
|
2020-09-02 10:54:24 +00:00
|
|
|
}
|
2017-11-26 13:55:44 +00:00
|
|
|
|
|
|
|
// Top left corner of the shapes's bounding box, rounded down to include partial tiles
|
2020-10-02 10:36:44 +00:00
|
|
|
layer.tilemapLayer.worldToTileXY(shape.left, shape.top, true, pointStart, camera);
|
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
var xStart = pointStart.x;
|
|
|
|
var yStart = pointStart.y;
|
2017-11-26 13:55:44 +00:00
|
|
|
|
|
|
|
// Bottom right corner of the shapes's bounding box, rounded up to include partial tiles
|
2020-10-02 10:36:44 +00:00
|
|
|
layer.tilemapLayer.worldToTileXY(shape.right, shape.bottom, true, pointEnd, camera);
|
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
var xEnd = Math.ceil(pointEnd.x);
|
|
|
|
var yEnd = Math.ceil(pointEnd.y);
|
2017-11-26 13:55:44 +00:00
|
|
|
|
|
|
|
// Tiles within bounding rectangle of shape. Bounds are forced to be at least 1 x 1 tile in size
|
|
|
|
// to grab tiles for shapes that don't have a height or width (e.g. a horizontal line).
|
|
|
|
var width = Math.max(xEnd - xStart, 1);
|
|
|
|
var height = Math.max(yEnd - yStart, 1);
|
2020-10-02 10:36:44 +00:00
|
|
|
|
2017-11-26 13:55:44 +00:00
|
|
|
var tiles = GetTilesWithin(xStart, yStart, width, height, filteringOptions, layer);
|
|
|
|
|
|
|
|
var tileWidth = layer.tileWidth;
|
|
|
|
var tileHeight = layer.tileHeight;
|
2020-09-02 10:54:24 +00:00
|
|
|
|
2017-11-26 13:55:44 +00:00
|
|
|
if (layer.tilemapLayer)
|
|
|
|
{
|
|
|
|
tileWidth *= layer.tilemapLayer.scaleX;
|
|
|
|
tileHeight *= layer.tilemapLayer.scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
var results = [];
|
|
|
|
var tileRect = new Geom.Rectangle(0, 0, tileWidth, tileHeight);
|
2020-09-02 10:54:24 +00:00
|
|
|
|
2017-11-26 13:55:44 +00:00
|
|
|
for (var i = 0; i < tiles.length; i++)
|
|
|
|
{
|
|
|
|
var tile = tiles[i];
|
2020-10-02 10:36:44 +00:00
|
|
|
|
2020-11-20 12:39:18 +00:00
|
|
|
layer.tilemapLayer.tileToWorldXY(tile.x, tile.y, point, camera);
|
2020-10-02 10:36:44 +00:00
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
tileRect.x = point.x;
|
|
|
|
tileRect.y = point.y;
|
2020-10-02 10:36:44 +00:00
|
|
|
|
2017-11-26 13:55:44 +00:00
|
|
|
if (intersectTest(shape, tileRect))
|
|
|
|
{
|
|
|
|
results.push(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetTilesWithinShape;
|