Updated initialise boat function so it can now initialise boats with distance apart in meters.

Created a second prototype function which is more testable compared to the initial design. New function takes in parameters (starting marks, yacht starting position, yacht) and initialise yacht correctly with position.

#story[1117]
This commit is contained in:
Zhi You Tan
2017-08-06 21:16:14 +12:00
parent 81c2a8e0fd
commit a470cb66a2
3 changed files with 93 additions and 22 deletions
@@ -7,9 +7,12 @@ import java.util.Map;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seng302.model.GeoPoint;
import seng302.model.Player;
import seng302.model.Yacht;
import seng302.gameServer.server.messages.BoatActionType;
import seng302.model.mark.CompoundMark;
import seng302.utilities.GeoUtility;
/**
* A Static class to hold information about the current state of the game (model)
@@ -77,8 +80,11 @@ public class GameState implements Runnable {
public static void addPlayer(Player player) {
players.add(player);
String playerText = player.getYacht().getSourceId() + " " + player.getYacht().getBoatName() + " " + player.getYacht().getCountry();
Platform.runLater(() -> observablePlayers.add(playerText)); //Had to add this to handle javaFX window using array
String playerText =
player.getYacht().getSourceId() + " " + player.getYacht().getBoatName() + " " + player
.getYacht().getCountry();
Platform.runLater(() -> observablePlayers
.add(playerText)); //Had to add this to handle javaFX window using array
playerStringMap.put(player, playerText);
}
@@ -175,6 +181,7 @@ public class GameState implements Runnable {
/**
* Generates a new ID based off the size of current players + 1
*
* @return a playerID to be allocated to a new connetion
*/
public static Integer getUniquePlayerID() {
@@ -215,4 +222,38 @@ public class GameState implements Runnable {
System.out.println("Lng: " + playerYacht.getLocation().getLng());
System.out.println("-----------------------\n");
}
// /**
// * Calculates and initialise a yacht given its index in the starting position. Position is
// * calculated starting with 0 being the first boat. Position 0 will spawn in the MIDDLE of mark1
// * and mark2, position 1 will spawn 50m LEFT of position 0, position 2 will spawn 50m RIGHT of
// * position 0, position 3 will spawn 100m LEFT of position 0, and so forth.
// *
// * @param mark1 first mark of the starting composite mark
// * @param mark2 second mark of the starting composite mark
// * @param boatIndex boat starting position
// * @param yacht yacht to be reposition
// */
// public static void startBoatInPosition(GeoPoint mark1, GeoPoint mark2, Integer boatIndex,
// Yacht yacht) {
// // TODO: 6/08/2017 zyt10 - check for mark1 being the right side from heading
// // Calculating midpoint
// Double perpendicularAngle = GeoUtility.getBearing(mark1, mark2);
// Double length = GeoUtility.getDistance(mark1, mark2);
// GeoPoint midpoint = GeoUtility.getGeoCoordinate(mark1, perpendicularAngle, length / 2);
//
// // Setting each boats position side by side
// double DISTANCEFACTOR = 50.0; // distance apart in meters
// int distanceApart = boatIndex / 2;
//
// if (boatIndex % 2 == 1 && boatIndex != 0) {
// distanceApart++;
// distanceApart *= 1;
// }
//
// GeoPoint spawnMark = GeoUtility
// .getGeoCoordinate(midpoint, perpendicularAngle, distanceApart * DISTANCEFACTOR);
//
// yacht.setLocation(spawnMark);
// }
}
@@ -187,24 +187,21 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
GeoPoint midpoint = GeoUtility.getGeoCoordinate(startMark1, perpendicularAngle, length / 2);
// Setting each boats position side by side
double distanceApart = 0.0005; // magic number for boat spawn distance apart
double DISTANCEFACTOR = 50.0; // distance apart in meters
int boatIndex = 0;
int boatSpawnDirection = -1; // positive for left and negative for right
for (Yacht yacht : GameState.getYachts().values()) {
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));
int distanceApart = boatIndex / 2;
if (boatSpawnDirection == -1) {
boatSpawnDirection = 1;
if (boatIndex % 2 == 1 && boatIndex != 0) {
distanceApart++;
distanceApart *= -1;
}
GeoPoint spawnMark = GeoUtility
.getGeoCoordinate(midpoint, perpendicularAngle, distanceApart * DISTANCEFACTOR);
yacht.setLocation(spawnMark);
boatIndex++;
} else {
boatSpawnDirection = -1;
}
}
}
}
@@ -3,13 +3,46 @@ package seng302.gameServer.server;
import static junit.framework.TestCase.assertTrue;
import org.junit.Test;
import seng302.gameServer.GameState;
import seng302.gameServer.MainServerThread;
import seng302.model.GeoPoint;
import seng302.model.Yacht;
import seng302.utilities.GeoUtility;
/**
* Created by ryantan on 5/08/2017.
*/
public class TestInitialiseBoatPosition {
private GeoPoint mark1 = new GeoPoint(50, 50);
private GeoPoint mark2 = new GeoPoint(0, 0);
private GameState gameState = new GameState("");
@Test
public void testInitialiseBoatPosition(){
// // Calculating midpoint
// Double perpendicularAngle = GeoUtility.getBearing(mark1, mark2);
// Double length = GeoUtility.getDistance(mark1, mark2);
// GeoPoint midpoint = GeoUtility.getGeoCoordinate(mark1, perpendicularAngle, length / 2);
//
// // Create 8 yacht in game state
// for (int i = 0; i < 8; i++) {
// GameState.addYacht(i, new Yacht("Yacht", i, "1", "Yacht" + i, "Yacht" + i, "Test" ));
// }
//
// int i = 0;
// for (Yacht yacht : GameState.getYachts().values()) {
// GameState.startBoatInPosition(mark1, mark2, i, yacht);
// double distance = GeoUtility.getDistance(midpoint, yacht.getLocation());
// System.out.println(i + " " + distance);
//
// double distanceApart = i / 2;
// if (i % 2 == 1 && i != 0) {
// distanceApart++;
// }
//
// assertTrue(distance <= (distanceApart * 50.01) && distance >= (distanceApart * 49.99));
// i++;
// }
}
}