diff --git a/core/assets/crosshair.png b/core/assets/crosshair.png new file mode 100644 index 0000000..5635c25 Binary files /dev/null and b/core/assets/crosshair.png differ diff --git a/core/assets/grass.png b/core/assets/grass.png index f1b5151..1ae9b3b 100644 Binary files a/core/assets/grass.png and b/core/assets/grass.png differ diff --git a/core/assets/man.atlas b/core/assets/man.atlas new file mode 100644 index 0000000..ae74293 --- /dev/null +++ b/core/assets/man.atlas @@ -0,0 +1,229 @@ +man.png +size: 256, 128 +format: RGBA8888 +filter: Nearest,Nearest +repeat: none +man_Up + rotate: false + xy: 0,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_Up + rotate: false + xy: 32,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_Up + rotate: false + xy: 64,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_Up + rotate: false + xy: 96,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_Down + rotate: false + xy: 128,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_Down + rotate: false + xy: 160,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_Down + rotate: false + xy: 192,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_Down + rotate: false + xy: 224,0 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_Left + rotate: false + xy: 0,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_Left + rotate: false + xy: 32,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_Left + rotate: false + xy: 64,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_Left + rotate: false + xy: 96,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_Right + rotate: false + xy: 128,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_Right + rotate: false + xy: 160,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_Right + rotate: false + xy: 192,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_Right + rotate: false + xy: 224,32 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_UpLeft + rotate: false + xy: 0,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_UpLeft + rotate: false + xy: 32,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_UpLeft + rotate: false + xy: 64,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_UpLeft + rotate: false + xy: 96,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_UpRight + rotate: false + xy: 128,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_UpRight + rotate: false + xy: 160,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_UpRight + rotate: false + xy: 192,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_UpRight + rotate: false + xy: 224,64 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_DownLeft + rotate: false + xy: 0,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_DownLeft + rotate: false + xy: 32,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_DownLeft + rotate: false + xy: 64,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_DownLeft + rotate: false + xy: 96,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 +man_DownRight + rotate: false + xy: 128,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 0 +man_DownRight + rotate: false + xy: 160,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 1 +man_DownRight + rotate: false + xy: 192,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 2 +man_DownRight + rotate: false + xy: 224,96 + size: 32,32 + orig: 32,32 + offset: 0,0 + index: 3 diff --git a/core/assets/man.png b/core/assets/man.png new file mode 100644 index 0000000..aa2beb9 Binary files /dev/null and b/core/assets/man.png differ diff --git a/core/assets/wood.png b/core/assets/wood.png new file mode 100644 index 0000000..78c96e1 Binary files /dev/null and b/core/assets/wood.png differ diff --git a/core/src/com/mygdx/game/Chunk.java b/core/src/com/mygdx/game/Chunk.java new file mode 100644 index 0000000..9b4ad8a --- /dev/null +++ b/core/src/com/mygdx/game/Chunk.java @@ -0,0 +1,5 @@ +package com.mygdx.game; + +public class Chunk { + +} diff --git a/core/src/com/mygdx/game/GUI.java b/core/src/com/mygdx/game/GUI.java new file mode 100644 index 0000000..cff7127 --- /dev/null +++ b/core/src/com/mygdx/game/GUI.java @@ -0,0 +1,27 @@ +package com.mygdx.game; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; + +public class GUI { + private BitmapFont font = new BitmapFont(); + private Mouse mouse; + private OrthographicCamera cam; + + public GUI(Mouse mouse, OrthographicCamera cam) { + this.font = new BitmapFont(); + this.mouse = mouse; + this.cam = cam; + } + + public void render(SpriteBatch batch){ + font.draw(batch, "x: "+ mouse.getTileX(cam) + "y: " + mouse.getTileY(cam),-10, 0); + } + + public void dispose(){ + font.dispose(); + } +} diff --git a/core/src/com/mygdx/game/InputController.java b/core/src/com/mygdx/game/InputController.java index 20c21f0..dc1b002 100644 --- a/core/src/com/mygdx/game/InputController.java +++ b/core/src/com/mygdx/game/InputController.java @@ -3,42 +3,61 @@ package com.mygdx.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.graphics.g2d.Animation; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; -public class InputController { +public class InputController implements Restrictions { private OrthographicCamera cam; + private Player player; + private Animation animation; + float pressTime, unpressTime; + int i = 0; - public InputController(OrthographicCamera cam) { + public InputController(OrthographicCamera cam, Player player) { this.cam = cam; + this.player = player; } public void handleInput() { + i++; + if (Gdx.input.isKeyPressed(Input.Keys.A)) { - cam.zoom += 0.02; + cam.zoom += 1; } if (Gdx.input.isKeyPressed(Input.Keys.Q)) { - cam.zoom -= 0.02; - } - if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - cam.translate(-3, 0, 0); - } - if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - cam.translate(3, 0, 0); - } - if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) { - cam.translate(0, -3, 0); - } - if (Gdx.input.isKeyPressed(Input.Keys.UP)) { - cam.translate(0, 3, 0); + cam.zoom -= 1; } - cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth); + if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) { + i = 0; + directionAnimation("Left"); + player.addX(-MOVEMENT_SPEED); + cam.translate(-MOVEMENT_SPEED,0); + } + if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)&& i > KEY_DELAY) { + i = 0; + directionAnimation("Right"); + player.addX(MOVEMENT_SPEED); + cam.translate(MOVEMENT_SPEED,0); + } + if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& i > KEY_DELAY) { + i = 0; + directionAnimation("Down"); + player.addY(-MOVEMENT_SPEED); + cam.translate(0,-MOVEMENT_SPEED); + } + if (Gdx.input.isKeyPressed(Input.Keys.UP)&& i > KEY_DELAY) { + i = 0; + directionAnimation("Up"); + player.addY(MOVEMENT_SPEED); + cam.translate(0,MOVEMENT_SPEED); + } + cam.update(); + } - float effectiveViewportWidth = cam.viewportWidth * cam.zoom; - float effectiveViewportHeight = cam.viewportHeight * cam.zoom; - cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f); - cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f); + public void directionAnimation(String direction) { + player.setAnimation(new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + direction))); } } diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index 91b4703..f8e3e49 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -3,36 +3,45 @@ package com.mygdx.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.GL30; +import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import static com.mygdx.game.Restrictions.*; + public class Main extends ApplicationAdapter { private SpriteBatch batch; private WorldRenderer worldRenderer; + private GUI gui; + private Mouse mouse; + private OrthographicCamera cam; @Override public void create () { - batch = new SpriteBatch(); - World world = new World(); - Player player = new Player(0, 0); - - worldRenderer = new WorldRenderer(world, player); + 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); + batch = new SpriteBatch(); + gui = new GUI(mouse,cam); } @Override public void render () { - Gdx.gl.glClearColor(0, 0, 0, 1); - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - + Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); batch.begin(); worldRenderer.render(batch); + gui.render(batch); batch.end(); } @Override public void dispose () { worldRenderer.dispose(); + gui.dispose(); batch.dispose(); } } diff --git a/core/src/com/mygdx/game/Mouse.java b/core/src/com/mygdx/game/Mouse.java new file mode 100644 index 0000000..bf61281 --- /dev/null +++ b/core/src/com/mygdx/game/Mouse.java @@ -0,0 +1,50 @@ +package com.mygdx.game; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; + +import static java.lang.Math.round; + +public class Mouse implements Restrictions { + Player player; + Texture crosshair = new Texture("core/assets/crosshair.png"); + + public Mouse(Player player) { + this.player = player; + } + + public void selectedTile(SpriteBatch batch, OrthographicCamera cam){ + Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0); + cam.unproject(mousePos); + batch.draw(crosshair, getSelectedX(mousePos), getSelectedY(mousePos)); + } + + public int getSelectedX(Vector3 mousePos) { + return round((mousePos.x-8)/16)*16; + } + + public int getSelectedY(Vector3 mousePos) { + return round((mousePos.y-8)/16)*16; + } + + 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; + } + + public int getTileY(OrthographicCamera cam){ + Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0); + cam.unproject(mousePos); + return (getSelectedY(mousePos) + player.getY()/2)/TILE_SIZE; + } + + + public void render(SpriteBatch batch, OrthographicCamera cam) { + selectedTile(batch,cam); + System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam)); + } +} diff --git a/core/src/com/mygdx/game/Player.java b/core/src/com/mygdx/game/Player.java index cdfaef8..abf0e3c 100644 --- a/core/src/com/mygdx/game/Player.java +++ b/core/src/com/mygdx/game/Player.java @@ -1,13 +1,29 @@ package com.mygdx.game; -import javafx.util.Pair; +import com.badlogic.gdx.graphics.g2d.Animation; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.utils.Array; -public class Player { +public class Player implements Restrictions { private int x,y = 0; + private String spriteName; + private TextureAtlas textureAtlas; + private Animation animation; - public Player(int x, int y) { - this.x = x; - this.y = y; + Player(int x, int y) { + this.x = 0; + this.y = 0; + this.spriteName = "man"; + this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas"); + Array keyFrames = textureAtlas.findRegions(spriteName + "_Down"); + float frameDuration = 1 / 4f; + this.animation = new Animation<>(frameDuration, keyFrames); + } + + public void render(Batch batch){ + float elapsedTime = 1f; + batch.draw(getAnimation().getKeyFrame(elapsedTime, true), getX(), getY(), TILE_SIZE, TILE_SIZE); } public int getX() { @@ -25,4 +41,32 @@ public class Player { public void setY(int y) { this.y = y; } + + public void addX(int x){ + this.x += x; + } + + public void addY(int y){ + this.y += y; + } + + String getSpriteName() { + return spriteName; + } + + public void setSpriteName(String tempSpriteName) { + spriteName = tempSpriteName; + } + + TextureAtlas getTextureAtlas() { + return textureAtlas; + } + + Animation getAnimation() { + return animation; + } + + void setAnimation(Animation animation) { + this.animation = animation; + } } diff --git a/core/src/com/mygdx/game/Restrictions.java b/core/src/com/mygdx/game/Restrictions.java new file mode 100644 index 0000000..79384c7 --- /dev/null +++ b/core/src/com/mygdx/game/Restrictions.java @@ -0,0 +1,9 @@ +package com.mygdx.game; + +public interface Restrictions { + int TILE_SIZE = 16; + float VIEWPORT_HEIGHT = 9/2f; + float VIEWPORT_WIDTH = 16/2f; + int MOVEMENT_SPEED = 16; + int KEY_DELAY = 20; +} diff --git a/core/src/com/mygdx/game/World.java b/core/src/com/mygdx/game/World.java index 8703036..b089bf3 100644 --- a/core/src/com/mygdx/game/World.java +++ b/core/src/com/mygdx/game/World.java @@ -6,7 +6,7 @@ import javafx.util.Pair; import java.util.HashMap; import java.util.Map; -public class World { +public class World implements Restrictions{ Map,Block> blocks; public World(HashMap, Block> blocks) { @@ -31,8 +31,11 @@ public class World { } public void fillWorld(){ - this.addTile(1,1,new Block("grass")); - this.addTile(0,0,new Block("grass")); + 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")); } - } diff --git a/core/src/com/mygdx/game/WorldRenderer.java b/core/src/com/mygdx/game/WorldRenderer.java index 3337be0..a6182f7 100644 --- a/core/src/com/mygdx/game/WorldRenderer.java +++ b/core/src/com/mygdx/game/WorldRenderer.java @@ -1,42 +1,44 @@ package com.mygdx.game; import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector3; -public class WorldRenderer { - private Texture texture; +public class WorldRenderer implements Restrictions { + private Mouse mouse; private OrthographicCamera cam; private World world; private Player player; private InputController inputController; - public WorldRenderer(World world, Player player) { + public WorldRenderer(World world, Player player, Mouse mouse, OrthographicCamera cam) { this.player = player; this.world = world; - - cam = new OrthographicCamera(10, 7); - inputController = new InputController(cam); + this.mouse = mouse; + this.cam = cam; + inputController = new InputController(this.cam,this.player); } - public void render(Batch batch){ + public void render(SpriteBatch batch){ + drawWorld(batch, player); inputController.handleInput(); batch.setProjectionMatrix(cam.combined); - drawWorld(batch, player); cam.update(); + mouse.render(batch,cam); + player.render(batch); } private void drawWorld(Batch batch,Player player) { - for(int x = player.getX()-10; x