FIGURED OUT HOW TO UNLOAD CHUNKS \o/

This commit is contained in:
2020-04-09 11:24:14 -06:00
parent 7ec0d5eb5c
commit f95e9bdacc
4 changed files with 55 additions and 3 deletions

View File

@@ -16,5 +16,4 @@ public class Block {
public String getName() { public String getName() {
return name; return name;
} }
} }

View File

@@ -12,6 +12,20 @@ public class Chunks {
public static HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>(); public static HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
private long seed = SEED; 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){ public void generateChunk(int x, int y){
int startX = x << CHUNK_SHIFT; int startX = x << CHUNK_SHIFT;
int startY = y << CHUNK_SHIFT; int startY = y << CHUNK_SHIFT;
@@ -119,5 +133,4 @@ public class Chunks {
if (moisture < 0.66) return "TropicalSeasonalForest"; if (moisture < 0.66) return "TropicalSeasonalForest";
return "TropicalRainForest"; return "TropicalRainForest";
} }
} }

View File

@@ -12,6 +12,13 @@ public class World {
} }
} }
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() { public static void loadChunks() {
for (int i = -(RENDER_DISTANCE); i < RENDER_DISTANCE; i++) { for (int i = -(RENDER_DISTANCE); i < RENDER_DISTANCE; i++) {
for (int j = -(RENDER_DISTANCE); j < RENDER_DISTANCE; j++) { 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
);
}
}
} }

View File

@@ -59,6 +59,7 @@ public class Main extends ApplicationAdapter {
if(delta > RENDER_TIME) { if(delta > RENDER_TIME) {
delta -= RENDER_TIME; delta -= RENDER_TIME;
World.loadChunks(); World.loadChunks();
World.unloadChunks();
} }
gui.render(batch); gui.render(batch);