Skip to content

Commit 5bc09a2

Browse files
committed
Update AP physics to handle tiles where tileSize !== baseTileSize
1 parent 108b6bd commit 5bc09a2

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

src/physics/arcade/inc/CollideSpriteVsTilemapLayer.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,30 @@ var CollideSpriteVsTilemapLayer = function (sprite, tilemapLayer, collideCallbac
1111
return false;
1212
}
1313

14-
var mapData = tilemapLayer.getTilesWithinWorldXY(
15-
body.position.x, body.position.y,
16-
body.width, body.height
17-
);
14+
var x = body.position.x;
15+
var y = body.position.y;
16+
var w = body.width;
17+
var h = body.height;
18+
19+
// TODO: this logic should be encapsulated within the Tilemap API at some point.
20+
// If the map's base tile size differs from the layer's tile size, we need to adjust the
21+
// selection area by the difference between the two.
22+
var layerData = tilemapLayer.layer;
23+
if (layerData.tileWidth > layerData.baseTileWidth)
24+
{
25+
// The x origin of a tile is the left side, so x and width need to be adjusted.
26+
let xDiff = (layerData.tileWidth - layerData.baseTileWidth) * tilemapLayer.scaleX;
27+
x -= xDiff;
28+
w += xDiff;
29+
}
30+
if (layerData.tileHeight > layerData.baseTileHeight)
31+
{
32+
// The y origin of a tile is the bottom side, so just the height needs to be adjusted.
33+
let yDiff = (layerData.tileHeight - layerData.baseTileHeight) * tilemapLayer.scaleY;
34+
h += yDiff;
35+
}
36+
37+
var mapData = tilemapLayer.getTilesWithinWorldXY(x, y, w, h);
1838

1939
if (mapData.length === 0)
2040
{
@@ -29,6 +49,14 @@ var CollideSpriteVsTilemapLayer = function (sprite, tilemapLayer, collideCallbac
2949
tile = mapData[i];
3050
tileWorldRect.left = tilemapLayer.tileToWorldX(tile.x);
3151
tileWorldRect.top = tilemapLayer.tileToWorldY(tile.y);
52+
53+
// If the map's base tile size differs from the layer's tile size, only the top of the rect
54+
// needs to be adjusted since it's origin is (0, 1).
55+
if (tile.baseHeight !== tile.height)
56+
{
57+
tileWorldRect.top -= (tile.height - tile.baseHeight) * tilemapLayer.scaleY;
58+
}
59+
3260
tileWorldRect.right = tileWorldRect.left + tile.width * tilemapLayer.scaleX;
3361
tileWorldRect.bottom = tileWorldRect.top + tile.height * tilemapLayer.scaleY;
3462

0 commit comments

Comments
 (0)