Implemented a chunk system and also updated to lwjgl3

This commit is contained in:
2020-03-29 07:53:52 -06:00
parent 47badea54b
commit 53b367cc0f
9 changed files with 82 additions and 63 deletions

View File

@@ -21,7 +21,7 @@ allprojects {
version = '1.0' version = '1.0'
ext { ext {
appName = "my-gdx-game" appName = "my-gdx-game"
gdxVersion = '1.9.10' gdxVersion = '1.9.11-SNAPSHOT'
roboVMVersion = '2.3.8' roboVMVersion = '2.3.8'
box2DLightsVersion = '1.4' box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0' ashleyVersion = '1.7.0'
@@ -44,7 +44,7 @@ project(":desktop") {
dependencies { dependencies {
implementation project(":core") implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop" api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop" api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"

View File

@@ -1,5 +1,26 @@
package com.mygdx.game; package com.mygdx.game;
public class Chunk { import com.badlogic.gdx.graphics.Texture;
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

@@ -0,0 +1,39 @@
package com.mygdx.game;
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 render(){
populateChunk(0,0);
}
}

View File

@@ -18,20 +18,20 @@ public class Main extends ApplicationAdapter {
@Override @Override
public void create () { public void create () {
World world = new World(); Chunkloading chunkloading = new Chunkloading();
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(world, player, mouse,cam); worldRenderer = new WorldRenderer(chunkloading, player, mouse,cam);
batch = new SpriteBatch(); batch = new SpriteBatch();
gui = new GUI(mouse,cam); gui = new GUI(mouse,cam);
} }
@Override @Override
public void render () { public void render () {
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin(); batch.begin();
worldRenderer.render(batch); worldRenderer.render(batch);
gui.render(batch); gui.render(batch);

View File

@@ -33,7 +33,8 @@ 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){

View File

@@ -6,4 +6,5 @@ public interface Restrictions {
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 = 20;
int CHUNK_SIZE = 16;
} }

View File

@@ -1,41 +0,0 @@
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import javafx.util.Pair;
import java.util.HashMap;
import java.util.Map;
public class World implements Restrictions{
Map<Pair<Integer,Integer>,Block> blocks;
public World(HashMap<Pair<Integer, Integer>, Block> blocks) {
this.blocks = blocks;
}
public World() {
this.blocks = new HashMap<>();
fillWorld();
}
public void addTile(int x, int y, Block block){
this.blocks.put(new Pair<>(x,y),block);
}
public Block getTile(int x, int y){
return this.blocks.get(new Pair<>(x,y));
}
public Texture getTileTexture(int x, int y){
return this.blocks.get(new Pair<>(x,y)).getTexture();
}
public void fillWorld(){
for(int x = -10; x< 10;x++){
for(int y = -10; y<10;y++){
this.addTile(x,y,new Block("grass"));
}
}
this.addTile(1,1 , new Block("wood"));
}
}

View File

@@ -3,25 +3,25 @@ package com.mygdx.game;
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;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
public class WorldRenderer implements Restrictions { public class WorldRenderer implements Restrictions {
private Mouse mouse; private Mouse mouse;
private OrthographicCamera cam; private OrthographicCamera cam;
private World world;
private Player player; private Player player;
private InputController inputController; private InputController inputController;
private Chunkloading chunkloading;
public WorldRenderer(World world, Player player, Mouse mouse, OrthographicCamera cam) { public WorldRenderer(Chunkloading chunkloading, Player player, Mouse mouse, OrthographicCamera cam) {
this.player = player; this.player = player;
this.world = world; this.chunkloading = chunkloading;
this.mouse = mouse; this.mouse = mouse;
this.cam = cam; this.cam = cam;
inputController = new InputController(this.cam,this.player); inputController = new InputController(this.cam,this.player);
} }
public void render(SpriteBatch batch){ public void render(SpriteBatch batch){
drawWorld(batch, player); chunkloading.render();
drawWorld(batch,player);
inputController.handleInput(); inputController.handleInput();
batch.setProjectionMatrix(cam.combined); batch.setProjectionMatrix(cam.combined);
cam.update(); cam.update();
@@ -29,16 +29,16 @@ public class WorldRenderer implements Restrictions {
player.render(batch); player.render(batch);
} }
private void drawWorld(Batch batch,Player player) { private void drawWorld(Batch batch,Player player) {
for(int x = -10; x<10; x++) { for(int x = 0; x<CHUNK_SIZE; x++) {
for(int y = -10; y<10; y++) { for(int y = 0; y<CHUNK_SIZE; y++) {
try { try {
batch.draw(world.getTile(x,y).getTexture(), (x * TILE_SIZE), (y * TILE_SIZE)); batch.draw(chunkloading.getChunk(0,0).getBlock(x,y).getTexture(), (x * TILE_SIZE), (y * TILE_SIZE));
} catch (Exception e){} } catch (Exception e){}
} }
} }
} }
public void dispose(){ public void dispose(){
} }
} }

View File

@@ -2,15 +2,13 @@ package com.mygdx.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.mygdx.game.Main; import com.mygdx.game.Main;
public class DesktopLauncher { public class DesktopLauncher {
public static void main (String[] arg) { public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.foregroundFPS = 60; new Lwjgl3Application(new Main(), config);
config.resizable = true;
config.width = 1280;
config.height = 720;
new LwjglApplication(new Main(), config);
} }
} }