Implemented boats spawning in parallel at the start line with spacing.

Added two more colours to support up to eight boats.

#story[1117]
This commit is contained in:
Zhi You Tan
2017-08-05 00:31:36 +12:00
parent 8af80e6c3a
commit a727014fcb
3 changed files with 42 additions and 25 deletions
@@ -98,12 +98,14 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
static void serverLog(String message, int logLevel) { static void serverLog(String message, int logLevel) {
if (logLevel <= LOG_LEVEL) { if (logLevel <= LOG_LEVEL) {
System.out.println("[SERVER " + LocalDateTime.now().toLocalTime().toString() + "] " + message); System.out.println(
"[SERVER " + LocalDateTime.now().toLocalTime().toString() + "] " + message);
} }
} }
/** /**
* A client has tried to connect to the server * A client has tried to connect to the server
*
* @param serverToClientThread The player that connected * @param serverToClientThread The player that connected
*/ */
@Override @Override
@@ -117,6 +119,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
/** /**
* A player has left the game, remove the player from the GameState * A player has left the game, remove the player from the GameState
*
* @param player The player that left * @param player The player that left
*/ */
@Override @Override
@@ -168,30 +171,40 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
} }
/** /**
* Initialise boats to specific spaced out geopoint behind starting line. * Initialise boats to specific spaced out geopoints behind starting line.
*/ */
private void initialiseBoatPosition() { private void initialiseBoatPosition() {
System.out.println("ran"); // Getting the start line compound marks
RaceXMLData raceXMLData = gameClient.getCourseData(); CompoundMark cm = gameClient.getCourseData().getCompoundMarks().get(1);
CompoundMark cm = raceXMLData.getCompoundMarks().get(1); GeoPoint startMark1 = new GeoPoint(cm.getMarks().get(0).getLat(),
GeoPoint geoPoint1 = new GeoPoint(cm.getMarks().get(0).getLat(), cm.getMarks().get(0).getLng()); cm.getMarks().get(0).getLng());
GeoPoint geoPoint2 = new GeoPoint(cm.getMarks().get(1).getLat(), cm.getMarks().get(1).getLng()); GeoPoint startMark2 = new GeoPoint(cm.getMarks().get(1).getLat(),
Double perpendicularAngle = GeoUtility.getBearing(geoPoint1, geoPoint2); cm.getMarks().get(1).getLng());
Double x = geoPoint1.getLat() + Math.sin(perpendicularAngle) * 1000; // Calculating midpoint
Double y = geoPoint1.getLng() + Math.cos(perpendicularAngle) * 1000; Double perpendicularAngle = GeoUtility.getBearing(startMark1, startMark2);
Double length = GeoUtility.getDistance(startMark1, startMark2);
ServerToClientThread stct0 = serverToClientThreads.get(0); GeoPoint midpoint = GeoUtility.getGeoCoordinate(startMark1, perpendicularAngle, length / 2);
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());
// Setting each boats position side by side
double distanceApart = 0.0005; // magic number for boat spawn distance apart
int boatIndex = 0;
int boatSpawnDirection = -1; // positive for left and negative for right
for (Yacht yacht : GameState.getYachts().values()) { for (Yacht yacht : GameState.getYachts().values()) {
System.out.println("GS: " + yacht.getLat() + " " + yacht.getLon()); Double x =
midpoint.getLat() + boatSpawnDirection * boatIndex * Math.sin(perpendicularAngle)
* distanceApart;
Double y =
midpoint.getLng() + boatSpawnDirection * boatIndex * Math.cos(perpendicularAngle)
* distanceApart;
yacht.setLocation(new GeoPoint(x, y));
if (boatSpawnDirection == -1) {
boatSpawnDirection = 1;
boatIndex++;
} else {
boatSpawnDirection = -1;
}
} }
} }
} }
+2 -2
View File
@@ -6,12 +6,12 @@ import javafx.scene.paint.Color;
* Enum for generating colours. * Enum for generating colours.
*/ */
public enum Colors { public enum Colors {
RED, PERU, SEAGREEN, GREEN, BLUE, PURPLE; RED, PERU, GOLD, GREEN, BLUE, PURPLE, DEEPPINK, GRAY;
static Integer index = 0; static Integer index = 0;
public static Color getColor() { public static Color getColor() {
if (index == 6) { if (index == 8) {
index = 0; index = 0;
} }
return Color.valueOf(values()[index++].toString()); return Color.valueOf(values()[index++].toString());
+4
View File
@@ -406,4 +406,8 @@ public class Yacht {
public void addLocationListener (YachtLocationListener listener) { public void addLocationListener (YachtLocationListener listener) {
locationListeners.add(listener); locationListeners.add(listener);
} }
public void setLocation(GeoPoint geoPoint) {
location = geoPoint;
}
} }