Fixed chunkloading system, chunks are deleted if aren't visited after a long time
This commit is contained in:
@@ -21,10 +21,10 @@ public class InputController implements Restrictions {
|
|||||||
i++;
|
i++;
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
|
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
|
||||||
cam.zoom += 1;
|
cam.zoom += 5;
|
||||||
}
|
}
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
|
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
|
||||||
cam.zoom -= 1;
|
cam.zoom -= 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
|
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
|
||||||
|
|||||||
@@ -8,19 +8,20 @@ import java.util.HashMap;
|
|||||||
import static com.mygdx.game.Restrictions.*;
|
import static com.mygdx.game.Restrictions.*;
|
||||||
|
|
||||||
public class Superchunks {
|
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() {}
|
private Superchunks() {}
|
||||||
|
|
||||||
public static void generateSuperchunk(int x, int y) {
|
public static void generateSuperchunk(int x, int y) {
|
||||||
|
Chunks chunks;
|
||||||
int startX = x * SUPER_CHUNK_SIZE;
|
int startX = x * SUPER_CHUNK_SIZE;
|
||||||
int startY = y * SUPER_CHUNK_SIZE;
|
int startY = y * SUPER_CHUNK_SIZE;
|
||||||
int endX = startX + SUPER_CHUNK_SIZE;
|
int endX = startX + SUPER_CHUNK_SIZE;
|
||||||
int endY = startY + 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++) {
|
for (int j = startY; j != endY; j++) {
|
||||||
Chunks chunks = new Chunks();
|
chunks = new Chunks();
|
||||||
BlockMaterials.create();
|
BlockMaterials.create();
|
||||||
chunks.generateChunk(i, j);
|
chunks.generateChunk(i, j);
|
||||||
overworld.put(new Pair<>(i, j), chunks);
|
overworld.put(new Pair<>(i, j), chunks);
|
||||||
@@ -30,35 +31,39 @@ public class Superchunks {
|
|||||||
|
|
||||||
|
|
||||||
public static void loadChunks(Player player) {
|
public static void loadChunks(Player player) {
|
||||||
|
|
||||||
int chunkY = 0;
|
int chunkY = 0;
|
||||||
int chunkX = 0;
|
int chunkX = 0;
|
||||||
//Top-right
|
//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));
|
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||||
chunkX = (player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
chunkX = (player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||||
}
|
}
|
||||||
//Top-left
|
//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));
|
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
|
//Bottom-left
|
||||||
else if(0>=player.getX() && 0>=player.getY()){
|
else if (0 >= player.getX() && 0 >= player.getY()) {
|
||||||
chunkY = ((player.getY()-48*CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
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));
|
chunkX = ((player.getX() - 48 * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||||
}
|
}
|
||||||
//Bottom-right
|
//Bottom-right
|
||||||
else if(0<player.getX() && 0>player.getY()){
|
else if (0 < player.getX() && 0 > player.getY()) {
|
||||||
chunkY = ((player.getY()-48*CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
chunkY = ((player.getY() - 48 * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||||
chunkX = ((player.getX()) / (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 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++) {
|
||||||
generateSuperchunk(chunkX+i,chunkY+j);
|
generateSuperchunk(chunkX + i, chunkY + j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,10 @@ public class WorldRenderer implements Restrictions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void render(SpriteBatch batch) {
|
public void render(SpriteBatch batch) {
|
||||||
|
try {
|
||||||
drawWorld(batch);
|
drawWorld(batch);
|
||||||
|
}catch (Exception e){}
|
||||||
|
|
||||||
inputController.handleInput();
|
inputController.handleInput();
|
||||||
batch.setProjectionMatrix(cam.combined);
|
batch.setProjectionMatrix(cam.combined);
|
||||||
cam.update();
|
cam.update();
|
||||||
|
|||||||
@@ -19,9 +19,9 @@ public class Main extends ApplicationAdapter {
|
|||||||
private WorldRenderer worldRenderer;
|
private WorldRenderer worldRenderer;
|
||||||
private GUI gui;
|
private GUI gui;
|
||||||
private FitViewport fitViewport;
|
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 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
|
float timeSinceLastUpdate = 0f; //accumulator
|
||||||
private float chunkSeconds;
|
|
||||||
private Player player;
|
private Player player;
|
||||||
|
|
||||||
|
|
||||||
@@ -50,12 +50,15 @@ public class Main extends ApplicationAdapter {
|
|||||||
|
|
||||||
fixedStep();
|
fixedStep();
|
||||||
|
|
||||||
chunkSeconds +=Gdx.graphics.getRawDeltaTime();
|
delta +=Gdx.graphics.getDeltaTime();
|
||||||
if(chunkSeconds > RENDER_TIME){
|
if(delta > RENDER_TIME) {
|
||||||
chunkSeconds -= RENDER_TIME;
|
delta -= RENDER_TIME;
|
||||||
|
Superchunks.overworld.clear();
|
||||||
Superchunks.loadChunks(player);
|
Superchunks.loadChunks(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
batch.begin();
|
batch.begin();
|
||||||
worldRenderer.render(batch);
|
worldRenderer.render(batch);
|
||||||
gui.render(batch);
|
gui.render(batch);
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ public interface Restrictions {
|
|||||||
int CHUNK_SIZE = 16;
|
int CHUNK_SIZE = 16;
|
||||||
int SUPER_CHUNK_SIZE = 3;
|
int SUPER_CHUNK_SIZE = 3;
|
||||||
int RENDER_DISTANCE = 3;
|
int RENDER_DISTANCE = 3;
|
||||||
int RENDER_TIME = 1;
|
float RENDER_TIME = 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user