add ability to see which tile was selected when you click on it

This commit is contained in:
Sem van der Hoeven
2020-05-25 20:44:20 +02:00
parent 6c85ae9949
commit 75f1c26349
4 changed files with 42 additions and 25 deletions

View File

@@ -27,7 +27,7 @@ public class MainGame extends ApplicationAdapter{
private GameInputProcessor gameInputProcessor;
private Map map;
private MapRenderer mapRenderer;
public MapRenderer mapRenderer;

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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 +
'}';
}
}