Skip to content

Commit

Permalink
Fixed code smell isPositionOccupied SpaceGameArea
Browse files Browse the repository at this point in the history
  • Loading branch information
Foref committed Oct 19, 2023
1 parent 55acf5a commit 31f6545
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions source/core/src/main/com/csse3200/game/areas/SpaceGameArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class SpaceGameArea extends GameArea {
private ArrayList<Entity> targetTables;
private Random random;
int randomX, randomY;
private Vector2 goalPosition;

/**
* Constructor for initializing terrain area
Expand Down Expand Up @@ -147,26 +148,25 @@ private void spawnAsteroids() {
*/
public Entity spawnGoal() {
random = new Random();
//int randomX, randomY;
GridPoint2 position;
// Continue generating random positions until an unoccupied position is found.
do {
randomX = random.nextInt(terrain.getMapBounds(0).x);
randomY = random.nextInt(terrain.getMapBounds(0).y);
position = new GridPoint2(randomX, randomY);
} while (isPositionOccupied(position));
goalPosition = new Vector2((float) randomX, (float) randomY);
} while (isPositionOccupied(goalPosition));
Entity newGoal = MinigameObjectFactory.createObstacleGameGoal(WORMHOLE_SIZE, WORMHOLE_SIZE);
spawnEntityAt(newGoal, position, false, false);
spawnEntityAtVector(newGoal, goalPosition);
goal = newGoal;
return goal;
}

/**
* Check if a position is occupied by an entity.
*/
public boolean isPositionOccupied(GridPoint2 position) {
public boolean isPositionOccupied(Vector2 position) {
// Loop through the list of existing entities and check if any entity occupies the given position.
for (Entity entity : targetTables) {
//GridPoint2 and Vector2 is unrelated
if (entity != null && entity.getPosition().equals(position)) {
return true; // Position is occupied.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class MiniGameAssetsConfig {
public String[] textureAtlasPaths = null;
public String[] backgroundMusicPath = null;


@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -28,13 +29,16 @@ public boolean equals(Object o) {
return Objects.equals(backgroundMusicPath, that.backgroundMusicPath);
}

/*
Arrays problem, will fix later, currently assume there is no need for hashcode.
@Override
public int hashCode() {
int result = Arrays.hashCode(texturePaths);
result = 31 * result + Arrays.hashCode(textureAtlasPaths);
result = 31 * result + (backgroundMusicPath != null ? backgroundMusicPath.hashCode() : 0);
return result;
}
*/

/**
* Loads all assets contained within this config class
Expand Down

0 comments on commit 31f6545

Please sign in to comment.