Skip to content

Commit

Permalink
similar changes to HaxeFlixel/flixel-addons#384
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Apr 14, 2023
1 parent b373c8f commit c8c898c
Showing 1 changed file with 20 additions and 37 deletions.
57 changes: 20 additions & 37 deletions flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -730,43 +730,30 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
position.putWeak();
}

// Figure out what tiles we need to check against
var selectionX:Int = Math.floor((object.x - xPos) / scaledTileWidth);
var selectionY:Int = Math.floor((object.y - yPos) / scaledTileHeight);
var selectionWidth:Int = selectionX + Math.ceil(object.width / scaledTileWidth) + 1;
var selectionHeight:Int = selectionY + Math.ceil(object.height / scaledTileHeight) + 1;

// Then bound these coordinates by the map edges
selectionX = Std.int(FlxMath.bound(selectionX, 0, widthInTiles));
selectionY = Std.int(FlxMath.bound(selectionY, 0, heightInTiles));
selectionWidth = Std.int(FlxMath.bound(selectionWidth, 0, widthInTiles));
selectionHeight = Std.int(FlxMath.bound(selectionHeight, 0, heightInTiles));

// Then loop through this selection of tiles
var rowStart:Int = selectionY * widthInTiles;
var deltaX:Float = xPos - last.x;
var deltaY:Float = yPos - last.y;

for (row in selectionY...selectionHeight)
inline function bindInt(value:Int, min:Int, max:Int)
{
var column = selectionX;
return Std.int(FlxMath.bound(value, min, max));
}

while (column < selectionWidth)
{
var index:Int = rowStart + column;
if (index < 0 || index > _data.length - 1)
{
column++;
continue;
}
// Figure out what tiles we need to check against, and bind them by the map edges
final minTileX:Int = bindInt(Math.floor((object.x - xPos) / scaledTileWidth), 0, widthInTiles);
final minTileY:Int = bindInt(Math.floor((object.y - yPos) / scaledTileHeight), 0, heightInTiles);
final maxTileX:Int = bindInt(Math.ceil((object.x + object.width - xPos) / scaledTileWidth), 0, widthInTiles);
final maxTileY:Int = bindInt(Math.ceil((object.y + object.height - yPos) / scaledTileHeight), 0, heightInTiles);

// Loop through the range of tiles and call the callback on them, accordingly
final deltaX:Float = xPos - last.x;
final deltaY:Float = yPos - last.y;

var dataIndex:Int = _data[index];
for (row in minTileY...maxTileY)
{
for (column in minTileX...maxTileX)
{
final mapIndex:Int = (row * widthInTiles) + column;
final dataIndex:Int = _data[mapIndex];
if (dataIndex < 0)
{
column++;
continue;
}


final tile = _tileObjects[dataIndex];
tile.width = scaledTileWidth;
tile.height = scaledTileHeight;
Expand Down Expand Up @@ -799,18 +786,14 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
{
if (tile.callbackFunction != null && (tile.filter == null || isOfType(object, tile.filter)))
{
tile.mapIndex = rowStart + column;
tile.mapIndex = mapIndex;
tile.callbackFunction(tile, object);
}

if (tile.allowCollisions != NONE)
results = true;
}

column++;
}

rowStart += widthInTiles;
}

return results;
Expand Down

0 comments on commit c8c898c

Please sign in to comment.