Skip to content

Commit

Permalink
dd getRowAt, getColumnAt, rowExists and columnExists
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Jun 6, 2024
1 parent 9b03044 commit 05a9d56
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
52 changes: 48 additions & 4 deletions flixel/tile/FlxBaseTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,39 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
{
throw "computeDimensions must be implemented";
}


/**
* Finds the row number that overlaps the given Y in world space
* @param worldY A Y coordinate in the world
* @param bind If true, it will prevent out of range values
* @return A row index, where 0 is the top-most row
* @since 5.9.0
*/
public function getRowAt(worldY:Float, bind = false):Int
{
throw "getRowAt must be implemented";
}

/**
* Finds the row number that overlaps the given X in world space
* @param worldX A X coordinate in the world
* @param bind If true, it will prevent out of range values
* @return A column index, where 0 is the left-most column
* @since 5.9.0
*/
public function getColumnAt(worldX:Float, bind = false):Int
{
throw "getColumnAt must be implemented";
}

public function getTileIndexByCoords(coord:FlxPoint):Int
{
throw "getTileIndexByCoords must be implemented";
return 0;
}

public function getTileCoordsByIndex(index:Int, midpoint = true):FlxPoint
{
throw "getTileCoordsByIndex must be implemented";
return null;
}

/**
Expand Down Expand Up @@ -762,7 +784,7 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
*/
public overload extern inline function tileExists(column:Int, row:Int):Bool
{
return column >= 0 && row >= 0 && column < widthInTiles && row < heightInTiles;
return columnExists(column) && rowExists(row);
}

/**
Expand All @@ -778,6 +800,28 @@ class FlxBaseTilemap<Tile:FlxObject> extends FlxObject
return mapIndex >= 0 && mapIndex < _data.length;
}

/**
* Whether a row exists at the given map location
*
* @param column The desired location in the map
* @since 5.9.0
*/
public overload extern inline function columnExists(column:Int):Bool
{
return column >= 0 && column < widthInTiles;
}

/**
* Whether a row exists at the given map location
*
* @param row The desired location in the map
* @since 5.9.0
*/
public overload extern inline function rowExists(row:Int):Bool
{
return row >= 0 && row < heightInTiles;
}

/**
* Finds the tile instance at a particular column and row
*
Expand Down
37 changes: 27 additions & 10 deletions flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -781,19 +781,16 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
for (column in minTileX...maxTileX)
{
final mapIndex:Int = (row * widthInTiles) + column;
final dataIndex:Int = _data[mapIndex] < 0 ? 0 : _data[mapIndex];
final tileIndex:Int = _data[mapIndex] < 0 ? 0 : _data[mapIndex];// TODO: still need to check -1?

final tile = _tileObjects[dataIndex];
final tile = _tileObjects[tileIndex];
tile.orientAt(xPos, yPos, column, row);
if (tile.overlapsObject(object))
{
if (filter == null || filter(tile))
{
if (stopAtFirst)
return true;

result = true;
}
if (stopAtFirst)
return true;

result = true;
}
}
}
Expand Down Expand Up @@ -833,7 +830,27 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>

return results;
}


public function getRowAt(worldY:Float, bind = false):Int
{
final unboundRow = Math.floor(worldY / scaledTileHeight);

if (bind)
return unboundRow < 0 ? 0 : (bind > heightInTiles ? heightInTiles : unboundRow);

return unbound;
}

public function getColumnAt(worldX:Float, bind = false):Int
{
final unboundColumn = Math.floor(worldX / scaledTileWidth);

if (bind)
return unboundColumn < 0 ? 0 : (bind > widthInTiles ? widthInTiles : unboundColumn);

return unboundColumn;
}

override public function getTileIndexByCoords(coord:FlxPoint):Int
{
var localX = coord.x - x;
Expand Down

0 comments on commit 05a9d56

Please sign in to comment.