From f9b78f423610ff60ffdd2b697b8eda34c6b00a06 Mon Sep 17 00:00:00 2001 From: jzitnik-dev Date: Thu, 13 Mar 2025 10:39:00 +0100 Subject: [PATCH] feat: Change grass logic to grow next to grass --- .../services/grass/GrassGrowingLogic.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) 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 index 282e122..c33c887 100644 --- a/src/main/java/cz/jzitnik/game/logic/services/grass/GrassGrowingLogic.java +++ b/src/main/java/cz/jzitnik/game/logic/services/grass/GrassGrowingLogic.java @@ -30,7 +30,7 @@ public class GrassGrowingLogic implements CustomLogicInterface { 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) { + if (world[y - 1][x].stream().allMatch(block -> block.isGhost() || block.isMob() || block.getBlockId().equals("air")) && random.nextInt(3) < 1 && isGrassBesides(world, x, y)) { world[y][x].removeIf(block -> block.getBlockId().equals("dirt")); world[y][x].add(ItemBlockSupplier.getBlock("grass")); } @@ -46,4 +46,35 @@ public class GrassGrowingLogic implements CustomLogicInterface { } -} \ No newline at end of file + private boolean isGrass(List[][] world, int x, int y) { + return world[y][x].stream().anyMatch(i -> i.getBlockId().equals("grass")); + } + + private boolean isGrassBesides(List[][] world, int x, int y) { + if (isGrass(world, x + 1, y)) { + return true; + } + + if (isGrass(world, x + 1, y + 1)) { + return true; + } + + if (isGrass(world, x + 1, y - 1)) { + return true; + } + + if (isGrass(world, x - 1, y)) { + return true; + } + + if (isGrass(world, x - 1, y + 1)) { + return true; + } + + if (isGrass(world, x - 1, y - 1)) { + return true; + } + + return false; + } +}