mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
WIP: Connected game client to main server thread to pass compound mark variable.
Boats are initialised in main server thread behind start line before game starts. #story[1117]
This commit is contained in:
@@ -7,7 +7,14 @@ import java.util.ArrayList;
|
||||
import java.util.Observable;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.model.stream.xml.parser.RaceXMLData;
|
||||
import seng302.utilities.GeoUtility;
|
||||
import seng302.visualiser.GameClient;
|
||||
|
||||
/**
|
||||
* A class describing the overall server, which creates and collects server threads for each client
|
||||
@@ -25,6 +32,8 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
private ServerSocket serverSocket = null;
|
||||
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
|
||||
|
||||
private GameClient gameClient;
|
||||
|
||||
public MainServerThread() {
|
||||
try {
|
||||
serverSocket = new ServerSocket(PORT);
|
||||
@@ -130,6 +139,8 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
}
|
||||
|
||||
public void startGame() {
|
||||
initialiseBoatPosition();
|
||||
|
||||
Timer t = new Timer();
|
||||
|
||||
t.schedule(new TimerTask() {
|
||||
@@ -146,4 +157,41 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
public void terminate() {
|
||||
terminated = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass GameClient to main server thread so it can access the properties inside.
|
||||
*
|
||||
* @param gameClient gameClient
|
||||
*/
|
||||
public void setGameClient(GameClient gameClient) {
|
||||
this.gameClient = gameClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise boats to specific spaced out geopoint behind starting line.
|
||||
*/
|
||||
private void initialiseBoatPosition() {
|
||||
System.out.println("ran");
|
||||
RaceXMLData raceXMLData = gameClient.getCourseData();
|
||||
CompoundMark cm = raceXMLData.getCompoundMarks().get(1);
|
||||
GeoPoint geoPoint1 = new GeoPoint(cm.getMarks().get(0).getLat(), cm.getMarks().get(0).getLng());
|
||||
GeoPoint geoPoint2 = new GeoPoint(cm.getMarks().get(1).getLat(), cm.getMarks().get(1).getLng());
|
||||
Double perpendicularAngle = GeoUtility.getBearing(geoPoint1, geoPoint2);
|
||||
|
||||
Double x = geoPoint1.getLat() + Math.sin(perpendicularAngle) * 1000;
|
||||
Double y = geoPoint1.getLng() + Math.cos(perpendicularAngle) * 1000;
|
||||
|
||||
ServerToClientThread stct0 = serverToClientThreads.get(0);
|
||||
Yacht yacht0 = GameState.getYachts().get(stct0.getYacht().getSourceId());
|
||||
ServerToClientThread stct1 = serverToClientThreads.get(1);
|
||||
Yacht yacht1 = GameState.getYachts().get(stct1.getYacht().getSourceId());
|
||||
yacht1.updateLocation(x,y, yacht1.getHeading(), yacht1.getVelocity());
|
||||
|
||||
System.out.println(yacht0.getLat() + " " + yacht0.getLon());
|
||||
System.out.println(yacht1.getLat() + " " + yacht1.getLon());
|
||||
|
||||
for (Yacht yacht : GameState.getYachts().values()) {
|
||||
System.out.println("GS: " + yacht.getLat() + " " + yacht.getLon());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,8 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
private XMLGenerator xml;
|
||||
|
||||
private Yacht yacht;
|
||||
|
||||
public ServerToClientThread(Socket socket) {
|
||||
this.socket = socket;
|
||||
BufferedReader fn;
|
||||
@@ -98,7 +100,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
sourceId = GameState.getUniquePlayerID();
|
||||
if (threeWayHandshake(sourceId)) {
|
||||
serverLog("Successful handshake. Client allocated id: " + sourceId, 0);
|
||||
Yacht yacht = new Yacht(
|
||||
yacht = new Yacht(
|
||||
"Yacht", sourceId, sourceId.toString(), fName, fName + " " + lName, "NZ"
|
||||
);
|
||||
// Yacht yacht = new Yacht("Kappa", "Kap", new GeoPoint(57.6708220, 11.8321340), 90.0);
|
||||
@@ -366,4 +368,8 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public Yacht getYacht() {
|
||||
return yacht;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user