diff --git a/core/src/com/mygdx/game/Character/Mouse.java b/core/src/com/mygdx/game/Character/Mouse.java index 4a64b32..90eacbf 100644 --- a/core/src/com/mygdx/game/Character/Mouse.java +++ b/core/src/com/mygdx/game/Character/Mouse.java @@ -34,8 +34,7 @@ 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){ @@ -47,6 +46,6 @@ public class Mouse implements Restrictions { public void render(SpriteBatch batch, OrthographicCamera cam) { selectedTile(batch,cam); - System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam)); + //System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam)); } } diff --git a/core/src/com/mygdx/game/Dimension/Blocks.java b/core/src/com/mygdx/game/Dimension/Blocks.java new file mode 100644 index 0000000..481f4fe --- /dev/null +++ b/core/src/com/mygdx/game/Dimension/Blocks.java @@ -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 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")); + } +} diff --git a/core/src/com/mygdx/game/Dimension/Chunks.java b/core/src/com/mygdx/game/Dimension/Chunks.java new file mode 100644 index 0000000..47ac09f --- /dev/null +++ b/core/src/com/mygdx/game/Dimension/Chunks.java @@ -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, 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, Blocks> getBlocks() { + return blocks; + } + + public void setBlocks(HashMap, Blocks> blocks) { + this.blocks = blocks; + } +} diff --git a/core/src/com/mygdx/game/World/WorldRenderer.java b/core/src/com/mygdx/game/Dimension/WorldRenderer.java similarity index 54% rename from core/src/com/mygdx/game/World/WorldRenderer.java rename to core/src/com/mygdx/game/Dimension/WorldRenderer.java index ebc8e0e..ab0721c 100644 --- a/core/src/com/mygdx/game/World/WorldRenderer.java +++ b/core/src/com/mygdx/game/Dimension/WorldRenderer.java @@ -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.g2d.Batch; @@ -7,37 +7,31 @@ import com.mygdx.game.Character.InputController; import com.mygdx.game.Character.Mouse; import com.mygdx.game.Character.Player; import com.mygdx.game.Restrictions; +import net.dermetfan.utils.Pair; public class WorldRenderer implements Restrictions { private Mouse mouse; private OrthographicCamera cam; private Player player; 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.chunkloading = chunkloading; this.mouse = mouse; this.cam = cam; + this.worlds = worlds; inputController = new InputController(this.cam, this.player); } - private void drawWorld(Batch batch) { - for (int i = 0; i != SUPER_CHUNK_SIZE; i++) { - for (int j = 0; j != SUPER_CHUNK_SIZE; j++) { - drawChunk(batch, i, j); - } - } + public void create(){ + worlds.create(); } - private void drawChunk(Batch batch, int i, int j) { - for (int x = 0; x < CHUNK_SIZE; x++) { - for (int y = 0; y < CHUNK_SIZE; y++) { - try { - 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) { - } + public void drawWorld(Batch batch) { + for (Pair worldpair: Worlds.overworld.keySet()) { + for (Pair chunkpair:Chunks.blocks.keySet()) { + batch.draw( Worlds.overworld.get(worldpair).getBlocks().get(chunkpair).getTexture("grass"), chunkpair.getKey() * TILE_SIZE, chunkpair.getValue()* TILE_SIZE); } } } diff --git a/core/src/com/mygdx/game/Dimension/Worlds.java b/core/src/com/mygdx/game/Dimension/Worlds.java new file mode 100644 index 0000000..063990b --- /dev/null +++ b/core/src/com/mygdx/game/Dimension/Worlds.java @@ -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 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(); + } +} diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index 807287f..3355543 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -7,9 +7,9 @@ import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.mygdx.game.Character.Mouse; import com.mygdx.game.Character.Player; +import com.mygdx.game.Dimension.Worlds; import com.mygdx.game.UI.GUI; -import com.mygdx.game.World.Chunkloading; -import com.mygdx.game.World.WorldRenderer; +import com.mygdx.game.Dimension.WorldRenderer; import static com.mygdx.game.Restrictions.*; @@ -19,20 +19,27 @@ public class Main extends ApplicationAdapter { private GUI gui; private Mouse mouse; 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 public void create () { - Chunkloading chunkloading = new Chunkloading(); - chunkloading.create(); - Gdx.graphics.setWindowedMode(1024,576); - 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(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(); gui = new GUI(mouse,cam); } @@ -40,10 +47,17 @@ public class Main extends ApplicationAdapter { @Override public void render () { 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(); worldRenderer.render(batch); gui.render(batch); batch.end(); + System.gc(); } @Override diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java index d93e3ad..21c62b3 100644 --- a/core/src/com/mygdx/game/Restrictions.java +++ b/core/src/com/mygdx/game/Restrictions.java @@ -5,8 +5,7 @@ public interface Restrictions { float VIEWPORT_HEIGHT = 9/2f; float VIEWPORT_WIDTH = 16/2f; int MOVEMENT_SPEED = 16; - int KEY_DELAY = 20; + int KEY_DELAY = 10; int CHUNK_SIZE = 16; int SUPER_CHUNK_SIZE = 3; - } diff --git a/core/src/com/mygdx/game/World/Block.java b/core/src/com/mygdx/game/World/Block.java deleted file mode 100644 index 39a038b..0000000 --- a/core/src/com/mygdx/game/World/Block.java +++ /dev/null @@ -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; - } -} diff --git a/core/src/com/mygdx/game/World/Chunk.java b/core/src/com/mygdx/game/World/Chunk.java deleted file mode 100644 index 67ab792..0000000 --- a/core/src/com/mygdx/game/World/Chunk.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.mygdx.game.World; - -import net.dermetfan.utils.Pair; - -import java.util.HashMap; - -public class Chunk { - private HashMap,Block> blocks= new HashMap<>(); - - public HashMap, 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, Block> blocks) { - this.blocks = blocks; - } -} diff --git a/core/src/com/mygdx/game/World/Chunkloading.java b/core/src/com/mygdx/game/World/Chunkloading.java deleted file mode 100644 index 3534501..0000000 --- a/core/src/com/mygdx/game/World/Chunkloading.java +++ /dev/null @@ -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, 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, Chunk> getChunkHashMap() { - return chunkHashMap; - } - - public void setChunkHashMap(HashMap, 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); - - } - } - } -}