Skip to content
Snippets Groups Projects
Commit dd6c87ff authored by Elrabu's avatar Elrabu
Browse files

eb093: Update: Added working multiplayer.

parent d9af1293
No related branches found
No related tags found
1 merge request!35Update: Added more Stuff to be transported over the server.
......@@ -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);
}
......
......@@ -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;
}
}
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
......@@ -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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment