forked from jzitnik/twodcraft
feat: Implemented grass growing
This commit is contained in:
parent
527cc0cf50
commit
cfe402ce43
@ -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<Block> 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user