From 629dac6ea3c8ab9014d4632bdaa740d71110587e Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Sun, 12 May 2019 21:40:52 +0200 Subject: [PATCH] getAdjacentTileCount Method now uses for-loop instead of many if statements --- Server/Server.iml | 57 ------------------- .../server/game/objects/GameObject.java | 33 ++++------- 2 files changed, 10 insertions(+), 80 deletions(-) delete mode 100644 Server/Server.iml diff --git a/Server/Server.iml b/Server/Server.iml deleted file mode 100644 index ff5c39c..0000000 --- a/Server/Server.iml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Server/src/main/java/net/simon987/server/game/objects/GameObject.java b/Server/src/main/java/net/simon987/server/game/objects/GameObject.java index b463fa4..49f740c 100755 --- a/Server/src/main/java/net/simon987/server/game/objects/GameObject.java +++ b/Server/src/main/java/net/simon987/server/game/objects/GameObject.java @@ -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++; } }