2025-03-09 15:28:01 +01:00

65 lines
2.0 KiB
Java

package cz.jzitnik.game.generation;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import java.util.List;
import java.util.Random;
public class Trees {
public static boolean isTreeNearby(List<Block>[][] world, int x, int y) {
int radius = 3; // Check within a 3-block radius for trees
for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < 512 && ny >= 0 && ny < 256) {
for (Block block : world[ny][nx]) {
if (block.getBlockId().equals("oak_log") || block.getBlockId().equals("oak_leaves")) {
return true;
}
}
}
}
}
return false;
}
public static void plantTrees(List<Block>[][] world, int[] terrainHeight) {
Random random = new Random();
for (int i = 10; i < 502; i += random.nextInt(20) + 20) {
int treeBase = terrainHeight[i];
if (treeBase - 3 < 0)
continue;
tree(world, i, treeBase);
}
}
public static void tree(List<Block>[][] world, int i, int treeBase) {
for (int j = 0; j < 3; j++) {
if (treeBase - j >= 0) {
world[treeBase - j - 1][i].add(ItemBlockSupplier.getBlock("oak_log"));
}
}
int leafY = treeBase - 4;
for (int layer = 0; layer < 3; layer++) {
int size = 5 - (layer * 2);
int offsetY = leafY - layer;
for (int dx = -size / 2; dx <= size / 2; dx++) {
int x = i + dx;
int y = offsetY;
if (x >= 0 && x < world[0].length && y >= 0) {
world[y][x].add(ItemBlockSupplier.getBlock("oak_leaves"));
}
}
}
}
}