From 180aad05decc7eefa87e4e45d6747c48f40e5361 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 17 Apr 2023 12:12:01 -0400 Subject: save --- .../net/tylermurphy/Minecraft/Scene/World.java | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 src/main/java/net/tylermurphy/Minecraft/Scene/World.java (limited to 'src/main/java/net/tylermurphy/Minecraft/Scene/World.java') diff --git a/src/main/java/net/tylermurphy/Minecraft/Scene/World.java b/src/main/java/net/tylermurphy/Minecraft/Scene/World.java new file mode 100755 index 0000000..d54ed92 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Scene/World.java @@ -0,0 +1,89 @@ +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()); + } + +} \ No newline at end of file -- cgit v1.2.3-freya