Skip to content

Commit 432c1c0

Browse files
committed
GetTilesWithin: clip rectangular area to map bounds
1 parent 4c34d09 commit 432c1c0

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

v3/src/gameobjects/tilemap/components/GetTilesWithin.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
// TODO: add options for filtering by empty, collides, interestingFace
2+
// Get tiles within the rectangular area specified. Note: this clips the x, y, w & h to the map's
3+
// boundries.
24
var GetTilesWithin = function (tileX, tileY, width, height, layer)
35
{
4-
if (tileX === undefined || tileX < 0) { tileX = 0; }
5-
if (tileY === undefined || tileY < 0) { tileY = 0; }
6-
if (width === undefined || tileX + width > layer.width)
6+
if (tileX === undefined) { tileX = 0; }
7+
if (tileY === undefined) { tileY = 0; }
8+
if (width === undefined) { width = layer.width; }
9+
if (height === undefined) { height = layer.height; }
10+
11+
// Clip x, y to top left of map, while shrinking width/height to match.
12+
if (tileX < 0)
13+
{
14+
width += tileX;
15+
tileX = 0;
16+
}
17+
if (tileY < 0)
18+
{
19+
height += tileY;
20+
tileY = 0;
21+
}
22+
23+
// Clip width and height to bottom right of map.
24+
if (tileX + width > layer.width)
725
{
826
width = Math.max(layer.width - tileX, 0);
927
}
10-
if (height === undefined || tileY + height > layer.height)
28+
if (tileY + height > layer.height)
1129
{
1230
height = Math.max(layer.height - tileY, 0);
1331
}

0 commit comments

Comments
 (0)