getAdjacentTileCount Method now uses for-loop instead of many if statements

This commit is contained in:
Christian Bauer
2019-05-12 21:40:52 +02:00
parent 55fdd93d9d
commit 629dac6ea3
2 changed files with 10 additions and 80 deletions

View File

@@ -157,32 +157,19 @@ public abstract class GameObject implements JSONSerializable, MongoSerializable
public int getAdjacentTileCount(boolean diagonals) {
int[] xPositions = {1, 0, -1, 0, 1, -1, 1, -1};
int[] yPositions = {0, 1, 0, -1, 1, 1, -1, -1};
int range = diagonals ? xPositions.length : xPositions.length / 2;
int count = 0;
if (getWorld().getTileMap().isInBounds(x + 1, y) && !getWorld().isTileBlocked(x + 1, y)) {
count++;
}
if (getWorld().getTileMap().isInBounds(x, y + 1) && !getWorld().isTileBlocked(x, y + 1)) {
count++;
}
if (getWorld().getTileMap().isInBounds(x - 1, y) && !getWorld().isTileBlocked(x - 1, y)) {
count++;
}
if (getWorld().getTileMap().isInBounds(x, y - 1) && !getWorld().isTileBlocked(x, y - 1)) {
count++;
}
for (int index = 0; index < range; index++) {
int currentX = x + xPositions[index];
int currentY = y + yPositions[index];
if (diagonals) {
if (getWorld().getTileMap().isInBounds(x + 1, y + 1) && !getWorld().isTileBlocked(x + 1, y + 1)) {
count++;
}
if (getWorld().getTileMap().isInBounds(x - 1, y + 1) && !getWorld().isTileBlocked(x - 1, y + 1)) {
count++;
}
if (getWorld().getTileMap().isInBounds(x + 1, y - 1) && !getWorld().isTileBlocked(x + 1, y - 1)) {
count++;
}
if (getWorld().getTileMap().isInBounds(x - 1, y - 1) && !getWorld().isTileBlocked(x - 1, y - 1)) {
if (getWorld().getTileMap().isInBounds(currentX, currentY) &&
!getWorld().isTileBlocked(currentX, currentY)) {
count++;
}
}