From 2476be3808bb0116a818bea52d368d0cac45bab6 Mon Sep 17 00:00:00 2001 From: Solargale Date: Tue, 31 Mar 2020 21:09:21 -0600 Subject: [PATCH] Moved the properties of Blocks into the class Block. Also made Blocks and Superchunks singletons, to prevent the creation of unnecessary objects --- build.gradle | 5 +- core/src/com/mygdx/game/Character/Player.java | 10 ++- core/src/com/mygdx/game/Dimension/Block.java | 63 +++++++++++++++++++ core/src/com/mygdx/game/Dimension/Blocks.java | 14 +++-- core/src/com/mygdx/game/Dimension/Chunks.java | 14 ++--- .../com/mygdx/game/Dimension/Superchunks.java | 38 +++++++++++ .../mygdx/game/Dimension/WorldRenderer.java | 26 +++++--- core/src/com/mygdx/game/Dimension/Worlds.java | 29 --------- core/src/com/mygdx/game/Main.java | 58 +++++++++++------ core/src/com/mygdx/game/Restrictions.java | 1 + .../mygdx/game/desktop/DesktopLauncher.java | 2 - 11 files changed, 182 insertions(+), 78 deletions(-) create mode 100644 core/src/com/mygdx/game/Dimension/Block.java create mode 100644 core/src/com/mygdx/game/Dimension/Superchunks.java delete mode 100644 core/src/com/mygdx/game/Dimension/Worlds.java diff --git a/build.gradle b/build.gradle index 88b2205..16e210c 100644 --- a/build.gradle +++ b/build.gradle @@ -77,6 +77,7 @@ project(":core") { api "net.dermetfan.libgdx-utils:libgdx-utils-box2d:0.13.4" api "de.tomgrill.gdxdialogs:gdx-dialogs-core:1.2.5" api "com.github.czyzby:gdx-kiwi:1.9.1.9.6" - + } -} + +} \ No newline at end of file diff --git a/core/src/com/mygdx/game/Character/Player.java b/core/src/com/mygdx/game/Character/Player.java index 8ab5510..1bfb63f 100644 --- a/core/src/com/mygdx/game/Character/Player.java +++ b/core/src/com/mygdx/game/Character/Player.java @@ -1,5 +1,6 @@ package com.mygdx.game.Character; +import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.TextureAtlas; @@ -12,9 +13,12 @@ public class Player implements Restrictions { private TextureAtlas textureAtlas; private Animation animation; - public Player(int x, int y) { - this.x = 0; - this.y = 0; + public Player(int x, int y, OrthographicCamera cam) { + this.x = x*TILE_SIZE; + this.y = y*TILE_SIZE; + + cam.translate(this.x,this.y); + this.spriteName = "man"; this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas"); Array keyFrames = textureAtlas.findRegions(spriteName + "_Down"); diff --git a/core/src/com/mygdx/game/Dimension/Block.java b/core/src/com/mygdx/game/Dimension/Block.java new file mode 100644 index 0000000..2931303 --- /dev/null +++ b/core/src/com/mygdx/game/Dimension/Block.java @@ -0,0 +1,63 @@ +package com.mygdx.game.Dimension; + +public class Block { + private String name; + private Boolean floor, wall, harvestable, breakable, passable; + + public Block(String name, Boolean floor, Boolean wall, Boolean harvestable, Boolean breakable, Boolean passable) { + this.name = name; + this.floor = floor; + this.wall = wall; + this.harvestable = harvestable; + this.breakable = breakable; + this.passable = passable; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Boolean getFloor() { + return floor; + } + + public void setFloor(Boolean floor) { + this.floor = floor; + } + + public Boolean getWall() { + return wall; + } + + public void setWall(Boolean wall) { + this.wall = wall; + } + + 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; + } +} diff --git a/core/src/com/mygdx/game/Dimension/Blocks.java b/core/src/com/mygdx/game/Dimension/Blocks.java index 481f4fe..851c806 100644 --- a/core/src/com/mygdx/game/Dimension/Blocks.java +++ b/core/src/com/mygdx/game/Dimension/Blocks.java @@ -1,19 +1,25 @@ 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 static HashMap materials = new HashMap<>(); + public static HashMap textures = new HashMap<>(); + + //Private so the singleton can't be instantiated + private Blocks() {} 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")); + materials.put("grass",new Block("grass",true,false,true,true,true)); + materials.put("wood",new Block("wood",false,true,true,true,false)); + + 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 index 47ac09f..030d868 100644 --- a/core/src/com/mygdx/game/Dimension/Chunks.java +++ b/core/src/com/mygdx/game/Dimension/Chunks.java @@ -7,9 +7,9 @@ import java.util.HashMap; import static com.mygdx.game.Restrictions.CHUNK_SIZE; public class Chunks { - public static HashMap, Blocks> blocks = new HashMap<>(); + public HashMap, Block> blocks = new HashMap<>(); - public Chunks(int x, int y) { + public void generateChunk(int x, int y){ int startX = x*CHUNK_SIZE; int startY = y*CHUNK_SIZE; int endX = startX + CHUNK_SIZE; @@ -18,20 +18,16 @@ public class Chunks { //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()); + blocks.put(new Pair<>(i, j), Blocks.materials.get("grass")); } } } - public static void create(){ - Blocks.create(); - } - - public HashMap, Blocks> getBlocks() { + public HashMap, Block> getBlocks() { return blocks; } - public void setBlocks(HashMap, Blocks> blocks) { + public void setBlocks(HashMap, Block> blocks) { this.blocks = blocks; } } diff --git a/core/src/com/mygdx/game/Dimension/Superchunks.java b/core/src/com/mygdx/game/Dimension/Superchunks.java new file mode 100644 index 0000000..c7be4fc --- /dev/null +++ b/core/src/com/mygdx/game/Dimension/Superchunks.java @@ -0,0 +1,38 @@ +package com.mygdx.game.Dimension; + +import net.dermetfan.utils.Pair; + +import java.util.HashMap; + +import static com.mygdx.game.Restrictions.RENDER_DISTANCE; +import static com.mygdx.game.Restrictions.SUPER_CHUNK_SIZE; + +public class Superchunks { + public static HashMap overworld = new HashMap<>(); + + private Superchunks() {} + + public static void generateSuperchunk(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++) { + Chunks chunks = new Chunks(); + Blocks.create(); + chunks.generateChunk(i, j); + overworld.put(new Pair<>(i, j), chunks); + } + } + } + + public static void generateWithPlayer(){ + for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE; i++) { + for (int j = -RENDER_DISTANCE; j < RENDER_DISTANCE; j++) { + generateSuperchunk(i,j); + } + } + } +} diff --git a/core/src/com/mygdx/game/Dimension/WorldRenderer.java b/core/src/com/mygdx/game/Dimension/WorldRenderer.java index ab0721c..878cf81 100644 --- a/core/src/com/mygdx/game/Dimension/WorldRenderer.java +++ b/core/src/com/mygdx/game/Dimension/WorldRenderer.java @@ -9,29 +9,35 @@ import com.mygdx.game.Character.Player; import com.mygdx.game.Restrictions; import net.dermetfan.utils.Pair; +import java.util.HashMap; + public class WorldRenderer implements Restrictions { private Mouse mouse; private OrthographicCamera cam; private Player player; private InputController inputController; - private Worlds worlds; - public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam, Worlds worlds) { + public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam) { this.player = player; this.mouse = mouse; this.cam = cam; - this.worlds = worlds; inputController = new InputController(this.cam, this.player); } - public void create(){ - worlds.create(); - } - + @SuppressWarnings("unchecked") 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); + for (Pair worldpair: Superchunks.overworld.keySet()) { + Chunks chunks = Superchunks.overworld.get(worldpair); + for (Pair chunkpair: chunks.blocks.keySet()) { + + HashMap, Block> blocks = chunks.getBlocks(); + Block chunkBlock = blocks.get(chunkpair); + String name = chunkBlock.getName(); + + int drawingLocationX = chunkpair.getKey() * TILE_SIZE; + int drawingLocationY = chunkpair.getValue()* TILE_SIZE; + + batch.draw(Blocks.textures.get(name),drawingLocationX ,drawingLocationY); } } } diff --git a/core/src/com/mygdx/game/Dimension/Worlds.java b/core/src/com/mygdx/game/Dimension/Worlds.java deleted file mode 100644 index 063990b..0000000 --- a/core/src/com/mygdx/game/Dimension/Worlds.java +++ /dev/null @@ -1,29 +0,0 @@ -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 3355543..e7d2a40 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -5,11 +5,14 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL30; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.ui.Label; +import com.badlogic.gdx.scenes.scene2d.ui.Table; 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.Dimension.Superchunks; import com.mygdx.game.Dimension.WorldRenderer; +import com.mygdx.game.UI.GUI; import static com.mygdx.game.Restrictions.*; @@ -19,7 +22,9 @@ public class Main extends ApplicationAdapter { private GUI gui; private Mouse mouse; private OrthographicCamera cam; - private Worlds worlds; + private Stage stage; + private Table table; + private Label label; 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 @@ -27,19 +32,31 @@ public class Main extends ApplicationAdapter { @Override public void 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; - 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(); + // + stage = new Stage(); + Gdx.input.setInputProcessor(stage); + + table = new Table(); + table.add(label); + table.setFillParent(true); + stage.addActor(table); + + table.setDebug(true); + + + + //Starting location of the player + Player player = new Player(0,0, cam); + + cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2); + cam.zoom = 25f; + + Superchunks.generateWithPlayer(); + + mouse = new Mouse(player); + worldRenderer = new WorldRenderer(player, mouse,cam); batch = new SpriteBatch(); gui = new GUI(mouse,cam); } @@ -48,16 +65,19 @@ public class Main extends ApplicationAdapter { 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 - } + fixedStep(); batch.begin(); worldRenderer.render(batch); gui.render(batch); batch.end(); - System.gc(); + } + + public void fixedStep() { + 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 + } } @Override diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java index 21c62b3..1e6b3e3 100644 --- a/core/src/com/mygdx/game/Restrictions.java +++ b/core/src/com/mygdx/game/Restrictions.java @@ -8,4 +8,5 @@ public interface Restrictions { int KEY_DELAY = 10; int CHUNK_SIZE = 16; int SUPER_CHUNK_SIZE = 3; + int RENDER_DISTANCE = 3; } diff --git a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java index 3acca1b..c475fb8 100644 --- a/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java +++ b/desktop/src/com/mygdx/game/desktop/DesktopLauncher.java @@ -1,7 +1,5 @@ package com.mygdx.game.desktop; -import com.badlogic.gdx.backends.lwjgl.LwjglApplication; -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;