Skip to content

Commit

Permalink
Merge pull request #87 from martinjonsson01/36-refactor-x-y
Browse files Browse the repository at this point in the history
36 refactor x y
  • Loading branch information
jonathan-lindqvist authored Oct 19, 2021
2 parents b0f894c + 159876c commit 0be7ee7
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 165 deletions.
Binary file modified documents/diagrams/com.thebois.listeners.events.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documents/diagrams/com.thebois.models.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions src/main/java/com/thebois/listeners/events/ObstaclePlacedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@
*/
public class ObstaclePlacedEvent {

private final int posX;
private final int posY;
private final int x;
private final int y;

/**
* Instantiates with a position for the new obstacle.
*
* @param posX The x-coordinate of the new obstacle.
* @param posY The y-coordinate of the new obstacle.
* @param x The x-coordinate of the new obstacle.
* @param y The y-coordinate of the new obstacle.
*/
public ObstaclePlacedEvent(final int posX, final int posY) {
this.posX = posX;
this.posY = posY;
public ObstaclePlacedEvent(final int x, final int y) {
this.x = x;
this.y = y;
}

public Position getPosition() {
return new Position(getPosX(), getPosY());
return new Position(getX(), getY());
}

public int getPosX() {
return posX;
public int getX() {
return x;
}

public int getPosY() {
return posY;
public int getY() {
return y;
}

}
62 changes: 31 additions & 31 deletions src/main/java/com/thebois/models/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
public final class Position implements IDeepClonable<Position>, Serializable {

private float posX;
private float posY;
private float x;
private float y;

/**
* Creates position at 0,0.
Expand All @@ -23,12 +23,12 @@ public Position() {
/**
* Creates position at specified coordinates.
*
* @param posX The x-coordinate.
* @param posY The y-coordinate.
* @param x The x-coordinate.
* @param y The y-coordinate.
*/
public Position(final float posX, final float posY) {
this.posX = posX;
this.posY = posY;
public Position(final float x, final float y) {
this.x = x;
this.y = y;
}

/**
Expand All @@ -39,42 +39,42 @@ public Position(final float posX, final float posY) {
* @return The distance between the points.
*/
public float distanceTo(final Position other) {
return distanceTo(other.getPosX(), other.getPosY());
return distanceTo(other.getX(), other.getY());
}

/**
* Calculates the euclidean distance between two positions.
*
* @param otherPosX The x-coordinate to compare distance to.
* @param otherPosY The y-coordinate to compare distance to.
* @param otherX The x-coordinate to compare distance to.
* @param otherY The y-coordinate to compare distance to.
*
* @return The distance between the points.
*/
public float distanceTo(final float otherPosX, final float otherPosY) {
final float distanceX = Math.abs(getPosX() - otherPosX);
final float distanceY = Math.abs(getPosY() - otherPosY);
public float distanceTo(final float otherX, final float otherY) {
final float distanceX = Math.abs(getX() - otherX);
final float distanceY = Math.abs(getY() - otherY);
return (float) Math.sqrt(distanceX * distanceX + distanceY * distanceY);
}

public float getPosX() {
return posX;
public float getX() {
return x;
}

public void setPosX(final float posX) {
this.posX = posX;
public void setX(final float x) {
this.x = x;
}

public float getPosY() {
return posY;
public float getY() {
return y;
}

public void setPosY(final float posY) {
this.posY = posY;
public void setY(final float y) {
this.y = y;
}

@Override
public int hashCode() {
return Objects.hash(posX, posY);
return Objects.hash(x, y);
}

@Override
Expand All @@ -86,12 +86,12 @@ public boolean equals(final Object other) {
return false;
}
final Position position = (Position) other;
return Float.compare(position.posX, posX) == 0 && Float.compare(position.posY, posY) == 0;
return Float.compare(position.x, x) == 0 && Float.compare(position.y, y) == 0;
}

@Override
public String toString() {
return "{" + "x=" + posX + ", y=" + posY + '}';
return "{" + "x=" + x + ", y=" + y + '}';
}

/**
Expand All @@ -101,7 +101,7 @@ public String toString() {
*/
@Override
public Position deepClone() {
return new Position(this.posX, this.posY);
return new Position(this.x, this.y);
}

/**
Expand All @@ -116,8 +116,8 @@ public Position deepClone() {
* @return The manhattan distance to the destination.
*/
public int manhattanDistanceTo(final Position destination) {
final float distanceX = Math.abs(getPosX() - destination.getPosX());
final float distanceY = Math.abs(getPosY() - destination.getPosY());
final float distanceX = Math.abs(getX() - destination.getX());
final float distanceY = Math.abs(getY() - destination.getY());
return Math.round(distanceX + distanceY);
}

Expand All @@ -129,7 +129,7 @@ public int manhattanDistanceTo(final Position destination) {
* @return The sum of the positions.
*/
public Position add(final Position other) {
return add(other.getPosX(), other.getPosY());
return add(other.getX(), other.getY());
}

/**
Expand All @@ -141,7 +141,7 @@ public Position add(final Position other) {
* @return A position with its coordinates added to.
*/
public Position add(final float otherX, final float otherY) {
return new Position(posX + otherX, posY + otherY);
return new Position(x + otherX, y + otherY);
}

/**
Expand All @@ -152,7 +152,7 @@ public Position add(final float otherX, final float otherY) {
* @return The difference.
*/
public Position subtract(final Position other) {
return subtract(other.getPosX(), other.getPosY());
return subtract(other.getX(), other.getY());
}

/**
Expand All @@ -175,7 +175,7 @@ public Position subtract(final float otherX, final float otherY) {
* @return The scaled position.
*/
public Position multiply(final float scalar) {
return new Position(posX * scalar, posY * scalar);
return new Position(x * scalar, y * scalar);
}

}
10 changes: 5 additions & 5 deletions src/main/java/com/thebois/models/beings/AbstractBeing.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ private boolean hasOvershotDestination(
final Position destination, final Position delta, final Position newPosition) {
// Destination has been overshot if the delta has changed sign before and after moving.
final Position newDelta = destination.subtract(newPosition);
return hasChangedSign(newDelta.getPosX(), delta.getPosX())
|| hasChangedSign(newDelta.getPosY(), delta.getPosY());
return hasChangedSign(newDelta.getX(), delta.getX()) || hasChangedSign(newDelta.getY(),
delta.getY());
}

private boolean hasChangedSign(final float posX, final float posX2) {
Expand Down Expand Up @@ -218,9 +218,9 @@ protected Position nearestNeighborOf(final Position destination) {
Position nearestNeighbor = new Position(Float.MAX_VALUE, Float.MAX_VALUE);
Position lastPosition;

for (int i = 0; i < positionOffsets.length; i++) {
final float x = destination.getPosX() + positionOffsets[i][0];
final float y = destination.getPosY() + positionOffsets[i][1];
for (final int[] positionOffset : positionOffsets) {
final float x = destination.getX() + positionOffset[0];
final float y = destination.getY() + positionOffset[1];
lastPosition = new Position(x, y);

if (getPosition().distanceTo(nearestNeighbor)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/thebois/models/world/IWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public interface IWorld extends Serializable {
/**
* Gets the tile at a given location.
*
* @param posX The x-coordinate to get the tile from.
* @param posY The y-coordinate to get the tile from.
* @param x The x-coordinate to get the tile from.
* @param y The y-coordinate to get the tile from.
*
* @return The tile at the given position.
*/
ITile getTileAt(int posX, int posY);
ITile getTileAt(int x, int y);

}
48 changes: 24 additions & 24 deletions src/main/java/com/thebois/models/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ private void replaceTilesInCanonicalMatrix(final ITile[][] tiles) {
private void replaceTileInCanonicalMatrix(final ITile tile) {
if (tile != null) {
final Position position = tile.getPosition();
final int posY = (int) position.getPosY();
final int posX = (int) position.getPosX();
canonicalMatrix[posY][posX] = tile;
final int y = (int) position.getY();
final int x = (int) position.getX();
canonicalMatrix[y][x] = tile;
}
}

Expand All @@ -109,8 +109,8 @@ public Iterable<Position> findEmptyPositions(final int count) {
}

private boolean isPositionEmpty(final Position position) {
final int x = (int) position.getPosX();
final int y = (int) position.getPosY();
final int x = (int) position.getX();
final int y = (int) position.getY();
return canonicalMatrix[y][x].getCost() < Float.MAX_VALUE;
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public Collection<IResource> getResources() {
* @return Whether the structure was built.
*/
public boolean createStructure(final StructureType type, final Position position) {
return createStructure(type, (int) position.getPosX(), (int) position.getPosY());
return createStructure(type, (int) position.getX(), (int) position.getY());
}

/**
Expand All @@ -192,8 +192,8 @@ public boolean createStructure(final StructureType type, final int x, final int
}

private boolean isPositionPlaceable(final Position position) {
final int posIntX = (int) position.getPosX();
final int posIntY = (int) position.getPosY();
final int posIntX = (int) position.getX();
final int posIntY = (int) position.getY();
if (posIntY < 0 || posIntY >= structureMatrix.length) {
return false;
}
Expand All @@ -203,8 +203,8 @@ private boolean isPositionPlaceable(final Position position) {
return isPositionEmpty(position);
}

private void postObstacleEvent(final int posX, final int posY) {
final ObstaclePlacedEvent obstacleEvent = new ObstaclePlacedEvent(posX, posY);
private void postObstacleEvent(final int x, final int y) {
final ObstaclePlacedEvent obstacleEvent = new ObstaclePlacedEvent(x, y);
Pawntastic.getEventBus().post(obstacleEvent);
}

Expand Down Expand Up @@ -233,13 +233,13 @@ public Optional<IStructure> getNearbyIncompleteStructure(
public Iterable<ITile> getNeighboursOf(final ITile tile) {
final ArrayList<ITile> tiles = new ArrayList<>(8);
final Position position = tile.getPosition();
final int posY = (int) position.getPosY();
final int posX = (int) position.getPosX();
final int y = (int) position.getY();
final int x = (int) position.getX();

final int startY = Math.max(0, posY - 1);
final int endY = Math.min(worldSize - 1, posY + 1);
final int startX = Math.max(0, posX - 1);
final int endX = Math.min(worldSize - 1, posX + 1);
final int startY = Math.max(0, y - 1);
final int endY = Math.min(worldSize - 1, y + 1);
final int startX = Math.max(0, x - 1);
final int endX = Math.min(worldSize - 1, x + 1);

for (int neighbourY = startY; neighbourY <= endY; neighbourY++) {
for (int neighbourX = startX; neighbourX <= endX; neighbourX++) {
Expand All @@ -255,22 +255,22 @@ public Iterable<ITile> getNeighboursOf(final ITile tile) {

@Override
public ITile getTileAt(final Position position) {
return getTileAt((int) position.getPosX(), (int) position.getPosY());
return getTileAt((int) position.getX(), (int) position.getY());
}

@Override
public ITile getTileAt(final int posX, final int posY) {
if (posX < 0 || posY < 0 || posX >= worldSize || posY >= worldSize) {
public ITile getTileAt(final int x, final int y) {
if (x < 0 || y < 0 || x >= worldSize || y >= worldSize) {
throw new IndexOutOfBoundsException("Given position is outside of the world.");
}
return canonicalMatrix[posY][posX];
return canonicalMatrix[y][x];
}

private boolean isDiagonalTo(final ITile tile, final ITile neighbour) {
final int tileX = (int) tile.getPosition().getPosX();
final int tileY = (int) tile.getPosition().getPosY();
final int neighbourX = (int) neighbour.getPosition().getPosX();
final int neighbourY = (int) neighbour.getPosition().getPosY();
final int tileX = (int) tile.getPosition().getX();
final int tileY = (int) tile.getPosition().getY();
final int neighbourX = (int) neighbour.getPosition().getX();
final int neighbourY = (int) neighbour.getPosition().getY();
final int deltaX = Math.abs(tileX - neighbourX);
final int deltaY = Math.abs(tileY - neighbourY);
return deltaX == 1 && deltaY == 1;
Expand Down
Loading

0 comments on commit 0be7ee7

Please sign in to comment.