package net.tylermurphy.Minecraft.Scene; import java.util.Collection; import java.util.HashMap; import java.util.stream.Collectors; import net.tylermurphy.Minecraft.Chunk.Cube; import net.tylermurphy.Minecraft.Scene.Objects.WorldOrigin; import net.tylermurphy.Minecraft.Chunk.Chunk; import net.tylermurphy.Minecraft.Tick.BlockUpdate; public class World { private static final HashMap chunks = new HashMap<>(); public static Camera camera; public static Player player; public static int seed; public static int renderDistance; public static WorldOrigin world_origin; public static byte getBlock(int x, int y, int z) { if(y<0 || y> 255) return Cube.AIR; int cx = (int)Math.floor(x/16.0) + world_origin.x()/16; int cz = (int)Math.floor(z/16.0) + world_origin.z()/16; Chunk chunk = getChunk(cx, cz); if(chunk==null) return Cube.NULL; int rx = x-16*(int)Math.floor(x/16.0); int rz = z-16*(int)Math.floor(z/16.0); return chunk.cubes[rx][y][rz]; } public static int getHighestBlock(int fx, int fz) { for(int y=255;y>=0;y--) { if(getBlock(fx,y,fz) != -1) return y; } return 255; } public static void setBlock(int x, int y, int z, byte id) { if(y<0 || y> 255) return; int cx = (int)Math.floor(x/16.0) + world_origin.x()/16; int cz = (int)Math.floor(z/16.0) + world_origin.z()/16; Chunk chunk = getChunk(cx, cz); if(chunk==null) return; int rx = x-16*(int)Math.floor(x/16.0); int rz = z-16*(int)Math.floor(z/16.0); BlockUpdate.createBlockUpdate(x, y, z, chunk.cubes[rx][y][rz], id); chunk.scheduleFutureMeshUpdate(); chunk.cubes[rx][y][rz] = id; if(id != -1 && y > 0 && chunk.cubes[rx][y - 1][rz] == 0) chunk.cubes[rx][y -1][rz] = 1; int ox = (rx%16) == 0 ? -1 : (rx%16) == 15 ? 1 : 0; int oz = (rz%16) == 0 ? -1 : (rz%16) == 15 ? 1 : 0; Chunk coc = null,coz = null; if(ox != 0) coc = getChunk(chunk.gridX + ox, chunk.gridZ); if(oz != 0) coz = getChunk(chunk.gridX, chunk.gridZ + oz); if(coc != null) { coc.scheduleFutureMeshUpdate(); } if(coz != null) { coz.scheduleFutureMeshUpdate(); } } public static Chunk getChunk(int x, int z){ Chunk c = chunks.get(x+":"+z); return c == null ? null : c.isSafe() ? c : null; } public static Chunk getChunkUnsafe(int x, int z){ return chunks.get(x+":"+z); } public static void addChunk(Chunk c){ chunks.put(c.gridX+":"+c.gridZ, c); } public static void removeChunk(int x, int z){ chunks.remove(x+":"+z); } public static Collection getChunks(){ return chunks.values().stream().filter(Chunk::isSafe).collect(Collectors.toList()); } }