fix: random neighbour field technically doesnt allow coordinates outside the map

This commit is contained in:
punchready 2021-07-24 22:29:57 +02:00
parent 23cceb9622
commit 4eb61a8f48
1 changed files with 8 additions and 3 deletions

View File

@ -710,18 +710,23 @@ public class GameLogic {
return options; return options;
} }
ArrayList<IntVector2> allOptions = new ArrayList<>();
for(IntVector2 dir: IntVector2.CardinalDirections) { for(IntVector2 dir: IntVector2.CardinalDirections) {
IntVector2 pos = start.copy().add(dir); IntVector2 pos = start.copy().add(dir);
if(pos.getX() < 0 || pos.getX() >= state.mapSize.getX() || pos.getY() < 0 || pos.getY() >= state.mapSize.getY()) { if(pos.getX() < 0 || pos.getX() >= state.mapSize.getX() || pos.getY() < 0 || pos.getY() >= state.mapSize.getY()) {
continue; continue;
} }
if(state.entities.findByPosition(pos).size() == 0) { allOptions.add(pos);
if(state.entities.findByPosition(pos).isEmpty()) {
options.add(pos); options.add(pos);
} }
} }
if(options.size() == 0) { if(options.isEmpty()) {
return getFreeNeighbour(state, start.copy().add(IntVector2.CardinalDirections[rand.nextInt(IntVector2.CardinalDirections.length)])); if(allOptions.isEmpty()) {
return allOptions;
}
return getFreeNeighbour(state, start.copy().add(allOptions.get(rand.nextInt(allOptions.size()))));
}else { }else {
return options; return options;
} }