From 75f1c26349df17faf41dfe6728664b60a03f792a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 25 May 2020 20:44:20 +0200 Subject: [PATCH] add ability to see which tile was selected when you click on it --- .../src/netwerkprog/game/client/MainGame.java | 2 +- .../game/client/game/map/MapRenderer.java | 28 ++++++------------- .../game/client/map/GameInputProcessor.java | 20 +++++++++++-- .../src/netwerkprog/game/client/map/Tile.java | 17 ++++++++++- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/core/src/netwerkprog/game/client/MainGame.java b/core/src/netwerkprog/game/client/MainGame.java index 16a53ad..1a3396a 100644 --- a/core/src/netwerkprog/game/client/MainGame.java +++ b/core/src/netwerkprog/game/client/MainGame.java @@ -27,7 +27,7 @@ public class MainGame extends ApplicationAdapter{ private GameInputProcessor gameInputProcessor; private Map map; - private MapRenderer mapRenderer; + public MapRenderer mapRenderer; diff --git a/core/src/netwerkprog/game/client/game/map/MapRenderer.java b/core/src/netwerkprog/game/client/game/map/MapRenderer.java index d6ca77b..ff59fdc 100644 --- a/core/src/netwerkprog/game/client/game/map/MapRenderer.java +++ b/core/src/netwerkprog/game/client/game/map/MapRenderer.java @@ -52,11 +52,11 @@ public class MapRenderer implements Renderable { x = 0; for (int col = 0; col < map.getWidth(); col++) { if (map.get(row, col) == ' ') { - tiles[row][col] = new Tile(FLOOR_TILE,x,y); + tiles[row][col] = new Tile(FLOOR_TILE,x,y, ' '); } else if (map.get(row, col) == '#') { - tiles[row][col] = new Tile(WALL_TILE,x,y); + tiles[row][col] = new Tile(WALL_TILE,x,y, '#'); } else if (map.get(row, col) == 'x') { - tiles[row][col] = new Tile(PATH_TILE,x,y); + tiles[row][col] = new Tile(PATH_TILE,x,y, 'x'); } x += 32; } @@ -83,26 +83,14 @@ public class MapRenderer implements Renderable { public void render() { batch.begin(); batch.setProjectionMatrix(camera.combined); -// for (int row = map.getHeight(); row >= 0; row--) { -// y += 32; -// x = 0; -// for (int col = 0; col < map.getWidth(); col++) { -// if (map.get(row, col) == ' ') { -// batch.draw(FLOOR_TILE, x, y); -// } else if (map.get(row, col) == '#') { -// batch.draw(WALL_TILE, x, y); -// } else if (map.get(row, col) == 'x') { -// batch.draw(PATH_TILE, x, y); -// } -// x += 32; - // } -// } - for (int row = 0; row < tiles.length; row++) { + + for (Tile[] tileRow : tiles) { for (int col = 0; col < tiles[0].length; col++) { - Tile cur = tiles[row][col]; - batch.draw(cur.getTextureRegion(),cur.x,cur.y); + Tile cur = tileRow[col]; + batch.draw(cur.getTextureRegion(), cur.x, cur.y); } } + batch.end(); x = 0; y = 0; diff --git a/core/src/netwerkprog/game/client/map/GameInputProcessor.java b/core/src/netwerkprog/game/client/map/GameInputProcessor.java index ebd2fcf..0d69417 100644 --- a/core/src/netwerkprog/game/client/map/GameInputProcessor.java +++ b/core/src/netwerkprog/game/client/map/GameInputProcessor.java @@ -1,9 +1,11 @@ package netwerkprog.game.client.map; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.TimeUtils; import netwerkprog.game.client.MainGame; import netwerkprog.game.util.application.InputTransform; @@ -89,7 +91,7 @@ public class GameInputProcessor implements InputProcessor { @Override public boolean keyUp(int keycode) { - System.out.println(camera.position.x + " , " + camera.position.y); +// System.out.println(camera.position.x + " , " + camera.position.y); if (keysList.contains(keycode)) { if (keycode == keysList.get(0)) { this.isWPressed = false; @@ -120,8 +122,20 @@ public class GameInputProcessor implements InputProcessor { @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { - float pointerX = InputTransform.getCursorToModelX((int)game.getScreenWidth(), (int)game.getScreenWidth(),screenX); - float pointerY = InputTransform.getCursorToModelY((int)game.getScreenHeight(),(int)game.getScreenHeight(),screenY); + + Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); + camera.unproject(touchPoint); + + for (int row = 0; row < game.mapRenderer.getTiles().length; row++) { + for (int col = 0; col < game.mapRenderer.getTiles()[0].length; col++) { + Tile tile = game.mapRenderer.getTiles()[row][col]; + if (tile.contains(touchPoint.x,touchPoint.y)) { + System.out.println(tile + " row: " + row + ", col: " + col); + //TODO make stuff happen with the tile + return true; + } + } + } return false; } diff --git a/core/src/netwerkprog/game/client/map/Tile.java b/core/src/netwerkprog/game/client/map/Tile.java index 00c9a9f..2e2dbc3 100644 --- a/core/src/netwerkprog/game/client/map/Tile.java +++ b/core/src/netwerkprog/game/client/map/Tile.java @@ -5,9 +5,11 @@ import com.badlogic.gdx.math.Rectangle; public class Tile extends Rectangle{ private TextureRegion textureRegion; + private char symbol; - public Tile(TextureRegion textureRegion, int xPos, int yPos) { + public Tile(TextureRegion textureRegion, int xPos, int yPos, char symbol) { this.textureRegion = textureRegion; + this.symbol = symbol; super.x = xPos; super.y = yPos; super.width = textureRegion.getRegionWidth(); @@ -17,4 +19,17 @@ public class Tile extends Rectangle{ public TextureRegion getTextureRegion() { return textureRegion; } + + public char getSymbol() { + return symbol; + } + + @Override + public String toString() { + return "Tile{" + + "symbol=" + symbol + + ", x=" + x + + ", y=" + y + + '}'; + } }