diff --git a/core/src/netwerkprog/game/client/Client.java b/core/src/netwerkprog/game/client/Client.java index 907c6fe..1a54dec 100644 --- a/core/src/netwerkprog/game/client/Client.java +++ b/core/src/netwerkprog/game/client/Client.java @@ -9,15 +9,15 @@ import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.Arrays; -import java.util.Scanner; public class Client extends Controller { - private int port; - private String hostname; - private DataParser parser; + private final int port; + private final String hostname; + private final DataParser parser; private boolean isConnected = true; - - + private Socket socket; + private Thread receiveThread; + private DataOutputStream outputStream; public Client(String hostname) { this.port = Data.port(); @@ -25,41 +25,26 @@ public class Client extends Controller { this.parser = new DataParser(); } + /** + * Starts the client process. + */ @Override public void run() { this.connect(); + this.receiveThread.start(); } + /** + * Connects the client to the server. + */ public void connect() { - System.out.println("[CLIENT] connecting to server on port " + port); - Scanner scanner = new Scanner(System.in); + System.out.println("[CLIENT] connecting to server on port " + this.port); try { - Socket socket = new Socket(hostname,port); + this.socket = new Socket(this.hostname, this.port); DataInputStream in = new DataInputStream(socket.getInputStream()); - DataOutputStream out = new DataOutputStream(socket.getOutputStream()); + this.outputStream = new DataOutputStream(socket.getOutputStream()); - Thread readSocketThread = new Thread( () -> { - receiveDataFromSocket(in); - }); - - readSocketThread.start(); - - String input = ""; - - while (!input.equals("\\quit")) { - input = scanner.nextLine(); - out.writeUTF(input); - } - - isConnected = false; - - socket.close(); - - try { - readSocketThread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + this.receiveThread = new Thread( () -> receive(in)); } catch (IOException e) { System.out.println("[CLIENT] there was an error connecting : " + e.getMessage()); @@ -69,17 +54,50 @@ public class Client extends Controller { } } - private void receiveDataFromSocket(DataInputStream in) { - String received = ""; + /** + * Sends a message to the server. + * @param message The message to send. + */ + public void send(String message) { + try { + this.outputStream.writeUTF(message); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Receives a message from the server. + * @param in The inputStream + */ + public void receive(DataInputStream in) { + Data data = null; while (isConnected) { try { - received = in.readUTF(); - + data = this.parser.parse(in.readUTF()); } catch (IOException e) { - System.out.println("exception caught - " + e.getMessage());; + e.printStackTrace(); + } + if (data != null) { + send(this.parser.parse(data)); } } } + public void disconnect() { + this.isConnected = false; + try { + this.receiveThread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + send("Disconnect"); + + try { + this.socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } 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/util/data/DataParser.java b/core/src/netwerkprog/game/util/data/DataParser.java index 938e4a6..487fad2 100644 --- a/core/src/netwerkprog/game/util/data/DataParser.java +++ b/core/src/netwerkprog/game/util/data/DataParser.java @@ -7,4 +7,8 @@ public class DataParser { public Data parse(String request) { return null; } + + public String parse(Data data) { + return null; + } }