diff --git a/core/assets/roof.png b/core/assets/roof.png new file mode 100644 index 0000000..21534ab Binary files /dev/null and b/core/assets/roof.png differ diff --git a/core/assets/sand.png b/core/assets/sand.png index a690716..3d354ca 100644 Binary files a/core/assets/sand.png and b/core/assets/sand.png differ diff --git a/core/assets/wall.png b/core/assets/wall.png new file mode 100644 index 0000000..df0596d Binary files /dev/null and b/core/assets/wall.png differ diff --git a/core/src/com/mygdx/game/Character/InputController.java b/core/src/com/mygdx/game/Character/InputController.java index 1d807ad..66ca8b7 100644 --- a/core/src/com/mygdx/game/Character/InputController.java +++ b/core/src/com/mygdx/game/Character/InputController.java @@ -3,6 +3,8 @@ package com.mygdx.game.Character; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.g2d.Animation; +import com.badlogic.gdx.math.Vector3; +import com.mygdx.game.Dimension.Chunks; import com.mygdx.game.Restrictions; import static com.mygdx.game.Main.cam; @@ -21,61 +23,62 @@ public class InputController implements Restrictions { if (Gdx.input.isKeyPressed(Input.Keys.Q)) { cam.zoom -= 5; } + if (Gdx.input.isTouched()) { + Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); + cam.unproject(mousePos); + int x = Mouse.getSelectedX(mousePos) >> TILE_SHIFT; + int y = Mouse.getSelectedY(mousePos) >> TILE_SHIFT; + + Chunks.placeBlock(x,y,"wood"); + } if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) { i = 0; directionAnimation("UpLeft"); Player.addX(-MOVEMENT_SPEED); Player.addY(MOVEMENT_SPEED); - cam.translate(-MOVEMENT_SPEED,MOVEMENT_SPEED); - } - else if (Gdx.input.isKeyPressed(Input.Keys.UP)&& Gdx.input.isKeyPressed(Input.Keys.RIGHT) &&i > KEY_DELAY) { + cam.translate(-MOVEMENT_SPEED, MOVEMENT_SPEED); + } else if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.RIGHT) && i > KEY_DELAY) { i = 0; directionAnimation("UpRight"); Player.addX(MOVEMENT_SPEED); Player.addY(MOVEMENT_SPEED); - cam.translate(MOVEMENT_SPEED,MOVEMENT_SPEED); - } - else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) { + cam.translate(MOVEMENT_SPEED, MOVEMENT_SPEED); + } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) { i = 0; directionAnimation("DownLeft"); Player.addX(-MOVEMENT_SPEED); Player.addY(-MOVEMENT_SPEED); - cam.translate(-MOVEMENT_SPEED,-MOVEMENT_SPEED); - } - else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& Gdx.input.isKeyPressed(Input.Keys.RIGHT) && i > KEY_DELAY) { + cam.translate(-MOVEMENT_SPEED, -MOVEMENT_SPEED); + } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyPressed(Input.Keys.RIGHT) && i > KEY_DELAY) { i = 0; directionAnimation("DownRight"); Player.addX(MOVEMENT_SPEED); Player.addY(-MOVEMENT_SPEED); - cam.translate(MOVEMENT_SPEED,-MOVEMENT_SPEED); - } - - else if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) { + cam.translate(MOVEMENT_SPEED, -MOVEMENT_SPEED); + } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) { i = 0; directionAnimation("Left"); Player.addX(-MOVEMENT_SPEED); - cam.translate(-MOVEMENT_SPEED,0); - } - else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)&& i > KEY_DELAY) { + cam.translate(-MOVEMENT_SPEED, 0); + } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && i > KEY_DELAY) { i = 0; directionAnimation("Right"); Player.addX(MOVEMENT_SPEED); - cam.translate(MOVEMENT_SPEED,0); - } - else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& i > KEY_DELAY) { + cam.translate(MOVEMENT_SPEED, 0); + } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && i > KEY_DELAY) { i = 0; directionAnimation("Down"); Player.addY(-MOVEMENT_SPEED); - cam.translate(0,-MOVEMENT_SPEED); + cam.translate(0, -MOVEMENT_SPEED); } - if (Gdx.input.isKeyPressed(Input.Keys.UP)&& i > KEY_DELAY) { + if (Gdx.input.isKeyPressed(Input.Keys.UP) && i > KEY_DELAY) { i = 0; directionAnimation("Up"); Player.addY(MOVEMENT_SPEED); - cam.translate(0,MOVEMENT_SPEED); + cam.translate(0, MOVEMENT_SPEED); } cam.update(); diff --git a/core/src/com/mygdx/game/Character/Mouse.java b/core/src/com/mygdx/game/Character/Mouse.java index 9b75805..e10a87a 100644 --- a/core/src/com/mygdx/game/Character/Mouse.java +++ b/core/src/com/mygdx/game/Character/Mouse.java @@ -12,7 +12,6 @@ import static com.mygdx.game.Main.cam; public class Mouse implements Restrictions { Texture crosshair = new Texture("core/assets/crosshair.png"); - public void selectedTile(SpriteBatch batch){ Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0); cam.unproject(mousePos); @@ -21,11 +20,11 @@ public class Mouse implements Restrictions { batch.draw(crosshair, x, y); } - public int getSelectedX(Vector3 mousePos) { + public static int getSelectedX(Vector3 mousePos) { return ((int)(mousePos.x)>>4)<<4; } - public int getSelectedY(Vector3 mousePos) { + public static int getSelectedY(Vector3 mousePos) { return ((int)(mousePos.y)>>4)<<4; } @@ -41,7 +40,6 @@ public class Mouse implements Restrictions { return (getSelectedY(mousePos) + Player.getY()>>1)/TILE_SIZE; } - public void render(SpriteBatch batch) { selectedTile(batch); //System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam)); diff --git a/core/src/com/mygdx/game/Dimension/Block.java b/core/src/com/mygdx/game/Dimension/Block.java index cdb93b0..b88c6f8 100644 --- a/core/src/com/mygdx/game/Dimension/Block.java +++ b/core/src/com/mygdx/game/Dimension/Block.java @@ -1,19 +1,31 @@ package com.mygdx.game.Dimension; +import com.badlogic.gdx.graphics.Texture; + public class Block { private String name; + private Texture texture; private Boolean harvestable; private Boolean breakable; private Boolean passable; public Block(String name) { this.name = name; - this.harvestable = harvestable; - this.breakable = breakable; - this.passable = passable; + this.texture = new Texture("core/assets/" + name + ".png"); + this.harvestable = true; + this.breakable = true; + this.passable = false; } public String getName() { return name; } + + public Texture getTexture() { + return texture; + } + + public void setTexture(Texture texture) { + this.texture = texture; + } } diff --git a/core/src/com/mygdx/game/Dimension/BlockMaterials.java b/core/src/com/mygdx/game/Dimension/BlockMaterials.java index 97e0953..7e42cad 100644 --- a/core/src/com/mygdx/game/Dimension/BlockMaterials.java +++ b/core/src/com/mygdx/game/Dimension/BlockMaterials.java @@ -1,20 +1,13 @@ package com.mygdx.game.Dimension; -import com.badlogic.gdx.graphics.Texture; - import java.util.HashMap; public class BlockMaterials { public static HashMap materials = new HashMap<>(); - public static HashMap textures = new HashMap<>(); //Private so the singleton can't be instantiated private BlockMaterials() {} - public static Texture getTexture(String material) { - return textures.get(material); - } - public static void create(){ materials.put("grass",new Block("grass")); materials.put("wood",new Block("wood")); @@ -23,13 +16,7 @@ public class BlockMaterials { materials.put("snow",new Block("snow")); materials.put("sand",new Block("sand")); materials.put("air",new Block("air")); - - textures.put("grass", new Texture("core/assets/grass.png")); - textures.put("wood", new Texture("core/assets/wood.png")); - textures.put("water", new Texture("core/assets/water.png")); - textures.put("stone", new Texture("core/assets/stone.png")); - textures.put("snow", new Texture("core/assets/snow.png")); - textures.put("sand", new Texture("core/assets/sand.png")); - textures.put("air", new Texture("core/assets/air.png")); + materials.put("roof",new Block("roof")); + materials.put("wall",new Block("wall")); } } diff --git a/core/src/com/mygdx/game/Dimension/Chunks.java b/core/src/com/mygdx/game/Dimension/Chunks.java index beab482..319adfe 100644 --- a/core/src/com/mygdx/game/Dimension/Chunks.java +++ b/core/src/com/mygdx/game/Dimension/Chunks.java @@ -1,6 +1,7 @@ //https://www.redblobgames.com/maps/terrain-from-noise/ package com.mygdx.game.Dimension; +import com.github.czyzby.kiwi.util.tuple.immutable.Pair; import com.github.czyzby.kiwi.util.tuple.immutable.Triple; import com.mygdx.game.OpenSimplexNoise; @@ -8,9 +9,28 @@ import java.util.HashMap; import static com.mygdx.game.Restrictions.*; -public class Chunks { +public class Chunks{ + + public static void create(){ + int i = 10; + setBlock(3+i,2,"wall"); + setBlock(4+i,2,"wall"); + setBlock(3+i,3,"roof"); + setBlock(4+i,3,"roof"); + } + + public static void setBlock(int x, int y, String name){ + buildings.put(new Pair<>(x,y),new Block(name)); + } + + public static void placeBlock(int x, int y, String name){ + Triple triple = new Triple<>(x, y,1); + Chunks.blocks.replace(triple, Chunks.blocks.get(triple), new Block(name)); + } + public static HashMap, Block> blocks = new HashMap<>(); - private long seed = SEED; + + public static HashMap, Block> buildings = new HashMap<>(); public void ungenerateChunk(int x, int y) { int startX = x << CHUNK_SHIFT; @@ -56,21 +76,17 @@ public class Chunks { } public Block getBuildings(int x, int y) { - if(x == 2 && y == 2) return new Block("wood"); - if(x == 2 && y == 1) return new Block("wood"); - if(x == 1 && y == 2) return new Block("wood"); - if(x == 1 && y == 1) return new Block("wood"); - return new Block("air"); + return buildings.getOrDefault(new Pair<>(x,y),new Block("air")); } public double noise1(double nx, double ny) { - OpenSimplexNoise gen1 = new OpenSimplexNoise(seed+1); + OpenSimplexNoise gen1 = new OpenSimplexNoise(SEED+1); return gen1.eval(nx, ny)/2 + 0.5; } public double noise2(double nx, double ny) { - OpenSimplexNoise gen2 = new OpenSimplexNoise(seed); + OpenSimplexNoise gen2 = new OpenSimplexNoise(SEED); return gen2.eval(nx, ny)/2 + 0.5; } @@ -153,7 +169,8 @@ public class Chunks { return "TropicalRainForest"; } - public Boolean isEmpty(int x, int y){ + public static Boolean isEmpty(int x, int y){ return blocks.get(new Triple<>(x * 8, y * 8,0)) == null; } + } diff --git a/core/src/com/mygdx/game/Dimension/World.java b/core/src/com/mygdx/game/Dimension/World.java index 748f80a..5c52aac 100644 --- a/core/src/com/mygdx/game/Dimension/World.java +++ b/core/src/com/mygdx/game/Dimension/World.java @@ -56,4 +56,3 @@ public class World { } } - diff --git a/core/src/com/mygdx/game/Dimension/WorldRenderer.java b/core/src/com/mygdx/game/Dimension/WorldRenderer.java index 0c50d83..10323eb 100644 --- a/core/src/com/mygdx/game/Dimension/WorldRenderer.java +++ b/core/src/com/mygdx/game/Dimension/WorldRenderer.java @@ -20,11 +20,9 @@ public class WorldRenderer implements Restrictions { public void drawWorld(Batch batch, int layer) { for (Triple chunkpair : Chunks.blocks.keySet()) { - String name = null; if(chunkpair.getThird() == layer) { - name = Chunks.blocks.get(chunkpair).getName(); batch.draw( - BlockMaterials.getTexture(name), + Chunks.blocks.get(chunkpair).getTexture(), chunkpair.getFirst() << TILE_SHIFT, chunkpair.getSecond() << TILE_SHIFT ); @@ -33,9 +31,9 @@ public class WorldRenderer implements Restrictions { } public void render(SpriteBatch batch) { + inputController.handleInput(); drawWorld(batch,0); drawWorld(batch,1); - inputController.handleInput(); batch.setProjectionMatrix(cam.combined); cam.update(); mouse.render(batch); diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index 42b0fda..fcc2d53 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -9,6 +9,7 @@ import com.badlogic.gdx.utils.viewport.FitViewport; import com.mygdx.game.Character.Mouse; import com.mygdx.game.Character.Player; import com.mygdx.game.Dimension.BlockMaterials; +import com.mygdx.game.Dimension.Chunks; import com.mygdx.game.Dimension.World; import com.mygdx.game.Dimension.WorldRenderer; import com.mygdx.game.UI.GUI; @@ -31,6 +32,7 @@ public class Main extends ApplicationAdapter { cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam); BlockMaterials.create(); + Chunks.create(); //Starting location of the player cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1); diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java index bf33889..31e7c78 100644 --- a/core/src/com/mygdx/game/Restrictions.java +++ b/core/src/com/mygdx/game/Restrictions.java @@ -6,7 +6,7 @@ public interface Restrictions { float VIEWPORT_HEIGHT = 9/2f; float VIEWPORT_WIDTH = 16/2f; - int MOVEMENT_SPEED = 1; + int MOVEMENT_SPEED = 2; int KEY_DELAY = 0; int TILE_SIZE = 16; @@ -19,5 +19,5 @@ public interface Restrictions { //int SUPER_CHUNK_SHIFT = 0; int RENDER_DISTANCE = 3; - float RENDER_TIME = 1f; + float RENDER_TIME = 1/60f; } diff --git a/core/src/com/mygdx/game/Storage.java b/core/src/com/mygdx/game/Storage.java new file mode 100644 index 0000000..59b41b2 --- /dev/null +++ b/core/src/com/mygdx/game/Storage.java @@ -0,0 +1,4 @@ +package com.mygdx.game; + +public class Storage { +}