diff --git a/core/src/com/mygdx/game/Dimension/Block.java b/core/src/com/mygdx/game/Dimension/Block.java index 2eddf09..4572a82 100644 --- a/core/src/com/mygdx/game/Dimension/Block.java +++ b/core/src/com/mygdx/game/Dimension/Block.java @@ -16,5 +16,4 @@ public class Block { public String getName() { return name; } - } diff --git a/core/src/com/mygdx/game/Dimension/Chunks.java b/core/src/com/mygdx/game/Dimension/Chunks.java index 63b6072..84f66fa 100644 --- a/core/src/com/mygdx/game/Dimension/Chunks.java +++ b/core/src/com/mygdx/game/Dimension/Chunks.java @@ -12,6 +12,20 @@ public class Chunks { public static HashMap, Block> blocks = new HashMap<>(); private long seed = SEED; + public void ungenerateChunk(int x, int y) { + int startX = x << CHUNK_SHIFT; + int startY = y << CHUNK_SHIFT; + int endX = startX + CHUNK_SIZE; + int endY = startY + CHUNK_SIZE; + + //Going from start of selected chunk to end of selected chunk in x and y + for (int i = startX; i != endX; i++){ + for (int j = startY; j != endY; j++) { + blocks.remove(new Pair<>(i, j), getTerrain(i, j)); + } + } + } + public void generateChunk(int x, int y){ int startX = x << CHUNK_SHIFT; int startY = y << CHUNK_SHIFT; @@ -119,5 +133,4 @@ public class Chunks { if (moisture < 0.66) return "TropicalSeasonalForest"; return "TropicalRainForest"; } - } diff --git a/core/src/com/mygdx/game/Dimension/World.java b/core/src/com/mygdx/game/Dimension/World.java index dc374f4..1e8705a 100644 --- a/core/src/com/mygdx/game/Dimension/World.java +++ b/core/src/com/mygdx/game/Dimension/World.java @@ -7,11 +7,18 @@ import static com.mygdx.game.Restrictions.*; public class World { public static void generateWorld(int x, int y) { Chunks chunks = new Chunks(); - if(chunks.isEmpty(x,y)){ + if (chunks.isEmpty(x, y)) { chunks.generateChunk(x, y); } } + public static void ungenerateWorld(int x, int y) { + Chunks chunks = new Chunks(); + if (!chunks.isEmpty(x, y)) { + chunks.ungenerateChunk(x, y); + } + } + public static void loadChunks() { for (int i = -(RENDER_DISTANCE); i < RENDER_DISTANCE; i++) { for (int j = -(RENDER_DISTANCE); j < RENDER_DISTANCE; j++) { @@ -22,5 +29,37 @@ public class World { } } } + + public static void unloadChunks() { + //Down + for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE+1; i++) { + ungenerateWorld( + Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)+i, + (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))+3 + ); + } + //Up + for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE+1; i++) { + ungenerateWorld( + Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)+i, + (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))-4 + ); + } + //Right + for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE+1; i++) { + ungenerateWorld( + Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)-4, + (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))+i + ); + } + //Left + for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE+1; i++) { + ungenerateWorld( + Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)+3, + (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))+i + ); + } + + } } diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index dc8bd9e..c7c96e3 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -59,6 +59,7 @@ public class Main extends ApplicationAdapter { if(delta > RENDER_TIME) { delta -= RENDER_TIME; World.loadChunks(); + World.unloadChunks(); } gui.render(batch);