From aea9532480f7c4b2bca2707b5ec414269376f44b Mon Sep 17 00:00:00 2001 From: Solargale Date: Sat, 4 Apr 2020 03:42:55 -0600 Subject: [PATCH] Fixed chunkloading system, chunks are deleted if aren't visited after a long time --- .../mygdx/game/Character/InputController.java | 4 +-- .../com/mygdx/game/Dimension/Superchunks.java | 29 +++++++++++-------- .../mygdx/game/Dimension/WorldRenderer.java | 5 +++- core/src/com/mygdx/game/Main.java | 11 ++++--- core/src/com/mygdx/game/Restrictions.java | 2 +- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/core/src/com/mygdx/game/Character/InputController.java b/core/src/com/mygdx/game/Character/InputController.java index 4ee47c0..d841346 100644 --- a/core/src/com/mygdx/game/Character/InputController.java +++ b/core/src/com/mygdx/game/Character/InputController.java @@ -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) { diff --git a/core/src/com/mygdx/game/Dimension/Superchunks.java b/core/src/com/mygdx/game/Dimension/Superchunks.java index 89410e1..7cf0009 100644 --- a/core/src/com/mygdx/game/Dimension/Superchunks.java +++ b/core/src/com/mygdx/game/Dimension/Superchunks.java @@ -8,19 +8,20 @@ import java.util.HashMap; import static com.mygdx.game.Restrictions.*; public class Superchunks { - public static HashMap, Chunks> overworld = new HashMap<>(); + public static HashMap, 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.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(0player.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); } } + } } + diff --git a/core/src/com/mygdx/game/Dimension/WorldRenderer.java b/core/src/com/mygdx/game/Dimension/WorldRenderer.java index 7287954..112a5cb 100644 --- a/core/src/com/mygdx/game/Dimension/WorldRenderer.java +++ b/core/src/com/mygdx/game/Dimension/WorldRenderer.java @@ -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(); diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index fc7bd6d..e65314a 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -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); diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java index 6cf6b1d..ae69899 100644 --- a/core/src/com/mygdx/game/Restrictions.java +++ b/core/src/com/mygdx/game/Restrictions.java @@ -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; }