Implemented a chunk system and also updated to lwjgl3
This commit is contained in:
@@ -1,5 +1,26 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
39
core/src/com/mygdx/game/Chunkloading.java
Normal file
39
core/src/com/mygdx/game/Chunkloading.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -18,20 +18,20 @@ public class Main extends ApplicationAdapter {
|
||||
|
||||
@Override
|
||||
public void create () {
|
||||
World world = new World();
|
||||
Chunkloading chunkloading = new Chunkloading();
|
||||
Player player = new Player(0, 0);
|
||||
mouse = new Mouse(player);
|
||||
cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
|
||||
cam.translate(TILE_SIZE/2,TILE_SIZE/2);
|
||||
cam.zoom = 25f;
|
||||
worldRenderer = new WorldRenderer(world, player, mouse,cam);
|
||||
worldRenderer = new WorldRenderer(chunkloading, player, mouse,cam);
|
||||
batch = new SpriteBatch();
|
||||
gui = new GUI(mouse,cam);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render () {
|
||||
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
batch.begin();
|
||||
worldRenderer.render(batch);
|
||||
gui.render(batch);
|
||||
|
||||
@@ -33,7 +33,8 @@ public class Mouse implements Restrictions {
|
||||
public int getTileX(OrthographicCamera cam){
|
||||
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
|
||||
cam.unproject(mousePos);
|
||||
return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE;
|
||||
return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE
|
||||
;
|
||||
}
|
||||
|
||||
public int getTileY(OrthographicCamera cam){
|
||||
|
||||
@@ -6,4 +6,5 @@ public interface Restrictions {
|
||||
float VIEWPORT_WIDTH = 16/2f;
|
||||
int MOVEMENT_SPEED = 16;
|
||||
int KEY_DELAY = 20;
|
||||
int CHUNK_SIZE = 16;
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -3,25 +3,25 @@ package com.mygdx.game;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
|
||||
public class WorldRenderer implements Restrictions {
|
||||
private Mouse mouse;
|
||||
private OrthographicCamera cam;
|
||||
private World world;
|
||||
private Player player;
|
||||
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.world = world;
|
||||
this.chunkloading = chunkloading;
|
||||
this.mouse = mouse;
|
||||
this.cam = cam;
|
||||
inputController = new InputController(this.cam,this.player);
|
||||
}
|
||||
|
||||
public void render(SpriteBatch batch){
|
||||
drawWorld(batch, player);
|
||||
chunkloading.render();
|
||||
drawWorld(batch,player);
|
||||
inputController.handleInput();
|
||||
batch.setProjectionMatrix(cam.combined);
|
||||
cam.update();
|
||||
@@ -29,16 +29,16 @@ public class WorldRenderer implements Restrictions {
|
||||
player.render(batch);
|
||||
}
|
||||
|
||||
|
||||
private void drawWorld(Batch batch,Player player) {
|
||||
for(int x = -10; x<10; x++) {
|
||||
for(int y = -10; y<10; y++) {
|
||||
for(int x = 0; x<CHUNK_SIZE; x++) {
|
||||
for(int y = 0; y<CHUNK_SIZE; y++) {
|
||||
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){}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dispose(){
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user