Use cached vec2s to avoid constant vector creation

This commit is contained in:
Richard Davey 2020-10-02 11:36:44 +01:00
parent f03ddf033b
commit aafd5c6cee
4 changed files with 33 additions and 11 deletions

View file

@ -5,6 +5,9 @@
*/
var GetTileAt = require('./GetTileAt');
var Vector2 = require('../../math/Vector2');
var point = new Vector2();
/**
* Gets a tile at the given world coordinates from the given layer.
@ -23,9 +26,11 @@ var GetTileAt = require('./GetTileAt');
*/
var GetTileAtWorldXY = function (worldX, worldY, nonNull, camera, layer)
{
var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
layer.tilemapLayer.worldToTileXY(worldX, worldY, true, point, camera);
var tileX = point.x;
var tileY = point.y;
return GetTileAt(tileX, tileY, nonNull, layer);
};

View file

@ -8,15 +8,16 @@ var Geom = require('../../geom/');
var GetTilesWithin = require('./GetTilesWithin');
var Intersects = require('../../geom/intersects/');
var NOOP = require('../../utils/NOOP');
var Vector2 = require('../../math/Vector2');
var TriangleToRectangle = function (triangle, rect)
{
return Intersects.RectangleToTriangle(rect, triangle);
};
// Note: Could possibly be optimized by copying the shape and shifting it into tilemapLayer
// coordinates instead of shifting the tiles.
var point = new Vector2();
var pointStart = new Vector2();
var pointEnd = new Vector2();
/**
* Gets the tiles that overlap with the given shape in the given layer. The shape must be a Circle,
@ -70,12 +71,14 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
}
// Top left corner of the shapes's bounding box, rounded down to include partial tiles
var pointStart = layer.tilemapLayer.worldToTileXY(shape.left, shape.top, true, undefined, camera);
layer.tilemapLayer.worldToTileXY(shape.left, shape.top, true, pointStart, camera);
var xStart = pointStart.x;
var yStart = pointStart.y;
// Bottom right corner of the shapes's bounding box, rounded up to include partial tiles
var pointEnd = layer.tilemapLayer.worldToTileXY(shape.right, shape.bottom, true, undefined, camera);
layer.tilemapLayer.worldToTileXY(shape.right, shape.bottom, true, pointEnd, camera);
var xEnd = Math.ceil(pointEnd.x);
var yEnd = Math.ceil(pointEnd.y);
@ -83,6 +86,7 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
// 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);
var tiles = GetTilesWithin(xStart, yStart, width, height, filteringOptions, layer);
var tileWidth = layer.tileWidth;
@ -100,9 +104,12 @@ var GetTilesWithinShape = function (shape, filteringOptions, camera, layer)
for (var i = 0; i < tiles.length; i++)
{
var tile = tiles[i];
var point = layer.tilemapLayer.tileToWorldXY(tile.x, tile.y, undefined, camera, layer);
layer.tilemapLayer.tileToWorldXY(tile.x, tile.y, point, camera, layer);
tileRect.x = point.x;
tileRect.y = point.y;
if (intersectTest(shape, tileRect))
{
results.push(tile);

View file

@ -5,6 +5,10 @@
*/
var GetTilesWithin = require('./GetTilesWithin');
var Vector2 = require('../../math/Vector2');
var pointStart = new Vector2();
var pointEnd = new Vector2();
/**
* Gets the tiles in the given rectangular area (in world coordinates) of the layer.
@ -27,14 +31,15 @@ var GetTilesWithin = require('./GetTilesWithin');
*/
var GetTilesWithinWorldXY = function (worldX, worldY, width, height, filteringOptions, camera, layer)
{
// Top left corner of the rect, rounded down to include partial tiles
var pointStart = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
layer.tilemapLayer.worldToTileXY(worldX, worldY, true, pointStart, camera);
var xStart = pointStart.x;
var yStart = pointStart.y;
// Bottom right corner of the rect, rounded up to include partial tiles
var pointEnd = layer.tilemapLayer.worldToTileXY(worldX + width, worldY + height, false, undefined, camera);
layer.tilemapLayer.worldToTileXY(worldX + width, worldY + height, false, pointEnd, camera);
var xEnd = Math.ceil(pointEnd.x);
var yEnd = Math.ceil(pointEnd.y);

View file

@ -5,6 +5,9 @@
*/
var HasTileAt = require('./HasTileAt');
var Vector2 = require('../../math/Vector2');
var point = new Vector2();
/**
* Checks if there is a tile at the given location (in world coordinates) in the given layer. Returns
@ -22,9 +25,11 @@ var HasTileAt = require('./HasTileAt');
*/
var HasTileAtWorldXY = function (worldX, worldY, camera, layer)
{
var point = layer.tilemapLayer.worldToTileXY(worldX, worldY, true, undefined, camera);
layer.tilemapLayer.worldToTileXY(worldX, worldY, true, point, camera);
var tileX = point.x;
var tileY = point.y;
return HasTileAt(tileX, tileY, layer);
};