From fed335b2f6a9916b13f25facfdced13899521daf Mon Sep 17 00:00:00 2001 From: Solargale Date: Thu, 9 Apr 2020 10:48:43 -0600 Subject: [PATCH] Optimized a MASSIVE amount of chunk generation code --- core/src/com/mygdx/game/Dimension/Block.java | 27 -------- core/src/com/mygdx/game/Dimension/Chunks.java | 12 ++-- .../com/mygdx/game/Dimension/Superchunks.java | 63 ------------------- core/src/com/mygdx/game/Dimension/World.java | 26 ++++++++ core/src/com/mygdx/game/Main.java | 7 ++- core/src/com/mygdx/game/Restrictions.java | 4 +- 6 files changed, 39 insertions(+), 100 deletions(-) delete mode 100644 core/src/com/mygdx/game/Dimension/Superchunks.java create mode 100644 core/src/com/mygdx/game/Dimension/World.java diff --git a/core/src/com/mygdx/game/Dimension/Block.java b/core/src/com/mygdx/game/Dimension/Block.java index 84ce188..2eddf09 100644 --- a/core/src/com/mygdx/game/Dimension/Block.java +++ b/core/src/com/mygdx/game/Dimension/Block.java @@ -17,31 +17,4 @@ public class Block { return name; } - public void setName(String name) { - this.name = name; - } - - public Boolean getHarvestable() { - return harvestable; - } - - public void setHarvestable(Boolean harvestable) { - this.harvestable = harvestable; - } - - public Boolean getBreakable() { - return breakable; - } - - public void setBreakable(Boolean breakable) { - this.breakable = breakable; - } - - public Boolean getPassable() { - return passable; - } - - public void setPassable(Boolean passable) { - this.passable = passable; - } } diff --git a/core/src/com/mygdx/game/Dimension/Chunks.java b/core/src/com/mygdx/game/Dimension/Chunks.java index 8b51bf7..a453267 100644 --- a/core/src/com/mygdx/game/Dimension/Chunks.java +++ b/core/src/com/mygdx/game/Dimension/Chunks.java @@ -10,16 +10,13 @@ import static com.mygdx.game.Restrictions.*; public class Chunks { public static HashMap, Block> blocks = new HashMap<>(); - private long seed = 1; - + private long seed = SEED; public void generateChunk(int x, int y){ int startX = x << CHUNK_SHIFT; int startY = y << CHUNK_SHIFT; - int startZ = 0; int endX = startX + CHUNK_SIZE; int endY = startY + CHUNK_SIZE; - int endZ = startY + CHUNK_HEIGHT; //Going from start of selected chunk to end of selected chunk in x and y for (int i = startX; i != endX; i++){ @@ -41,8 +38,12 @@ public class Chunks { return gen2.eval(nx, ny)/2 + 0.5; } + public Boolean isEmpty(int x, int y){ + return blocks.get(new Pair<>(x * 8, y * 8)) == null; + } + public Block getTerrain(int x, int y) { - double moisture, elevation,nx,ny = 0.0; + double moisture, elevation,nx,ny; double scale = 0.01; nx = x * scale; ny = y * scale; @@ -89,7 +90,6 @@ public class Chunks { return BlockMaterials.materials.get("water"); } - public String getBiome(double moisture,double elevation){ if (elevation < 0.1) return "Ocean"; if (elevation < 0.12) return "Beach"; diff --git a/core/src/com/mygdx/game/Dimension/Superchunks.java b/core/src/com/mygdx/game/Dimension/Superchunks.java deleted file mode 100644 index fbdaefd..0000000 --- a/core/src/com/mygdx/game/Dimension/Superchunks.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.mygdx.game.Dimension; - -import com.mygdx.game.Character.Player; -import net.dermetfan.utils.Pair; - -import java.util.HashMap; - -import static com.mygdx.game.Restrictions.*; - -public class Superchunks { - public static HashMap, Chunks> overworld = new HashMap<>(); - public static HashMap, Chunks> loadedChunks = new HashMap<>(); - - private Superchunks() {} - - public static void create(){ - BlockMaterials.create(); - } - - public static void generateSuperchunk(int x, int y) { - Chunks chunks = null; - Pair pair = null; - 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 j = startY; j != endY; j++) { - pair = new Pair<>(i, j); - if(overworld.containsKey(pair)){ - chunks = overworld.get(pair); - } - else{ - chunks = new Chunks(); - chunks.generateChunk(i, j); - } - loadedChunks.put(pair,chunks); - } - } - overworld.putAll(loadedChunks); - } - - - public static void loadChunks() { - - int chunkY = Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE); - int chunkX = Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE); - - if (0 > Player.getX()) chunkX /= (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE); //Left - if (0 > Player.getY()) chunkY /= (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE); //Down - - 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/Dimension/World.java b/core/src/com/mygdx/game/Dimension/World.java new file mode 100644 index 0000000..dc374f4 --- /dev/null +++ b/core/src/com/mygdx/game/Dimension/World.java @@ -0,0 +1,26 @@ +package com.mygdx.game.Dimension; + +import com.mygdx.game.Character.Player; + +import static com.mygdx.game.Restrictions.*; + +public class World { + public static void generateWorld(int x, int y) { + Chunks chunks = new Chunks(); + if(chunks.isEmpty(x,y)){ + chunks.generateChunk(x, y); + } + } + + public static void loadChunks() { + for (int i = -(RENDER_DISTANCE); i < RENDER_DISTANCE; i++) { + for (int j = -(RENDER_DISTANCE); j < RENDER_DISTANCE; j++) { + generateWorld( + i + Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE), + j + Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) + ); + } + } + } +} + diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index 7d5d2ea..2007e10 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -8,7 +8,8 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.utils.viewport.FitViewport; import com.mygdx.game.Character.Mouse; import com.mygdx.game.Character.Player; -import com.mygdx.game.Dimension.Superchunks; +import com.mygdx.game.Dimension.BlockMaterials; +import com.mygdx.game.Dimension.World; import com.mygdx.game.Dimension.WorldRenderer; import com.mygdx.game.UI.GUI; @@ -30,7 +31,7 @@ public class Main extends ApplicationAdapter { Gdx.graphics.setWindowedMode(1024,576); cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam); - Superchunks.create(); + BlockMaterials.create(); //Starting location of the player cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1); @@ -57,7 +58,7 @@ public class Main extends ApplicationAdapter { delta +=Gdx.graphics.getDeltaTime(); if(delta > RENDER_TIME) { delta -= RENDER_TIME; - Superchunks.loadChunks(); + World.loadChunks(); } gui.render(batch); diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java index 4bc3adb..86b2706 100644 --- a/core/src/com/mygdx/game/Restrictions.java +++ b/core/src/com/mygdx/game/Restrictions.java @@ -1,6 +1,8 @@ package com.mygdx.game; public interface Restrictions { + int SEED = 1; + float VIEWPORT_HEIGHT = 9/2f; float VIEWPORT_WIDTH = 16/2f; int MOVEMENT_SPEED = 2; @@ -16,6 +18,6 @@ public interface Restrictions { int CHUNK_HEIGHT_SHIFT = 2; int SUPER_CHUNK_SHIFT = 0; - int RENDER_DISTANCE = 2; + int RENDER_DISTANCE = 3; float RENDER_TIME = 1f; }