Fixed the chunk rendering issues and crashes

This commit is contained in:
2020-03-30 20:45:48 -06:00
parent 6d98bc5996
commit 00609bc8ed
10 changed files with 120 additions and 132 deletions

View File

@@ -34,8 +34,7 @@ public class Mouse implements Restrictions {
public int getTileX(OrthographicCamera cam){ public int getTileX(OrthographicCamera cam){
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0); Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
cam.unproject(mousePos); cam.unproject(mousePos);
return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE;
;
} }
public int getTileY(OrthographicCamera cam){ public int getTileY(OrthographicCamera cam){
@@ -47,6 +46,6 @@ public class Mouse implements Restrictions {
public void render(SpriteBatch batch, OrthographicCamera cam) { public void render(SpriteBatch batch, OrthographicCamera cam) {
selectedTile(batch,cam); selectedTile(batch,cam);
System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam)); //System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam));
} }
} }

View File

@@ -0,0 +1,19 @@
package com.mygdx.game.Dimension;
import com.badlogic.gdx.graphics.Texture;
import net.dermetfan.utils.Pair;
import java.util.HashMap;
public class Blocks {
private static HashMap<String,Texture> textures = new HashMap<>();
public Texture getTexture(String material) {
return textures.get(material);
}
public static void create(){
textures.put("grass",new Texture("core/assets/grass.png"));
textures.put("wood",new Texture("core/assets/wood.png"));
}
}

View File

@@ -0,0 +1,37 @@
package com.mygdx.game.Dimension;
import net.dermetfan.utils.Pair;
import java.util.HashMap;
import static com.mygdx.game.Restrictions.CHUNK_SIZE;
public class Chunks {
public static HashMap<Pair<Integer,Integer>, Blocks> blocks = new HashMap<>();
public Chunks(int x, int y) {
int startX = x*CHUNK_SIZE;
int startY = y*CHUNK_SIZE;
int endX = startX + CHUNK_SIZE;
int endY = startY + CHUNK_SIZE;
//Going from start of selected chunk to end of selected chunk in x and y
for (int i = startX; i != endX; i++){
for (int j = startY; j != endY; j++) {
blocks.put(new Pair<>(i, j), new Blocks());
}
}
}
public static void create(){
Blocks.create();
}
public HashMap<Pair<Integer, Integer>, Blocks> getBlocks() {
return blocks;
}
public void setBlocks(HashMap<Pair<Integer, Integer>, Blocks> blocks) {
this.blocks = blocks;
}
}

View File

@@ -1,4 +1,4 @@
package com.mygdx.game.World; package com.mygdx.game.Dimension;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
@@ -7,37 +7,31 @@ import com.mygdx.game.Character.InputController;
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.Restrictions; import com.mygdx.game.Restrictions;
import net.dermetfan.utils.Pair;
public class WorldRenderer implements Restrictions { public class WorldRenderer implements Restrictions {
private Mouse mouse; private Mouse mouse;
private OrthographicCamera cam; private OrthographicCamera cam;
private Player player; private Player player;
private InputController inputController; private InputController inputController;
private Chunkloading chunkloading; private Worlds worlds;
public WorldRenderer(Chunkloading chunkloading, Player player, Mouse mouse, OrthographicCamera cam) { public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam, Worlds worlds) {
this.player = player; this.player = player;
this.chunkloading = chunkloading;
this.mouse = mouse; this.mouse = mouse;
this.cam = cam; this.cam = cam;
this.worlds = worlds;
inputController = new InputController(this.cam, this.player); inputController = new InputController(this.cam, this.player);
} }
private void drawWorld(Batch batch) { public void create(){
for (int i = 0; i != SUPER_CHUNK_SIZE; i++) { worlds.create();
for (int j = 0; j != SUPER_CHUNK_SIZE; j++) {
drawChunk(batch, i, j);
}
}
} }
private void drawChunk(Batch batch, int i, int j) { public void drawWorld(Batch batch) {
for (int x = 0; x < CHUNK_SIZE; x++) { for (Pair worldpair: Worlds.overworld.keySet()) {
for (int y = 0; y < CHUNK_SIZE; y++) { for (Pair<Integer,Integer> chunkpair:Chunks.blocks.keySet()) {
try { batch.draw( Worlds.overworld.get(worldpair).getBlocks().get(chunkpair).getTexture("grass"), chunkpair.getKey() * TILE_SIZE, chunkpair.getValue()* TILE_SIZE);
batch.draw(chunkloading.getChunk(i, j).getBlock(x+i*CHUNK_SIZE,y+j*CHUNK_SIZE).getTexture(), (x * TILE_SIZE), (y * TILE_SIZE));
} catch (Exception ignored) {
}
} }
} }
} }

View File

@@ -0,0 +1,29 @@
package com.mygdx.game.Dimension;
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;
public class Worlds {
public static HashMap<Pair, Chunks> overworld = new HashMap<>();
public Worlds(int x, int y) {
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++) {
overworld.put(new Pair<>(i, j), new Chunks(i,j));
}
}
}
public void create() {
Chunks.create();
}
}

View File

@@ -7,9 +7,9 @@ import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.Worlds;
import com.mygdx.game.UI.GUI; import com.mygdx.game.UI.GUI;
import com.mygdx.game.World.Chunkloading; import com.mygdx.game.Dimension.WorldRenderer;
import com.mygdx.game.World.WorldRenderer;
import static com.mygdx.game.Restrictions.*; import static com.mygdx.game.Restrictions.*;
@@ -19,20 +19,27 @@ public class Main extends ApplicationAdapter {
private GUI gui; private GUI gui;
private Mouse mouse; private Mouse mouse;
private OrthographicCamera cam; private OrthographicCamera cam;
private Worlds worlds;
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
@Override @Override
public void create () { public void create () {
Chunkloading chunkloading = new Chunkloading();
chunkloading.create();
Gdx.graphics.setWindowedMode(1024,576); Gdx.graphics.setWindowedMode(1024,576);
Player player = new Player(0, 0); Player player = new Player(0, 0);
mouse = new Mouse(player); mouse = new Mouse(player);
cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT); cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
cam.translate(TILE_SIZE/2,TILE_SIZE/2); cam.translate(TILE_SIZE/2,TILE_SIZE/2);
cam.zoom = 25f; cam.zoom = 25f;
worldRenderer = new WorldRenderer(chunkloading, player, mouse,cam); worlds = new Worlds(0,0);
worlds = new Worlds(-1,0);
worlds = new Worlds(0,-1);
worlds = new Worlds(-1,-1);
worlds.create();
worldRenderer = new WorldRenderer(player, mouse,cam, worlds);
worldRenderer.create();
batch = new SpriteBatch(); batch = new SpriteBatch();
gui = new GUI(mouse,cam); gui = new GUI(mouse,cam);
} }
@@ -40,10 +47,17 @@ public class Main extends ApplicationAdapter {
@Override @Override
public void render () { public void render () {
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
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
timeSinceLastUpdate -= physicsUpdateSpeed; //Subtract physicsUpdateSpeed from timeSinceLastUpdate - if timeSinceLastUpdate is STILL >= physicsUpdateSpeed, repeat, if not, the loop breaks
}
batch.begin(); batch.begin();
worldRenderer.render(batch); worldRenderer.render(batch);
gui.render(batch); gui.render(batch);
batch.end(); batch.end();
System.gc();
} }
@Override @Override

View File

@@ -5,8 +5,7 @@ public interface Restrictions {
float VIEWPORT_HEIGHT = 9/2f; float VIEWPORT_HEIGHT = 9/2f;
float VIEWPORT_WIDTH = 16/2f; float VIEWPORT_WIDTH = 16/2f;
int MOVEMENT_SPEED = 16; int MOVEMENT_SPEED = 16;
int KEY_DELAY = 20; int KEY_DELAY = 10;
int CHUNK_SIZE = 16; int CHUNK_SIZE = 16;
int SUPER_CHUNK_SIZE = 3; int SUPER_CHUNK_SIZE = 3;
} }

View File

@@ -1,33 +0,0 @@
package com.mygdx.game.World;
import com.badlogic.gdx.graphics.Texture;
public class Block {
private String material;
private Texture texture;
public Block(){
material = null;
}
public Block(String tempMaterial) {
material = tempMaterial;
texture = new Texture("core/assets/"+tempMaterial+".png");
}
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
public Texture getTexture() {
return texture;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
}

View File

@@ -1,25 +0,0 @@
package com.mygdx.game.World;
import net.dermetfan.utils.Pair;
import java.util.HashMap;
public class Chunk {
private HashMap<Pair<Integer,Integer>,Block> blocks= new HashMap<>();
public HashMap<Pair<Integer, Integer>, Block> getBlocks() {
return blocks;
}
public Block getBlock(int x, int y){
return blocks.get(new Pair<>(x, y));
}
public void setBlock(Block block, int x, int y){
blocks.put(new Pair<>(x, y),block);
}
public void setBlocks(HashMap<Pair<Integer, Integer>, Block> blocks) {
this.blocks = blocks;
}
}

View File

@@ -1,45 +0,0 @@
package com.mygdx.game.World;
import com.mygdx.game.Restrictions;
import net.dermetfan.utils.Pair;
import java.util.HashMap;
public class Chunkloading implements Restrictions {
private HashMap<Pair<Integer, Integer>, Chunk> chunkHashMap = new HashMap<>();
public Chunk getChunk(int x, int y){
return chunkHashMap.get(new Pair<>(x, y));
}
public void setChunk(Chunk chunk, int x, int y){
chunkHashMap.put(new Pair<>(x, y),chunk);
}
public HashMap<Pair<Integer, Integer>, Chunk> getChunkHashMap() {
return chunkHashMap;
}
public void setChunkHashMap(HashMap<Pair<Integer, Integer>, Chunk> chunkHashMap) {
this.chunkHashMap = chunkHashMap;
}
public void populateChunk(int x, int y){
setChunk(new Chunk(),x,y);
for(int i = 0; i != CHUNK_SIZE; i++) {
for(int j = 0; j != CHUNK_SIZE; j++) {
getChunk(x,y).setBlock(new Block("grass"), i, j);
}
}
}
public void create(){
for(int x = 0; x!=SUPER_CHUNK_SIZE; x++){
for(int y = 0; y!=SUPER_CHUNK_SIZE; y++){
populateChunk(x,y);
}
}
}
}