From 0e6d4ab00b7296331cf1b510535abf4d72d16cb6 Mon Sep 17 00:00:00 2001 From: Solargale Date: Thu, 2 Apr 2020 02:56:58 -0600 Subject: [PATCH] Added a chunkloading system but not a unloading system --- build.gradle | 3 +- .../mygdx/game/Character/InputController.java | 2 +- .../com/mygdx/game/Dimension/Superchunks.java | 41 +++++++++++++++---- core/src/com/mygdx/game/Main.java | 13 ++++-- core/src/com/mygdx/game/Restrictions.java | 3 +- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 16e210c..2979861 100644 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,8 @@ project(":desktop") { api "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion" api "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop" api "de.tomgrill.gdxdialogs:gdx-dialogs-desktop:1.2.5" - + compile "org.mini2Dx:universal-tween-engine:6.3.3" + } } diff --git a/core/src/com/mygdx/game/Character/InputController.java b/core/src/com/mygdx/game/Character/InputController.java index 7435628..4ee47c0 100644 --- a/core/src/com/mygdx/game/Character/InputController.java +++ b/core/src/com/mygdx/game/Character/InputController.java @@ -49,7 +49,7 @@ public class InputController implements Restrictions { i = 0; directionAnimation("Up"); player.addY(MOVEMENT_SPEED); - //Tween.to(player,Tween.) + cam.translate(0,MOVEMENT_SPEED); } cam.update(); diff --git a/core/src/com/mygdx/game/Dimension/Superchunks.java b/core/src/com/mygdx/game/Dimension/Superchunks.java index f3e5f49..89410e1 100644 --- a/core/src/com/mygdx/game/Dimension/Superchunks.java +++ b/core/src/com/mygdx/game/Dimension/Superchunks.java @@ -5,8 +5,7 @@ import net.dermetfan.utils.Pair; import java.util.HashMap; -import static com.mygdx.game.Restrictions.CHUNK_SIZE; -import static com.mygdx.game.Restrictions.SUPER_CHUNK_SIZE; +import static com.mygdx.game.Restrictions.*; public class Superchunks { public static HashMap, Chunks> overworld = new HashMap<>(); @@ -14,8 +13,8 @@ public class Superchunks { private Superchunks() {} public static void generateSuperchunk(int x, int y) { - int startX = x*SUPER_CHUNK_SIZE; - int startY = y*SUPER_CHUNK_SIZE; + int startX = x * SUPER_CHUNK_SIZE; + int startY = y * SUPER_CHUNK_SIZE; int endX = startX + SUPER_CHUNK_SIZE; int endY = startY + SUPER_CHUNK_SIZE; @@ -29,11 +28,37 @@ public class Superchunks { } } - public static void generateWithPlayer(Player player){ - for (int i = -1+player.getX()/CHUNK_SIZE*SUPER_CHUNK_SIZE; i < 1+player.getX()/CHUNK_SIZE*SUPER_CHUNK_SIZE; i++) { - for (int j = -1+player.getY()/CHUNK_SIZE*SUPER_CHUNK_SIZE; j < 1+player.getY()/CHUNK_SIZE*SUPER_CHUNK_SIZE; j++) { - generateSuperchunk(i,j); + + public static void loadChunks(Player player) { + int chunkY = 0; + int chunkX = 0; + //Top-right + 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()-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)); + 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); } } } + + } diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index 92b21b1..fc7bd6d 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -21,6 +21,8 @@ public class Main extends ApplicationAdapter { private FitViewport fitViewport; 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; @Override @@ -30,7 +32,7 @@ public class Main extends ApplicationAdapter { fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam); //Starting location of the player - Player player = new Player(0, 0, cam); + player = new Player(0, 0, cam); cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2); cam.zoom = 25f; @@ -38,9 +40,6 @@ public class Main extends ApplicationAdapter { Mouse mouse = new Mouse(player); gui = new GUI(mouse,fitViewport); - Superchunks.generateWithPlayer(player); - - worldRenderer = new WorldRenderer(player, mouse, cam); batch = new SpriteBatch(); } @@ -51,6 +50,11 @@ public class Main extends ApplicationAdapter { fixedStep(); + chunkSeconds +=Gdx.graphics.getRawDeltaTime(); + if(chunkSeconds > RENDER_TIME){ + chunkSeconds -= RENDER_TIME; + Superchunks.loadChunks(player); + } batch.begin(); worldRenderer.render(batch); @@ -58,6 +62,7 @@ public class Main extends ApplicationAdapter { batch.end(); } + public void fixedStep() { timeSinceLastUpdate = Gdx.graphics.getDeltaTime(); //Accumulate delta time while(timeSinceLastUpdate >= physicsUpdateSpeed){ //If the accumulated delta-time is greater than, do a physics update - this can happen multiple times in a single frame, depending on fps/lag/physicsUpdateSpeed diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java index 1e6b3e3..6cf6b1d 100644 --- a/core/src/com/mygdx/game/Restrictions.java +++ b/core/src/com/mygdx/game/Restrictions.java @@ -5,8 +5,9 @@ public interface Restrictions { float VIEWPORT_HEIGHT = 9/2f; float VIEWPORT_WIDTH = 16/2f; int MOVEMENT_SPEED = 16; - int KEY_DELAY = 10; + int KEY_DELAY = 1; int CHUNK_SIZE = 16; int SUPER_CHUNK_SIZE = 3; int RENDER_DISTANCE = 3; + int RENDER_TIME = 1; }