Fixed chunkloading system, chunks are deleted if aren't visited after a long time

This commit is contained in:
2020-04-04 03:42:55 -06:00
parent 0e6d4ab00b
commit aea9532480
5 changed files with 31 additions and 20 deletions

View File

@@ -21,10 +21,10 @@ public class InputController implements Restrictions {
i++;
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
cam.zoom += 1;
cam.zoom += 5;
}
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
cam.zoom -= 1;
cam.zoom -= 5;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {

View File

@@ -8,19 +8,20 @@ import java.util.HashMap;
import static com.mygdx.game.Restrictions.*;
public class Superchunks {
public static HashMap<Pair<Integer,Integer>, Chunks> overworld = new HashMap<>();
public static HashMap<Pair<Integer, Integer>, Chunks> overworld = new HashMap<>();
private Superchunks() {}
public static void generateSuperchunk(int x, int y) {
Chunks chunks;
int startX = x * SUPER_CHUNK_SIZE;
int startY = y * SUPER_CHUNK_SIZE;
int endX = startX + SUPER_CHUNK_SIZE;
int endY = startY + SUPER_CHUNK_SIZE;
for (int i = startX; i != endX; i++){
for (int i = startX; i != endX; i++) {
for (int j = startY; j != endY; j++) {
Chunks chunks = new Chunks();
chunks = new Chunks();
BlockMaterials.create();
chunks.generateChunk(i, j);
overworld.put(new Pair<>(i, j), chunks);
@@ -30,35 +31,39 @@ public class Superchunks {
public static void loadChunks(Player player) {
int chunkY = 0;
int chunkX = 0;
//Top-right
if(0<=player.getX() && 0<=player.getY()) {
if (0 <= player.getX() && 0 <= player.getY()) {
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = (player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
}
//Top-left
else if(0>player.getX() && 0<player.getY()){
else if (0 > player.getX() && 0 < player.getY()) {
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = ((player.getX()-48*CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = ((player.getX() - 48 * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
}
//Bottom-left
else if(0>=player.getX() && 0>=player.getY()){
chunkY = ((player.getY()-48*CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = ((player.getX()-48*CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
else if (0 >= player.getX() && 0 >= player.getY()) {
chunkY = ((player.getY() - 48 * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = ((player.getX() - 48 * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
}
//Bottom-right
else if(0<player.getX() && 0>player.getY()){
chunkY = ((player.getY()-48*CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
else if (0 < player.getX() && 0 > player.getY()) {
chunkY = ((player.getY() - 48 * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = ((player.getX()) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
}
for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE; i++) {
for (int j = -RENDER_DISTANCE; j < RENDER_DISTANCE; j++) {
generateSuperchunk(chunkX+i,chunkY+j);
generateSuperchunk(chunkX + i, chunkY + j);
}
}
}
}

View File

@@ -42,7 +42,10 @@ public class WorldRenderer implements Restrictions {
}
public void render(SpriteBatch batch) {
drawWorld(batch);
try {
drawWorld(batch);
}catch (Exception e){}
inputController.handleInput();
batch.setProjectionMatrix(cam.combined);
cam.update();

View File

@@ -19,9 +19,9 @@ public class Main extends ApplicationAdapter {
private WorldRenderer worldRenderer;
private GUI gui;
private FitViewport fitViewport;
public static float delta = 0;
float physicsUpdateSpeed = 1/60f; //how often you want to do a physics-update, in this case every 1/60th of a sec
float timeSinceLastUpdate = 0f; //accumulator
private float chunkSeconds;
private Player player;
@@ -50,12 +50,15 @@ public class Main extends ApplicationAdapter {
fixedStep();
chunkSeconds +=Gdx.graphics.getRawDeltaTime();
if(chunkSeconds > RENDER_TIME){
chunkSeconds -= RENDER_TIME;
delta +=Gdx.graphics.getDeltaTime();
if(delta > RENDER_TIME) {
delta -= RENDER_TIME;
Superchunks.overworld.clear();
Superchunks.loadChunks(player);
}
batch.begin();
worldRenderer.render(batch);
gui.render(batch);

View File

@@ -9,5 +9,5 @@ public interface Restrictions {
int CHUNK_SIZE = 16;
int SUPER_CHUNK_SIZE = 3;
int RENDER_DISTANCE = 3;
int RENDER_TIME = 1;
float RENDER_TIME = 1;
}