improved entity collision check while generating

This commit is contained in:
2025-10-04 00:36:50 +02:00
parent 81524cfecf
commit f26c3a9e6d

View File

@@ -117,29 +117,36 @@ class TurtleGame extends StatelessGame {
private void generateNewSnack() {
Pos spawnPosition;
boolean isInRadius, isEmpty;
boolean isInRadius, collides;
int counter = 0;
do {
if(counter > 200) return;
int x = this.rnd.nextInt(-this.radius+2, this.radius-2);
int z = this.rnd.nextInt(-this.radius+2, this.radius-2);
spawnPosition = new Pos(x, 1, z);
isInRadius = new Pos(x, 1, z).distance(0, 1, 0) < this.radius-2;
isEmpty = this.snacks.stream().noneMatch(entity -> entity.getPosition().sameBlock(x, 1, z));
counter++;
} while (!(isEmpty && isInRadius));
Entity snack = new Entity(EntityType.FALLING_BLOCK);
FallingBlockMeta meta = (FallingBlockMeta) snack.getEntityMeta();
meta.setBlock(Block.CHORUS_FLOWER);
snack.setInstance(this);
snack.teleport(spawnPosition.add(0.5, 0, 0.5));
do {
if(counter > 200) {
snack.remove();
return;
}
int x = this.rnd.nextInt(-this.radius+2, this.radius-2);
int z = this.rnd.nextInt(-this.radius+2, this.radius-2);
spawnPosition = new Pos(x, 1, z).add(0.5, 0, 0.5);
Pos checkPosition = spawnPosition;
isInRadius = checkPosition.distance(0, 1, 0) < this.radius-3;
collides = this.getEntities().stream()
.filter(entity -> !entity.equals(snack))
.anyMatch(entity -> snack.getBoundingBox().intersectBox(entity.getPosition().sub(checkPosition), entity.getBoundingBox()));
counter++;
} while (!isInRadius || collides);
snack.teleport(spawnPosition);
this.snacks.add(snack);
}
@Override
protected void onStart() {
for (int i = 0; i < 3; i++) {
for (int i = 0; i < 100; i++) {
this.generateNewSnack();
}
}