Skip to content

Commit

Permalink
fix negative tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Jun 24, 2024
1 parent 55c8c82 commit b378d40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
19 changes: 9 additions & 10 deletions flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
for (column in minTileX...maxTileX)
{
final tile = getTileData(column, row);
if (tile == null)
continue;
tile.orientAt(xPos, yPos, column, row);
if (tile.overlapsObject(object) && (filter == null || filter(tile)))
{
Expand Down Expand Up @@ -933,7 +935,8 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
final endIndex = getMapIndex(end);

// If the starting tile is solid, return the starting position
if (getTileData(startIndex).solid)
final tile = getTileData(startIndex);
if (tile != null && tile.solid)
{
if (result != null)
result.copyFrom(start);
Expand Down Expand Up @@ -1040,8 +1043,9 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
final step = startY <= endY ? 1 : -1;
while (true)
{
var index = getMapIndex(x, y);
if (getTileData(index).solid)
final index = getMapIndex(x, y);
final tile = getTileData(index);
if (tile != null && tile.solid)
return index;

if (y == endY)
Expand Down Expand Up @@ -1102,7 +1106,8 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
var tileX = Math.floor(curX / scaledTileWidth);
var tileY = Math.floor(curY / scaledTileHeight);

if (getTileData(tileX, tileY).solid)
final tile = getTileData(tileX, tileY);
if (tile != null && tile.solid)
{
// Some basic helper stuff
tileX *= Std.int(scaledTileWidth);
Expand Down Expand Up @@ -1395,12 +1400,6 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
}
#end

/** Guards against -1 */
override function setTileHelper(mapIndex:Int, tileIndex:Int, updateGraphics:Bool = true):Bool
{
return super.setTileHelper(mapIndex, tileIndex < 0 ? 0 : tileIndex, updateGraphics);
}

/**
* Internal function used in setTileIndex() and the constructor to update the map.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/src/flixel/tile/FlxTilemapTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,34 @@ class FlxTilemapTest extends FlxTest
Assert.areEqual(tile.x, tile.last.x);
}

@Test
function testNegativeIndex()
{
final mapData = [
0, 0, 0,
0, 1, 0,
0, 0, 0
];
tilemap.loadMapFromArray(mapData, 3, 3, getBitmapData(), 8, 8);

tilemap.setTileIndex(4, -2);
Assert.areEqual(-2, tilemap.getTileIndex(4));

// cover entire map
final object = new FlxObject(4, 4, 16, 16);
object.last.set(object.x, object.y);

try
{
Assert.isFalse(tilemap.overlaps(object));
// TODO:test ray, rayStep and others
}
catch(e)
{
Assert.fail('tilemap.overlaps(object) threw exception: ' + e.toString());
}
}

function getBitmapData()
{
return new BitmapData(8*16, 8);
Expand Down

0 comments on commit b378d40

Please sign in to comment.