diff --git a/core/src/netwerkprog/game/client/MainGame.java b/core/src/netwerkprog/game/client/MainGame.java index 0f280b8..1a3396a 100644 --- a/core/src/netwerkprog/game/client/MainGame.java +++ b/core/src/netwerkprog/game/client/MainGame.java @@ -17,7 +17,7 @@ import netwerkprog.game.client.game.map.MapRenderer; import netwerkprog.game.client.map.GameInputProcessor; import netwerkprog.game.util.graphics.FrameRate; -public class MainGame extends ApplicationAdapter { +public class MainGame extends ApplicationAdapter{ SpriteBatch batch; float screenWidth; float screenHeight; @@ -27,7 +27,7 @@ public class MainGame extends ApplicationAdapter { private GameInputProcessor gameInputProcessor; private Map map; - private MapRenderer mapRenderer; + public MapRenderer mapRenderer; @@ -148,4 +148,6 @@ public class MainGame extends ApplicationAdapter { public int getHorizontalTileAmount() { return map.getWidth(); } + + } diff --git a/core/src/netwerkprog/game/client/game/map/MapRenderer.java b/core/src/netwerkprog/game/client/game/map/MapRenderer.java index 37457e2..1693171 100644 --- a/core/src/netwerkprog/game/client/game/map/MapRenderer.java +++ b/core/src/netwerkprog/game/client/game/map/MapRenderer.java @@ -4,6 +4,7 @@ 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 netwerkprog.game.client.map.Tile; import netwerkprog.game.util.graphics.Renderable; import com.badlogic.gdx.graphics.g2d.TextureRegion; @@ -22,7 +23,18 @@ public class MapRenderer implements Renderable { public static TextureRegion WALL_TILE; public static TextureRegion PATH_TILE; + private Tile[][] tiles; + private boolean isStarted = false; + + + /** + * makea a new mapRenderer object + * @param map the map object + * @param tileWidth the width of the tile + * @param batch the batch object so no new ones have to be made + * @param camera the camera object + */ public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) { this.map = map; this.tileWidth = tileWidth; @@ -32,13 +44,33 @@ public class MapRenderer implements Renderable { makeTiles(); } + /** + * loads all the images for the tiles and adds all the tiles to the array + */ private void makeTiles() { Texture texture = new Texture(Gdx.files.internal(tilePath)); - TextureRegion[][] tiles = TextureRegion.split(texture, 32, 32); - FLOOR_TILE = tiles[1][6]; - WALL_TILE = tiles[0][4]; - PATH_TILE = tiles[4][6]; + TextureRegion[][] tileTextures = TextureRegion.split(texture, 32, 32); + FLOOR_TILE = tileTextures[1][6]; + WALL_TILE = tileTextures[0][4]; + PATH_TILE = tileTextures[4][6]; + + this.tiles = new Tile[map.getHeight()][map.getWidth()]; + + 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) == ' ') { + tiles[row][col] = new Tile(FLOOR_TILE,x,y, ' '); + } else if (map.get(row, col) == '#') { + 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, 'x'); + } + x += 32; + } + } } public int getTileWidth() { @@ -61,21 +93,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 (Tile[] tileRow : tiles) { + for (int col = 0; col < tiles[0].length; col++) { + Tile cur = tileRow[col]; + batch.draw(cur.getTextureRegion(), cur.x, cur.y); } } -// batch.draw(FLOOR_TILE,100,100); + batch.end(); x = 0; y = 0; @@ -88,8 +113,12 @@ public class MapRenderer implements Renderable { public void resize(int screenWidth, int screenHeight) { cam = new OrthographicCamera(screenWidth, screenHeight); - cam.translate(screenWidth / 2, screenHeight / 2); + cam.translate(screenWidth / 2f, screenHeight / 2f); cam.update(); batch.setProjectionMatrix(cam.combined); } + + public Tile[][] getTiles() { + return tiles; + } } diff --git a/core/src/netwerkprog/game/client/map/GameInputProcessor.java b/core/src/netwerkprog/game/client/map/GameInputProcessor.java index ee33d38..d7ab70f 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; @@ -25,6 +27,12 @@ public class GameInputProcessor implements InputProcessor { private final float CAMERA_MOVE_SPEED = .3f; + /** + * makes a new game input processor + * + * @param camera the camera object to use + * @param game the game object to get objects from + */ public GameInputProcessor(OrthographicCamera camera, MainGame game) { this.camera = camera; this.game = game; @@ -37,12 +45,6 @@ public class GameInputProcessor implements InputProcessor { keysList.add(Input.Keys.D); camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f); -// -// float effectiveViewportWidth = camera.viewportWidth * camera.zoom; -// float effectiveViewportHeight = camera.viewportHeight * camera.zoom; -// -// camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, game.getScreenWidth() - effectiveViewportWidth / 2f); -// camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, game.getScreenHeight() - effectiveViewportHeight / 2f); } @@ -88,7 +90,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; @@ -119,6 +121,20 @@ public class GameInputProcessor implements InputProcessor { @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { + + 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 new file mode 100644 index 0000000..2e2dbc3 --- /dev/null +++ b/core/src/netwerkprog/game/client/map/Tile.java @@ -0,0 +1,35 @@ +package netwerkprog.game.client.map; + +import com.badlogic.gdx.graphics.g2d.TextureRegion; +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, char symbol) { + this.textureRegion = textureRegion; + this.symbol = symbol; + super.x = xPos; + super.y = yPos; + super.width = textureRegion.getRegionWidth(); + super.height = textureRegion.getRegionHeight(); + } + + public TextureRegion getTextureRegion() { + return textureRegion; + } + + public char getSymbol() { + return symbol; + } + + @Override + public String toString() { + return "Tile{" + + "symbol=" + symbol + + ", x=" + x + + ", y=" + y + + '}'; + } +}