Optimized a MASSIVE amount of chunk generation code
This commit is contained in:
@@ -17,31 +17,4 @@ public class Block {
|
|||||||
return name;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,16 +10,13 @@ import static com.mygdx.game.Restrictions.*;
|
|||||||
|
|
||||||
public class Chunks {
|
public class Chunks {
|
||||||
public static HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
|
public static HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
|
||||||
private long seed = 1;
|
private long seed = SEED;
|
||||||
|
|
||||||
|
|
||||||
public void generateChunk(int x, int y){
|
public void generateChunk(int x, int y){
|
||||||
int startX = x << CHUNK_SHIFT;
|
int startX = x << CHUNK_SHIFT;
|
||||||
int startY = y << CHUNK_SHIFT;
|
int startY = y << CHUNK_SHIFT;
|
||||||
int startZ = 0;
|
|
||||||
int endX = startX + CHUNK_SIZE;
|
int endX = startX + CHUNK_SIZE;
|
||||||
int endY = startY + 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
|
//Going from start of selected chunk to end of selected chunk in x and y
|
||||||
for (int i = startX; i != endX; i++){
|
for (int i = startX; i != endX; i++){
|
||||||
@@ -41,8 +38,12 @@ public class Chunks {
|
|||||||
return gen2.eval(nx, ny)/2 + 0.5;
|
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) {
|
public Block getTerrain(int x, int y) {
|
||||||
double moisture, elevation,nx,ny = 0.0;
|
double moisture, elevation,nx,ny;
|
||||||
double scale = 0.01;
|
double scale = 0.01;
|
||||||
nx = x * scale;
|
nx = x * scale;
|
||||||
ny = y * scale;
|
ny = y * scale;
|
||||||
@@ -89,7 +90,6 @@ public class Chunks {
|
|||||||
return BlockMaterials.materials.get("water");
|
return BlockMaterials.materials.get("water");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getBiome(double moisture,double elevation){
|
public String getBiome(double moisture,double elevation){
|
||||||
if (elevation < 0.1) return "Ocean";
|
if (elevation < 0.1) return "Ocean";
|
||||||
if (elevation < 0.12) return "Beach";
|
if (elevation < 0.12) return "Beach";
|
||||||
|
|||||||
@@ -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<Pair<Integer, Integer>, Chunks> overworld = new HashMap<>();
|
|
||||||
public static HashMap<Pair<Integer, Integer>, Chunks> loadedChunks = new HashMap<>();
|
|
||||||
|
|
||||||
private Superchunks() {}
|
|
||||||
|
|
||||||
public static void create(){
|
|
||||||
BlockMaterials.create();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void generateSuperchunk(int x, int y) {
|
|
||||||
Chunks chunks = null;
|
|
||||||
Pair<Integer, Integer> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
26
core/src/com/mygdx/game/Dimension/World.java
Normal file
26
core/src/com/mygdx/game/Dimension/World.java
Normal file
@@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,7 +8,8 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|||||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||||
import com.mygdx.game.Character.Mouse;
|
import com.mygdx.game.Character.Mouse;
|
||||||
import com.mygdx.game.Character.Player;
|
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.Dimension.WorldRenderer;
|
||||||
import com.mygdx.game.UI.GUI;
|
import com.mygdx.game.UI.GUI;
|
||||||
|
|
||||||
@@ -30,7 +31,7 @@ public class Main extends ApplicationAdapter {
|
|||||||
Gdx.graphics.setWindowedMode(1024,576);
|
Gdx.graphics.setWindowedMode(1024,576);
|
||||||
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
|
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
|
||||||
fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam);
|
fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam);
|
||||||
Superchunks.create();
|
BlockMaterials.create();
|
||||||
|
|
||||||
//Starting location of the player
|
//Starting location of the player
|
||||||
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
|
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
|
||||||
@@ -57,7 +58,7 @@ public class Main extends ApplicationAdapter {
|
|||||||
delta +=Gdx.graphics.getDeltaTime();
|
delta +=Gdx.graphics.getDeltaTime();
|
||||||
if(delta > RENDER_TIME) {
|
if(delta > RENDER_TIME) {
|
||||||
delta -= RENDER_TIME;
|
delta -= RENDER_TIME;
|
||||||
Superchunks.loadChunks();
|
World.loadChunks();
|
||||||
}
|
}
|
||||||
|
|
||||||
gui.render(batch);
|
gui.render(batch);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.mygdx.game;
|
package com.mygdx.game;
|
||||||
|
|
||||||
public interface Restrictions {
|
public interface Restrictions {
|
||||||
|
int SEED = 1;
|
||||||
|
|
||||||
float VIEWPORT_HEIGHT = 9/2f;
|
float VIEWPORT_HEIGHT = 9/2f;
|
||||||
float VIEWPORT_WIDTH = 16/2f;
|
float VIEWPORT_WIDTH = 16/2f;
|
||||||
int MOVEMENT_SPEED = 2;
|
int MOVEMENT_SPEED = 2;
|
||||||
@@ -16,6 +18,6 @@ public interface Restrictions {
|
|||||||
int CHUNK_HEIGHT_SHIFT = 2;
|
int CHUNK_HEIGHT_SHIFT = 2;
|
||||||
int SUPER_CHUNK_SHIFT = 0;
|
int SUPER_CHUNK_SHIFT = 0;
|
||||||
|
|
||||||
int RENDER_DISTANCE = 2;
|
int RENDER_DISTANCE = 3;
|
||||||
float RENDER_TIME = 1f;
|
float RENDER_TIME = 1f;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user