From dd6c87ff3514a2398e40c298dc2b6893200bad86 Mon Sep 17 00:00:00 2001 From: Elrabu <busch.elias@web.de> Date: Mon, 11 Dec 2023 10:57:32 +0100 Subject: [PATCH] eb093: Update: Added working multiplayer. --- .../battlearena/Model/Multiplayer/Client.java | 2 +- .../Model/Multiplayer/ConnectionHandler.java | 70 ++++++----- .../Model/Multiplayer/TestMap.java | 114 ++++++++++++++++++ src/main/java/module-info.java | 1 + 4 files changed, 155 insertions(+), 32 deletions(-) create mode 100644 src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestMap.java diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java index 0b6b7c5f..23b1c65c 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java @@ -23,7 +23,7 @@ public class Client { public int[] sendcords(int[] cords) throws IOException { String message = String.join(",", Arrays.stream(cords).mapToObj(String::valueOf).toArray(String[]::new)); out.println(message); - log.info("Sent coordinates: " + message); + // log.info("Sent coordinates: " + message); String resp = in.readLine(); return convertStringToArray(resp); } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java index 67529ff1..6154d069 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java @@ -11,48 +11,57 @@ public class ConnectionHandler { private static int pX = 0; private static int pY = 0; - private int enemyX; - private int enemyY; + private static int enemyX = 0; + private static int enemyY = 0; private static int playerID = 0; private static int[] coordinates = {pX,pY,playerID}; private static final Logger log = LogManager.getLogger(ConnectionHandler.class); - public static void main(String[] args) throws IOException, InterruptedException { + public void startHandler() throws IOException, InterruptedException { Client client = new Client(); client.startConnection("localhost", 4444); - while (true){ - int[] cords = client.sendcords(coordinates); - System.out.println("Enemy X: " + cords[0]); - System.out.println("Enemy Y: " + cords[1]); - System.out.println("Your PlayerID is " + cords[2]); - pX++; - pY++; - playerID = cords[2]; - //Assign the Values to the message to send: - coordinates[0] = pX; - coordinates[1] = pY; - coordinates[2] = playerID; - - TimeUnit.SECONDS.sleep(1); - } - //stop the connection: - //client.stopConnection(); + ConnectionThread connectionthread = new ConnectionThread(client); + connectionthread.start(); + } - class ConnectionThread implements Runnable { - public void run() { + private static class ConnectionThread extends Thread { + private final Client client; + + public ConnectionThread(Client client) { + this.client = client; + } + public void run() { try { - Thread.sleep(250); - } catch (InterruptedException e){ - log.error("Error in Thread.sleep()"); + while (!Thread.interrupted()) { + int[] cords = client.sendcords(coordinates); + enemyX = cords[0]; + enemyY = cords[1]; + // System.out.println("Enemy X: " + enemyX); + // System.out.println("Enemy Y: " + enemyY); + // System.out.println("Your PlayerID is " + cords[2]); + playerID = cords[2]; + // Assign the Values to the message to send: + coordinates[0] = pX; + coordinates[1] = pY; + coordinates[2] = playerID; + + Thread.sleep(16); + } + } catch (InterruptedException | IOException e) { + e.printStackTrace(); // Handle the exception as needed } } } + public int getPlayerID() { + return playerID; + } + public int getpX() { return pX; } @@ -72,17 +81,16 @@ public class ConnectionHandler { public int getEnemyX() { return enemyX; } - - public void setEnemyX(int enemyX) { - this.enemyX = enemyX; - } - public int getEnemyY() { return enemyY; } + public void setEnemyX(int enemyX) { + ConnectionHandler.enemyX = enemyX; + } + public void setEnemyY(int enemyY) { - this.enemyY = enemyY; + ConnectionHandler.enemyY = enemyY; } } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestMap.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestMap.java new file mode 100644 index 00000000..549744eb --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/TestMap.java @@ -0,0 +1,114 @@ +package de.hdm_stuttgart.battlearena.Model.Multiplayer; + +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.Pane; +import javafx.scene.paint.Color; +import javafx.scene.shape.Rectangle; +import javafx.stage.Stage; +import java.util.concurrent.TimeUnit; + +import java.io.IOException; + +public class TestMap extends Application { + private static final int SQUARE_SIZE = 50; + private static Rectangle square; + private static Rectangle enemy; + private Color color = Color.BLACK; + + @Override + public void start(Stage primaryStage) throws IOException, InterruptedException { + + ConnectionHandler handler = new ConnectionHandler(); + handler.startHandler(); + + TimeUnit.SECONDS.sleep(1); + + square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE, color); + enemy = new Rectangle(SQUARE_SIZE, SQUARE_SIZE, color); + + Pane root = new Pane(); + root.getChildren().add(square); + root.getChildren().add(enemy); + + Scene scene = new Scene(root, 400, 400); + + scene.setOnKeyPressed(event -> handleKeyPress(event.getCode(), handler)); + + primaryStage.setTitle("Player #" + handler.getPlayerID()); + primaryStage.setScene(scene); + primaryStage.show(); + + if(handler.getPlayerID() == 1){ + square.setX(50); + square.setY(50); + enemy.setX(200); + enemy.setY(50); + } else if(handler.getPlayerID() == 2){ + square.setX(200); + square.setY(50); + enemy.setX(50); + enemy.setY(50); + } + + UpdateThread update = new UpdateThread(handler); + update.start(); + + } + + private void handleKeyPress(KeyCode code, ConnectionHandler handler) { + switch (code) { + case UP: + square.setY(square.getY() - 10); + handler.setpY(handler.getpY() - 10); + break; + case DOWN: + square.setY(square.getY() + 10); + handler.setpY(handler.getpY() + 10); + break; + case LEFT: + square.setX(square.getX() - 10); + handler.setpX(handler.getpX() - 10); + break; + case RIGHT: + square.setX(square.getX() + 10); + handler.setpX(handler.getpX() + 10); + break; + } + } + + private static class UpdateThread extends Thread { + private final ConnectionHandler handler; + + private UpdateThread(ConnectionHandler handler) { + this.handler = handler; + } + + public void run() { + while (true){ + + handler.setpX((int) square.getX()); + handler.setpY((int) square.getY()); + + //receive cords + enemy.setX(handler.getEnemyX()); + enemy.setY(handler.getEnemyY()); + + + try { + Thread.sleep(16); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + + } + } + + public static void main(String[] args) { + launch(args); + + } +} \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index fdd0d1f6..dc5aa99a 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -8,4 +8,5 @@ module gui { opens de.hdm_stuttgart.battlearena to javafx.fxml; exports de.hdm_stuttgart.battlearena.Main; exports de.hdm_stuttgart.battlearena.Controller; + exports de.hdm_stuttgart.battlearena.Model.Multiplayer; } -- GitLab