diff --git a/src/main/java/cz/jzitnik/game/logic/services/grass/GrassGrowingLogic.java b/src/main/java/cz/jzitnik/game/logic/services/grass/GrassGrowingLogic.java new file mode 100644 index 0000000..282e122 --- /dev/null +++ b/src/main/java/cz/jzitnik/game/logic/services/grass/GrassGrowingLogic.java @@ -0,0 +1,49 @@ +package cz.jzitnik.game.logic.services.grass; + +import cz.jzitnik.game.Game; +import cz.jzitnik.game.annotations.CustomLogic; +import cz.jzitnik.game.entities.Block; +import cz.jzitnik.game.entities.items.ItemBlockSupplier; +import cz.jzitnik.game.logic.CustomLogicInterface; + +import java.util.*; + +@CustomLogic +public class GrassGrowingLogic implements CustomLogicInterface { + private static final int RADIUS = 35; + private final Random random = new Random(); + + @Override + public void nextIteration(Game game) { + int[] data = game.getPlayerCords(); + var world = game.getWorld(); + int playerX = data[0]; + int playerY = data[1]; + + int startX = Math.max(0, playerX - RADIUS); + int startY = Math.max(0, playerY - RADIUS); + int endX = Math.min(world[0].length - 1, playerX + RADIUS); + int endY = Math.min(world.length - 1, playerY + RADIUS); + + for (int x = startX; x <= endX; x++) { + for (int y = startY; y <= endY; y++) { + List blocks = world[y][x]; + if (blocks.stream().anyMatch(block -> block.getBlockId().equals("dirt"))) { + // Dirt + if (world[y - 1][x].stream().allMatch(block -> block.isGhost() || block.isMob() || block.getBlockId().equals("air")) && random.nextInt(3) < 1) { + world[y][x].removeIf(block -> block.getBlockId().equals("dirt")); + world[y][x].add(ItemBlockSupplier.getBlock("grass")); + } + } else if (blocks.stream().anyMatch(block -> block.getBlockId().equals("grass"))) { + // Grass + if (!world[y - 1][x].stream().allMatch(block -> block.isGhost() || block.isMob() || block.getBlockId().equals("air")) && random.nextInt(3) < 1) { + world[y][x].removeIf(block -> block.getBlockId().equals("grass")); + world[y][x].add(ItemBlockSupplier.getBlock("dirt")); + } + } + } + } + + } + +} \ No newline at end of file