Added bounds checking to the TileMap class.

This commit is contained in:
Aaron Helton
2017-07-23 00:27:45 -04:00
parent e6f37d1b44
commit bc688393f9

View File

@@ -65,13 +65,26 @@ public class TileMap
tile.setTileImplementation(newImpl);
}
private final boolean inBounds(int x, int y)
{
return !(x < 0 || x >= columns || y < 0 || y >= rows);
}
public String getTilePropertyAt(String property, int x, int y)
{
if(!inBounds(x, y))
{
return null;
}
return tiles[(y * columns) + x].getTileImplementation().getProperty(property);
}
public void setTilePropertyAt(String property, String value, int x, int y)
{
if(!inBounds(x, y))
{
return;
}
Tile tile = tiles[(y * columns) + x];
TileImpl newImpl = tile.getTileImplementation().setProperty(property, value);
setTileImplementation(tile, newImpl);
@@ -79,11 +92,19 @@ public class TileMap
public Object getTileResourceAt(String resource, int x, int y)
{
if(!inBounds(x, y))
{
return null;
}
return tiles[(y * columns) + x].getTileImplementation().getResource(resource);
}
public void setTileResourceAt(String resourceKey, Object resource, int x, int y)
{
if(!inBounds(x, y))
{
return;
}
Tile tile = tiles[(y * columns) + x];
TileImpl newImpl = tile.getTileImplementation().setResource(resourceKey, resource);
setTileImplementation(tile, newImpl);