From bc688393f9f86ebdcf994cdd1d86c6b73d8c49e6 Mon Sep 17 00:00:00 2001 From: Aaron Helton Date: Sun, 23 Jul 2017 00:27:45 -0400 Subject: [PATCH] Added bounds checking to the TileMap class. --- src/com/aargonian/tile/TileMap.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/com/aargonian/tile/TileMap.java b/src/com/aargonian/tile/TileMap.java index 4924a31..03e09b5 100644 --- a/src/com/aargonian/tile/TileMap.java +++ b/src/com/aargonian/tile/TileMap.java @@ -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);